示例#1
0
 /**
  * Save a question and redirect to the main listing when done
  *
  * @return     void
  */
 private function _save()
 {
     // Login required
     if (User::isGuest()) {
         return $this->_browse();
     }
     // Check for request forgeries
     Request::checkToken();
     Lang::load('com_answers');
     // Incoming
     $tags = Request::getVar('tags', '');
     $funds = Request::getInt('funds', 0);
     $reward = Request::getInt('reward', 0);
     // If offering a reward, do some checks
     if ($reward) {
         // Is it an actual number?
         if (!is_numeric($reward)) {
             App::abort(500, Lang::txt('COM_ANSWERS_REWARD_MUST_BE_NUMERIC'));
             return;
         }
         // Are they offering more than they can afford?
         if ($reward > $funds) {
             App::abort(500, Lang::txt('COM_ANSWERS_INSUFFICIENT_FUNDS'));
             return;
         }
     }
     // Initiate class and bind posted items to database fields
     $fields = Request::getVar('question', array(), 'post', 'none', 2);
     $row = new \Components\Answers\Models\Question($fields['id']);
     if (!$row->bind($fields)) {
         $this->setError($row->getError());
         return $this->_new($row);
     }
     if ($reward && $this->banking) {
         $row->set('reward', 1);
     }
     // Ensure the user added a tag
     /*
     if (!$tags)
     {
     	$this->setError(Lang::txt('COM_ANSWERS_QUESTION_MUST_HAVE_TAG'));
     	return $this->_new($row);
     }
     */
     // Store new content
     if (!$row->store(true)) {
         $row->set('tags', $tags);
         $this->setError($row->getError());
         return $this->_new($row);
     }
     // Hold the reward for this question if we're banking
     if ($reward && $this->banking) {
         $BTL = new \Hubzero\Bank\Teller($this->database, User::get('id'));
         $BTL->hold($reward, Lang::txt('COM_ANSWERS_HOLD_REWARD_FOR_BEST_ANSWER'), 'answers', $row->get('id'));
     }
     // Add the tags
     $row->tag($tags);
     // Add the tag to link to the resource
     $tag = $this->model->isTool() ? 'tool:' . $this->model->resource->alias : 'resource:' . $this->model->resource->id;
     $row->addTag($tag, User::get('id'), $this->model->isTool() ? 0 : 1);
     // Get users who need to be notified on every question
     $config = Component::params('com_answers');
     $apu = $config->get('notify_users', '');
     $apu = explode(',', $apu);
     $apu = array_map('trim', $apu);
     $receivers = array();
     // Get tool contributors if question is about a tool
     if ($tags) {
         $tags = explode(',', $tags);
         if (count($tags) > 0) {
             require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'tables' . DS . 'author.php';
             require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'tables' . DS . 'version.php';
             $TA = new \Components\Tools\Tables\Author($this->database);
             $objV = new \Components\Tools\Tables\Version($this->database);
             if ($this->model->isTool()) {
                 $toolname = $this->model->resource->alias;
                 $rev = $objV->getCurrentVersionProperty($toolname, 'revision');
                 $authors = $TA->getToolAuthors('', 0, $toolname, $rev);
                 if (count($authors) > 0) {
                     foreach ($authors as $author) {
                         $receivers[] = $author->uidNumber;
                     }
                 }
             }
         }
     }
     if (!empty($apu)) {
         foreach ($apu as $u) {
             $user = User::getInstance($u);
             if ($user) {
                 $receivers[] = $user->get('id');
             }
         }
     }
     $receivers = array_unique($receivers);
     // Send the message
     if (!empty($receivers)) {
         // Send a message about the new question to authorized users (specified admins or related content authors)
         $from = array('email' => Config::get('mailfrom'), 'name' => Config::get('sitename') . ' ' . Lang::txt('COM_ANSWERS_ANSWERS'), 'multipart' => md5(date('U')));
         // Build the message subject
         $subject = Lang::txt('COM_ANSWERS_ANSWERS') . ', ' . Lang::txt('new question about content you author or manage');
         // Build the message
         $eview = new \Hubzero\Mail\View(array('base_path' => PATH_CORE . DS . 'components' . DS . 'com_answers' . DS . 'site', 'name' => 'emails', 'layout' => 'question_plaintext'));
         $eview->option = 'com_answers';
         $eview->sitename = Config::get('sitename');
         $eview->question = $row;
         $eview->id = $row->get('id', 0);
         $eview->boundary = $from['multipart'];
         $message['plaintext'] = $eview->loadTemplate(false);
         $message['plaintext'] = str_replace("\n", "\r\n", $message['plaintext']);
         // HTML message
         $eview->setLayout('question_html');
         $message['multipart'] = $eview->loadTemplate();
         $message['multipart'] = str_replace("\n", "\r\n", $message['multipart']);
         if (!Event::trigger('xmessage.onSendMessage', array('new_question_admin', $subject, $message, $from, $receivers, 'com_answers'))) {
             $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED'));
         }
     }
     // Redirect to the question
     App::redirect(Route::url('index.php?option=' . $this->option . '&id=' . $this->model->resource->id . '&active=' . $this->_name));
 }
示例#2
0
 /**
  * Save a question and redirect to the main listing when done
  *
  * @return     void
  */
 private function _save()
 {
     // Login required
     if (User::isGuest()) {
         return $this->_browse();
     }
     // Check for request forgeries
     Request::checkToken();
     // Incoming
     $tags = Request::getVar('tags', '');
     $funds = Request::getInt('funds', 0);
     $reward = Request::getInt('reward', 0);
     // If offering a reward, do some checks
     if ($reward) {
         // Is it an actual number?
         if (!is_numeric($reward)) {
             App::abort(500, Lang::txt('COM_ANSWERS_REWARD_MUST_BE_NUMERIC'));
             return;
         }
         // Are they offering more than they can afford?
         if ($reward > $funds) {
             App::abort(500, Lang::txt('COM_ANSWERS_INSUFFICIENT_FUNDS'));
             return;
         }
     }
     // Initiate class and bind posted items to database fields
     $fields = Request::getVar('question', array(), 'post', 'none', 2);
     $row = new \Components\Answers\Models\Question($fields['id']);
     if (!$row->bind($fields)) {
         $this->setError($row->getError());
         return $this->_new($row);
     }
     if ($reward && $this->banking) {
         $row->set('reward', 1);
     }
     // Store new content
     if (!$row->store(true)) {
         $row->set('tags', $tags);
         $this->setError($row->getError());
         return $this->_new($row);
     }
     // Hold the reward for this question if we're banking
     if ($reward && $this->banking) {
         $BTL = new \Hubzero\Bank\Teller($this->database, User::get('id'));
         $BTL->hold($reward, Lang::txt('COM_ANSWERS_HOLD_REWARD_FOR_BEST_ANSWER'), 'answers', $row->get('id'));
     }
     // Add the tags
     $row->tag($tags);
     // Add the tag to link to the publication
     $identifier = $this->publication->get('alias') ? $this->publication->get('alias') : $this->publication->get('id');
     $tag = $this->publication->isTool() ? 'tool' . $identifier : 'publication' . $identifier;
     $row->addTag($tag, User::get('id'), $this->publication->isTool() ? 0 : 1);
     // Redirect to the question
     App::redirect(Route::url($this->publication->link() . '&active=questions'));
 }