You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
5.5 KiB
202 lines
5.5 KiB
<?php |
|
|
|
namespace common\modules\blog\controllers\manage; |
|
|
|
use common\modules\blog\entities\BlogPost; |
|
use common\modules\blog\entities\BlogTag; |
|
use common\modules\blog\forms\BlogPostForm; |
|
use common\modules\blog\forms\search\BlogPostSearch; |
|
use common\modules\blog\services\BlogPostManageService; |
|
use Yii; |
|
use yii\web\Controller; |
|
use yii\web\NotFoundHttpException; |
|
use yii\filters\VerbFilter; |
|
use yii\filters\AccessControl; |
|
use yii\web\Response; |
|
|
|
class PostController extends Controller |
|
{ |
|
private $service; |
|
|
|
public function __construct($id, $module, BlogPostManageService $service, $config = []) |
|
{ |
|
parent::__construct($id, $module, $config); |
|
$this->service = $service; |
|
} |
|
|
|
public function behaviors(): array |
|
{ |
|
return [ |
|
'access' => [ |
|
'class' => AccessControl::className(), |
|
'rules' => [ |
|
[ |
|
'allow' => true, |
|
'roles' => ['BlogManagement'], |
|
], |
|
[ // all the action are accessible to admin |
|
'allow' => true, |
|
'roles' => ['admin'], |
|
], |
|
], |
|
], |
|
'verbs' => [ |
|
'class' => VerbFilter::className(), |
|
'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->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->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; |
|
} |
|
|
|
/** |
|
* @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.'); |
|
} |
|
}
|
|
|