• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / datagrid / 7048127238

30 Nov 2023 02:47PM UTC coverage: 34.081%. First build
7048127238

Pull #1060

github

paveljanda
Minor CSS changes for the next verison of datagrid
Pull Request #1060: [7.x] Next

118 of 431 new or added lines in 54 files covered. (27.38%)

1125 of 3301 relevant lines covered (34.08%)

0.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.26
/src/DataSource/NetteDatabaseTableDataSource.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Datagrid\DataSource;
4

5
use Contributte\Datagrid\AggregationFunction\IAggregationFunction;
6
use Contributte\Datagrid\Exception\DatagridDateTimeHelperException;
7
use Contributte\Datagrid\Filter\FilterDate;
8
use Contributte\Datagrid\Filter\FilterDateRange;
9
use Contributte\Datagrid\Filter\FilterMultiSelect;
10
use Contributte\Datagrid\Filter\FilterRange;
11
use Contributte\Datagrid\Filter\FilterSelect;
12
use Contributte\Datagrid\Filter\FilterText;
13
use Contributte\Datagrid\Utils\DateTimeHelper;
14
use Contributte\Datagrid\Utils\Sorting;
15
use LogicException;
16
use Nette\Database\Table\Selection;
17
use Nette\Utils\Strings;
18

19
class NetteDatabaseTableDataSource extends FilterableDataSource implements IDataSource
20
{
21

22
        /** @var array */
23
        protected array $data = [];
24

25
        public function __construct(protected Selection $dataSource, protected string $primaryKey)
1✔
26
        {
27
        }
1✔
28

29
        public function getCount(): int
30
        {
31
                $dataSourceSqlBuilder = $this->dataSource->getSqlBuilder();
1✔
32

33
                try {
34
                        $primary = $this->dataSource->getPrimary();
1✔
35

NEW
36
                } catch (LogicException) {
×
37
                        if ($dataSourceSqlBuilder->getGroup() !== '') {
×
38
                                return $this->dataSource->count(
×
39
                                        'DISTINCT ' . Strings::replace($dataSourceSqlBuilder->getGroup(), '~ (DESC|ASC)~')
×
40
                                );
41
                        }
42

43
                        return $this->dataSource->count('*');
×
44
                }
45

46
                if ($dataSourceSqlBuilder->getGroup() !== '') {
1✔
47
                        return $this->dataSource->count(
×
48
                                'DISTINCT ' . Strings::replace($dataSourceSqlBuilder->getGroup(), '~ (DESC|ASC)~')
×
49
                        );
50
                }
51

52
                return $this->dataSource->count(
1✔
53
                        $this->dataSource->getName() . '.' . (is_array($primary) ? reset($primary) : $primary)
1✔
54
                );
55
        }
56

57
        /**
58
         * {@inheritDoc}
59
         */
60
        public function getData(): array
61
        {
62
                return $this->data !== []
1✔
63
                        ? $this->data
1✔
64
                        : $this->dataSource->fetchAll();
1✔
65
        }
66

67
        /**
68
         * {@inheritDoc}
69
         */
70
        public function filterOne(array $condition): IDataSource
1✔
71
        {
72
                $this->dataSource->where($condition)->limit(1);
1✔
73

74
                return $this;
1✔
75
        }
76

77
        /**
78
         * @phpstan-param positive-int|0 $offset
79
         * @phpstan-param positive-int|0 $limit
80
         */
81
        public function limit(int $offset, int $limit): IDataSource
1✔
82
        {
83
                $this->data = $this->dataSource->limit($limit, $offset)->fetchAll();
1✔
84

85
                return $this;
1✔
86
        }
87

88
        public function sort(Sorting $sorting): IDataSource
1✔
89
        {
90
                if (is_callable($sorting->getSortCallback())) {
1✔
91
                        call_user_func(
×
92
                                $sorting->getSortCallback(),
×
93
                                $this->dataSource,
×
94
                                $sorting->getSort()
×
95
                        );
96

97
                        return $this;
×
98
                }
99

100
                $sort = $sorting->getSort();
1✔
101

102
                if ($sort !== []) {
1✔
103
                        $this->dataSource->getSqlBuilder()->setOrder([], []);
1✔
104

105
                        foreach ($sort as $column => $order) {
1✔
106
                                $this->dataSource->order(sprintf('%s %s', $column, $order));
1✔
107
                        }
108
                } else {
109
                        /**
110
                         * Has the statement already a order by clause?
111
                         */
112
                        if ($this->dataSource->getSqlBuilder()->getOrder() === []) {
×
113
                                $this->dataSource->order($this->primaryKey);
×
114
                        }
115
                }
116

117
                return $this;
1✔
118
        }
119

120
        public function processAggregation(IAggregationFunction $function): void
121
        {
122
                $function->processDataSource(clone $this->dataSource);
×
123
        }
124

125
        protected function applyFilterDate(FilterDate $filter): void
126
        {
127
                $conditions = $filter->getCondition();
×
128

129
                try {
130
                        $date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
×
131

NEW
132
                        $this->dataSource->where(sprintf('DATE(%s) = ?', $filter->getColumn()), $date->format('Y-m-d'));
×
NEW
133
                } catch (DatagridDateTimeHelperException) {
×
134
                        // ignore the invalid filter value
135
                }
136
        }
137

138
        protected function applyFilterDateRange(FilterDateRange $filter): void
139
        {
140
                $conditions = $filter->getCondition();
×
141

142
                $valueFrom = $conditions[$filter->getColumn()]['from'];
×
143
                $valueTo = $conditions[$filter->getColumn()]['to'];
×
144

145
                if ($valueFrom) {
×
146
                        try {
147
                                $dateFrom = DateTimeHelper::tryConvertToDateTime($valueFrom, [$filter->getPhpFormat()]);
×
148
                                $dateFrom->setTime(0, 0, 0);
×
149

150
                                $this->dataSource->where(
×
NEW
151
                                        sprintf('DATE(%s) >= ?', $filter->getColumn()),
×
152
                                        $dateFrom->format('Y-m-d')
×
153
                                );
NEW
154
                        } catch (DatagridDateTimeHelperException) {
×
155
                                // ignore the invalid filter value
156
                        }
157
                }
158

159
                if ($valueTo) {
×
160
                        try {
161
                                $dateTo = DateTimeHelper::tryConvertToDateTime($valueTo, [$filter->getPhpFormat()]);
×
162
                                $dateTo->setTime(23, 59, 59);
×
163

NEW
164
                                $this->dataSource->where(
×
NEW
165
                                        sprintf('DATE(%s) <= ?', $filter->getColumn()),
×
NEW
166
                                        $dateTo->format('Y-m-d')
×
167
                                );
NEW
168
                        } catch (DatagridDateTimeHelperException) {
×
169
                                // ignore the invalid filter value
170
                        }
171
                }
172
        }
173

174
        protected function applyFilterRange(FilterRange $filter): void
1✔
175
        {
176
                $conditions = $filter->getCondition();
1✔
177

178
                $valueFrom = $conditions[$filter->getColumn()]['from'];
1✔
179
                $valueTo = $conditions[$filter->getColumn()]['to'];
1✔
180

181
                if ($valueFrom) {
1✔
182
                        $this->dataSource->where(sprintf('%s >= ?', $filter->getColumn()), $valueFrom);
1✔
183
                }
184

185
                if ($valueTo) {
1✔
186
                        $this->dataSource->where(sprintf('%s <= ?', $filter->getColumn()), $valueTo);
1✔
187
                }
188
        }
1✔
189

190
        protected function applyFilterText(FilterText $filter): void
1✔
191
        {
192
                $or = [];
1✔
193
                $args = [];
1✔
194
                $bigOr = '(';
1✔
195
                $bigOrArgs = [];
1✔
196
                $condition = $filter->getCondition();
1✔
197

198
                foreach ($condition as $column => $value) {
1✔
199
                        $like = '(';
1✔
200
                        $args = [];
1✔
201

202
                        if ($filter->isExactSearch()) {
1✔
203
                                $like .= sprintf('%s = ? OR ', $column);
1✔
204
                                $args[] = sprintf('%s', $value);
1✔
205
                        } else {
206
                                $words = $filter->hasSplitWordsSearch() === false ? [$value] : explode(' ', $value);
1✔
207

208
                                foreach ($words as $word) {
1✔
209
                                        $like .= sprintf('%s LIKE ? OR ', $column);
1✔
210
                                        $args[] = sprintf('%%%s%%', $word);
1✔
211
                                }
212
                        }
213

214
                        $like = substr($like, 0, strlen($like) - 4) . ')';
1✔
215

216
                        $or[] = $like;
1✔
217
                        $bigOr .= sprintf('%s OR ', $like);
1✔
218
                        $bigOrArgs = [...$bigOrArgs, ...$args];
1✔
219
                }
220

221
                if (count($or) > 1) {
1✔
222
                        $bigOr = substr($bigOr, 0, strlen($bigOr) - 4) . ')';
1✔
223

224
                        $query = [...[$bigOr], ...$bigOrArgs];
1✔
225

226
                        call_user_func_array([$this->dataSource, 'where'], $query);
1✔
227
                } else {
228
                        $query = [...$or, ...$args];
1✔
229

230
                        call_user_func_array([$this->dataSource, 'where'], $query);
1✔
231
                }
232
        }
1✔
233

234
        protected function applyFilterMultiSelect(FilterMultiSelect $filter): void
235
        {
236
                $condition = $filter->getCondition();
×
237
                $values = $condition[$filter->getColumn()];
×
238
                $or = '(';
×
239

NEW
240
                if ((is_countable($values) ? count($values) : 0) > 1) {
×
NEW
241
                        $length = is_countable($values) ? count($values) : 0;
×
242
                        $i = 1;
×
243

NEW
244
                        for ($iterator = 0; $iterator < (is_countable($values) ? count($values) : 0); $iterator++) {
×
245
                                if ($i === $length) {
×
246
                                        $or .= $filter->getColumn() . ' = ?)';
×
247
                                } else {
248
                                        $or .= $filter->getColumn() . ' = ? OR ';
×
249
                                }
250

251
                                $i++;
×
252
                        }
253

254
                        array_unshift($values, $or);
×
255

256
                        call_user_func_array([$this->dataSource, 'where'], $values);
×
257
                } else {
258
                        $this->dataSource->where($filter->getColumn() . ' = ?', reset($values));
×
259
                }
260
        }
261

262
        protected function applyFilterSelect(FilterSelect $filter): void
1✔
263
        {
264
                $this->dataSource->where($filter->getCondition());
1✔
265
        }
1✔
266

267
        protected function getDataSource(): Selection
268
        {
269
                return $this->dataSource;
×
270
        }
271

272
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc