Commit cfa46f4f87f027a0e1b9384045c25294143beeed
1 parent
3a45f80d
-Speakers admin ready
Showing
4 changed files
with
411 additions
and
0 deletions
Show diff stats
| 1 | +<?php | |
| 2 | + | |
| 3 | + namespace backend\controllers; | |
| 4 | + | |
| 5 | + use function array_merge; | |
| 6 | + use artbox\core\admin\actions\Create; | |
| 7 | + use artbox\core\admin\actions\Delete; | |
| 8 | + use backend\actions\Index; | |
| 9 | + use artbox\core\admin\actions\Update; | |
| 10 | + use artbox\core\admin\actions\View; | |
| 11 | + use artbox\core\admin\interfaces\ControllerInterface; | |
| 12 | + use artbox\core\admin\widgets\Form; | |
| 13 | + use common\models\speaker\Speaker; | |
| 14 | + use yii\filters\AccessControl; | |
| 15 | + use yii\filters\VerbFilter; | |
| 16 | + use yii\web\Controller; | |
| 17 | + use yii\web\NotFoundHttpException; | |
| 18 | + | |
| 19 | + class SpeakerController extends Controller implements ControllerInterface | |
| 20 | + { | |
| 21 | + public function behaviors() | |
| 22 | + { | |
| 23 | + return [ | |
| 24 | + 'verbs' => [ | |
| 25 | + 'class' => VerbFilter::className(), | |
| 26 | + 'actions' => [ | |
| 27 | + 'delete' => [ 'POST' ], | |
| 28 | + ], | |
| 29 | + ], | |
| 30 | + 'access' => [ | |
| 31 | + 'class' => AccessControl::className(), | |
| 32 | + 'rules' => [ | |
| 33 | + [ | |
| 34 | + 'allow' => true, | |
| 35 | + 'roles' => [ '@' ], | |
| 36 | + ], | |
| 37 | + ], | |
| 38 | + ], | |
| 39 | + ]; | |
| 40 | + } | |
| 41 | + public function actions() | |
| 42 | + { | |
| 43 | + return [ | |
| 44 | + 'index' => [ | |
| 45 | + 'class' => Index::className(), | |
| 46 | + 'columns' => [ | |
| 47 | + 'name' => [ | |
| 48 | + 'type' => Index::ACTION_COL, | |
| 49 | + ], | |
| 50 | + 'sort' => [ | |
| 51 | + 'type' => Index::POSITION_COL, | |
| 52 | + ], | |
| 53 | + 'status' => [ | |
| 54 | + 'type' => Index::STATUS_COL, | |
| 55 | + ], | |
| 56 | + ], | |
| 57 | + 'model' => Speaker::className(), | |
| 58 | + 'hasLanguage' => true, | |
| 59 | + 'enableMassDelete' => true, | |
| 60 | + 'modelPrimaryKey' => 'id', | |
| 61 | + ], | |
| 62 | + 'create' => array_merge([ 'class' => Create::className() ], self::fieldsConfig()), | |
| 63 | + 'update' => array_merge([ 'class' => Update::className() ], self::fieldsConfig()), | |
| 64 | + 'view' => [ | |
| 65 | + 'class' => View::className(), | |
| 66 | + 'model' => Speaker::className(), | |
| 67 | + 'hasAlias' => true, | |
| 68 | + 'hasGallery' => true, | |
| 69 | + 'languageFields' => [ | |
| 70 | + [ | |
| 71 | + 'name' => 'title', | |
| 72 | + 'type' => Form::STRING, | |
| 73 | + ], | |
| 74 | + [ | |
| 75 | + 'name' => 'body', | |
| 76 | + 'type' => Form::WYSIWYG, | |
| 77 | + ], | |
| 78 | + ], | |
| 79 | + 'fields' => [ | |
| 80 | + [ | |
| 81 | + 'name' => 'image_id', | |
| 82 | + 'type' => Form::IMAGE, | |
| 83 | + ], | |
| 84 | + ], | |
| 85 | + ], | |
| 86 | + 'delete' => [ | |
| 87 | + 'class' => Delete::className(), | |
| 88 | + ], | |
| 89 | + ]; | |
| 90 | + } | |
| 91 | + | |
| 92 | + public function findModel($id) | |
| 93 | + { | |
| 94 | + $model = Speaker::find() | |
| 95 | + ->with('languages') | |
| 96 | + ->where([ 'id' => $id ]) | |
| 97 | + ->one(); | |
| 98 | + if ($model !== null) { | |
| 99 | + return $model; | |
| 100 | + } else { | |
| 101 | + throw new NotFoundHttpException('The requested page does not exist.'); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + public function newModel() | |
| 106 | + { | |
| 107 | + return new Speaker(); | |
| 108 | + } | |
| 109 | + | |
| 110 | + public function deleteModel($id) | |
| 111 | + { | |
| 112 | + $page = Speaker::find() | |
| 113 | + ->with('languages') | |
| 114 | + ->where( | |
| 115 | + [ | |
| 116 | + 'id' => $id, | |
| 117 | + ] | |
| 118 | + ) | |
| 119 | + ->one(); | |
| 120 | + | |
| 121 | + return $page->delete(); | |
| 122 | + } | |
| 123 | + | |
| 124 | + protected static function fieldsConfig() | |
| 125 | + { | |
| 126 | + return [ | |
| 127 | + 'model' => Speaker::className(), | |
| 128 | + 'hasAlias' => false, | |
| 129 | + 'hasGallery' => false, | |
| 130 | + 'languageFields' => [ | |
| 131 | + [ | |
| 132 | + 'name' => 'name', | |
| 133 | + 'type' => Form::STRING, | |
| 134 | + ], | |
| 135 | + [ | |
| 136 | + 'name' => 'position', | |
| 137 | + 'type' => Form::TEXTAREA, | |
| 138 | + ], | |
| 139 | + [ | |
| 140 | + 'name' => 'organization', | |
| 141 | + 'type' => Form::TEXTAREA, | |
| 142 | + ], | |
| 143 | + ], | |
| 144 | + 'fields' => [ | |
| 145 | + | |
| 146 | + [ | |
| 147 | + 'name' => 'image_id', | |
| 148 | + 'type' => Form::IMAGE, | |
| 149 | + ], | |
| 150 | + [ | |
| 151 | + 'name' => 'status', | |
| 152 | + 'type' => Form::BOOL, | |
| 153 | + ], | |
| 154 | + [ | |
| 155 | + 'name' => 'sort', | |
| 156 | + 'type' => Form::NUMBER, | |
| 157 | + ], | |
| 158 | + ], | |
| 159 | + ]; | |
| 160 | + } | |
| 161 | + | |
| 162 | + public function actionSettings() | |
| 163 | + { | |
| 164 | + return $this->render('settings'); | |
| 165 | + } | |
| 166 | + } | |
| 0 | 167 | \ No newline at end of file | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | + namespace common\models\speaker; | |
| 4 | + | |
| 5 | + use artbox\core\models\Image; | |
| 6 | + use artbox\core\models\Language; | |
| 7 | + use yii\db\ActiveQuery; | |
| 8 | + use yii\db\ActiveRecord; | |
| 9 | + use yii2tech\ar\position\PositionBehavior; | |
| 10 | + use yii2tech\ar\variation\VariationBehavior; | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * Class Page | |
| 14 | + * | |
| 15 | + * @package common\models\speaker | |
| 16 | + * @property integer $id | |
| 17 | + * @property integer $sort | |
| 18 | + * @property boolean $status | |
| 19 | + * @property Image $image | |
| 20 | + * @method ActiveQuery hasDefaultVariationRelation(); | |
| 21 | + */ | |
| 22 | + class Speaker extends ActiveRecord | |
| 23 | + { | |
| 24 | + public function rules() | |
| 25 | + { | |
| 26 | + return [ | |
| 27 | + [ | |
| 28 | + [ | |
| 29 | + 'sort', | |
| 30 | + 'image_id', | |
| 31 | + ], | |
| 32 | + 'integer', | |
| 33 | + ], | |
| 34 | + [ | |
| 35 | + [ | |
| 36 | + 'status', | |
| 37 | + ], | |
| 38 | + 'boolean', | |
| 39 | + ], | |
| 40 | + ]; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public function behaviors() | |
| 44 | + { | |
| 45 | + return [ | |
| 46 | + 'translations' => [ | |
| 47 | + 'class' => VariationBehavior::className(), | |
| 48 | + 'variationsRelation' => 'languages', | |
| 49 | + 'defaultVariationRelation' => 'language', | |
| 50 | + 'variationOptionReferenceAttribute' => 'language_id', | |
| 51 | + 'optionModelClass' => Language::className(), | |
| 52 | + 'defaultVariationOptionReference' => function () { | |
| 53 | + return Language::getCurrent()->id; | |
| 54 | + }, | |
| 55 | + 'optionQueryFilter' => function (ActiveQuery $query) { | |
| 56 | + $query->where( | |
| 57 | + [ | |
| 58 | + 'status' => true, | |
| 59 | + ] | |
| 60 | + ); | |
| 61 | + }, | |
| 62 | + ], | |
| 63 | + 'positionBehavior' => [ | |
| 64 | + 'class' => PositionBehavior::className(), | |
| 65 | + 'positionAttribute' => 'sort', | |
| 66 | + ], | |
| 67 | + ]; | |
| 68 | + } | |
| 69 | + | |
| 70 | + public static function tableName() | |
| 71 | + { | |
| 72 | + return 'speaker'; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * @return \yii\db\ActiveQuery | |
| 77 | + */ | |
| 78 | + public function getLanguages() | |
| 79 | + { | |
| 80 | + return $this->hasMany(SpeakerLang::className(), [ 'speaker_id' => 'id' ]); | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * @return \yii\db\ActiveQuery | |
| 85 | + */ | |
| 86 | + public function getLanguage() | |
| 87 | + { | |
| 88 | + return $this->hasMany(SpeakerLang::className(), [ 'speaker_id' => 'id' ]) | |
| 89 | + ->where( | |
| 90 | + [ | |
| 91 | + 'language_id' => Language::getCurrent()->id, | |
| 92 | + ] | |
| 93 | + ); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * @return \yii\db\ActiveQuery | |
| 98 | + */ | |
| 99 | + public function getImage() | |
| 100 | + { | |
| 101 | + return $this->hasOne(Image::className(), [ 'id' => 'image_id' ]); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * @return array | |
| 106 | + */ | |
| 107 | + public function attributeLabels() | |
| 108 | + { | |
| 109 | + return [ | |
| 110 | + 'sort' => \Yii::t('core', 'Sort'), | |
| 111 | + 'status' => \Yii::t('core', 'Status'), | |
| 112 | + 'image_id' => \Yii::t('core', 'Image'), | |
| 113 | + ]; | |
| 114 | + } | |
| 115 | + } | |
| 0 | 116 | \ No newline at end of file | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | + namespace common\models\speaker; | |
| 4 | + | |
| 5 | + use yii\db\ActiveRecord; | |
| 6 | + | |
| 7 | + /** | |
| 8 | + * Class SpeakerLang | |
| 9 | + * | |
| 10 | + * @property string $name | |
| 11 | + * @property string $position | |
| 12 | + * @property string $organization | |
| 13 | + * @property integer $speaker_id | |
| 14 | + * @property integer $language_id | |
| 15 | + * @package common\models\page | |
| 16 | + */ | |
| 17 | + class SpeakerLang extends ActiveRecord | |
| 18 | + { | |
| 19 | + public static function primaryKey() | |
| 20 | + { | |
| 21 | + return [ | |
| 22 | + 'speaker_id', | |
| 23 | + 'language_id', | |
| 24 | + ]; | |
| 25 | + } | |
| 26 | + | |
| 27 | + public function rules() | |
| 28 | + { | |
| 29 | + return [ | |
| 30 | + [ | |
| 31 | + [ | |
| 32 | + 'name', | |
| 33 | + 'organization', | |
| 34 | + 'position', | |
| 35 | + ], | |
| 36 | + 'string', | |
| 37 | + ], | |
| 38 | + [ | |
| 39 | + [ | |
| 40 | + 'speaker_id', | |
| 41 | + 'language_id', | |
| 42 | + ], | |
| 43 | + 'integer', | |
| 44 | + ], | |
| 45 | + ]; | |
| 46 | + } | |
| 47 | + | |
| 48 | + public static function tableName() | |
| 49 | + { | |
| 50 | + return 'speaker_lang'; | |
| 51 | + } | |
| 52 | + | |
| 53 | + public function attributeLabels() | |
| 54 | + { | |
| 55 | + return [ | |
| 56 | + 'name' => \Yii::t('app', 'Speaker Name'), | |
| 57 | + 'organization' => \Yii::t('app', 'Organization'), | |
| 58 | + 'position' => \Yii::t('app', 'Position'), | |
| 59 | + ]; | |
| 60 | + } | |
| 61 | + } | |
| 0 | 62 | \ No newline at end of file | ... | ... |
console/migrations/m180830_085211_create_speakers_tables.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | + use yii\db\Migration; | |
| 4 | + | |
| 5 | + /** | |
| 6 | + * Class m180830_085211_create_speakers_tables | |
| 7 | + */ | |
| 8 | + class m180830_085211_create_speakers_tables extends Migration | |
| 9 | + { | |
| 10 | + /** | |
| 11 | + * {@inheritdoc} | |
| 12 | + */ | |
| 13 | + public function safeUp() | |
| 14 | + { | |
| 15 | + $this->createTable( | |
| 16 | + 'speaker', | |
| 17 | + [ | |
| 18 | + 'id' => $this->primaryKey(), | |
| 19 | + 'sort' => $this->integer(), | |
| 20 | + 'image_id' => $this->integer(), | |
| 21 | + 'status' => $this->boolean(), | |
| 22 | + ] | |
| 23 | + ); | |
| 24 | + | |
| 25 | + $this->createTable( | |
| 26 | + 'speaker_lang', | |
| 27 | + [ | |
| 28 | + 'speaker_id' => $this->integer(), | |
| 29 | + 'language_id' => $this->integer(), | |
| 30 | + 'name' => $this->string(), | |
| 31 | + 'organization' => $this->text(), | |
| 32 | + 'position' => $this->text(), | |
| 33 | + ] | |
| 34 | + ); | |
| 35 | + | |
| 36 | + $this->createIndex( | |
| 37 | + 'spkr_idx', | |
| 38 | + 'speaker_lang', | |
| 39 | + [ | |
| 40 | + 'speaker_id', | |
| 41 | + 'language_id', | |
| 42 | + ], | |
| 43 | + true | |
| 44 | + ); | |
| 45 | + | |
| 46 | + $this->addForeignKey('spkr_fk', 'speaker_lang', 'speaker_id', 'speaker', 'id', 'CASCADE', 'CASCADE'); | |
| 47 | + | |
| 48 | + $this->addForeignKey('lng_fk', 'speaker_lang', 'language_id', 'language', 'id', 'CASCADE', 'CASCADE'); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * {@inheritdoc} | |
| 53 | + */ | |
| 54 | + public function safeDown() | |
| 55 | + { | |
| 56 | + $this->dropForeignKey('lng_fk', 'speaker_lang'); | |
| 57 | + | |
| 58 | + $this->dropForeignKey('spkr_fk', 'speaker_lang'); | |
| 59 | + | |
| 60 | + $this->dropIndex( | |
| 61 | + 'spkr_idx', | |
| 62 | + 'speaker_lang' | |
| 63 | + ); | |
| 64 | + | |
| 65 | + $this->dropTable('speaker_lang'); | |
| 66 | + | |
| 67 | + $this->dropTable('speaker'); | |
| 68 | + } | |
| 69 | + } | ... | ... |