/** * Prepare output comment post information array * @return array|null */ public function make() { if ($this->_record === null) { return null; } // build user data $userName = __('Unknown'); $userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg'; $userObject = $this->_record->getUser(); if ($userObject !== null) { $userName = $userObject->getProfile()->getNickname(); $userAvatar = $userObject->getProfile()->getAvatarUrl('small'); } else { if (!Str::likeEmpty($this->_record->guest_name)) { $userName = App::$Security->strip_tags($this->_record->guest_name); } } // return output json data $res = ['type' => $this->_type, 'id' => $this->_record->id, 'text' => $this->_record->message, 'date' => Date::convertToDatetime($this->_record->created_at, Date::FORMAT_TO_HOUR), 'pathway' => $this->_record->pathway, 'moderate' => (int) $this->_record->moderate, 'user' => ['id' => $this->_record->user_id, 'name' => $userName, 'avatar' => $userAvatar]]; if ($this->_type === 'post' && method_exists($this->_record, 'getAnswerCount') && $this->_calcAnswers) { $res['answers'] = $this->_record->getAnswerCount(); } elseif ($this->_type === 'answer') { $res['comment_id'] = $this->_record->comment_id; } return $res; }
public function getCommentId() { $id = $this->_record->id; if ($this->type === 'answer') { $id = $this->_record->getCommentPost()->id; } return $id; }
/** * Build search results. Should return array collection: [AbstractSearchResult] * @return array */ public function getResult() { // search in comments post $query = CommentPost::where('moderate', '=', 0)->search($this->query)->take($this->limit)->get(); // check if response is empty if ($query->count() < 1) { return []; } // build output $result = []; foreach ($query as $item) { /** @var CommentPost $item */ $snippet = App::$Security->strip_tags($item->message); $snippet = Text::snippet($snippet); // make unique instance object $instance = new AbstractSearchResult(); $instance->setTitle(App::$Translate->get('Search', 'Comment on the page')); $instance->setSnippet($snippet); $instance->setUri($item->pathway . '#comments-list'); $instance->setDate($item->created_at); $instance->setRelevance((int) $item->relevance); // add instance to result set $result[] = $instance; } return $result; }
/** * Make database query and return results * @return object */ private function makeQuery() { $records = CommentPost::where('lang', $this->lang)->where('moderate', 0); if ($records === null || $records->count() < 1) { return null; } return $records->orderBy('id', 'DESC')->take($this->count)->get(); }
/** * Check if comment answer conditions is ok. Will throw exception if not. * @return bool * @throws JsonException */ public function check() { // check if user is auth'd or guest name is defined if (!App::$User->isAuth() && ((int) $this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) { throw new JsonException(__('Guest name is not defined')); } // guest moderation if (!App::$User->isAuth() && (bool) $this->_configs['guestModerate']) { $captcha = App::$Request->request->get('captcha'); if (!App::$Captcha->validate($captcha)) { throw new JsonException(__('Captcha is incorrect! Click on image to refresh and try again')); } } // check if replayTo is defined if ($this->replayTo < 1) { throw new JsonException(__('Comment post thread is not founded')); } // check if message length is correct if (Str::length($this->message) < (int) $this->_configs['minLength'] || Str::length($this->message) > (int) $this->_configs['maxLength']) { throw new JsonException(__('Message length is incorrect. Current: %cur%, min - %min%, max - %max%', ['cur' => Str::length($this->message), 'min' => $this->_configs['minLength'], 'max' => $this->_configs['maxLength']])); } $count = CommentPost::where('id', '=', $this->replayTo)->count(); if ($count !== 1) { throw new JsonException(__('Comment post thread is not founded')); } // check to prevent spam $query = CommentAnswer::where('user_id', '=', $this->_userId)->orWhere('ip', '=', $this->ip)->orderBy('created_at', 'DESC')->first(); // something is founded :D if ($query !== null) { $answerTime = Date::convertToTimestamp($query->created_at); $delay = $answerTime + $this->_configs['delay'] - time(); if ($delay > 0) { // sounds like config time is not passed now throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); } } return true; }
/** * Insert new comment in table and return active record object * @return CommentPost */ public function buildRecord() { $record = new CommentPost(); $record->pathway = $this->pathway; $record->user_id = $this->_userId; $record->guest_name = $this->guestName; $record->message = $this->message; $record->lang = App::$Request->getLanguage(); // check if premoderation is enabled and user is guest if ((int) $this->_configs['guestModerate'] === 1 && $this->_userId < 1) { $record->moderate = 1; } $record->save(); return $record; }
/** * Moderate guest comments and answer - make it publish * @param string $type * @param int $id * @return string * @throws NotFoundException * @throws \Ffcms\Core\Exception\SyntaxException * @throws \Ffcms\Core\Exception\NativeException */ public function actionPublish($type, $id = 0) { // check if it multiple accept ids if ($id === 0 || (int) $id < 1) { $ids = $this->request->query->get('selected'); if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) { $id = $ids; } else { throw new NotFoundException('Bad conditions'); } } else { $id = [$id]; } // build query $query = null; switch ($type) { case static::TYPE_COMMENT: $query = CommentPost::whereIn('id', $id)->where('moderate', '=', 1); break; case static::TYPE_ANSWER: $query = CommentAnswer::whereIn('id', $id)->where('moderate', '=', 1); break; } // check if result is not empty if ($query === null || $query->count() < 1) { throw new NotFoundException(__('No comments found for this condition')); } // initialize moderation model $model = new FormCommentModerate($query, $type); // check if form is submited if ($model->send()) { $model->make(); App::$Session->getFlashBag()->add('success', __('Comments or answers are successful published')); $this->response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index')); } return $this->view->render('publish', ['model' => $model]); }
/** * Get commentaries count for pathway. Pathway should be array [itemId => pathway] * @throws NativeException * @return string */ public function actionCount() { // set headers $this->setJsonHeader(); // get configs $configs = AppRecord::getConfigs('widget', 'Comments'); // get path array from request $path = $this->request->query->get('path'); if (!Obj::isArray($path) || count($path) < 1) { throw new NativeException('Wrong query params'); } $count = []; // for each item in path array calculate comments count foreach ($path as $id => $uri) { $query = CommentPost::where('pathway', '=', $uri)->where('moderate', '=', 0); // check if comments is depend of language locale if ((int) $configs['onlyLocale'] === 1) { $query = $query->where('lang', '=', $this->request->getLanguage()); } // set itemId => count $count[(int) $id] = $query->count(); } // render json response return json_encode(['status' => 1, 'count' => $count]); }
/** * Calculate comments on moderation */ private function calcCommentsModerate() { $this->comments = CommentPost::where('moderate', '=', 1)->count(); $this->comments += CommentAnswer::where('moderate', '=', 1)->count(); }
/** * Get comment post object * @return CommentPost|null */ public function getCommentPost() { return CommentPost::where('id', '=', $this->comment_id)->first(); }