/** * Get answers count for post comment id * @return int */ public function getAnswerCount() { // check if count is cached if (MainApp::$Memory->get('commentpost.answer.count.' . $this->id) !== null) { return MainApp::$Memory->get('commentpost.answer.count.' . $this->id); } // get count from db $count = CommentAnswer::where('comment_id', '=', $this->id)->where('moderate', '=', 0)->count(); // save in cache MainApp::$Memory->set('commentpost.answer.count.' . $this->id, $count); return $count; }
/** * 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; }
/** * List answers by comment id as json object * @param int $commentId * @return string * @throws ForbiddenException * @throws NotFoundException */ public function actionShowanswers($commentId) { $this->setJsonHeader(); // check input data if (!Obj::isLikeInt($commentId) || (int) $commentId < 1) { throw new ForbiddenException('Input data is incorrect'); } // get configs $configs = AppRecord::getConfigs('widget', 'Comments'); // get data from db by comment id $records = CommentAnswer::where('comment_id', '=', $commentId)->where('moderate', '=', 0); if ((int) $configs['onlyLocale'] === 1) { $records = $records->where('lang', '=', $this->request->getLanguage()); } // check objects count if ($records->count() < 1) { throw new NotFoundException(__('No answers for comment is founded')); } // prepare output $response = []; foreach ($records->get() as $row) { $commentAnswer = new EntityCommentData($row); $response[] = $commentAnswer->make(); } return json_encode(['status' => 1, 'data' => $response]); }
/** * Calculate comments on moderation */ private function calcCommentsModerate() { $this->comments = CommentPost::where('moderate', '=', 1)->count(); $this->comments += CommentAnswer::where('moderate', '=', 1)->count(); }