Completed
Pull Request — master (#648)
by
unknown
12:11
created

GroupActionCollection   B

Complexity

Total Complexity 30

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 30
c 0
b 0
f 0
lcom 1
cbo 16
dl 0
loc 203
rs 8.4614

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F addToFormContainer() 0 86 13
B submitted() 0 24 5
A addGroupSelectAction() 0 6 2
A addGroupMultiSelectAction() 0 6 2
A addGroupTextAction() 0 6 2
A addGroupTextareaAction() 0 6 2
A getGroupAction() 0 10 3
1
<?php declare(strict_types = 1);
2
3
namespace Ublaboo\DataGrid\GroupAction;
4
5
use Nette;
6
use Nette\Application\UI\Form;
7
use Nette\SmartObject;
8
use Ublaboo\DataGrid\DataGrid;
9
use Ublaboo\DataGrid\Exception\DataGridGroupActionException;
10
11
class GroupActionCollection
12
{
13
14
	use SmartObject;
15
16
	public const ID_ATTRIBUTE_PREFIX = 'group_action_item_';
17
18
	/**
19
	 * @var GroupAction[]
20
	 */
21
	protected $group_actions = [];
22
23
	/**
24
	 * @var DataGrid
25
	 */
26
	protected $datagrid;
27
28
	public function __construct(DataGrid $datagrid)
29
	{
30
		$this->datagrid = $datagrid;
31
	}
32
33
34
	/**
35
	 * Get assambled form
36
	 */
37
	public function addToFormContainer(Nette\Forms\Container $container): void
38
	{
39
		/** @var Nette\Application\UI\Form $form */
40
		$form = $container->lookup('Nette\Application\UI\Form');
41
		$translator = $form->getTranslator();
42
		$main_options = [];
43
44
		/**
45
		 * First foreach for filling "main" select
46
		 */
47
		foreach ($this->group_actions as $id => $action) {
48
			$main_options[$id] = $action->getTitle();
49
		}
50
51
		$container->addSelect('group_action', '', $main_options)
52
			->setPrompt('ublaboo_datagrid.choose');
53
54
		/**
55
		 * Second for creating select for each "sub"-action
56
		 */
57
		foreach ($this->group_actions as $id => $action) {
58
			$control = null;
59
60
			if ($action instanceof GroupSelectAction) {
61
				if ($action->hasOptions()) {
62
					if ($action instanceof GroupMultiSelectAction) {
63
						$control = $container->addMultiSelect($id, '', $action->getOptions());
64
						$control->setAttribute('data-datagrid-multiselect-id', static::ID_ATTRIBUTE_PREFIX . $id);
0 ignored issues
show
Documentation introduced by
static::ID_ATTRIBUTE_PREFIX . $id is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
						$control->setAttribute('data-style', 'hidden');
0 ignored issues
show
Documentation introduced by
'hidden' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
						$control->setAttribute('data-selected-icon-check', DataGrid::$icon_prefix . 'check');
0 ignored issues
show
Documentation introduced by
\Ublaboo\DataGrid\DataGr...:$icon_prefix . 'check' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
					} else {
68
						$control = $container->addSelect($id, '', $action->getOptions());
69
					}
70
71
					$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id);
0 ignored issues
show
Documentation introduced by
static::ID_ATTRIBUTE_PREFIX . $id is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
				}
73
74
			} elseif ($action instanceof GroupTextAction) {
75
				$control = $container->addText($id, '');
76
77
				$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id)
0 ignored issues
show
Documentation introduced by
static::ID_ATTRIBUTE_PREFIX . $id is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to the method Nette\Forms\Rules::endCondition() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
78
					->addConditionOn($container['group_action'], Form::EQUAL, $id)
79
					->setRequired($translator->translate('ublaboo_datagrid.choose_input_required'))
80
					->endCondition();
81
82
			} elseif ($action instanceof GroupTextareaAction) {
83
				$control = $container->addTextarea($id, '');
84
85
				$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id)
0 ignored issues
show
Documentation introduced by
static::ID_ATTRIBUTE_PREFIX . $id is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
					->addConditionOn($container['group_action'], Form::EQUAL, $id)
87
					->setRequired($translator->translate('ublaboo_datagrid.choose_input_required'));
88
			}
89
90
			if ($control) {
91
				/**
92
				 * User may set a class to the form control
93
				 */
94
				if ($class = $action->getClass()) {
95
					$control->setAttribute('class', $class);
0 ignored issues
show
Documentation introduced by
$class is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
				}
97
98
				/**
99
				 * User may set additional attribtues to the form control
100
				 */
101
				foreach ($action->getAttributes() as $name => $value) {
102
					$control->setAttribute($name, $value);
103
				}
104
			}
105
		}
106
107
		foreach ($this->group_actions as $id => $action) {
108
			$container['group_action']->addCondition(Form::EQUAL, $id)
109
				->toggle(static::ID_ATTRIBUTE_PREFIX . $id);
110
		}
111
112
		$container['group_action']->addCondition(Form::FILLED)
113
			->toggle(strtolower($this->datagrid->getName()) . 'group_action_submit');
114
115
		$container->addSubmit('submit', 'ublaboo_datagrid.execute')
116
			->setValidationScope([$container])
117
			->setAttribute('id', strtolower($this->datagrid->getName()) . 'group_action_submit');
0 ignored issues
show
Documentation introduced by
strtolower($this->datagr.... 'group_action_submit' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
119
		if ($form instanceof Nette\ComponentModel\IComponent) {
120
			$form->onSubmit[] = [$this, 'submitted'];
121
		}
122
	}
123
124
125
	/**
126
	 * Pass "sub"-form submission forward to custom submit function
127
	 */
128
	public function submitted(Form $form): void
129
	{
130
		if (!isset($form['group_action']['submit']) || !$form['group_action']['submit']->isSubmittedBy()) {
131
			return;
132
		}
133
134
		$values = $form->getValues();
135
		$values = $values['group_action'];
136
137
		if ($values->group_action === 0 || $values->group_action === null) {
138
			return;
139
		}
140
141
		/**
142
		 * @todo Define items IDs
143
		 */
144
		$http_ids = $form->getHttpData(Form::DATA_LINE | Form::DATA_KEYS, strtolower($this->datagrid->getName()) . '_group_action_item[]');
145
		$ids = array_keys($http_ids);
146
147
		$id = $values->group_action;
148
		$this->group_actions[$id]->onSelect($ids, $values->{$id} ?? null);
149
150
		$form['group_action']['group_action']->setValue(null);
151
	}
152
153
154
	/**
155
	 * Add one group action (select box) to collection of actions
156
	 *
157
	 * @param array  $options
158
	 */
159
	public function addGroupSelectAction(string $title, array $options): GroupAction
160
	{
161
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
162
163
		return $this->group_actions[$id] = new GroupSelectAction($title, $options);
164
	}
165
166
167
	/**
168
	 * Add one group action (multiselect box) to collection of actions
169
	 *
170
	 * @param array  $options
171
	 */
172
	public function addGroupMultiSelectAction(string $title, array $options): GroupAction
173
	{
174
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
175
176
		return $this->group_actions[$id] = new GroupMultiSelectAction($title, $options);
177
	}
178
179
180
	/**
181
	 * Add one group action (text input) to collection of actions
182
	 */
183
	public function addGroupTextAction(string $title): GroupAction
184
	{
185
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
186
187
		return $this->group_actions[$id] = new GroupTextAction($title);
188
	}
189
190
191
	/**
192
	 * Add one group action (textarea) to collection of actions
193
	 */
194
	public function addGroupTextareaAction(string $title): GroupAction
195
	{
196
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
197
198
		return $this->group_actions[$id] = new GroupTextareaAction($title);
199
	}
200
201
202
	public function getGroupAction(string $title): GroupAction
203
	{
204
		foreach ($this->group_actions as $action) {
205
			if ($action->getTitle() === $title) {
206
				return $action;
207
			}
208
		}
209
210
		throw new DataGridGroupActionException("Group action $title does not exist.");
211
	}
212
213
}
214