<?php

    namespace common\modules\comment\rbac;

    use yii\rbac\Rule;

    class ArtboxCommentCreateRule extends Rule
    {

        public $name = 'canCommentCreateArtbox';

        public function execute($user, $item, $params)
        {
            if($params[ 'model' ] == \common\models\Project::className()) {
                return $this->checkProject($user, $item, $params);
            }
            return true;
        }

        public function checkProject($user, $item, $params)
        {
            $project = \common\models\Project::findOne($params['model_id']);
            if($project->user_id == $user) {
                return false;
            }
            $comment = \common\modules\comment\models\CommentProject::find()
                                                                    ->where([ 'model'    => $params[ 'model' ],
                                                                              'model_id' => $params[ 'model_id' ],
                                                                              'user_id'  => $user,
                                                                    ])->one();
            if(empty($comment)) {
                return true;
            } else {
                return false;
            }
        }

    }