service = $service; } public function behaviors(): array { return [ 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['BlogManagement'], ], [ // all the action are accessible to admin 'allow' => true, 'roles' => ['admin'], ], ], ], 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], 'activate' => ['POST'], 'draft' => ['POST'], 'delete-photo' => ['POST'], 'move-photo-up' => ['POST'], 'move-photo-down' => ['POST'], ], ], ]; } /** * @return string */ public function actionIndex() { $searchModel = new BlogPostSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render( 'index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * @param $id * * @return string * @throws NotFoundHttpException */ public function actionView($id) { $post = $this->findModel($id); return $this->render('view', [ 'post' => $post, ]); } /** * @return string|Response */ public function actionCreate() { $form = new BlogPostForm(); $form->action = 'create'; $form->published_at = date('d.m.Y H:i:s'); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { $post = $this->service->create($form); return $this->redirect(['view', 'id' => $post->id]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } return $this->render('create', [ 'model' => $form, ]); } /** * @param $id * * @return string|\yii\web\Response * @throws NotFoundHttpException */ public function actionUpdate($id) { $post = $this->findModel($id); $form = new BlogPostForm($post); $form->action = 'update'; $form->published_at = date('d.m.Y H:i:s', $form->published_at); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { $this->service->edit($post->id, $form); return $this->redirect(['view', 'id' => $post->id]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } return $this->render('update', [ 'model' => $form, 'post' => $post, ]); } /** * @param $id * * @return Response */ public function actionDelete($id) { try { $this->service->remove($id); } catch (\DomainException $e) { Yii::$app->session->setFlash('error', $e->getMessage()); } return $this->redirect(['index']); } /** * @param integer $id * @return mixed */ public function actionActivate($id) { try { $this->service->activate($id); } catch (\DomainException $e) { Yii::$app->session->setFlash('error', $e->getMessage()); } return $this->redirect(['view', 'id' => $id]); } /** * @param integer $id * @return mixed */ public function actionDraft($id) { try { $this->service->draft($id); } catch (\DomainException $e) { Yii::$app->session->setFlash('error', $e->getMessage()); } return $this->redirect(['view', 'id' => $id]); } public function actionTagSearch($q = null, $id = null) { \Yii::$app->response->format = Response::FORMAT_JSON; $out = ['results' => ['id' => '', 'text' => '']]; if (!is_null($q)) { $data = BlogTag::find()->select('name as id, name as text')->andWhere(['like', 'name', $q])->orderBy('name')->limit(20)->asArray()->all(); $out['results'] = array_values($data); } elseif ($id > 0) { $tag_name = BlogTag::findOne($id)->name; $out['results'] = ['id' => $tag_name, 'text' => $tag_name]; } return $out; } public function actionPostSearch($q = null, $id = null) { \Yii::$app->response->format = Response::FORMAT_JSON; $out = ['results' => ['id' => '', 'text' => '']]; if (!is_null($q)) { $data = BlogPost::find()->select('id, title as text')->andWhere(['like', 'title', $q])->limit(20)->asArray()->all(); $out['results'] = array_values($data); } elseif ($id > 0) { $tag_name = BlogPost::findOne($id)->title; $out['results'] = ['id' => $tag_name, 'text' => $tag_name]; } else { $data = BlogPost::find()->select('id, title as text')->orderBy(['created_at' => SORT_DESC])->limit(20)->asArray()->all(); $out['results'] = array_values($data); } return $out; } /** * @param $id * * @return BlogPost * @throws NotFoundHttpException */ protected function findModel($id): BlogPost { if (($model = BlogPost::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException('The requested page does not exist.'); } }