Commit 2e9cac7fc0311b82aefb12f0e47e8c05655024fa
1 parent
8c6c4506
Testing
Showing
4 changed files
with
306 additions
and
0 deletions
Show diff stats
| 1 | +{ | |
| 2 | + "name": "artweb/yii2-artbox", | |
| 3 | + "description": "Yii2 ligth-weight CMS", | |
| 4 | + "license": "BSD-3-Clause", | |
| 5 | + "require": { | |
| 6 | + "php": ">=7.0", | |
| 7 | + "yiisoft/yii2": "*" | |
| 8 | + }, | |
| 9 | + "autoload": { | |
| 10 | + "psr-0": { | |
| 11 | + "artweb\\artbox\\": "" | |
| 12 | + } | |
| 13 | + } | |
| 14 | +} | |
| 0 | 15 | \ No newline at end of file | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | + namespace backend\controllers; | |
| 4 | + | |
| 5 | + use common\models\Article; | |
| 6 | + use common\models\ArticleSearch; | |
| 7 | + use Yii; | |
| 8 | + use yii\web\Controller; | |
| 9 | + use yii\web\NotFoundHttpException; | |
| 10 | + use yii\filters\VerbFilter; | |
| 11 | + use developeruz\db_rbac\behaviors\AccessBehavior; | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * ArticleController implements the CRUD actions for Article model. | |
| 15 | + */ | |
| 16 | + class ArticleController extends Controller | |
| 17 | + { | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @inheritdoc | |
| 21 | + */ | |
| 22 | + public function behaviors() | |
| 23 | + { | |
| 24 | + return [ | |
| 25 | + 'access' => [ | |
| 26 | + 'class' => AccessBehavior::className(), | |
| 27 | + 'rules' => [ | |
| 28 | + 'site' => [ | |
| 29 | + [ | |
| 30 | + 'actions' => [ | |
| 31 | + 'login', | |
| 32 | + 'error', | |
| 33 | + ], | |
| 34 | + 'allow' => true, | |
| 35 | + ], | |
| 36 | + ], | |
| 37 | + ], | |
| 38 | + ], | |
| 39 | + 'verbs' => [ | |
| 40 | + 'class' => VerbFilter::className(), | |
| 41 | + 'actions' => [ | |
| 42 | + 'delete' => [ 'POST' ], | |
| 43 | + ], | |
| 44 | + ], | |
| 45 | + ]; | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Lists all Article models. | |
| 50 | + * @return mixed | |
| 51 | + */ | |
| 52 | + public function actionIndex() | |
| 53 | + { | |
| 54 | + $searchModel = new ArticleSearch(); | |
| 55 | + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); | |
| 56 | + | |
| 57 | + return $this->render('index', [ | |
| 58 | + 'searchModel' => $searchModel, | |
| 59 | + 'dataProvider' => $dataProvider, | |
| 60 | + ]); | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Displays a single Article model. | |
| 65 | + * | |
| 66 | + * @param integer $id | |
| 67 | + * | |
| 68 | + * @return mixed | |
| 69 | + */ | |
| 70 | + public function actionView($id) | |
| 71 | + { | |
| 72 | + return $this->render('view', [ | |
| 73 | + 'model' => $this->findModel($id), | |
| 74 | + ]); | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Creates a new Article model. | |
| 79 | + * If creation is successful, the browser will be redirected to the 'view' page. | |
| 80 | + * @return mixed | |
| 81 | + */ | |
| 82 | + public function actionCreate() | |
| 83 | + { | |
| 84 | + $model = new Article(); | |
| 85 | + $model->generateLangs(); | |
| 86 | + if($model->load(Yii::$app->request->post())) { | |
| 87 | + $model->loadLangs(\Yii::$app->request); | |
| 88 | + if($model->save() && $model->transactionStatus) { | |
| 89 | + return $this->redirect([ | |
| 90 | + 'view', | |
| 91 | + 'id' => $model->id, | |
| 92 | + ]); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + return $this->render('create', [ | |
| 96 | + 'model' => $model, | |
| 97 | + 'modelLangs' => $model->modelLangs, | |
| 98 | + ]); | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Updates an existing Article model. | |
| 103 | + * If update is successful, the browser will be redirected to the 'view' page. | |
| 104 | + * | |
| 105 | + * @param integer $id | |
| 106 | + * | |
| 107 | + * @return mixed | |
| 108 | + */ | |
| 109 | + public function actionUpdate($id) | |
| 110 | + { | |
| 111 | + $model = $this->findModel($id); | |
| 112 | + $model->generateLangs(); | |
| 113 | + if($model->load(Yii::$app->request->post())) { | |
| 114 | + $model->loadLangs(\Yii::$app->request); | |
| 115 | + if($model->save() && $model->transactionStatus) { | |
| 116 | + return $this->redirect([ | |
| 117 | + 'view', | |
| 118 | + 'id' => $model->id, | |
| 119 | + ]); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + return $this->render('update', [ | |
| 123 | + 'model' => $model, | |
| 124 | + 'modelLangs' => $model->modelLangs, | |
| 125 | + ]); | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Deletes an existing Article model. | |
| 130 | + * If deletion is successful, the browser will be redirected to the 'index' page. | |
| 131 | + * | |
| 132 | + * @param integer $id | |
| 133 | + * | |
| 134 | + * @return mixed | |
| 135 | + */ | |
| 136 | + public function actionDelete($id) | |
| 137 | + { | |
| 138 | + $this->findModel($id) | |
| 139 | + ->delete(); | |
| 140 | + | |
| 141 | + return $this->redirect([ 'index' ]); | |
| 142 | + } | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * Finds the Article model based on its primary key value. | |
| 146 | + * If the model is not found, a 404 HTTP exception will be thrown. | |
| 147 | + * | |
| 148 | + * @param integer $id | |
| 149 | + * | |
| 150 | + * @return Article the loaded model | |
| 151 | + * @throws NotFoundHttpException if the model cannot be found | |
| 152 | + */ | |
| 153 | + protected function findModel($id) | |
| 154 | + { | |
| 155 | + if(( $model = Article::find() | |
| 156 | + ->where([ 'id' => $id ]) | |
| 157 | + ->with('lang') | |
| 158 | + ->one() ) !== NULL | |
| 159 | + ) { | |
| 160 | + return $model; | |
| 161 | + } else { | |
| 162 | + throw new NotFoundHttpException('The requested page does not exist.'); | |
| 163 | + } | |
| 164 | + } | |
| 165 | + } | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | + namespace artweb\artbox\models; | |
| 4 | + | |
| 5 | + use common\models\ArticleLang; | |
| 6 | + use common\modules\language\behaviors\LanguageBehavior; | |
| 7 | + use common\behaviors\SaveImgBehavior; | |
| 8 | + use Yii; | |
| 9 | + use yii\behaviors\TimestampBehavior; | |
| 10 | + use yii\db\ActiveQuery; | |
| 11 | + use yii\db\ActiveRecord; | |
| 12 | + use yii\web\Request; | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * This is the model class for table "articles". | |
| 16 | + * | |
| 17 | + * @property integer $id | |
| 18 | + * @property integer $created_at | |
| 19 | + * @property string $image | |
| 20 | + * * From language behavior * | |
| 21 | + * @property ArticleLang $lang | |
| 22 | + * @property ArticleLang[] $langs | |
| 23 | + * @property ArticleLang $objectLang | |
| 24 | + * @property string $ownerKey | |
| 25 | + * @property string $langKey | |
| 26 | + * @property ArticleLang[] $modelLangs | |
| 27 | + * @property bool $transactionStatus | |
| 28 | + * @method string getOwnerKey() | |
| 29 | + * @method void setOwnerKey( string $value ) | |
| 30 | + * @method string getLangKey() | |
| 31 | + * @method void setLangKey( string $value ) | |
| 32 | + * @method ActiveQuery getLangs() | |
| 33 | + * @method ActiveQuery getLang( integer $language_id ) | |
| 34 | + * @method ArticleLang[] generateLangs() | |
| 35 | + * @method void loadLangs( Request $request ) | |
| 36 | + * @method bool linkLangs() | |
| 37 | + * @method bool saveLangs() | |
| 38 | + * @method bool getTransactionStatus() | |
| 39 | + * * End language behavior * | |
| 40 | + * * From SaveImgBehavior | |
| 41 | + * @property string|null $imageFile | |
| 42 | + * @property string|null $imageUrl | |
| 43 | + * @method string|null getImageFile( int $field ) | |
| 44 | + * @method string|null getImageUrl( int $field ) | |
| 45 | + * * End SaveImgBehavior | |
| 46 | + */ | |
| 47 | + class Article extends ActiveRecord | |
| 48 | + { | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * @inheritdoc | |
| 52 | + */ | |
| 53 | + public static function tableName() | |
| 54 | + { | |
| 55 | + return 'article'; | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * @inheritdoc | |
| 60 | + */ | |
| 61 | + public function behaviors() | |
| 62 | + { | |
| 63 | + return [ | |
| 64 | + [ | |
| 65 | + 'class' => SaveImgBehavior::className(), | |
| 66 | + 'fields' => [ | |
| 67 | + [ | |
| 68 | + 'name' => 'image', | |
| 69 | + 'directory' => 'article', | |
| 70 | + ], | |
| 71 | + ], | |
| 72 | + ], | |
| 73 | + 'language' => [ | |
| 74 | + 'class' => LanguageBehavior::className(), | |
| 75 | + ], | |
| 76 | + [ | |
| 77 | + 'class' => TimestampBehavior::className(), | |
| 78 | + 'updatedAtAttribute' => false, | |
| 79 | + ], | |
| 80 | + ]; | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * @inheritdoc | |
| 85 | + */ | |
| 86 | + public function rules() | |
| 87 | + { | |
| 88 | + return [ | |
| 89 | + [ | |
| 90 | + [ 'created_at' ], | |
| 91 | + 'safe', | |
| 92 | + ], | |
| 93 | + [ | |
| 94 | + [ 'created_at' ], | |
| 95 | + 'filter', | |
| 96 | + 'filter' => function($value) { | |
| 97 | + return strtotime($value) ? : time(); | |
| 98 | + }, | |
| 99 | + ], | |
| 100 | + ]; | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * @inheritdoc | |
| 105 | + */ | |
| 106 | + public function attributeLabels() | |
| 107 | + { | |
| 108 | + return [ | |
| 109 | + 'id' => Yii::t('app', 'ID'), | |
| 110 | + 'created_at' => Yii::t('app', 'Date'), | |
| 111 | + 'image' => Yii::t('app', 'Image'), | |
| 112 | + 'imageUrl' => Yii::t('app', 'Image'), | |
| 113 | + ]; | |
| 114 | + } | |
| 115 | + } | ... | ... |