Passed
Push — master ( b2168e...f1daef )
by Paweł
03:26
created

modules/admin/widgets/CategoriesWidget.php (1 issue)

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\models\Account;
14
use app\models\AccountTag;
15
use app\modules\admin\models\Tag;
16
use app\modules\admin\widgets\base\ProfileSideWidget;
17
use kartik\select2\Select2;
18
use yii\helpers\Html;
19
use yii\helpers\StringHelper;
20
21
class CategoriesWidget extends ProfileSideWidget
22
{
23
    public $header = 'Categories';
24
    public $headerIcon = 'tags';
25
    public $modalToggleButton = [
26
        'label' => 'Update',
27
        'class' => 'btn btn-xs btn-link',
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    public $formAction;
34
    /**
35
     * @var \app\modules\admin\models\Account|\app\modules\admin\models\Tag
36
     */
37
    public $model;
38
39
    /**
40
     * @var \app\models\Category[]
41
     */
42
    protected $modelCategories = [];
43
44
    /**
45
     * @var \app\models\Category[]
46
     */
47
    protected $categories = [];
48
49
    public function init()
50
    {
51
        parent::init();
52
        $this->modalHeader = $this->header;
53
        $this->formAction = $this->formAction ?: ['categories', 'id' => $this->model->id];
54
    }
55
56
    public function run()
57
    {
58
        $categoryManager = \Yii::createObject(CategoryManager::class);
59
        /** @var \app\models\User $identity */
60
        $identity = \Yii::$app->user->identity;
61
62
        $this->categories = $categoryManager->getForUser($identity);
63
        $this->modelCategories = $categoryManager->getForUserAccounts($identity, $this->model);
64
        parent::run();
65
    }
66
67
    protected function renderBoxContent()
68
    {
69
        echo "<p>";
70
        foreach ($this->modelCategories as $tag) {
71
            echo sprintf('<span class="label label-default">%s</span> ', Html::encode($tag->name));
72
        }
73
        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...
74
            echo \Yii::$app->formatter->nullDisplay;
75
        }
76
        echo "</p>";
77
    }
78
79
    protected function renderModalContent()
80
    {
81
        echo Html::beginForm($this->formAction);
82
        echo "<div class=\"form-group\">";
83
        echo Select2::widget([
84
            'name' => strtolower(StringHelper::basename(get_class($this->model)) . '_tags'),
85
            'options' => [
86
                'multiple' => true,
87
                'placeholder' => 'Select tags...',
88
            ],
89
            'pluginOptions' => [
90
                'tags' => true,
91
            ],
92
            'data' => array_combine(ArrayHelper::getColumn($this->categories, 'name'), ArrayHelper::getColumn($this->categories, 'name')),
93
            'value' => ArrayHelper::getColumn($this->modelCategories, 'name'),
94
        ]);
95
        echo "</div>";
96
97
        echo Html::submitButton('Update', ['class' => 'btn btn-small btn-primary']);
98
99
        echo Html::endForm();
100
    }
101
}