示例#1
0
 /**
  * Set the state of one or more questions
  *
  * @return  void
  */
 public function stateTask()
 {
     // Check for request forgeries
     Request::checkToken(['get', 'post']);
     // Incoming
     $ids = Request::getVar('id', array());
     $ids = !is_array($ids) ? array($ids) : $ids;
     $publish = $this->getTask() == 'close' ? 1 : 0;
     // Check for an ID
     if (count($ids) < 1) {
         $action = $publish == 1 ? Lang::txt('COM_ANSWERS_SET_STATE_CLOSE') : Lang::txt('COM_ANSWERS_SET_STATE_OPEN');
         App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_ANSWERS_ERROR_SELECT_QUESTION_TO', $action), 'error');
         return;
     }
     foreach ($ids as $id) {
         // Update record(s)
         $aq = new Question(intval($id));
         if (!$aq->exists()) {
             continue;
         }
         $aq->set('state', $publish);
         if ($publish == 1) {
             $aq->adjustCredits();
         }
         if (!$aq->store()) {
             throw new Exception($aq->getError(), 500);
         }
     }
     // Set message
     if ($publish == 1) {
         $message = Lang::txt('COM_ANSWERS_QUESTIONS_CLOSED', count($ids));
     } else {
         if ($publish == 0) {
             $message = Lang::txt('COM_ANSWERS_QUESTIONS_OPENED', count($ids));
         }
     }
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), $message);
 }