/** * 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; }
/** * Check comment add conditions. On bad conditions will be throw'd exception. * @throws JsonException * @return boolean */ 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')); } // check if pathway is empty if (Str::likeEmpty($this->pathway)) { throw new JsonException(__('Wrong target pathway')); } // 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']])); } // 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 delay between 2 comments from 1 user or 1 ip $query = CommentPost::where('user_id', '=', $this->_userId)->orWhere('ip', '=', $this->ip)->orderBy('created_at', 'DESC')->first(); // check if latest post time for this user is founded if ($query !== null) { $postTime = Date::convertToTimestamp($query->created_at); $delay = $postTime + $this->_configs['delay'] - time(); if ($delay > 0) { throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); } } return true; }
/** * 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; }
/** * 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(); }