Completed
Push — master ( 7855c2...b6f450 )
by Alexey
07:32
created

DynamicList::getCols()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
nc 3
nop 0
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Active form input dynamic list
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ui\ActiveForm\Input;
13
14
class DynamicList extends \Ui\ActiveForm\Input {
15
16
  public function draw() {
17
    $inputName = $this->colName();
18
    $inputLabel = $this->colLabel();
19
    $inputOptions = [
20
        'activeForm' => $this->activeForm,
21
        'cols' => $this->getCols(),
22
        'values' => $this->value(),
23
        'source' => !empty($this->colParams['source']) ? $this->colParams['source'] : 'relation',
24
        'modelPk' => $this->activeForm->model->pk()
25
    ];
26
    $this->form->input('dynamicList', $inputName, $inputLabel, $inputOptions);
27
    return true;
28
  }
29
30
  public function parseRequest($request) {
31
    $modelName = $this->modelName;
32
    switch ($this->colParams['source']) {
33
      case'options':
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
34
35
        break;
36
      default:
37
        $rels = [];
38
        $relation = $modelName::getRelation($this->colParams['relation']);
39
        if (!empty($request[$this->colName]) && $this->activeForm->model->pk()) {
40
          switch ($relation['type']) {
41
            case 'relModel':
42
              foreach ($request[$this->colName] as $row) {
43
                $rels[$row['relItem']] = true;
44
              }
45
              $relModels = $relation['relModel']::getList(['where' => [$modelName::index(), $this->activeForm->model->pk()], 'key' => $relation['model']::index()]);
46
              foreach ($relModels as $model) {
47
                if (empty($rels[$model->{$relation['model']::index()}])) {
48
                  $model->delete();
49
                } else {
50
                  unset($rels[$model->{$relation['model']::index()}]);
51
                }
52
              }
53
              foreach ($rels as $relId => $trash) {
54
                $model = new $relation['relModel']([
55
                    $modelName::index() => $this->activeForm->model->pk(),
56
                    $relation['model']::index() => $relId
57
                ]);
58
                $model->save();
59
              }
60
              break;
61
          }
62
        }
63
    }
64
  }
65
66
  public function value() {
67
    $values = [];
68
    switch ($this->colParams['source']) {
69
      case'options':
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
70
71
        break;
72
      default:
73
        if ($this->activeForm->model) {
74
          $items = $this->activeForm->model->{$this->colParams['relation']}(['array' => true]);
75
          foreach ($items as $key => $item) {
76
            $values[] = ['relItem' => $key];
77
          }
78
        }
79
    }
80
81
    return $values;
82
  }
83
84
  public function getCols() {
85
    $modelName = $this->modelName;
86
    switch ($this->colParams['source']) {
87
      case'options':
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
88
        foreach ($this->colParams['options']['inputs'] as $colName => $col) {
89
          $inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($col['type']);
90
          $input = new $inputClassName();
91
          $input->form = $this->form;
92
          $input->activeForm = $this->activeForm;
93
          $input->activeFormParams = $this->activeFormParams;
94
          $input->modelName = $this->modelName;
95
          $input->colName = "[{$this->colName}][{$colName}][]";
96
          $input->colParams = $col;
97
          $input->options = !empty($col['options']) ? $col['options'] : [];
98
          $cols[] = ['input' => $input, 'col' => $col];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$cols was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cols = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
99
        }
100
        break;
101
      default:
102
        $relation = $modelName::getRelation($this->colParams['relation']);
103
        $cols = [];
104
        switch ($relation['type']) {
105
          case 'relModel':
106
            $cols['relItem'] = [
107
                'col' => [
108
                    'label' => $relation['model']::objectName(),
109
                    'type' => 'select',
110
                    'options' => [
111
                        'values' => $relation['model']::getList(['forSelect' => true])
112
                    ]
113
                ]
114
            ];
115
            break;
116
        }
117
    }
118
    return $cols;
119
  }
120
121
}
122