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

contributte / datagrid / 8264560336

13 Mar 2024 12:24PM UTC coverage: 34.102% (-10.4%) from 44.533%
8264560336

push

github

web-flow
[7.x] Next (#1060)

* PHP 8.0

* Translator: switch from ITranslator to Translator (#973)

* [7.x] Bootstrap 5 + PHP 8 + vanilla javascript (#1021)

* Bootstrap 5

* Bootstrap 5 (docs)

* Bootstrap 5

* form-control -> form-select

* Bump bootstrap-select for Bootstrap 5 support

* Removed `input-sm` from Bootstrap 3

See https://getbootstrap.com/docs/4.0/migration/#forms-1

* Bootstrap 5: When selectpicker, replace form-select classes with form-control and refresh it

* Hide `underline` also for `dropdown-item`. And merged into one CSS rule.

* Update the filterMultiSelect initialization

* Text-align: left -> start

Co-authored-by: Radim Vaculík <radim.vaculik@gmail.com>
Co-authored-by: Jaroslav Líbal <jaroslav.libal@neatous.cz>

* [7.x] phpstan-deprecation-rules (#1061)

* Fix sort

* Add method for setting custom Action href, v6.x (#853)

* [7.x] Nextras ORM 4 support, closes #984

* Fix ElasticsearchDataSource.php data source (#1041)

* Error: Typed property Ublaboo\DataGrid\InlineEdit\InlineEdit::$itemID must not be accessed before initialization

* composer: allow-plugins: php-http/discovery

* ItemDetailForm: $httpPost: Typed property must not be accessed...

* phpstan: revert  --memory-limit=4G

* NetteDatabaseSelectionHelper: Context -> Explorer, getSupplementalDriver -> getDriver

* Update README.md

* Templates: fix variables

* data-bs-toggle attribute for multiaction button (#1072)

* dependabot.yml (#1078)

* Allow nette/utils:4.0 (#1077)

* Add onColumnShow and onColumnHide event methods (#1076)

* Add onColumnShow and onColumnHide event methods

* Add native type array

* Removed duplicity

* Return value of BackedEnum when reading Doctrine entity property. (#1081)

* Return value of BackedEnum when reading array value (#1083)

* Added method to check if filter is on default values; Closes #1082 (#1084)

* ublaboo -> contributte (#1067) (#1075)

* Delete depe... (continued)

117 of 435 new or added lines in 54 files covered. (26.9%)

613 existing lines in 31 files now uncovered.

1124 of 3296 relevant lines covered (34.1%)

0.34 hits per line

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

51.11
/src/DataSource/DibiFluentDataSource.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Datagrid\DataSource;
4

5
use Contributte\Datagrid\AggregationFunction\IAggregatable;
6
use Contributte\Datagrid\AggregationFunction\IAggregationFunction;
7
use Contributte\Datagrid\Exception\DatagridDateTimeHelperException;
8
use Contributte\Datagrid\Filter\FilterDate;
9
use Contributte\Datagrid\Filter\FilterDateRange;
10
use Contributte\Datagrid\Filter\FilterMultiSelect;
11
use Contributte\Datagrid\Filter\FilterRange;
12
use Contributte\Datagrid\Filter\FilterSelect;
13
use Contributte\Datagrid\Filter\FilterText;
14
use Contributte\Datagrid\Utils\DateTimeHelper;
15
use Contributte\Datagrid\Utils\Sorting;
16
use dibi;
17
use Dibi\Fluent;
18
use Dibi\Helpers;
19
use ReflectionClass;
20

21
class DibiFluentDataSource extends FilterableDataSource implements IDataSource, IAggregatable
22
{
23

24
        protected array $data = [];
25

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

30
        public function getCount(): int
31
        {
32
                return $this->dataSource->count();
1✔
33
        }
34

35
        /**
36
         * {@inheritDoc}
37
         */
38
        public function getData(): array
39
        {
40
                return $this->data !== [] ? $this->data : $this->dataSource->fetchAll();
1✔
41
        }
42

43
        /**
44
         * {@inheritDoc}
45
         */
46
        public function filterOne(array $condition): IDataSource
1✔
47
        {
48
                $this->dataSource->where($condition)->limit(1);
1✔
49

50
                return $this;
1✔
51
        }
52

53
        public function limit(int $offset, int $limit): IDataSource
1✔
54
        {
55
                $this->dataSource->limit($limit)->offset($offset);
1✔
56

57
                $this->data = $this->dataSource->fetchAll();
1✔
58

59
                return $this;
1✔
60
        }
61

62
        public function sort(Sorting $sorting): IDataSource
1✔
63
        {
64
                if (is_callable($sorting->getSortCallback())) {
1✔
65
                        call_user_func(
×
66
                                $sorting->getSortCallback(),
×
67
                                $this->dataSource,
×
68
                                $sorting->getSort()
×
69
                        );
70

71
                        return $this;
×
72
                }
73

74
                $sort = $sorting->getSort();
1✔
75

76
                if ($sort !== []) {
1✔
77
                        $this->dataSource->removeClause('ORDER BY');
1✔
78
                        $this->dataSource->orderBy($sort);
1✔
79
                } else {
80
                        /**
81
                         * Has the statement already a order by clause?
82
                         */
83
                        $this->dataSource->clause('ORDER BY');
×
84

85
                        $reflection = new ReflectionClass(Fluent::class);
×
86

87
                        $cursorProperty = $reflection->getProperty('cursor');
×
88
                        $cursorProperty->setAccessible(true);
×
89
                        $cursor = $cursorProperty->getValue($this->dataSource);
×
90

91
                        if (!(bool) $cursor) {
×
92
                                $this->dataSource->orderBy($this->primaryKey);
×
93
                        }
94
                }
95

96
                return $this;
1✔
97
        }
98

99
        public function processAggregation(IAggregationFunction $function): void
100
        {
101
                $function->processDataSource(clone $this->dataSource);
×
102
        }
103

104
        protected function applyFilterDate(FilterDate $filter): void
105
        {
106
                $conditions = $filter->getCondition();
×
107

108
                try {
109
                        $date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
×
110

111
                        $this->dataSource->where('DATE(%n) = ?', $filter->getColumn(), $date->format('Y-m-d'));
×
NEW
112
                } catch (DatagridDateTimeHelperException) {
×
113
                        // ignore the invalid filter value
114
                }
115
        }
116

117
        protected function applyFilterDateRange(FilterDateRange $filter): void
118
        {
119
                $conditions = $filter->getCondition();
×
120

121
                $valueFrom = $conditions[$filter->getColumn()]['from'];
×
122
                $valueTo = $conditions[$filter->getColumn()]['to'];
×
123

124
                if ($valueFrom) {
×
125
                        try {
126
                                $dateFrom = DateTimeHelper::tryConvertToDateTime($valueFrom, [$filter->getPhpFormat()]);
×
127
                                $dateFrom->setTime(0, 0, 0);
×
128

129
                                $this->dataSource->where('DATE(%n) >= ?', $filter->getColumn(), $dateFrom);
×
NEW
130
                        } catch (DatagridDateTimeHelperException) {
×
131
                                // ignore the invalid filter value
132
                        }
133
                }
134

135
                if ($valueTo) {
×
136
                        try {
137
                                $dateTo = DateTimeHelper::tryConvertToDateTime($valueTo, [$filter->getPhpFormat()]);
×
138
                                $dateTo->setTime(23, 59, 59);
×
139

140
                                $this->dataSource->where('DATE(%n) <= ?', $filter->getColumn(), $dateTo);
×
NEW
141
                        } catch (DatagridDateTimeHelperException) {
×
142
                                // ignore the invalid filter value
143
                        }
144
                }
145
        }
146

147
        protected function applyFilterRange(FilterRange $filter): void
1✔
148
        {
149
                $conditions = $filter->getCondition();
1✔
150

151
                $valueFrom = $conditions[$filter->getColumn()]['from'];
1✔
152
                $valueTo = $conditions[$filter->getColumn()]['to'];
1✔
153

154
                if ($valueFrom || $valueFrom !== '') {
1✔
155
                        $this->dataSource->where('%n >= ?', $filter->getColumn(), $valueFrom);
1✔
156
                }
157

158
                if ($valueTo || $valueTo !== '') {
1✔
159
                        $this->dataSource->where('%n <= ?', $filter->getColumn(), $valueTo);
1✔
160
                }
161
        }
1✔
162

163
        protected function applyFilterText(FilterText $filter): void
1✔
164
        {
165
                $condition = $filter->getCondition();
1✔
166
                $driver = $this->dataSource->getConnection()->getDriver();
1✔
167
                $or = [];
1✔
168

169
                foreach ($condition as $column => $value) {
1✔
170
                        $column = Helpers::escape($driver, $column, dibi::IDENTIFIER);
1✔
171

172
                        if ($filter->isExactSearch()) {
1✔
173
                                $this->dataSource->where(sprintf('%s = %%s', $column), $value);
1✔
174

175
                                continue;
1✔
176
                        }
177

178
                        $words = $filter->hasSplitWordsSearch() === false ? [$value] : explode(' ', $value);
1✔
179

180
                        foreach ($words as $word) {
1✔
181
                                $or[] = [sprintf('%s LIKE %%~like~', $column), $word];
1✔
182
                        }
183
                }
184

185
                if (count($or) > 1) {
1✔
186
                        $this->dataSource->where('(%or)', $or);
1✔
187
                } else {
188
                        $this->dataSource->where($or);
1✔
189
                }
190
        }
1✔
191

192
        protected function applyFilterMultiSelect(FilterMultiSelect $filter): void
193
        {
194
                $condition = $filter->getCondition();
×
195
                $values = $condition[$filter->getColumn()];
×
196

NEW
197
                if ((is_countable($values) ? count($values) : 0) > 1) {
×
198
                        $value1 = array_shift($values);
×
NEW
199
                        $length = count($values);
×
200
                        $i = 1;
×
201

202
                        $this->dataSource->where('(%n = ?', $filter->getColumn(), $value1);
×
203

204
                        foreach ($values as $value) {
×
205
                                if ($i === $length) {
×
206
                                        $this->dataSource->__call('or', ['%n = ?)', $filter->getColumn(), $value]);
×
207
                                } else {
208
                                        $this->dataSource->__call('or', ['%n = ?', $filter->getColumn(), $value]);
×
209
                                }
210

211
                                $i++;
×
212
                        }
213
                } else {
214
                        $this->dataSource->where('%n = ?', $filter->getColumn(), reset($values));
×
215
                }
216
        }
217

218
        protected function applyFilterSelect(FilterSelect $filter): void
1✔
219
        {
220
                $this->dataSource->where($filter->getCondition());
1✔
221
        }
1✔
222

223
        protected function getDataSource(): Fluent
224
        {
225
                return $this->dataSource;
×
226
        }
227

228
}
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