Completed
Push — master ( da997d...aea0be )
by Martin
24:25 queued 21:10
created

GroupActionCollection::submitted()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 6
eloc 12
nc 3
nop 1
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\GroupAction;
10
11
use Nette;
12
use Nette\Application\UI\Form;
13
use Ublaboo\DataGrid\DataGrid;
14
use Ublaboo\DataGrid\Exception\DataGridGroupActionException;
15
16
class GroupActionCollection extends Nette\Object
17
{
18
19
	const ID_ATTRIBUTE_PREFIX = 'group_action_item_';
20
21
	/**
22
	 * @var GroupAction[]
23
	 */
24
	protected $group_actions = [];
25
26
	/**
27
	 * @var DataGrid
28
	 */
29
	protected $datagrid;
30
31
32
	public function __construct(DataGrid $datagrid)
33
	{
34
		$this->datagrid = $datagrid;
35
	}
36
37
38
	/**
39
	 * Get assambled form
40
	 * @param Nette\Forms\Container $container
41
	 * @return void
42
	 */
43
	public function addToFormContainer($container)
44
	{
45
		/** @var Nette\Application\UI\Form $form */
46
		$form = $container->lookup('Nette\Application\UI\Form');
47
		$translator = $form->getTranslator();
48
		$main_options = [];
49
50
		/**
51
		 * First foreach for filling "main" select
52
		 */
53
		foreach ($this->group_actions as $id => $action) {
54
			$main_options[$id] = $action->getTitle();
55
		}
56
57
		$container->addSelect('group_action', '', $main_options)
58
			->setPrompt('ublaboo_datagrid.choose');
59
60
		/**
61
		 * Second for creating select for each "sub"-action
62
		 */
63
		foreach ($this->group_actions as $id => $action) {
64
			$control = NULL;
65
66
			if ($action instanceof GroupSelectAction) {
67
				if ($action->hasOptions()) {
68
					if ($action instanceof GroupMultiSelectAction) {
69
						$control = $container->addMultiSelect($id, '', $action->getOptions());
70
						$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...
71
						$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...
72
						$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...
73
					} else {
74
						$control = $container->addSelect($id, '', $action->getOptions());
75
					}
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...
78
				}
79
80
			} else if ($action instanceof GroupTextAction) {
81
				$control = $container->addText($id, '');
82
83
				$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id)
0 ignored issues
show
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...
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...
84
					->addConditionOn($container['group_action'], Form::EQUAL, $id)
85
						->setRequired($translator->translate('ublaboo_datagrid.choose_input_required'))
86
					->endCondition();
87
88
			} else if ($action instanceof GroupTextareaAction) {
89
				$control = $container->addTextarea($id, '');
90
91
				$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...
92
					->addConditionOn($container['group_action'], Form::EQUAL, $id)
93
						->setRequired($translator->translate('ublaboo_datagrid.choose_input_required'));
94
			}
95
96
			if ($control) {
97
				/**
98
				 * User may set a class to the form control
99
				 */
100
				if ($class = $action->getClass()) {
101
					$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...
102
				}
103
104
				/**
105
				 * User may set additional attribtues to the form control
106
				 */
107
				foreach ($action->getAttributes() as $name => $value) {
108
					$control->setAttribute($name, $value);
109
				}
110
			}
111
		}
112
113
		foreach ($this->group_actions as $id => $action) {
114
			$container['group_action']->addCondition(Form::EQUAL, $id)
115
				->toggle(static::ID_ATTRIBUTE_PREFIX.$id);
116
		}
117
118
		$container['group_action']->addCondition(Form::FILLED)
119
			->toggle(strtolower($this->datagrid->getName()) . 'group_action_submit');
120
121
		$container->addSubmit('submit', 'ublaboo_datagrid.execute')
122
			->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...
123
124
		if ($form instanceof Nette\ComponentModel\IComponent) {
125
			$form->onSubmit[] = [$this, 'submitted'];
126
		}
127
	}
128
129
130
	/**
131
	 * Pass "sub"-form submission forward to custom submit function
132
	 * @param  Form   $form
133
	 * @return void
134
	 */
135
	public function submitted(Form $form)
136
	{
137
		if (!isset($form['group_action']['submit']) || !$form['group_action']['submit']->isSubmittedBy()) {
138
			return;
139
		}
140
141
		$values = $form->getValues();
142
		$values = $values['group_action'];
143
144
		if ($values->group_action === 0 || is_null($values->group_action)) {
145
			return;
146
		}
147
148
		/**
149
		 * @todo Define items IDs
150
		 */
151
		$http_ids = $form->getHttpData(Form::DATA_LINE|Form::DATA_KEYS, strtolower($this->datagrid->getName()) . '_group_action_item[]');
152
		$ids = array_keys($http_ids);
153
154
		$id = $values->group_action;
155
		$this->group_actions[$id]->onSelect($ids, isset($values->{$id}) ? $values->{$id} : NULL);
156
157
		$form['group_action']['group_action']->setValue(NULL);
158
	}
159
160
161
	/**
162
	 * Add one group action (select box) to collection of actions
163
	 *
164
	 * @param string $title
165
	 * @param array  $options
166
	 *
167
	 * @return GroupAction
168
	 */
169
	public function addGroupSelectAction($title, $options)
170
	{
171
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
172
173
		return $this->group_actions[$id] = new GroupSelectAction($title, $options);
174
	}
175
176
177
	/**
178
	 * Add one group action (multiselect box) to collection of actions
179
	 *
180
	 * @param string $title
181
	 * @param array  $options
182
	 *
183
	 * @return GroupAction
184
	 */
185
	public function addGroupMultiSelectAction($title, $options)
186
	{
187
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
188
189
		return $this->group_actions[$id] = new GroupMultiSelectAction($title, $options);
190
	}
191
192
193
	/**
194
	 * Add one group action (text input) to collection of actions
195
	 *
196
	 * @param string $title
197
	 *
198
	 * @return GroupAction
199
	 */
200
	public function addGroupTextAction($title)
201
	{
202
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
203
204
		return $this->group_actions[$id] = new GroupTextAction($title);
205
	}
206
207
208
	/**
209
	 * Add one group action (textarea) to collection of actions
210
	 *
211
	 * @param string $title
212
	 *
213
	 * @return GroupAction
214
	 */
215
	public function addGroupTextareaAction($title)
216
	{
217
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
218
219
		return $this->group_actions[$id] = new GroupTextareaAction($title);
220
	}
221
222
223
	/**
224
	 * @param  string $title
225
	 * @return GroupAction
226
	 */
227
	public function getGroupAction($title)
228
	{
229
		foreach ($this->group_actions as $action) {
230
			if ($action->getTitle() === $title) {
231
				return $action;
232
			}
233
		}
234
235
		throw new DataGridGroupActionException("Group action $title does not exist.");
236
	}
237
238
}
239