1 | <?php |
||
28 | 1 | class NetteDatabase extends \Nette\Object implements IDataSource |
|
29 | { |
||
30 | /** @var \Nette\Database\Table\Selection */ |
||
31 | protected $selection; |
||
32 | |||
33 | /** |
||
34 | * @param \Nette\Database\Table\Selection $selection |
||
35 | */ |
||
36 | public function __construct(\Nette\Database\Table\Selection $selection) |
||
40 | |||
41 | /** |
||
42 | * @return \Nette\Database\Table\Selection |
||
43 | */ |
||
44 | public function getSelection() |
||
45 | { |
||
46 | return $this->selection; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param Condition $condition |
||
51 | * @param \Nette\Database\Table\Selection $selection |
||
52 | */ |
||
53 | protected function makeWhere(Condition $condition, \Nette\Database\Table\Selection $selection = NULL) |
||
54 | { |
||
55 | $selection = $selection === NULL |
||
56 | 1 | ? $this->selection |
|
57 | 1 | : $selection; |
|
58 | |||
59 | 1 | if ($condition->callback) { |
|
60 | call_user_func_array($condition->callback, [$condition->value, $selection]); |
||
61 | } else { |
||
62 | 1 | call_user_func_array([$selection, 'where'], $condition->__toArray()); |
|
63 | } |
||
64 | 1 | } |
|
65 | |||
66 | /********************************** inline editation helpers ************************************/ |
||
67 | |||
68 | /** |
||
69 | * Default callback for an inline editation save. |
||
70 | * @param mixed $id |
||
71 | * @param array $values |
||
72 | * @param string $idCol |
||
73 | * @return bool |
||
74 | */ |
||
75 | public function update($id, array $values, $idCol) |
||
76 | { |
||
77 | return (bool) $this->getSelection() |
||
78 | ->where('?name = ?', $idCol, $id) |
||
79 | ->update($values); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Default callback used when an editable column has customRender. |
||
84 | * @param mixed $id |
||
85 | * @param string $idCol |
||
86 | * @return \Nette\Database\Table\ActiveRow|bool |
||
87 | */ |
||
88 | public function getRow($id, $idCol) |
||
89 | { |
||
90 | return $this->getSelection() |
||
91 | ->where('?name = ?', $idCol, $id) |
||
92 | ->fetch(); |
||
93 | } |
||
94 | |||
95 | /********************************** interface IDataSource ************************************/ |
||
96 | |||
97 | /** |
||
98 | * @return int |
||
99 | */ |
||
100 | public function getCount() |
||
104 | |||
105 | /** |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getData() |
||
112 | |||
113 | /** |
||
114 | * @param array $conditions |
||
115 | */ |
||
116 | public function filter(array $conditions) |
||
117 | { |
||
118 | 1 | foreach ($conditions as $condition) { |
|
119 | 1 | $this->makeWhere($condition); |
|
120 | 1 | } |
|
121 | 1 | } |
|
122 | |||
123 | /** |
||
124 | * @param int $offset |
||
125 | * @param int $limit |
||
126 | */ |
||
127 | public function limit($offset, $limit) |
||
131 | |||
132 | /** |
||
133 | * @param array $sorting |
||
134 | */ |
||
135 | public function sort(array $sorting) |
||
141 | |||
142 | /** |
||
143 | * @param mixed $column |
||
144 | * @param array $conditions |
||
145 | * @param int $limit |
||
146 | * @return array |
||
147 | * @throws Exception |
||
148 | */ |
||
149 | public function suggest($column, array $conditions, $limit) |
||
176 | } |
||
177 |