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