Ejemplo n.º 1
0
 /**
  * Named scope to get entries for a certain model
  *
  * @param \yii\db\ActiveRecord $model the model to get the audit trail for
  * @return \asinfotrack\yii2\comments\models\CommentQuery
  * @throws \yii\base\InvalidParamException if the model is not of type ActiveRecord
  * @throws \yii\base\InvalidConfigException if the models pk is empty or invalid
  */
 public function subject($model)
 {
     ComponentConfig::isActiveRecord($model, true);
     static::validateModel($model);
     $this->modelClass($model::className());
     $this->andWhere(['foreign_pk' => static::createPrimaryKeyJson($model)]);
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     //assert proper model is set
     if ($this->subject === null || !ComponentConfig::isActiveRecord($this->subject, false)) {
         $msg = Yii::t('app', 'Setting the model property of type ActiveRecord is mandatory');
         throw new InvalidConfigException($msg);
     }
     ComponentConfig::hasBehavior($this->subject, CommentsBehavior::className(), true);
     //assert proper comment model
     if ($this->commentModel === null || !$this->commentModel instanceof Comment) {
         $msg = Yii::t('app', 'No proper comment model set');
         throw new InvalidConfigException($msg);
     }
 }
Ejemplo n.º 3
0
 /**
  * @inheritdoc
  *
  * @throws \yii\base\InvalidConfigException
  */
 public function init()
 {
     //assert proper model is set
     if ($this->subject === null || !ComponentConfig::isActiveRecord($this->subject, false)) {
         $msg = Yii::t('app', 'Setting the model property of type ActiveRecord is mandatory');
         throw new InvalidConfigException($msg);
     }
     ComponentConfig::hasBehavior($this->subject, CommentsBehavior::className(), true);
     //load the comments
     $this->loadComments();
     //prepare options
     Html::addCssClass($this->options, 'widget-comments');
     Html::addCssClass($this->commentOptions, 'comment');
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     //assert proper model is set
     if ($this->model === null || !ComponentConfig::isActiveRecord($this->model)) {
         $msg = Yii::t('app', 'Please set a proper model of type active record');
         throw new InvalidConfigException($msg);
     }
     //assert action is set
     if (empty($this->action)) {
         $msg = Yii::t('app', 'Please sepcify an action to call');
         throw new InvalidConfigException($msg);
     }
     //fetch the boolean format
     if ($this->booleanFormat === null) {
         $this->booleanFormat = Yii::$app->formatter->booleanFormat;
     }
     //prepare ajax url
     $controller = $this->controller !== null ? $this->controller : Inflector::camel2id(StringHelper::basename($this->model->className()));
     $this->ajaxUrl = Url::to(['/' . $controller . '/' . $this->action]);
     //set label
     $this->label = Yii::$app->formatter->asBoolean($this->model->{$this->booleanAttribute});
 }
Ejemplo n.º 5
0
 /**
  * Sets the subject model
  *
  * @param \yii\db\ActiveRecord $subject
  */
 public function setSubject($subject)
 {
     //validate subject model
     ComponentConfig::isActiveRecord($subject, true);
     ComponentConfig::hasBehavior($subject, CommentsBehavior::className(), true);
     //only on unsaved comments
     if (!$this->isNewRecord) {
         $msg = Yii::t('app', 'The subject model can only be set manually on unsaved comments');
         throw new InvalidCallException($msg);
     }
     //set values from subject model
     $this->model_class = $subject->className();
     $this->foreign_pk = PrimaryKey::asJson($subject);
     $this->subject = $subject;
 }
Ejemplo n.º 6
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     //validate proper owner config
     ComponentConfig::isActiveRecord($owner, true);
     parent::attach($owner);
 }
 /**
  * @inheritdoc
  *
  * @throws \yii\base\InvalidConfigException
  */
 public function init()
 {
     //validate model is active record
     $this->modelInstance = new $this->targetClass();
     ComponentConfig::isActiveRecord($this->modelInstance, true);
 }