/**
  * Callback for submit-button.
  *
  */
 public function callbackSubmit()
 {
     $users = new \CR\Users\User();
     $users->setDI($this->di);
     $now = date('Y-m-d H:i:s');
     $user = $users->find($this->id);
     $user->deleted = $now;
     $user->active = null;
     $user->save();
     if ($this->di->UserloginController->checkLoginSimple()) {
         $this->di->session->set('acronym', null);
         $this->di->session->set('id', null);
         $this->di->session->set('email', null);
         $this->di->session->set('isAdmin', null);
         $this->di->session->set('colortheme', null);
     }
     return true;
 }
 /**
  * Get user data
  *
  * @param array $data questions fetched from db
  *
  * @return array $data questions with user data, answers and comments
  */
 public function getRelatedData($data)
 {
     // If $data array not empty, convert question content from markdown to html, and get user data, Gravatars and tags
     if (is_array($data)) {
         foreach ($data as $id => &$answer) {
             $answer->filteredcontent = $this->textFilter->doFilter($answer->getProperties()['content'], 'shortcode, markdown');
             $users = new \CR\Users\User();
             $users->setDI($this->di);
             $answer->user = $users->find($answer->getProperties()['answerUserId']);
             $answer->user->stats = $this->UsersController->getUserStats($answer->getProperties()['answerUserId']);
             $answer->user->gravatar = 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($answer->user->getProperties()['email']))) . '.jpg?d=identicon';
         }
     }
     return $data;
 }
 /**
  * Get user data, tags, answers and comments
  *
  * @param array $data questions fetched from db
  *
  * @return array $data questions with user data, answers and comments
  */
 public function getRelatedData($data)
 {
     // If $data array not empty, convert question content from markdown to html, and get user data, Gravatars and tags
     if (is_array($data)) {
         foreach ($data as $id => &$question) {
             $question->filteredcontent = $this->textFilter->doFilter($question->getProperties()['content'], 'shortcode, markdown');
             // Get user info
             $users = new \CR\Users\User();
             $users->setDI($this->di);
             $question->user = $users->find($question->getProperties()['questionUserId']);
             $question->user->gravatar = 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($question->user->getProperties()['email']))) . '.jpg?d=identicon';
             $question->user->stats = $this->UsersController->getUserStats($question->getProperties()['questionUserId']);
             // Get associated tags
             $tagIDlist = $this->getSelectedTagIDs($question->getProperties()['id']);
             $question->tags = array();
             foreach ($tagIDlist as $value) {
                 $tag = new \CR\Tag\Tag();
                 $tag->setDI($this->di);
                 $res = $tag->find($value->idTag);
                 if (!$res->deleted) {
                     $question->tags[] = $res;
                 }
             }
             // Sort tags in alphabetical order by name
             usort($question->tags, function ($a, $b) {
                 return strcmp($a->name, $b->name);
             });
             // Get no of answers to question
             $this->db->select("COUNT(*) AS noOfAnswers")->from('answer')->where("questionId = " . $question->getProperties()['id'])->execute();
             $res = $this->db->fetchAll();
             $question->noOfAnswers = $res[0]->noOfAnswers;
         }
     }
     return $data;
 }
 /**
  * View all comments for a page.
  *
  * @param $id integer with question/answer id, $type string with type (question or answer), $pageId int for redirect to question page
  *
  * @return void
  */
 public function viewCommentsAction($id, $type, $pageId)
 {
     if (null == $this->session->get('sorting')) {
         $this->session->set('sorting', 'ASC');
         $change_sorting = 'DESC';
     }
     $set_sorting = $this->request->getGet('sorting');
     switch ($set_sorting) {
         case 'DESC':
             $change_sorting = 'ASC';
             $this->session->set('sorting', 'DESC');
             break;
         case 'ASC':
             $change_sorting = 'DESC';
             $this->session->set('sorting', 'ASC');
             break;
         default:
             $change_sorting = $this->session->get('sorting') === 'ASC' ? 'DESC' : 'ASC';
             break;
     }
     $sorting = 'created ' . $this->session->get('sorting');
     $all = $this->comments->query("c.*")->from('comment AS c')->where("c.deleted IS NULL")->join("comment2" . $type . " AS c2x", "c.id = c2x.idComment")->andWhere("c2x.id" . ucfirst($type) . " = ?")->groupBy("c.id")->orderBy("c." . $sorting)->execute([$id]);
     $this->views->add('comment/comment-container-top', [], 'main-extended');
     $noForm = false;
     // If $all array not empty, get info and list comments
     if (is_array($all)) {
         // convert comment content from markdown to html, and get user object and Gravatar
         foreach ($all as $xid => &$comment) {
             $comment->filteredcontent = $this->textFilter->doFilter($comment->getProperties()['content'], 'shortcode, markdown');
             $users = new \CR\Users\User();
             $users->setDI($this->di);
             $comment->user = $users->find($comment->getProperties()['commentUserId']);
             $comment->user->gravatar = 'http://www.gravatar.com/avatar/' . md5(strtolower(trim($comment->user->getProperties()['email']))) . '.jpg?s=50&d=identicon';
         }
         if (count($all) > 0) {
             $this->views->add('comment/header', ['comments' => $all, 'sorting' => $change_sorting], 'main-extended');
             foreach ($all as $comment_post) {
                 // Get form view if add/edit is clicked
                 $noForm = false;
                 if ($this->request->getGet('editcomment') && $comment_post->getProperties()['id'] == $this->request->getGet('commentid')) {
                     if (!$this->di->UserloginController->checkLoginCorrectUser($comment_post->getProperties()['commentUserId'])) {
                         // Not logged in
                         $this->di->UserloginController->redirectToLogin('Endast ' . $comment_post->user->getProperties()['acronym'] . ' kan redigera kommentaren');
                     }
                     $noForm = true;
                     $this->edit(array('comment' => $comment_post, 'type' => $type, 'pageId' => $pageId));
                 } else {
                     $vote = $this->vote->checkVote($comment_post, 'comment');
                     $this->views->add('comment/view', ['comment' => $comment_post, 'vote' => $vote, 'qid' => $pageId], 'main-extended');
                 }
             }
         }
     }
     // Insert form for new comment if button is clicked and user is logged in
     if ($this->request->getGet($type . 'comment') && $id == $this->request->getGet('postid')) {
         if ($this->di->UserloginController->checkLoginSimple()) {
             $noForm = true;
             $this->add(array('postId' => $id, 'type' => $type, 'pageId' => $pageId));
         } else {
             // Not logged in
             $this->di->UserloginController->redirectToLogin('Logga in för att kommentera');
         }
     } else {
         $this->views->add('comment/bottom', ['noForm' => $noForm, 'type' => $type, 'postId' => $id], 'main-extended');
     }
     $this->views->add('comment/comment-container-bottom', [], 'main-extended');
 }