Completed
Push — master ( e53043...ebbcfb )
by Pavel
06:02
created

GroupActionCollection::addGroupTextAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
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
            ->setValidationScope([$container])
123
			->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...
124
125
		if ($form instanceof Nette\ComponentModel\IComponent) {
126
			$form->onSubmit[] = [$this, 'submitted'];
127
		}
128
	}
129
130
131
	/**
132
	 * Pass "sub"-form submission forward to custom submit function
133
	 * @param  Form   $form
134
	 * @return void
135
	 */
136
	public function submitted(Form $form)
137
	{
138
		if (!isset($form['group_action']['submit']) || !$form['group_action']['submit']->isSubmittedBy()) {
139
			return;
140
		}
141
142
		$values = $form->getValues();
143
		$values = $values['group_action'];
144
145
		if ($values->group_action === 0 || is_null($values->group_action)) {
146
			return;
147
		}
148
149
		/**
150
		 * @todo Define items IDs
151
		 */
152
		$http_ids = $form->getHttpData(Form::DATA_LINE|Form::DATA_KEYS, strtolower($this->datagrid->getName()) . '_group_action_item[]');
153
		$ids = array_keys($http_ids);
154
155
		$id = $values->group_action;
156
		$this->group_actions[$id]->onSelect($ids, isset($values->{$id}) ? $values->{$id} : NULL);
157
158
		$form['group_action']['group_action']->setValue(NULL);
159
	}
160
161
162
	/**
163
	 * Add one group action (select box) to collection of actions
164
	 *
165
	 * @param string $title
166
	 * @param array  $options
167
	 *
168
	 * @return GroupAction
169
	 */
170
	public function addGroupSelectAction($title, $options)
171
	{
172
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
173
174
		return $this->group_actions[$id] = new GroupSelectAction($title, $options);
175
	}
176
177
178
	/**
179
	 * Add one group action (multiselect box) to collection of actions
180
	 *
181
	 * @param string $title
182
	 * @param array  $options
183
	 *
184
	 * @return GroupAction
185
	 */
186
	public function addGroupMultiSelectAction($title, $options)
187
	{
188
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
189
190
		return $this->group_actions[$id] = new GroupMultiSelectAction($title, $options);
191
	}
192
193
194
	/**
195
	 * Add one group action (text input) to collection of actions
196
	 *
197
	 * @param string $title
198
	 *
199
	 * @return GroupAction
200
	 */
201
	public function addGroupTextAction($title)
202
	{
203
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
204
205
		return $this->group_actions[$id] = new GroupTextAction($title);
206
	}
207
208
209
	/**
210
	 * Add one group action (textarea) to collection of actions
211
	 *
212
	 * @param string $title
213
	 *
214
	 * @return GroupAction
215
	 */
216
	public function addGroupTextareaAction($title)
217
	{
218
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
219
220
		return $this->group_actions[$id] = new GroupTextareaAction($title);
221
	}
222
223
224
	/**
225
	 * @param  string $title
226
	 * @return GroupAction
227
	 */
228
	public function getGroupAction($title)
229
	{
230
		foreach ($this->group_actions as $action) {
231
			if ($action->getTitle() === $title) {
232
				return $action;
233
			}
234
		}
235
236
		throw new DataGridGroupActionException("Group action $title does not exist.");
237
	}
238
239
}
240