Commit c4f4776ae0bbaab0a9b5e926a736ba607e4d7e49
1 parent
c2648e65
- package offers
Showing
6 changed files
with
531 additions
and
0 deletions
Show diff stats
| 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\Package; | |
| 19 | + use yii\filters\AccessControl; | |
| 20 | + use yii\filters\VerbFilter; | |
| 21 | + use yii\web\Controller; | |
| 22 | + use yii\web\NotFoundHttpException; | |
| 23 | + | |
| 24 | + class PackageController 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 | + 'updated_at' => [ | |
| 57 | + 'type' => Index::DATETIME_COL, | |
| 58 | + ], | |
| 59 | + 'sort' => [ | |
| 60 | + 'type' => Index::POSITION_COL, | |
| 61 | + ], | |
| 62 | + 'status' => [ | |
| 63 | + 'type' => Index::STATUS_COL, | |
| 64 | + ], | |
| 65 | + ], | |
| 66 | + 'model' => Package::className(), | |
| 67 | + 'hasLanguage' => true, | |
| 68 | + 'enableMassDelete' => true, | |
| 69 | + 'modelPrimaryKey' => 'id', | |
| 70 | + ], | |
| 71 | + 'create' => array_merge([ 'class' => Create::className() ], self::fieldsConfig()), | |
| 72 | + 'update' => array_merge([ 'class' => Update::className() ], self::fieldsConfig()), | |
| 73 | + 'view' => [ | |
| 74 | + 'class' => View::className(), | |
| 75 | + 'model' => Package::className(), | |
| 76 | + 'hasAlias' => true, | |
| 77 | + 'languageFields' => [ | |
| 78 | + [ | |
| 79 | + 'name' => 'title', | |
| 80 | + 'type' => Form::STRING, | |
| 81 | + ], | |
| 82 | + [ | |
| 83 | + 'name' => 'body', | |
| 84 | + 'type' => Form::WYSIWYG, | |
| 85 | + ], | |
| 86 | + ], | |
| 87 | + 'fields' => [ | |
| 88 | + ], | |
| 89 | + ], | |
| 90 | + 'delete' => [ | |
| 91 | + 'class' => Delete::className(), | |
| 92 | + ], | |
| 93 | + ]; | |
| 94 | + } | |
| 95 | + | |
| 96 | + public function findModel($id) | |
| 97 | + { | |
| 98 | + $model = Package::find() | |
| 99 | + ->with('languages') | |
| 100 | + ->where([ 'id' => $id ]) | |
| 101 | + ->one(); | |
| 102 | + if ($model !== null) { | |
| 103 | + return $model; | |
| 104 | + } else { | |
| 105 | + throw new NotFoundHttpException('The requested page does not exist.'); | |
| 106 | + } | |
| 107 | + } | |
| 108 | + | |
| 109 | + public function newModel() | |
| 110 | + { | |
| 111 | + return new Package(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + public function deleteModel($id) | |
| 115 | + { | |
| 116 | + $page = Package::find() | |
| 117 | + ->with('languages.alias') | |
| 118 | + ->where( | |
| 119 | + [ | |
| 120 | + 'id' => $id, | |
| 121 | + ] | |
| 122 | + ) | |
| 123 | + ->one(); | |
| 124 | + $langs = call_user_func( | |
| 125 | + [ | |
| 126 | + $page, | |
| 127 | + 'getVariationModels', | |
| 128 | + ] | |
| 129 | + ); | |
| 130 | + foreach ($langs as $lang) { | |
| 131 | + if ($lang->alias !== null) { | |
| 132 | + $lang->alias->delete(); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + | |
| 136 | + return $page->delete(); | |
| 137 | + } | |
| 138 | + | |
| 139 | + protected static function fieldsConfig() | |
| 140 | + { | |
| 141 | + return [ | |
| 142 | + 'model' => Package::className(), | |
| 143 | + 'hasAlias' => true, | |
| 144 | + 'languageFields' => [ | |
| 145 | + [ | |
| 146 | + 'name' => 'title', | |
| 147 | + 'type' => Form::STRING, | |
| 148 | + ], | |
| 149 | + [ | |
| 150 | + 'name' => 'body', | |
| 151 | + 'type' => Form::WYSIWYG, | |
| 152 | + ], | |
| 153 | + ], | |
| 154 | + 'fields' => [ | |
| 155 | + [ | |
| 156 | + 'name' => 'image_id', | |
| 157 | + 'type' => Form::IMAGE | |
| 158 | + ], | |
| 159 | + [ | |
| 160 | + 'name' => 'status', | |
| 161 | + 'type' => Form::BOOL, | |
| 162 | + ], | |
| 163 | + [ | |
| 164 | + 'name' => 'sort', | |
| 165 | + 'type' => Form::NUMBER, | |
| 166 | + ], | |
| 167 | + ], | |
| 168 | + ]; | |
| 169 | + } | |
| 170 | + } | |
| 0 | 171 | \ 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\Image; | |
| 6 | +use artbox\core\models\traits\AliasableTrait; | |
| 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 "package". | |
| 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 PackageLang[] $serviceLangs | |
| 28 | + * @property Language[] $languages | |
| 29 | + * from VariationBehavior | |
| 30 | + * @method ActiveQuery hasDefaultVariationRelation(); | |
| 31 | + */ | |
| 32 | +class Package extends ActiveRecord | |
| 33 | +{ | |
| 34 | + use AliasableTrait; | |
| 35 | + /** | |
| 36 | + * {@inheritdoc} | |
| 37 | + */ | |
| 38 | + public static function tableName() | |
| 39 | + { | |
| 40 | + return 'package'; | |
| 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 | + ]; | |
| 74 | + } | |
| 75 | + /** | |
| 76 | + * {@inheritdoc} | |
| 77 | + */ | |
| 78 | + public function rules() | |
| 79 | + { | |
| 80 | + return [ | |
| 81 | + [['sort', 'created_at', 'updated_at'], 'default', 'value' => null], | |
| 82 | + [['sort', 'created_at', 'updated_at', 'image_id'], 'integer'], | |
| 83 | + [['status'], 'boolean'], | |
| 84 | + ]; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * {@inheritdoc} | |
| 89 | + */ | |
| 90 | + public function attributeLabels() | |
| 91 | + { | |
| 92 | + return [ | |
| 93 | + 'id' => Yii::t('app', 'ID'), | |
| 94 | + 'sort' => Yii::t('app', 'Sort'), | |
| 95 | + 'status' => Yii::t('app', 'Status'), | |
| 96 | + 'created_at' => Yii::t('app', 'Created At'), | |
| 97 | + 'updated_at' => Yii::t('app', 'Updated At'), | |
| 98 | + 'image_id' => Yii::t('app', 'Image'), | |
| 99 | + ]; | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * @return \yii\db\ActiveQuery | |
| 104 | + */ | |
| 105 | + public function getLanguages() | |
| 106 | + { | |
| 107 | + return $this->hasMany(PackageLang::className(), ['package_id' => 'id']); | |
| 108 | + } | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + public function getLanguage(){ | |
| 113 | + return $this->hasDefaultVariationRelation(); | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * @return string | |
| 118 | + */ | |
| 119 | + public function getRoute() | |
| 120 | + { | |
| 121 | + return Json::encode( | |
| 122 | + [ | |
| 123 | + 'service/view', | |
| 124 | + 'id' => $this->id, | |
| 125 | + ] | |
| 126 | + ); | |
| 127 | + } | |
| 128 | + | |
| 129 | + public function getImage(){ | |
| 130 | + return $this->hasOne(Image::className(), ['id' => 'image_id']); | |
| 131 | + } | |
| 132 | + | |
| 133 | +} | ... | ... |
| 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 "package_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 PackageLang extends \yii\db\ActiveRecord | |
| 22 | + { | |
| 23 | + /** | |
| 24 | + * {@inheritdoc} | |
| 25 | + */ | |
| 26 | + public static function tableName() | |
| 27 | + { | |
| 28 | + return 'package_lang'; | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * {@inheritdoc} | |
| 33 | + */ | |
| 34 | + public function rules() | |
| 35 | + { | |
| 36 | + return [ | |
| 37 | + [ | |
| 38 | + [ | |
| 39 | + 'package_id', | |
| 40 | + 'language_id', | |
| 41 | + 'alias_id', | |
| 42 | + ], | |
| 43 | + 'default', | |
| 44 | + 'value' => null, | |
| 45 | + ], | |
| 46 | + [ | |
| 47 | + [ | |
| 48 | + 'package_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 | + 'package_id', | |
| 66 | + 'language_id', | |
| 67 | + ], | |
| 68 | + 'unique', | |
| 69 | + 'targetAttribute' => [ | |
| 70 | + 'package_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 | + [ 'package_id' ], | |
| 90 | + 'exist', | |
| 91 | + 'skipOnError' => true, | |
| 92 | + 'targetClass' => Service::className(), | |
| 93 | + 'targetAttribute' => [ 'package_id' => 'id' ], | |
| 94 | + ], | |
| 95 | + ]; | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * {@inheritdoc} | |
| 100 | + */ | |
| 101 | + public function attributeLabels() | |
| 102 | + { | |
| 103 | + return [ | |
| 104 | + 'package_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(Package::className(), [ 'id' => 'service_id' ]); | |
| 134 | + } | |
| 135 | + } | ... | ... |
console/migrations/m180525_140759_create_package_table.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +use yii\db\Migration; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Handles the creation of table `package`. | |
| 7 | + */ | |
| 8 | +class m180525_140759_create_package_table extends Migration | |
| 9 | +{ | |
| 10 | + /** | |
| 11 | + * {@inheritdoc} | |
| 12 | + */ | |
| 13 | + public function safeUp() | |
| 14 | + { | |
| 15 | + $this->createTable('package', [ | |
| 16 | + 'id' => $this->primaryKey(), | |
| 17 | + 'sort' => $this->integer(), | |
| 18 | + 'status' => $this->boolean(), | |
| 19 | + 'created_at' => $this->integer(), | |
| 20 | + 'updated_at' => $this->integer() | |
| 21 | + ]); | |
| 22 | + } | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * {@inheritdoc} | |
| 26 | + */ | |
| 27 | + public function safeDown() | |
| 28 | + { | |
| 29 | + $this->dropTable('package'); | |
| 30 | + } | |
| 31 | +} | ... | ... |
console/migrations/m180525_141010_create_package_lang_table.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +use yii\db\Migration; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Handles the creation of table `package_lang`. | |
| 7 | + */ | |
| 8 | +class m180525_141010_create_package_lang_table extends Migration | |
| 9 | +{ | |
| 10 | + /** | |
| 11 | + * {@inheritdoc} | |
| 12 | + */ | |
| 13 | + public function safeUp() | |
| 14 | + { | |
| 15 | + $this->createTable('package_lang', [ | |
| 16 | + 'package_id'=> $this->integer(32)->notNull(), | |
| 17 | + 'language_id'=> $this->integer(32)->notNull(), | |
| 18 | + 'title'=> $this->string(255)->notNull(), | |
| 19 | + 'body'=> $this->text()->notNull(), | |
| 20 | + 'alias_id'=> $this->integer(), | |
| 21 | + 'PRIMARY KEY(package_id, language_id)', | |
| 22 | + ]); | |
| 23 | + | |
| 24 | + $this->addForeignKey('package_lang_package_fk', | |
| 25 | + 'package_lang', | |
| 26 | + 'package_id', | |
| 27 | + 'package', | |
| 28 | + 'id', | |
| 29 | + 'CASCADE', | |
| 30 | + 'CASCADE'); | |
| 31 | + $this->addForeignKey('package_lang_language_fk', | |
| 32 | + 'package_lang', | |
| 33 | + 'language_id', | |
| 34 | + 'language', | |
| 35 | + 'id', | |
| 36 | + 'CASCADE', | |
| 37 | + 'CASCADE'); | |
| 38 | + $this->addForeignKey('package_lang_alias_fk', | |
| 39 | + 'package_lang', | |
| 40 | + 'alias_id', | |
| 41 | + 'alias', | |
| 42 | + 'id', | |
| 43 | + 'SET NULL', | |
| 44 | + 'CASCADE'); | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * {@inheritdoc} | |
| 49 | + */ | |
| 50 | + public function safeDown() | |
| 51 | + { | |
| 52 | + $this->dropForeignKey('package_lang_package_fk', 'package_lang'); | |
| 53 | + $this->dropForeignKey('package_lang_language_fk', 'package_lang'); | |
| 54 | + $this->dropForeignKey('package_lang_alias_fk', 'package_lang'); | |
| 55 | + $this->dropTable('package_lang'); | |
| 56 | + } | |
| 57 | +} | ... | ... |