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

contributte / datagrid / 8685602316

13 Mar 2024 12:24PM UTC coverage: 34.102%. Remained the same
8685602316

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%)

1455 existing lines in 67 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

0.0
/src/GroupAction/GroupActionCollection.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Datagrid\GroupAction;
4

5
use Contributte\Datagrid\Datagrid;
6
use Contributte\Datagrid\Exception\DatagridGroupActionException;
7
use Nette\Application\UI\Form;
8
use Nette\Forms\Container;
9
use Nette\Forms\Controls\SelectBox;
10
use Nette\Forms\Controls\SubmitButton;
11
use Nette\Forms\Form as NetteForm;
12
use UnexpectedValueException;
13

14
class GroupActionCollection
15
{
16

17
        private const ID_ATTRIBUTE_PREFIX = '_item_';
18

19
        /** @var array<GroupAction> */
20
        protected array $groupActions = [];
21

NEW
22
        public function __construct(protected Datagrid $datagrid)
×
23
        {
24
        }
25

26
        public function addToFormContainer(Container $container): void
27
        {
28
                /** @var Form $form */
UNCOV
29
                $form = $container->lookup(Form::class);
×
30
                $lookupPath = $container->lookupPath();
×
31
                $translator = $form->getTranslator();
×
32
                $main_options = [];
×
33

UNCOV
34
                if ($translator === null) {
×
NEW
35
                        throw new UnexpectedValueException();
×
36
                }
37

38
                /**
39
                 * First foreach for adding button actions
40
                 */
UNCOV
41
                foreach ($this->groupActions as $id => $action) {
×
42
                        if ($action instanceof GroupButtonAction) {
×
43
                                $control = $container->addSubmit((string) $id, $action->getTitle());
×
44

45
                                /**
46
                                 * User may set a class to the form control
47
                                 */
NEW
48
                                $control->setHtmlAttribute('class', $action->getClass());
×
49

50
                                /**
51
                                 * User may set additional attribtues to the form control
52
                                 */
53
                                foreach ($action->getAttributes() as $name => $value) {
×
NEW
54
                                        $control->setHtmlAttribute($name, $value);
×
55
                                }
56
                        }
57
                }
58

59
                /**
60
                 * Second foreach for filling "main" select
61
                 */
UNCOV
62
                foreach ($this->groupActions as $id => $action) {
×
63
                        if (! $action instanceof GroupButtonAction) {
×
64
                                $main_options[$id] = $action->getTitle();
×
65
                        }
66
                }
67

68
                $groupActionSelect = $container->addSelect('group_action', '', $main_options)
×
NEW
69
                        ->setPrompt('contributte_datagrid.choose');
×
70

71
                /**
72
                 * Third for creating select for each "sub"-action
73
                 */
74
                foreach ($this->groupActions as $id => $action) {
×
UNCOV
75
                        $control = null;
×
76

77
                        if ($action instanceof GroupSelectAction) {
×
UNCOV
78
                                if ($action->hasOptions()) {
×
79
                                        if ($action instanceof GroupMultiSelectAction) {
×
80
                                                $control = $container->addMultiSelect((string) $id, '', $action->getOptions());
×
NEW
81
                                                $control->setHtmlAttribute('data-datagrid-multiselect-id', $lookupPath . self::ID_ATTRIBUTE_PREFIX . $id);
×
NEW
82
                                                $control->setHtmlAttribute('data-style', 'hidden');
×
NEW
83
                                                $control->setHtmlAttribute('data-selected-icon-check', Datagrid::$iconPrefix . 'check');
×
84
                                        } else {
85
                                                $control = $container->addSelect((string) $id, '', $action->getOptions());
×
86
                                        }
87

NEW
88
                                        $control->setHtmlAttribute('id', $lookupPath . self::ID_ATTRIBUTE_PREFIX . $id);
×
89
                                }
90
                        } elseif ($action instanceof GroupTextAction) {
×
91
                                $control = $container->addText((string) $id, '');
×
92

NEW
93
                                $control->setHtmlAttribute('id', $lookupPath . self::ID_ATTRIBUTE_PREFIX . $id)
×
NEW
94
                                        ->addConditionOn($groupActionSelect, Form::Equal, $id)
×
NEW
95
                                        ->setRequired('contributte_datagrid.choose_input_required')
×
UNCOV
96
                                        ->endCondition();
×
97

UNCOV
98
                        } elseif ($action instanceof GroupTextareaAction) {
×
99
                                $control = $container->addTextArea((string) $id, '');
×
100

NEW
101
                                $control->setHtmlAttribute('id', $lookupPath . self::ID_ATTRIBUTE_PREFIX . $id)
×
NEW
102
                                        ->addConditionOn($groupActionSelect, Form::Equal, $id)
×
NEW
103
                                        ->setRequired('contributte_datagrid.choose_input_required');
×
104
                        }
105

UNCOV
106
                        if (isset($control)) {
×
107
                                /**
108
                                 * User may set a class to the form control
109
                                 */
NEW
110
                                $control->setHtmlAttribute('class', $action->getClass());
×
111

112
                                /**
113
                                 * User may set additional attribtues to the form control
114
                                 */
UNCOV
115
                                foreach ($action->getAttributes() as $name => $value) {
×
NEW
116
                                        $control->setHtmlAttribute($name, $value);
×
117
                                }
118
                        }
119
                }
120

121
                if ($main_options !== []) {
×
122
                        foreach (array_keys($this->groupActions) as $id) {
×
NEW
123
                                $groupActionSelect->addCondition(Form::Equal, $id)
×
124
                                        ->toggle($lookupPath . self::ID_ATTRIBUTE_PREFIX . $id);
×
125
                        }
126

NEW
127
                        $groupActionSelect->addCondition(Form::Filled)
×
128
                                ->toggle(
×
UNCOV
129
                                        strtolower($this->datagrid->getFullName()) . 'group_action_submit'
×
130
                                );
131

NEW
132
                        $container->addSubmit('submit', 'contributte_datagrid.execute')
×
UNCOV
133
                                ->setValidationScope([$container])
×
NEW
134
                                ->setHtmlAttribute(
×
UNCOV
135
                                        'id',
×
UNCOV
136
                                        strtolower($this->datagrid->getFullName()) . 'group_action_submit'
×
137
                                );
138
                } else {
UNCOV
139
                        unset($container['group_action']);
×
140
                }
141

UNCOV
142
                $form->onSubmit[] = function (NetteForm $form): void {
×
143
                        $this->submitted($form);
144
                };
145
        }
146

147
        /**
148
         * Pass "sub"-form submission forward to custom submit function
149
         */
150
        public function submitted(NetteForm $form): void
151
        {
152
                $submitter = $this->getFormSubmitter($form);
×
153

UNCOV
154
                if (! $submitter instanceof SubmitButton) {
×
155
                        return;
×
156
                }
157

UNCOV
158
                $values = (array) $form->getValues();
×
UNCOV
159
                $values = $values['group_action'];
×
160

161
                if (
162
                        ($submitter->getName() === 'submit' && $submitter->isSubmittedBy())
×
163
                         && ($values->group_action === 0 || $values->group_action === null)) {
×
164
                        return;
×
165
                }
166

167
                $httpIds = $form->getHttpData(
×
NEW
168
                        Form::DataLine | Form::DataKeys,
×
169
                        strtolower($this->datagrid->getFullName()) . '_group_action_item[]'
×
170
                );
171

172
                $ids = array_keys($httpIds);
×
173

UNCOV
174
                if ($submitter->getName() === 'submit') {
×
175
                        $id = $values->group_action;
×
UNCOV
176
                        $this->groupActions[$id]->onSelect($ids, $values[$id] ?? null);
×
177

178
                        if (!$form['group_action'] instanceof Container) {
×
NEW
179
                                throw new UnexpectedValueException();
×
180
                        }
181

UNCOV
182
                        if (isset($form['group_action']['group_action'])) {
×
UNCOV
183
                                if (!$form['group_action']['group_action'] instanceof SelectBox) {
×
NEW
184
                                        throw new UnexpectedValueException();
×
185
                                }
186

UNCOV
187
                                $form['group_action']['group_action']->setValue(null);
×
188
                        }
189
                } else {
190
                        $groupButtonAction = $this->groupActions[$submitter->getName()];
×
191

192
                        if (!$groupButtonAction instanceof GroupButtonAction) {
×
NEW
193
                                throw new UnexpectedValueException('This action is supposed to be a GroupButtonAction');
×
194
                        }
195

UNCOV
196
                        $groupButtonAction->onClick($ids);
×
197
                }
198
        }
199

200
        /**
201
         * Add one group button action to collection of actions
202
         */
203
        public function addGroupButtonAction(string $title, ?string $class = null): GroupButtonAction
204
        {
NEW
205
                $id = count($this->groupActions) > 0 ? count($this->groupActions) + 1 : 1;
×
206

207
                return $this->groupActions[$id] = new GroupButtonAction($title, $class);
×
208
        }
209

210
        /**
211
         * Add one group action (select box) to collection of actions
212
         */
213
        public function addGroupSelectAction(string $title, array $options): GroupAction
214
        {
NEW
215
                $id = count($this->groupActions) > 0 ? count($this->groupActions) + 1 : 1;
×
216

UNCOV
217
                return $this->groupActions[$id] = new GroupSelectAction($title, $options);
×
218
        }
219

220
        /**
221
         * Add one group action (multiselect box) to collection of actions
222
         */
223
        public function addGroupMultiSelectAction(string $title, array $options): GroupAction
224
        {
NEW
225
                $id = count($this->groupActions) > 0 ? count($this->groupActions) + 1 : 1;
×
226

UNCOV
227
                return $this->groupActions[$id] = new GroupMultiSelectAction($title, $options);
×
228
        }
229

230
        /**
231
         * Add one group action (text input) to collection of actions
232
         */
233
        public function addGroupTextAction(string $title): GroupAction
234
        {
NEW
235
                $id = count($this->groupActions) > 0 ? count($this->groupActions) + 1 : 1;
×
236

UNCOV
237
                return $this->groupActions[$id] = new GroupTextAction($title);
×
238
        }
239

240
        /**
241
         * Add one group action (textarea) to collection of actions
242
         */
243
        public function addGroupTextareaAction(string $title): GroupAction
244
        {
NEW
245
                $id = count($this->groupActions) > 0 ? count($this->groupActions) + 1 : 1;
×
246

UNCOV
247
                return $this->groupActions[$id] = new GroupTextareaAction($title);
×
248
        }
249

250
        public function getGroupAction(string $title): GroupAction
251
        {
UNCOV
252
                foreach ($this->groupActions as $action) {
×
UNCOV
253
                        if ($action->getTitle() === $title) {
×
UNCOV
254
                                return $action;
×
255
                        }
256
                }
257

NEW
258
                throw new DatagridGroupActionException(sprintf('Group action %s does not exist.', $title));
×
259
        }
260

261
        private function getFormSubmitter(NetteForm $form): ?SubmitButton
262
        {
UNCOV
263
                $container = $form['group_action'];
×
264

UNCOV
265
                if (!$container instanceof Container) {
×
NEW
266
                        throw new UnexpectedValueException();
×
267
                }
268

UNCOV
269
                if (isset($container['submit'])) {
×
UNCOV
270
                        if (!$container['submit'] instanceof SubmitButton) {
×
NEW
271
                                throw new UnexpectedValueException();
×
272
                        }
273

UNCOV
274
                        if ($container['submit']->isSubmittedBy()) {
×
UNCOV
275
                                return $container['submit'];
×
276
                        }
277
                }
278

UNCOV
279
                foreach ($container->getComponents() as $component) {
×
UNCOV
280
                        if ($component instanceof SubmitButton && $component->isSubmittedBy()) {
×
UNCOV
281
                                return $component;
×
282
                        }
283
                }
284

UNCOV
285
                return null;
×
286
        }
287

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