/** * Инициализация виджета */ public function init() { parent::init(); $this->model = new Comment(); $this->models = !empty($this->models) ? $this->models : Comment::find()->page()->approved()->all(); $this->publishAssets(); $this->buildCommentsArray(); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Comment::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'parent_id' => $this->parent_id, 'user_id' => $this->user_id, 'likes' => $this->likes, 'status' => $this->status, 'notify' => $this->notify, 'created' => $this->created, 'updated' => $this->updated]); $query->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'author', $this->author])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'ip', $this->ip]); $query->orderBy(['created' => SORT_DESC]); return $dataProvider; }
/** * Отправляет пачками письма пользователям о новых комментариях * @param $newComment */ protected function sendUserNotifies($newComment) { foreach (Comment::find()->page($newComment->url)->notify()->all() as $subscriber) { // Если email нового комментария (отправителя) совпадает с email подписчика, // то выходит что это один и тот же человек, ему уведомление не высылаем, пропускаем итерацию цикла if ($newComment->getEmail() === $subscriber->getEmail()) { continue; } $message = $this->renderPartial('../../extensions/comments_widget/views/email/notifyUser', ['newComment' => $newComment, 'userName' => $subscriber->getAuthor(), 'userEmail' => $subscriber->getEmail(), 'hash' => $subscriber->getHash()], true); $this->module->sendMail($subscriber->getEmail(), 'Новый комментарий на сайте "' . \Yii::$app->name . '"', $message); } }
/** * Загружает модели по массиву с id'шниками * @param $ids * @return \CActiveRecord[] */ private function loadModels($ids) { return Comment::find()->where(['id' => $ids])->all(); }