From 2e9cac7fc0311b82aefb12f0e47e8c05655024fa Mon Sep 17 00:00:00 2001 From: yarik Date: Sun, 6 Nov 2016 19:05:17 +0200 Subject: [PATCH] Testing --- Module.php | 12 ++++++++++++ composer.json | 14 ++++++++++++++ controllers/ArticleController.php | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ models/Article.php | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 306 insertions(+), 0 deletions(-) create mode 100644 Module.php create mode 100644 composer.json create mode 100755 controllers/ArticleController.php create mode 100755 models/Article.php diff --git a/Module.php b/Module.php new file mode 100644 index 0000000..be4b68e --- /dev/null +++ b/Module.php @@ -0,0 +1,12 @@ +=7.0", + "yiisoft/yii2": "*" + }, + "autoload": { + "psr-0": { + "artweb\\artbox\\": "" + } + } +} \ No newline at end of file diff --git a/controllers/ArticleController.php b/controllers/ArticleController.php new file mode 100755 index 0000000..2c6c2e3 --- /dev/null +++ b/controllers/ArticleController.php @@ -0,0 +1,165 @@ + [ + 'class' => AccessBehavior::className(), + 'rules' => [ + 'site' => [ + [ + 'actions' => [ + 'login', + 'error', + ], + 'allow' => true, + ], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => [ 'POST' ], + ], + ], + ]; + } + + /** + * Lists all Article models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new ArticleSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Article model. + * + * @param integer $id + * + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Article model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Article(); + $model->generateLangs(); + if($model->load(Yii::$app->request->post())) { + $model->loadLangs(\Yii::$app->request); + if($model->save() && $model->transactionStatus) { + return $this->redirect([ + 'view', + 'id' => $model->id, + ]); + } + } + return $this->render('create', [ + 'model' => $model, + 'modelLangs' => $model->modelLangs, + ]); + } + + /** + * Updates an existing Article model. + * If update is successful, the browser will be redirected to the 'view' page. + * + * @param integer $id + * + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + $model->generateLangs(); + if($model->load(Yii::$app->request->post())) { + $model->loadLangs(\Yii::$app->request); + if($model->save() && $model->transactionStatus) { + return $this->redirect([ + 'view', + 'id' => $model->id, + ]); + } + } + return $this->render('update', [ + 'model' => $model, + 'modelLangs' => $model->modelLangs, + ]); + } + + /** + * Deletes an existing Article model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * + * @param integer $id + * + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id) + ->delete(); + + return $this->redirect([ 'index' ]); + } + + /** + * Finds the Article model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * + * @param integer $id + * + * @return Article the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if(( $model = Article::find() + ->where([ 'id' => $id ]) + ->with('lang') + ->one() ) !== NULL + ) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } + } diff --git a/models/Article.php b/models/Article.php new file mode 100755 index 0000000..af2a8e5 --- /dev/null +++ b/models/Article.php @@ -0,0 +1,115 @@ + SaveImgBehavior::className(), + 'fields' => [ + [ + 'name' => 'image', + 'directory' => 'article', + ], + ], + ], + 'language' => [ + 'class' => LanguageBehavior::className(), + ], + [ + 'class' => TimestampBehavior::className(), + 'updatedAtAttribute' => false, + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ 'created_at' ], + 'safe', + ], + [ + [ 'created_at' ], + 'filter', + 'filter' => function($value) { + return strtotime($value) ? : time(); + }, + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'created_at' => Yii::t('app', 'Date'), + 'image' => Yii::t('app', 'Image'), + 'imageUrl' => Yii::t('app', 'Image'), + ]; + } + } -- libgit2 0.21.4