Operation::handleOperations()   B
last analyzed

Complexity

Conditions 8
Paths 36

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8.3646

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 23
cts 28
cp 0.8214
rs 8.0835
c 0
b 0
f 0
cc 8
nc 36
nop 1
crap 8.3646
1
<?php
2
3
/**
4
 * This file is part of the Grido (https://github.com/o5/grido)
5
 *
6
 * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
7
 *
8
 * For the full copyright and license information, please view
9
 * the file LICENSE.md that was distributed with this source code.
10
 */
11
12
namespace Grido\Components;
13
14
use Grido\Grid;
15
use Grido\Helpers;
16
use Grido\Exception;
17
18
/**
19
 * Operation with one or more rows.
20
 *
21
 * @package     Grido
22
 * @subpackage  Components
23
 * @author      Petr Bugyík
24
 *
25
 * @property-read string $primaryKey
26
 * @method void onSubmit(string $operation, array $ids) Description
27
 */
28
class Operation extends Component
29 1
{
30
    const ID = 'operations';
31
32
    /** @var array callback on operation submit */
33
    public $onSubmit;
34
35
    /** @var string */
36
    protected $primaryKey;
37
38
    /**
39
     * @param \Grido\Grid $grid
40
     * @param array $operations
41
     * @param callback $onSubmit - callback after operation submit
42
     */
43
    public function __construct($grid, array $operations, $onSubmit)
44
    {
45 1
        $this->grid = $grid;
46 1
        $grid->addComponent($this, self::ID);
47
48 1
        $grid['form'][$grid::BUTTONS]->addSubmit(self::ID, 'OK')
49 1
            ->onClick[] = [$this, 'handleOperations'];
50
51 1
        $grid['form']->addContainer(self::ID)
52 1
            ->addSelect(self::ID, 'Selected', $operations)
53 1
            ->setPrompt('Grido.Selected');
54
55 1
        $grid->onRender[] = function(Grid $grid) {
56 1
            $this->addCheckers($grid['form'][Operation::ID]);
57 1
        };
58
59 1
        $this->onSubmit[] = $onSubmit;
60 1
    }
61
62
    /**
63
     * Sets client side confirm for operation.
64
     * @param string $operation
65
     * @param string $message
66
     * @return Operation
67
     */
68
    public function setConfirm($operation, $message)
69
    {
70 1
        $message = $this->translate($message);
71 1
        $this->grid->onRender[] = function(Grid $grid) use ($operation, $message) {
72 1
            $grid['form'][Operation::ID][Operation::ID]->getControlPrototype()->setAttribute(
73 1
                "data-grido-confirm-$operation", $message
74 1
            );
75 1
        };
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * Sets primary key.
82
     * @param string $primaryKey
83
     * @return Operation
84
     */
85
    public function setPrimaryKey($primaryKey)
86
    {
87 1
        $this->primaryKey = $primaryKey;
88 1
        return $this;
89
    }
90
91
    /**********************************************************************************************/
92
93
    /**
94
     * @return string
95
     */
96
    public function getPrimaryKey()
97
    {
98 1
        if ($this->primaryKey === NULL) {
99 1
            $this->primaryKey = $this->grid->primaryKey;
100 1
        }
101
102 1
        return $this->primaryKey;
103
    }
104
105
    /**********************************************************************************************/
106
107
    /**
108
     * @param \Nette\Forms\Controls\SubmitButton $button
109
     * @internal
110
     */
111
    public function handleOperations(\Nette\Forms\Controls\SubmitButton $button)
112
    {
113 1
        $grid = $this->getGrid();
114 1
        !empty($grid->onRegistered) && $grid->onRegistered($grid);
115 1
        $form = $button->getForm();
116 1
        $this->addCheckers($form[self::ID]);
117
118 1
        $values = $form[self::ID]->values;
119 1
        if (empty($values[self::ID])) {
120 1
            $httpData = $form->getHttpData();
121 1
            if (!empty($httpData[self::ID][self::ID]) && $operation = $httpData[self::ID][self::ID]) {
122 1
                $grid->__triggerUserNotice("Operation with name '$operation' does not exist.");
123 1
            }
124
125 1
            $grid->reload();
126
        }
127
128 1
        $ids = [];
129 1
        $operation = $values[self::ID];
130 1
        unset($values[self::ID]);
131
132 1
        foreach ($values as $key => $val) {
133 1
            if ($val) {
134 1
                $ids[] = (string) $key;
135 1
            }
136 1
        }
137
138 1
        $this->onSubmit($operation, $ids);
139 1
        $grid->page = 1;
140
141 1
        if ($this->presenter->isAjax()) {
142
            $grid['form'][self::ID][self::ID]->setValue(NULL);
143
            $grid->getData(TRUE, FALSE);
144
        }
145
146 1
        $grid->reload();
147
    }
148
149
    /**
150
     * @param \Nette\Forms\Container $container
151
     * @throws Exception
152
     * @internal
153
     */
154
    public function addCheckers(\Nette\Forms\Container $container)
155
    {
156 1
        $items = $this->grid->getData();
157 1
        $primaryKey = $this->getPrimaryKey();
158
159 1
        foreach ($items as $item) {
0 ignored issues
show
Bug introduced by
The expression $items of type array|object<Grido\DataS...tabase\Table\Selection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
160
            try {
161 1
                $primaryValue = $this->grid->getProperty($item, $primaryKey);
162 1
                if (!isset($container[$primaryValue])) {
163 1
                    $container->addCheckbox(Helpers::formatColumnName($primaryValue))
164 1
                        ->controlPrototype->title = $primaryValue;
165 1
                }
166 1
            } catch (\Exception $e) {
167
                throw new Exception(
168
                    'You should define some else primary key via $grid->setPrimaryKey() '.
169
                    "because currently defined '$primaryKey' key is not suitable for operation feature."
170
                );
171
            }
172 1
        }
173 1
    }
174
}
175