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

contributte / datagrid / 8230471191

11 Mar 2024 09:25AM UTC coverage: 34.102%. First build
8230471191

Pull #1060

github

radimvaculik
Fix phpstan
Pull Request #1060: [7.x] Next

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

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 */
29
                $form = $container->lookup(Form::class);
×
30
                $lookupPath = $container->lookupPath();
×
31
                $translator = $form->getTranslator();
×
32
                $main_options = [];
×
33

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

38
                /**
39
                 * First foreach for adding button actions
40
                 */
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
                 */
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) {
×
75
                        $control = null;
×
76

77
                        if ($action instanceof GroupSelectAction) {
×
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')
×
96
                                        ->endCondition();
×
97

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

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
                                 */
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(
×
129
                                        strtolower($this->datagrid->getFullName()) . 'group_action_submit'
×
130
                                );
131

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

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

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

158
                $values = (array) $form->getValues();
×
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

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

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

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

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

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

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

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

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

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

250
        public function getGroupAction(string $title): GroupAction
251
        {
252
                foreach ($this->groupActions as $action) {
×
253
                        if ($action->getTitle() === $title) {
×
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
        {
263
                $container = $form['group_action'];
×
264

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

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

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

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

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