Completed
Push — master ( bf6b48...8613c0 )
by Pavel
9s
created

GroupActionCollection::addGroupAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
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
14
class GroupActionCollection extends Nette\Object
15
{
16
17
	const ID_ATTRIBUTE_PREFIX = 'group_action_item_';
18
19
	/**
20
	 * @var GroupAction[]
21
	 */
22
	protected $group_actions = [];
23
24
	/**
25
	 * Get assambled form
26
	 * @param Nette\Forms\Container $container
27
	 * @return void
28
	 */
29
	public function addToFormContainer($container)
30
	{
31
		/** @var Nette\Application\UI\Form $form */
32
		$form = $container->lookup('Nette\Application\UI\Form');
33
		$translator = $form->getTranslator();
34
		$main_options = [];
35
36
		/**
37
		 * First foreach for filling "main" select
38
		 */
39
		foreach ($this->group_actions as $id => $action) {
40
			$main_options[$id] = $action->getTitle();
41
		}
42
43
		$container->addSelect('group_action', '', $main_options)
44
			->setPrompt($translator->translate('ublaboo_datagrid.choose'));
45
46
		/**
47
		 * Second for creating select for each "sub"-action
48
		 */
49
		foreach ($this->group_actions as $id => $action) {
50
			if ($action instanceof GroupSelectAction) {
51
				if ($action->hasOptions()) {
52
					$container->addSelect($id, '', $action->getOptions())
53
						->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...
54
				}
55
			} else if ($action instanceof GroupTextAction) {
56
				$container->addText($id, '')
57
					->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...
58
					->addConditionOn($container['group_action'], Form::EQUAL, $id)
59
						->setRequired($translator->translate('ublaboo_datagrid.choose_input_required'));
0 ignored issues
show
Documentation introduced by
$translator->translate('...choose_input_required') 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...
60
			}
61
		}
62
63
		foreach ($this->group_actions as $id => $action) {
64
			$container['group_action']->addCondition(Form::EQUAL, $id)
65
				->toggle(static::ID_ATTRIBUTE_PREFIX.$id);
66
		}
67
68
		$container['group_action']->addCondition(Form::FILLED)
69
			->toggle('group_action_submit');
70
71
		$container->addSubmit('submit', $translator->translate('ublaboo_datagrid.execute'))
72
			->setAttribute('id', 'group_action_submit');
0 ignored issues
show
Documentation introduced by
'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...
73
74
		if ($form instanceof Nette\ComponentModel\IComponent) {
75
			$form->onSubmit[] = [$this, 'submitted'];
76
		}
77
	}
78
79
80
	/**
81
	 * Pass "sub"-form submission forward to custom submit function
82
	 * @param  Form   $form
83
	 * @return void
84
	 */
85
	public function submitted(Form $form)
86
	{
87
		if (!isset($form['group_action']['submit']) || !$form['group_action']['submit']->isSubmittedBy()) {
88
			return;
89
		}
90
91
		$values = $form->getValues();
92
		$values = $values['group_action'];
93
94
		if ($values->group_action === 0 || is_null($values->group_action)) {
95
			return;
96
		}
97
98
		/**
99
		 * @todo Define items IDs
100
		 */
101
		$http_ids = $form->getHttpData(Form::DATA_LINE|Form::DATA_KEYS, 'group_action_item[]');
102
		$ids = array_keys($http_ids);
103
104
		$id = $values->group_action;
105
		$this->group_actions[$id]->onSelect($ids, isset($values->{$id}) ? $values->{$id} : NULL);
106
107
		$form['group_action']['group_action']->setValue(NULL);
108
	}
109
110
111
	/**
112
	 * Add one group action (select box) to collection of actions
113
	 *
114
	 * @param string $title
115
	 * @param array  $options
116
	 *
117
	 * @return GroupAction
118
	 */
119
	public function addGroupSelectAction($title, $options)
120
	{
121
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
122
123
		return $this->group_actions[$id] = new GroupSelectAction($title, $options);
124
	}
125
126
	/**
127
	 * Add one group action (text input) to collection of actions
128
	 *
129
	 * @param string $title
130
	 *
131
	 * @return GroupAction
132
	 */
133
	public function addGroupTextAction($title)
134
	{
135
		$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1;
136
137
		return $this->group_actions[$id] = new GroupTextAction($title);
138
	}
139
140
}
141