CategoriesWidget::renderModalContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 21
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 31.01.2018
6
 */
7
8
namespace app\modules\admin\widgets;
9
10
11
use app\components\ArrayHelper;
12
use app\components\CategoryManager;
13
use app\modules\admin\widgets\base\ProfileSideWidget;
14
use kartik\select2\Select2;
15
use Yii;
16
use yii\helpers\Html;
17
use yii\helpers\StringHelper;
18
19
class CategoriesWidget extends ProfileSideWidget
20
{
21
    public $header = 'Categories';
22
    public $headerIcon = 'tags';
23
    public $modalToggleButton = [
24
        'label' => 'Update',
25
        'class' => 'btn btn-xs btn-link',
26
    ];
27
28
    /**
29
     * @var array
30
     */
31
    public $formAction;
32
    /**
33
     * @var \app\modules\admin\models\Account|\app\modules\admin\models\Tag
34
     */
35
    public $model;
36
37
    /**
38
     * @var \app\models\Category[]
39
     */
40
    protected $modelCategories = [];
41
42
    /**
43
     * @var \app\models\Category[]
44
     */
45
    protected $categories = [];
46
47
    public function init()
48
    {
49
        parent::init();
50
        $this->modalHeader = $this->header;
51
        $this->formAction = $this->formAction ?: ['categories', 'id' => $this->model->id];
52
    }
53
54
    public function run()
55
    {
56
        $categoryManager = Yii::createObject(CategoryManager::class);
57
        /** @var \app\models\User $identity */
58
        $identity = Yii::$app->user->identity;
59
60
        $this->categories = $categoryManager->getForUser($identity);
61
        $this->modelCategories = $categoryManager->getForUserAccounts($identity, $this->model);
62
        parent::run();
63
    }
64
65
    protected function renderBoxContent()
66
    {
67
        echo "<p>";
68
        foreach ($this->modelCategories as $tag) {
69
            echo sprintf('<span class="label label-default">%s</span> ', Html::encode($tag->name));
70
        }
71
        if (!$this->modelCategories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->modelCategories of type app\models\Category[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
72
            echo Yii::$app->formatter->nullDisplay;
73
        }
74
        echo "</p>";
75
    }
76
77
    protected function renderModalContent()
78
    {
79
        echo Html::beginForm($this->formAction);
80
        echo "<div class=\"form-group\">";
81
        echo Select2::widget([
82
            'name' => strtolower(StringHelper::basename(get_class($this->model)) . '_tags'),
83
            'options' => [
84
                'multiple' => true,
85
                'placeholder' => 'Select tags...',
86
            ],
87
            'pluginOptions' => [
88
                'tags' => true,
89
            ],
90
            'data' => array_combine(ArrayHelper::getColumn($this->categories, 'name'), ArrayHelper::getColumn($this->categories, 'name')),
91
            'value' => ArrayHelper::getColumn($this->modelCategories, 'name'),
92
        ]);
93
        echo "</div>";
94
95
        echo Html::submitButton('Update', ['class' => 'btn btn-small btn-primary']);
96
97
        echo Html::endForm();
98
    }
99
}