1: <?php
2: namespace Budovy;
3:
4: class UnitRepository extends Repository {
5: 6: 7: 8: 9:
10: function getPairData() {
11: return $this->getTable()->select('unit.id, CONCAT(place.name, \' - \', unit.name) AS name')->order('place.name ASC, unit.name ASC')->fetchPairs('id', 'name');
12: }
13:
14: 15: 16: 17: 18:
19:
20: public function findById($id) {
21: return $this->getTable()->select('unit.*, place.name AS place')->wherePrimary($id)->fetch();
22: }
23:
24: 25: 26: 27: 28: 29: 30: 31: 32:
33: function getGridSelection($filter, $order, $page, $limit) {
34: $selection = $this->getTable()->select('unit.*, place.name AS place');
35:
36: if (isset($order[0]))
37: $selection->order(implode(' ', $order));
38:
39: $selection->page($page, $limit);
40:
41: return $this->addGridFilter($selection, $filter);
42: }
43:
44: 45: 46: 47: 48: 49:
50: function getGridCount($filter) {
51: $selection = $this->getTable()->select('COUNT(*) AS cnt');
52:
53: $result = $this->addGridFilter($selection, $filter)->fetch();
54:
55: return $result->cnt;
56: }
57:
58: 59: 60: 61: 62: 63: 64:
65: private function addGridFilter(\Nette\Database\Table\Selection $selection, $filter) {
66: $filters = array();
67:
68: foreach ($filter as $k => $v) {
69: if ($k == 'place')
70: $filters['id_place=?'] = $v;
71: else
72: $filters[$k . ' LIKE ?'] = "%$v%";
73: }
74:
75: return $selection->where($filters);
76: }
77:
78: 79: 80: 81: 82: 83:
84: function getAssignedDocuments($id_unit) {
85: return $this->getTable()->select(':unit_document.document.id')->where('unit.id', $id_unit)->where(':unit_document.document.id IS NOT NULL');
86: }
87:
88: 89: 90: 91: 92: 93:
94: function assignDocument($id_unit, $id_document) {
95: $this->connection->table('unit_document')->insert(array('id_unit' => $id_unit, 'id_document' => $id_document));
96: }
97:
98: 99: 100: 101: 102: 103:
104: function removeAssignedDocument($id_unit, $id_document) {
105: $this->connection->table('unit_document')->where(array('id_unit' => $id_unit, 'id_document' => $id_document))->delete();
106: }
107: }