Completed
Push — master ( f97612...da997d )
by Martin
21:53 queued 11:54
created

GroupActionCollection   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 7
dl 0
loc 223
rs 9.8
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F addToFormContainer() 0 85 13
B submitted() 0 24 6
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
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);
71
						$control->setAttribute('data-style', 'hidden');
72
						$control->setAttribute('data-selected-icon-check', DataGrid::$icon_prefix . 'check');
73
					} else {
74
						$control = $container->addSelect($id, '', $action->getOptions());
75
					}
76
77
					$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id);
78
				}
79
80
			} else if ($action instanceof GroupTextAction) {
81
				$control = $container->addText($id, '');
82
83
				$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id)
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)
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);
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');
0 ignored issues
show
Bug introduced by
Consider using $this->datagrid->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
120
121
		$container->addSubmit('submit', 'ublaboo_datagrid.execute')
122
			->setAttribute('id', strtolower($this->datagrid->getName()) . 'group_action_submit');
0 ignored issues
show
Bug introduced by
Consider using $this->datagrid->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
123
124
		if ($form instanceof Nette\ComponentModel\IComponent) {
0 ignored issues
show
Bug introduced by
The class Nette\ComponentModel\IComponent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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[]');
0 ignored issues
show
Bug introduced by
Consider using $this->datagrid->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
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