|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Grido (https://github.com/o5/grido) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz) |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the file LICENSE.md that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Grido\DataSources; |
|
13
|
|
|
|
|
14
|
|
|
use Grido\Exception; |
|
15
|
|
|
use Grido\Components\Filters\Condition; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Nette Database data source. |
|
19
|
|
|
* |
|
20
|
|
|
* @package Grido |
|
21
|
|
|
* @subpackage DataSources |
|
22
|
|
|
* @author Petr Bugyík |
|
23
|
|
|
* |
|
24
|
|
|
* @property-read \Nette\Database\Table\Selection $selection |
|
25
|
|
|
* @property-read int $count |
|
26
|
|
|
* @property-read array $data |
|
27
|
|
|
*/ |
|
28
|
1 |
|
class NetteDatabase implements IDataSource |
|
29
|
|
|
{ |
|
30
|
|
|
use \Nette\SmartObject; |
|
31
|
|
|
|
|
32
|
|
|
/** @var \Nette\Database\Table\Selection */ |
|
33
|
|
|
protected $selection; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \Nette\Database\Table\Selection $selection |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function __construct(\Nette\Database\Table\Selection $selection) |
|
39
|
1 |
|
{ |
|
40
|
|
|
$this->selection = $selection; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return \Nette\Database\Table\Selection |
|
45
|
1 |
|
*/ |
|
46
|
1 |
|
public function getSelection() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->selection; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Condition $condition |
|
53
|
|
|
* @param \Nette\Database\Table\Selection $selection |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function makeWhere(Condition $condition, \Nette\Database\Table\Selection $selection = NULL) |
|
56
|
1 |
|
{ |
|
57
|
1 |
|
$selection = $selection === NULL |
|
58
|
|
|
? $this->selection |
|
59
|
1 |
|
: $selection; |
|
60
|
1 |
|
|
|
61
|
1 |
|
if ($condition->callback) { |
|
62
|
1 |
|
call_user_func_array($condition->callback, [$condition->value, $selection]); |
|
63
|
|
|
} else { |
|
64
|
1 |
|
call_user_func_array([$selection, 'where'], $condition->__toArray()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/********************************** inline editation helpers ************************************/ |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Default callback for an inline editation save. |
|
72
|
|
|
* @param mixed $id |
|
73
|
|
|
* @param array $values |
|
74
|
|
|
* @param string $idCol |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function update($id, array $values, $idCol) |
|
78
|
1 |
|
{ |
|
79
|
1 |
|
return (bool) $this->getSelection() |
|
80
|
|
|
->where('?name = ?', $idCol, $id) |
|
81
|
|
|
->update($values); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Default callback used when an editable column has customRender. |
|
86
|
|
|
* @param mixed $id |
|
87
|
|
|
* @param string $idCol |
|
88
|
|
|
* @return \Nette\Database\Table\ActiveRow|bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getRow($id, $idCol) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->getSelection() |
|
93
|
|
|
->where('?name = ?', $idCol, $id) |
|
94
|
|
|
->fetch(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/********************************** interface IDataSource ************************************/ |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return int |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function getCount() |
|
103
|
|
|
{ |
|
104
|
|
|
return (int) $this->selection->count('*'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function getData() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->selection; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param array $conditions |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function filter(array $conditions) |
|
119
|
1 |
|
{ |
|
120
|
1 |
|
foreach ($conditions as $condition) { |
|
121
|
1 |
|
$this->makeWhere($condition); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param int $offset |
|
127
|
|
|
* @param int $limit |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function limit($offset, $limit) |
|
130
|
1 |
|
{ |
|
131
|
|
|
$this->selection->limit($limit, $offset); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param array $sorting |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function sort(array $sorting) |
|
138
|
1 |
|
{ |
|
139
|
1 |
|
foreach ($sorting as $column => $sort) { |
|
140
|
1 |
|
$this->selection->order("$column $sort"); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param mixed $column |
|
146
|
|
|
* @param array $conditions |
|
147
|
|
|
* @param int $limit |
|
148
|
|
|
* @return array |
|
149
|
|
|
* @throws Exception |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function suggest($column, array $conditions, $limit) |
|
152
|
1 |
|
{ |
|
153
|
1 |
|
$selection = clone $this->selection; |
|
154
|
|
|
is_string($column) && $selection->select("DISTINCT $column")->order($column); |
|
155
|
1 |
|
$selection->limit($limit); |
|
156
|
1 |
|
|
|
157
|
1 |
|
foreach ($conditions as $condition) { |
|
158
|
|
|
$this->makeWhere($condition, $selection); |
|
159
|
1 |
|
} |
|
160
|
1 |
|
|
|
161
|
1 |
|
$items = []; |
|
162
|
1 |
|
foreach ($selection as $row) { |
|
163
|
1 |
|
if (is_string($column)) { |
|
164
|
1 |
|
$value = (string) $row[$column]; |
|
165
|
1 |
|
} elseif (is_callable($column)) { |
|
166
|
|
|
$value = (string) $column($row); |
|
167
|
|
|
} else { |
|
168
|
|
|
$type = gettype($column); |
|
169
|
|
|
throw new Exception("Column of suggestion must be string or callback, $type given."); |
|
170
|
1 |
|
} |
|
171
|
1 |
|
|
|
172
|
|
|
$items[$value] = \Latte\Runtime\Filters::escapeHtml($value); |
|
173
|
1 |
|
} |
|
174
|
1 |
|
|
|
175
|
|
|
is_callable($column) && sort($items); |
|
176
|
|
|
return array_values($items); |
|
177
|
1 |
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|