Passed
Push — master ( fe7b3f...22651e )
by Andrey
08:30
created

ProductController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
c 0
b 0
f 0
dl 0
loc 130
rs 10
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdditionFields() 0 30 4
A actionDelete() 0 7 2
A actionView() 0 7 2
A actionUpdate() 0 7 2
A getModelName() 0 3 1
A getSearchModelName() 0 3 1
A actionIndex() 0 7 2
A actionCreate() 0 7 2
1
<?php
2
3
namespace app\controllers\admin;
4
5
use yii\data\Pagination;
6
use app\models\{Page, Product, ProductSearch};
7
use app\traits\{LanguageTrait, AdminBeforeActionTrait, AccessTrait};
8
use Itstructure\MFUploader\models\OwnerMediafile;
9
use Itstructure\MFUploader\models\album\Album;
10
use Itstructure\MFUploader\interfaces\UploadModelInterface;
11
use Itstructure\AdminModule\controllers\CommonAdminController;
12
13
/**
14
 * Class ProductController
15
 * ProductController implements the CRUD actions for Page model.
16
 *
17
 * @package app\controllers\admin
18
 */
19
class ProductController extends CommonAdminController
20
{
21
    use LanguageTrait, AdminBeforeActionTrait, AccessTrait;
0 ignored issues
show
Bug introduced by
The trait app\traits\LanguageTrait requires the property $request which is not provided by app\controllers\admin\ProductController.
Loading history...
Bug introduced by
The trait app\traits\AccessTrait requires the property $user which is not provided by app\controllers\admin\ProductController.
Loading history...
introduced by
The trait app\traits\AdminBeforeActionTrait requires some properties which are not provided by app\controllers\admin\ProductController: $request, $controller
Loading history...
22
23
    /**
24
     * @var bool
25
     */
26
    protected $isMultilanguage = true;
27
28
    /**
29
     * @return mixed|string
30
     */
31
    public function actionIndex()
32
    {
33
        if (!$this->checkAccessToIndex()) {
34
            return $this->accessError();
35
        }
36
37
        return parent::actionIndex();
38
    }
39
40
    /**
41
     * @param int|string $id
42
     *
43
     * @return mixed
44
     */
45
    public function actionView($id)
46
    {
47
        if (!$this->checkAccessToView()) {
48
            return $this->accessError();
49
        }
50
51
        return parent::actionView($id);
52
    }
53
54
    /**
55
     * @return mixed|string|\yii\web\Response
56
     */
57
    public function actionCreate()
58
    {
59
        if (!$this->checkAccessToCreate()) {
60
            return $this->accessError();
61
        }
62
63
        return parent::actionCreate();
64
    }
65
66
    /**
67
     * @param int|string $id
68
     *
69
     * @return string|\yii\web\Response
70
     */
71
    public function actionUpdate($id)
72
    {
73
        if (!$this->checkAccessToUpdate()) {
74
            return $this->accessError();
75
        }
76
77
        return parent::actionUpdate($id);
78
    }
79
80
    /**
81
     * @param int|string $id
82
     *
83
     * @return mixed|\yii\web\Response
84
     */
85
    public function actionDelete($id)
86
    {
87
        if (!$this->checkAccessToDelete()) {
88
            return $this->accessError();
89
        }
90
91
        return parent::actionDelete($id);
92
    }
93
94
    /**
95
     * Get addition fields for the view template.
96
     *
97
     * @return array
98
     */
99
    protected function getAdditionFields(): array
100
    {
101
        if ($this->action->id == 'create' || $this->action->id == 'update') {
102
            $fields = [];
103
104
            $fields['pages'] = Page::getMenu();
105
            $fields['albums'] = Album::find()->select([
106
                'id', 'title'
107
            ])->all();
108
109
            if ($this->action->id == 'update') {
110
                $mediafilesQuery = OwnerMediafile::getMediaFilesQuery([
111
                    'owner' => Product::tableName(),
112
                    'ownerId' => $this->model->getId(),
113
                    'ownerAttribute' => UploadModelInterface::FILE_TYPE_IMAGE,
114
                ]);
115
                $media_pages = new Pagination([
116
                    'defaultPageSize' => 6,
117
                    'totalCount' => $mediafilesQuery->count()
118
                ]);
119
                $fields['images'] = $mediafilesQuery->offset($media_pages->offset)
120
                    ->limit($media_pages->limit)
121
                    ->all();
122
                $fields['media_pages'] = $media_pages;
123
            }
124
125
            return $fields;
126
        }
127
128
        return $this->additionFields;
129
    }
130
131
    /**
132
     * Returns Product model name.
133
     *
134
     * @return string
135
     */
136
    protected function getModelName():string
137
    {
138
        return Product::class;
139
    }
140
141
    /**
142
     * Returns ProductSearch model name.
143
     *
144
     * @return string
145
     */
146
    protected function getSearchModelName():string
147
    {
148
        return ProductSearch::class;
149
    }
150
}
151