Commit 6d62827f7930bf4d2d3d828eca577dd9e0b76902
1 parent
c237629a
services
Showing
8 changed files
with
616 additions
and
3 deletions
Show diff stats
1 | +<?php | |
2 | + | |
3 | + namespace backend\behaviors; | |
4 | + | |
5 | + use yii\base\Behavior; | |
6 | + use yii\db\ActiveRecord; | |
7 | + | |
8 | + /** | |
9 | + * Class LevelBehavior | |
10 | + * | |
11 | + * @package artbox\catalog\behaviors | |
12 | + */ | |
13 | + class LevelBehavior extends Behavior | |
14 | + { | |
15 | + public $levelField = 'level'; | |
16 | + | |
17 | + public $parentIdField = 'parent_id'; | |
18 | + | |
19 | + /** | |
20 | + * @inheritdoc | |
21 | + */ | |
22 | + public function events() | |
23 | + { | |
24 | + return [ | |
25 | + ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', | |
26 | + ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave', | |
27 | + ]; | |
28 | + } | |
29 | + | |
30 | + public function beforeSave($event) | |
31 | + { | |
32 | + /** | |
33 | + * @var ActiveRecord $owner | |
34 | + */ | |
35 | + $levelField = $this->levelField; | |
36 | + $parentIdField = $this->parentIdField; | |
37 | + $owner = $this->owner; | |
38 | + | |
39 | + if (empty($owner->$parentIdField) && $owner->$parentIdField == 0) { | |
40 | + $owner->$levelField = 0; | |
41 | + } else { | |
42 | + $parent = $owner::findOne($owner->$parentIdField); | |
43 | + $owner->$levelField = (int) $parent->$levelField + 1; | |
44 | + } | |
45 | + | |
46 | + } | |
47 | + } | |
0 | 48 | \ No newline at end of file | ... | ... |
backend/controllers/PageController.php
... | ... | @@ -175,11 +175,11 @@ |
175 | 175 | 'name' => 'categoryIds', |
176 | 176 | 'type' => Form::RELATION, |
177 | 177 | 'relationAttribute' => 'title', |
178 | - 'relationName' => 'categories', | |
179 | - 'multiple' => true, | |
178 | + 'relationName' => 'parent', | |
179 | + 'multiple' => false, | |
180 | 180 | ], |
181 | 181 | [ |
182 | - 'name' => 'in_menu', | |
182 | + 'name' => 'status', | |
183 | 183 | 'type' => Form::BOOL, |
184 | 184 | ], |
185 | 185 | [ | ... | ... |
1 | +<?php | |
2 | + /** | |
3 | + * Created by PhpStorm. | |
4 | + * User: stes | |
5 | + * Date: 22.05.18 | |
6 | + * Time: 14:44 | |
7 | + */ | |
8 | + | |
9 | + namespace backend\controllers; | |
10 | + | |
11 | + use artbox\core\admin\actions\Create; | |
12 | + use artbox\core\admin\actions\Delete; | |
13 | + use artbox\core\admin\actions\Index; | |
14 | + use artbox\core\admin\actions\Update; | |
15 | + use artbox\core\admin\actions\View; | |
16 | + use artbox\core\admin\interfaces\ControllerInterface; | |
17 | + use artbox\core\admin\widgets\Form; | |
18 | + use common\models\Service; | |
19 | + use yii\filters\AccessControl; | |
20 | + use yii\filters\VerbFilter; | |
21 | + use yii\web\Controller; | |
22 | + use yii\web\NotFoundHttpException; | |
23 | + | |
24 | + class ServiceController extends Controller implements ControllerInterface | |
25 | + { | |
26 | + public function behaviors() | |
27 | + { | |
28 | + return [ | |
29 | + 'verbs' => [ | |
30 | + 'class' => VerbFilter::className(), | |
31 | + 'actions' => [ | |
32 | + 'delete' => [ 'POST' ], | |
33 | + ], | |
34 | + ], | |
35 | + 'access' => [ | |
36 | + 'class' => AccessControl::className(), | |
37 | + 'rules' => [ | |
38 | + [ | |
39 | + 'allow' => true, | |
40 | + 'roles' => [ '@' ], | |
41 | + ], | |
42 | + ], | |
43 | + ], | |
44 | + ]; | |
45 | + } | |
46 | + | |
47 | + public function actions() | |
48 | + { | |
49 | + return [ | |
50 | + 'index' => [ | |
51 | + 'class' => Index::className(), | |
52 | + 'columns' => [ | |
53 | + 'title' => [ | |
54 | + 'type' => Index::ACTION_COL, | |
55 | + ], | |
56 | + 'parent' => [ | |
57 | + 'type' => Index::RELATION_COL, | |
58 | + 'columnConfig' => [ | |
59 | + 'relationField' => 'title', | |
60 | + ], | |
61 | + ], | |
62 | + 'updated_at' => [ | |
63 | + 'type' => Index::DATETIME_COL, | |
64 | + ], | |
65 | + 'sort' => [ | |
66 | + 'type' => Index::POSITION_COL, | |
67 | + ], | |
68 | + 'status' => [ | |
69 | + 'type' => Index::STATUS_COL, | |
70 | + ], | |
71 | + ], | |
72 | + 'model' => Service::className(), | |
73 | + 'hasLanguage' => true, | |
74 | + 'enableMassDelete' => true, | |
75 | + 'modelPrimaryKey' => 'id', | |
76 | + ], | |
77 | + 'create' => array_merge([ 'class' => Create::className() ], self::fieldsConfig()), | |
78 | + 'update' => array_merge([ 'class' => Update::className() ], self::fieldsConfig()), | |
79 | + 'view' => [ | |
80 | + 'class' => View::className(), | |
81 | + 'model' => Service::className(), | |
82 | + 'hasAlias' => true, | |
83 | + 'languageFields' => [ | |
84 | + [ | |
85 | + 'name' => 'title', | |
86 | + 'type' => Form::STRING, | |
87 | + ], | |
88 | + [ | |
89 | + 'name' => 'body', | |
90 | + 'type' => Form::WYSIWYG, | |
91 | + ], | |
92 | + ], | |
93 | + 'fields' => [ | |
94 | + [ | |
95 | + 'name' => 'parent_id', | |
96 | + 'type' => Form::RELATION, | |
97 | + 'relationAttribute' => 'title', | |
98 | + 'relationName' => 'parent', | |
99 | + 'multiple' => false, | |
100 | + ], | |
101 | + ], | |
102 | + ], | |
103 | + 'delete' => [ | |
104 | + 'class' => Delete::className(), | |
105 | + ], | |
106 | + ]; | |
107 | + } | |
108 | + | |
109 | + public function findModel($id) | |
110 | + { | |
111 | + $model = Service::find() | |
112 | + ->with('languages') | |
113 | + ->where([ 'id' => $id ]) | |
114 | + ->one(); | |
115 | + if ($model !== null) { | |
116 | + return $model; | |
117 | + } else { | |
118 | + throw new NotFoundHttpException('The requested page does not exist.'); | |
119 | + } | |
120 | + } | |
121 | + | |
122 | + public function newModel() | |
123 | + { | |
124 | + return new Service(); | |
125 | + } | |
126 | + | |
127 | + public function deleteModel($id) | |
128 | + { | |
129 | + $page = Service::find() | |
130 | + ->with('languages.alias') | |
131 | + ->where( | |
132 | + [ | |
133 | + 'id' => $id, | |
134 | + ] | |
135 | + ) | |
136 | + ->one(); | |
137 | + $langs = call_user_func( | |
138 | + [ | |
139 | + $page, | |
140 | + 'getVariationModels', | |
141 | + ] | |
142 | + ); | |
143 | + foreach ($langs as $lang) { | |
144 | + if ($lang->alias !== null) { | |
145 | + $lang->alias->delete(); | |
146 | + } | |
147 | + } | |
148 | + | |
149 | + return $page->delete(); | |
150 | + } | |
151 | + | |
152 | + protected static function fieldsConfig() | |
153 | + { | |
154 | + return [ | |
155 | + 'model' => Service::className(), | |
156 | + 'hasAlias' => true, | |
157 | + 'languageFields' => [ | |
158 | + [ | |
159 | + 'name' => 'title', | |
160 | + 'type' => Form::STRING, | |
161 | + ], | |
162 | + [ | |
163 | + 'name' => 'body', | |
164 | + 'type' => Form::WYSIWYG, | |
165 | + ], | |
166 | + ], | |
167 | + 'fields' => [ | |
168 | + | |
169 | + [ | |
170 | + 'name' => 'parent_id', | |
171 | + 'type' => Form::RELATION, | |
172 | + 'relationAttribute' => 'title', | |
173 | + 'relationName' => 'parent', | |
174 | + 'multiple' => true, | |
175 | + ], | |
176 | + [ | |
177 | + 'name' => 'status', | |
178 | + 'type' => Form::BOOL, | |
179 | + ], | |
180 | + [ | |
181 | + 'name' => 'sort', | |
182 | + 'type' => Form::NUMBER, | |
183 | + ], | |
184 | + ], | |
185 | + ]; | |
186 | + } | |
187 | + } | |
0 | 188 | \ No newline at end of file | ... | ... |
backend/views/layouts/menu_items.php
1 | +<?php | |
2 | + | |
3 | +namespace common\models; | |
4 | + | |
5 | +use artbox\core\models\traits\AliasableTrait; | |
6 | +use backend\behaviors\LevelBehavior; | |
7 | +use Yii; | |
8 | +use artbox\core\models\Language; | |
9 | +use yii\behaviors\TimestampBehavior; | |
10 | +use yii\db\ActiveQuery; | |
11 | +use yii\db\ActiveRecord; | |
12 | +use yii\helpers\Json; | |
13 | +use yii2tech\ar\position\PositionBehavior; | |
14 | +use yii2tech\ar\variation\VariationBehavior; | |
15 | + | |
16 | +/** | |
17 | + * This is the model class for table "service". | |
18 | + * | |
19 | + * @property int $id | |
20 | + * @property int $parent_id | |
21 | + * @property int $sort | |
22 | + * @property bool $status | |
23 | + * @property int $level | |
24 | + * @property int $created_at | |
25 | + * @property int $updated_at | |
26 | + * | |
27 | + * @property ServiceLang[] $serviceLangs | |
28 | + * @property Language[] $languages | |
29 | + * from VariationBehavior | |
30 | + * @method ActiveQuery hasDefaultVariationRelation(); | |
31 | + */ | |
32 | +class Service extends ActiveRecord | |
33 | +{ | |
34 | + use AliasableTrait; | |
35 | + /** | |
36 | + * {@inheritdoc} | |
37 | + */ | |
38 | + public static function tableName() | |
39 | + { | |
40 | + return 'service'; | |
41 | + } | |
42 | + | |
43 | + /** | |
44 | + * @return array | |
45 | + */ | |
46 | + public function behaviors() | |
47 | + { | |
48 | + return [ | |
49 | + 'translations' => [ | |
50 | + 'class' => VariationBehavior::className(), | |
51 | + 'variationsRelation' => 'languages', | |
52 | + 'defaultVariationRelation' => 'language', | |
53 | + 'variationOptionReferenceAttribute' => 'language_id', | |
54 | + 'optionModelClass' => Language::className(), | |
55 | + 'defaultVariationOptionReference' => function () { | |
56 | + return Language::getCurrent()->id; | |
57 | + }, | |
58 | + 'optionQueryFilter' => function (ActiveQuery $query) { | |
59 | + $query->where( | |
60 | + [ | |
61 | + 'status' => true, | |
62 | + ] | |
63 | + ); | |
64 | + }, | |
65 | + ], | |
66 | + 'positionBehavior' => [ | |
67 | + 'class' => PositionBehavior::className(), | |
68 | + 'positionAttribute' => 'sort', | |
69 | + ], | |
70 | + 'timestamp' => [ | |
71 | + 'class' => TimestampBehavior::className(), | |
72 | + ], | |
73 | + 'level' => [ | |
74 | + 'class' => LevelBehavior::className(), | |
75 | + ], | |
76 | + ]; | |
77 | + } | |
78 | + /** | |
79 | + * {@inheritdoc} | |
80 | + */ | |
81 | + public function rules() | |
82 | + { | |
83 | + return [ | |
84 | + [['parent_id', 'sort', 'level', 'created_at', 'updated_at'], 'default', 'value' => null], | |
85 | + [['sort', 'level', 'created_at', 'updated_at'], 'integer'], | |
86 | + [['status'], 'boolean'], | |
87 | + ]; | |
88 | + } | |
89 | + | |
90 | + /** | |
91 | + * {@inheritdoc} | |
92 | + */ | |
93 | + public function attributeLabels() | |
94 | + { | |
95 | + return [ | |
96 | + 'id' => Yii::t('app', 'ID'), | |
97 | + 'parent_id' => Yii::t('app', 'Parent ID'), | |
98 | + 'sort' => Yii::t('app', 'Sort'), | |
99 | + 'status' => Yii::t('app', 'Status'), | |
100 | + 'level' => Yii::t('app', 'Level'), | |
101 | + 'created_at' => Yii::t('app', 'Created At'), | |
102 | + 'updated_at' => Yii::t('app', 'Updated At'), | |
103 | + ]; | |
104 | + } | |
105 | + | |
106 | + /** | |
107 | + * @return \yii\db\ActiveQuery | |
108 | + */ | |
109 | + public function getLanguages() | |
110 | + { | |
111 | + return $this->hasMany(ServiceLang::className(), ['service_id' => 'id']); | |
112 | + } | |
113 | + | |
114 | + /** | |
115 | + * @return \yii\db\ActiveQuery | |
116 | + */ | |
117 | + public function getParent(){ | |
118 | + return $this->hasOne(Service::className(), ['id' => 'parent_id']); | |
119 | + } | |
120 | + | |
121 | + | |
122 | + public function getLanguage(){ | |
123 | + return $this->hasDefaultVariationRelation(); | |
124 | + } | |
125 | + | |
126 | + /** | |
127 | + * @return string | |
128 | + */ | |
129 | + public function getRoute() | |
130 | + { | |
131 | + return Json::encode( | |
132 | + [ | |
133 | + 'service/view', | |
134 | + 'id' => $this->id, | |
135 | + ] | |
136 | + ); | |
137 | + } | |
138 | +} | ... | ... |
1 | +<?php | |
2 | + | |
3 | + namespace common\models; | |
4 | + | |
5 | + use artbox\core\models\Alias; | |
6 | + use artbox\core\models\Language; | |
7 | + use Yii; | |
8 | + | |
9 | + /** | |
10 | + * This is the model class for table "service_lang". | |
11 | + * | |
12 | + * @property int $service_id | |
13 | + * @property int $language_id | |
14 | + * @property string $title | |
15 | + * @property string $body | |
16 | + * @property int $alias_id | |
17 | + * @property Alias $alias | |
18 | + * @property Language $language | |
19 | + * @property Service $service | |
20 | + */ | |
21 | + class ServiceLang extends \yii\db\ActiveRecord | |
22 | + { | |
23 | + /** | |
24 | + * {@inheritdoc} | |
25 | + */ | |
26 | + public static function tableName() | |
27 | + { | |
28 | + return 'service_lang'; | |
29 | + } | |
30 | + | |
31 | + /** | |
32 | + * {@inheritdoc} | |
33 | + */ | |
34 | + public function rules() | |
35 | + { | |
36 | + return [ | |
37 | + [ | |
38 | + [ | |
39 | + 'service_id', | |
40 | + 'language_id', | |
41 | + 'alias_id', | |
42 | + ], | |
43 | + 'default', | |
44 | + 'value' => null, | |
45 | + ], | |
46 | + [ | |
47 | + [ | |
48 | + 'service_id', | |
49 | + 'language_id', | |
50 | + 'alias_id', | |
51 | + ], | |
52 | + 'integer', | |
53 | + ], | |
54 | + [ | |
55 | + [ 'body' ], | |
56 | + 'string', | |
57 | + ], | |
58 | + [ | |
59 | + [ 'title' ], | |
60 | + 'string', | |
61 | + 'max' => 255, | |
62 | + ], | |
63 | + [ | |
64 | + [ | |
65 | + 'service_id', | |
66 | + 'language_id', | |
67 | + ], | |
68 | + 'unique', | |
69 | + 'targetAttribute' => [ | |
70 | + 'service_id', | |
71 | + 'language_id', | |
72 | + ], | |
73 | + ], | |
74 | + [ | |
75 | + [ 'alias_id' ], | |
76 | + 'exist', | |
77 | + 'skipOnError' => true, | |
78 | + 'targetClass' => Alias::className(), | |
79 | + 'targetAttribute' => [ 'alias_id' => 'id' ], | |
80 | + ], | |
81 | + [ | |
82 | + [ 'language_id' ], | |
83 | + 'exist', | |
84 | + 'skipOnError' => true, | |
85 | + 'targetClass' => Language::className(), | |
86 | + 'targetAttribute' => [ 'language_id' => 'id' ], | |
87 | + ], | |
88 | + [ | |
89 | + [ 'service_id' ], | |
90 | + 'exist', | |
91 | + 'skipOnError' => true, | |
92 | + 'targetClass' => Service::className(), | |
93 | + 'targetAttribute' => [ 'service_id' => 'id' ], | |
94 | + ], | |
95 | + ]; | |
96 | + } | |
97 | + | |
98 | + /** | |
99 | + * {@inheritdoc} | |
100 | + */ | |
101 | + public function attributeLabels() | |
102 | + { | |
103 | + return [ | |
104 | + 'service_id' => Yii::t('app', 'Service ID'), | |
105 | + 'language_id' => Yii::t('app', 'Language ID'), | |
106 | + 'title' => Yii::t('app', 'Title'), | |
107 | + 'body' => Yii::t('app', 'Body'), | |
108 | + 'alias_id' => Yii::t('app', 'Alias ID'), | |
109 | + ]; | |
110 | + } | |
111 | + | |
112 | + /** | |
113 | + * @return \yii\db\ActiveQuery | |
114 | + */ | |
115 | + public function getAlias() | |
116 | + { | |
117 | + return $this->hasOne(Alias::className(), [ 'id' => 'alias_id' ]); | |
118 | + } | |
119 | + | |
120 | + /** | |
121 | + * @return \yii\db\ActiveQuery | |
122 | + */ | |
123 | + public function getLanguage() | |
124 | + { | |
125 | + return $this->hasOne(Language::className(), [ 'id' => 'language_id' ]); | |
126 | + } | |
127 | + | |
128 | + /** | |
129 | + * @return \yii\db\ActiveQuery | |
130 | + */ | |
131 | + public function getService() | |
132 | + { | |
133 | + return $this->hasOne(Service::className(), [ 'id' => 'service_id' ]); | |
134 | + } | |
135 | + } | ... | ... |
console/migrations/m180522_094150_create_service_table.php
0 → 100644
1 | +<?php | |
2 | + | |
3 | +use yii\db\Migration; | |
4 | + | |
5 | +/** | |
6 | + * Handles the creation of table `service`. | |
7 | + */ | |
8 | +class m180522_094150_create_service_table extends Migration | |
9 | +{ | |
10 | + /** | |
11 | + * {@inheritdoc} | |
12 | + */ | |
13 | + public function safeUp() | |
14 | + { | |
15 | + $this->createTable('service', [ | |
16 | + 'id' => $this->primaryKey(), | |
17 | + 'parent_id' => $this->integer(), | |
18 | + 'sort' => $this->integer(), | |
19 | + 'status' => $this->boolean(), | |
20 | + 'level' => $this->integer(), | |
21 | + 'created_at' => $this->integer(), | |
22 | + 'updated_at' => $this->integer() | |
23 | + ]); | |
24 | + | |
25 | + $this->addForeignKey('service_parent_fk', | |
26 | + 'service', | |
27 | + 'parent_id', | |
28 | + 'service', | |
29 | + 'id', | |
30 | + 'SET NULL', | |
31 | + 'CASCADE'); | |
32 | + } | |
33 | + | |
34 | + /** | |
35 | + * {@inheritdoc} | |
36 | + */ | |
37 | + public function safeDown() | |
38 | + { | |
39 | + $this->dropForeignKey('service_parent_fk', 'service'); | |
40 | + $this->dropTable('service'); | |
41 | + } | |
42 | +} | ... | ... |
console/migrations/m180522_094432_create_service_lang_table.php
0 → 100644
1 | +<?php | |
2 | + | |
3 | +use yii\db\Migration; | |
4 | + | |
5 | +/** | |
6 | + * Handles the creation of table `service_lang`. | |
7 | + */ | |
8 | +class m180522_094432_create_service_lang_table extends Migration | |
9 | +{ | |
10 | + /** | |
11 | + * {@inheritdoc} | |
12 | + */ | |
13 | + public function safeUp() | |
14 | + { | |
15 | + $this->createTable('service_lang', | |
16 | + [ | |
17 | + 'service_id'=> $this->integer(32)->notNull(), | |
18 | + 'language_id'=> $this->integer(32)->notNull(), | |
19 | + 'title'=> $this->string(255)->notNull(), | |
20 | + 'body'=> $this->text()->notNull(), | |
21 | + 'alias_id'=> $this->integer(), | |
22 | + 'PRIMARY KEY(service_id, language_id)', | |
23 | + ] | |
24 | + ); | |
25 | + | |
26 | + $this->addForeignKey('service_lang_service_fk', | |
27 | + 'service_lang', | |
28 | + 'service_id', | |
29 | + 'service', | |
30 | + 'id', | |
31 | + 'CASCADE', | |
32 | + 'CASCADE'); | |
33 | + $this->addForeignKey('service_lang_language_fk', | |
34 | + 'service_lang', | |
35 | + 'language_id', | |
36 | + 'language', | |
37 | + 'id', | |
38 | + 'CASCADE', | |
39 | + 'CASCADE'); | |
40 | + $this->addForeignKey('service_lang_alias_fk', | |
41 | + 'service_lang', | |
42 | + 'alias_id', | |
43 | + 'alias', | |
44 | + 'id', | |
45 | + 'SET NULL', | |
46 | + 'CASCADE'); | |
47 | + } | |
48 | + | |
49 | + /** | |
50 | + * {@inheritdoc} | |
51 | + */ | |
52 | + public function safeDown() | |
53 | + { | |
54 | + $this->dropForeignKey('service_lang_service_fk', 'service_lang'); | |
55 | + $this->dropForeignKey('service_lang_language_fk', 'service_lang'); | |
56 | + $this->dropForeignKey('service_lang_alias_fk', 'service_lang'); | |
57 | + $this->dropTable('service_lang'); | |
58 | + } | |
59 | +} | ... | ... |