Example #1
0
 /**
  * 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;
 }
Example #2
0
 public function getCommentId()
 {
     $id = $this->_record->id;
     if ($this->type === 'answer') {
         $id = $this->_record->getCommentPost()->id;
     }
     return $id;
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * Add comment answer to database and return active record object
  * @return CommentAnswer
  */
 public function buildRecord()
 {
     $record = new CommentAnswer();
     $record->comment_id = $this->replayTo;
     $record->user_id = $this->_userId;
     $record->guest_name = $this->guestName;
     $record->message = $this->message;
     $record->lang = App::$Request->getLanguage();
     $record->ip = $this->ip;
     // check if premoderation is enabled and user is guest
     if ((bool) $this->_configs['guestModerate'] && $this->_userId < 1) {
         $record->moderate = 1;
     }
     $record->save();
     // add notification for comment post owner
     $commentPost = $record->getCommentPost();
     if ($commentPost !== null && (int) $commentPost->user_id !== 0 && (int) $commentPost->user_id !== $this->_userId) {
         $notify = new EntityAddNotification((int) $commentPost->user_id);
         $notify->add($commentPost->pathway, EntityAddNotification::MSG_ADD_COMMENTANSWER, ['snippet' => Text::snippet($this->message, 50), 'post' => Text::snippet($commentPost->message, 50)]);
     }
     return $record;
 }
Example #5
0
 /**
  * List answers
  * @return string
  * @throws \Ffcms\Core\Exception\SyntaxException
  * @throws \Ffcms\Core\Exception\NativeException
  */
 public function actionAnswerlist()
 {
     // set current page and offset
     $page = (int) $this->request->query->get('page');
     $offset = $page * self::ITEM_PER_PAGE;
     // initialize ar answers model
     $query = new CommentAnswer();
     // build pagination list
     $pagination = new SimplePagination(['url' => ['comments/answerlist'], 'page' => $page, 'step' => self::ITEM_PER_PAGE, 'total' => $query->count()]);
     // get result as active records object with offset
     $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
     // render output view
     return $this->view->render('answer_list', ['records' => $records, 'pagination' => $pagination]);
 }
Example #6
0
 /**
  * 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();
 }