Advertisement
Venciity

QuestionsController

May 9th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. class QuestionsController extends BaseController {
  4.     private $db;
  5.  
  6.     public function onInit(){
  7.         $this->title = "Questions";
  8.         $this->db = new QuestionsModel();
  9.     }
  10.  
  11.     public function index($page = 0, $pageSize = 10){
  12.         $this->authorize();
  13.         $from = $page * $pageSize;
  14.         $this->page = $page;
  15.         $this->pageSize = $pageSize;
  16.         $this->questions = $this->db->getFilteredQuestions($from, $pageSize);
  17.         $this->renderView();
  18.     }
  19.  
  20.     public function create(){
  21.         $this->authorize();
  22.         if($this->isPost){
  23.             $text = $_POST['question_text'];
  24.             $content = $_POST['question_content'];
  25.             $categoryText = $_POST['question_category'];
  26.             $tags = $_POST['question_tags'];
  27.             $categoryId = $this->db->getCategoryIdByText($categoryText);
  28.             $userId = $this->db->getCurrentUserId();
  29.             if(strlen($text) <= 5){
  30.                 $this->addFieldValue('question_text', $text);
  31.                 $this->addValidationError('question_text', 'The question text symbols should be greater than 5');
  32.                 return $this->renderView(__FUNCTION__);
  33.             }
  34.             if($this->db->createQuestion($text, $content, $userId, $categoryId, $tags)){
  35.                 $this->addInfoMessage("Question created.");
  36.                 $this->redirect('questions');
  37.             } else{
  38.                 $this->addErrorMessage("Error creating question.");
  39.             }
  40.         }
  41.  
  42.         $this->renderView(__FUNCTION__);
  43.     }
  44.  
  45.     public function viewQuestionInfo($id){
  46.         $this->questionInfo = $this->db->getQuestionInfo($id);
  47.         $this->comments = $this->db->getAllComments($id);
  48.         $this->tags = $this->db->getQuestionTagsByQuestionId($id);
  49.         $this->authorize();
  50.         $this->renderView(__FUNCTION__);
  51.     }
  52.  
  53.     public function delete($id){
  54.         $this->authorize();
  55.         if($this->db->deleteQuestion($id)){
  56.             $this->addInfoMessage("Question deleted.");
  57.         } else {
  58.             $this->addErrorMessage("Error deleting question.");
  59.         }
  60.         $this->redirect('questions');
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement