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.
		
		
		
		
		
			
		
			
				
					
					
						
							159 lines
						
					
					
						
							4.2 KiB
						
					
					
				
			
		
		
	
	
							159 lines
						
					
					
						
							4.2 KiB
						
					
					
				<?php | 
						|
 | 
						|
namespace backend\controllers\post; | 
						|
 | 
						|
use core\forms\post\search\PostCommentSearch; | 
						|
use core\forms\post\PostCommentEditForm; | 
						|
use core\services\post\PostCommentManageService; | 
						|
use Yii; | 
						|
use core\entities\post\Post; | 
						|
use yii\web\Controller; | 
						|
use yii\web\NotFoundHttpException; | 
						|
use yii\filters\VerbFilter; | 
						|
use yii\filters\AccessControl; | 
						|
 | 
						|
class CommentController extends Controller | 
						|
{ | 
						|
    private $service; | 
						|
 | 
						|
    public function __construct($id, $module, PostCommentManageService $service, $config = []) | 
						|
    { | 
						|
        parent::__construct($id, $module, $config); | 
						|
        $this->service = $service; | 
						|
    } | 
						|
 | 
						|
    public function behaviors(): array | 
						|
    { | 
						|
        return [ | 
						|
            'access' => [ | 
						|
                'class' => AccessControl::className(), | 
						|
                'rules' => [ | 
						|
                    [ | 
						|
                        'allow' => true, | 
						|
                        'roles' => ['PostManagement'], | 
						|
                    ], | 
						|
                    [    // all the action are accessible to admin | 
						|
                        'allow' => true, | 
						|
                        'roles' => ['admin'], | 
						|
                    ], | 
						|
                ], | 
						|
            ], | 
						|
            'verbs' => [ | 
						|
                'class' => VerbFilter::className(), | 
						|
                'actions' => [ | 
						|
                    'delete' => ['POST'], | 
						|
                ], | 
						|
            ], | 
						|
        ]; | 
						|
    } | 
						|
 | 
						|
    /** | 
						|
     * @return mixed | 
						|
     */ | 
						|
    public function actionIndex() | 
						|
    { | 
						|
        $searchModel = new PostCommentSearch(); | 
						|
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams); | 
						|
 | 
						|
        return $this->render('index', [ | 
						|
            'searchModel' => $searchModel, | 
						|
            'dataProvider' => $dataProvider, | 
						|
        ]); | 
						|
    } | 
						|
 | 
						|
	/** | 
						|
	 * @param $post_id | 
						|
	 * @param $id | 
						|
	 * | 
						|
	 * @return string|\yii\web\Response | 
						|
	 * @throws NotFoundHttpException | 
						|
	 */ | 
						|
    public function actionUpdate($post_id, $id) | 
						|
    { | 
						|
        $post = $this->findModel($post_id); | 
						|
        $comment = $post->getComment($id); | 
						|
 | 
						|
        $form = new PostCommentEditForm($comment); | 
						|
        if ($form->load(Yii::$app->request->post()) && $form->validate()) { | 
						|
            try { | 
						|
                $this->service->edit($post->id, $comment->id, $form); | 
						|
                return $this->redirect(['view', 'post_id' => $post->id, 'id' => $comment->id]); | 
						|
            } catch (\DomainException $e) { | 
						|
                Yii::$app->errorHandler->logException($e); | 
						|
                Yii::$app->session->setFlash('error', $e->getMessage()); | 
						|
            } | 
						|
        } | 
						|
        return $this->render('update', [ | 
						|
            'post' => $post, | 
						|
            'model' => $form, | 
						|
            'comment' => $comment, | 
						|
        ]); | 
						|
    } | 
						|
 | 
						|
	/** | 
						|
	 * @param $post_id | 
						|
	 * @param $id | 
						|
	 * | 
						|
	 * @return string | 
						|
	 * @throws NotFoundHttpException | 
						|
	 */ | 
						|
    public function actionView($post_id, $id) | 
						|
    { | 
						|
        $post = $this->findModel($post_id); | 
						|
        $comment = $post->getComment($id); | 
						|
 | 
						|
        return $this->render('view', [ | 
						|
            'post' => $post, | 
						|
            'comment' => $comment, | 
						|
        ]); | 
						|
    } | 
						|
 | 
						|
	/** | 
						|
	 * @param $post_id | 
						|
	 * @param $id | 
						|
	 * | 
						|
	 * @return \yii\web\Response | 
						|
	 * @throws NotFoundHttpException | 
						|
	 */ | 
						|
    public function actionActivate($post_id, $id) | 
						|
    { | 
						|
        $post = $this->findModel($post_id); | 
						|
        try { | 
						|
            $this->service->activate($post->id, $id); | 
						|
        } catch (\DomainException $e) { | 
						|
            Yii::$app->session->setFlash('error', $e->getMessage()); | 
						|
        } | 
						|
        return $this->redirect(['view', 'post_id' => $post_id, 'id' => $id]); | 
						|
    } | 
						|
 | 
						|
	/** | 
						|
	 * @param $post_id | 
						|
	 * @param $id | 
						|
	 * | 
						|
	 * @return \yii\web\Response | 
						|
	 * @throws NotFoundHttpException | 
						|
	 */ | 
						|
    public function actionDelete($post_id, $id) | 
						|
    { | 
						|
        $post = $this->findModel($post_id); | 
						|
        try { | 
						|
            $this->service->remove($post->id, $id); | 
						|
        } catch (\DomainException $e) { | 
						|
            Yii::$app->session->setFlash('error', $e->getMessage()); | 
						|
        } | 
						|
        return $this->redirect(['index']); | 
						|
    } | 
						|
 | 
						|
    /** | 
						|
     * @param integer $id | 
						|
     * @return Post the loaded model | 
						|
     * @throws NotFoundHttpException if the model cannot be found | 
						|
     */ | 
						|
    protected function findModel($id): Post | 
						|
    { | 
						|
        if (($model = Post::findOne($id)) !== null) { | 
						|
            return $model; | 
						|
        } | 
						|
        throw new NotFoundHttpException('The requested page does not exist.'); | 
						|
    } | 
						|
}
 | 
						|
 |