/** * Short description for 'getToolVersions' * * Long description (if any) ... * * @param string $toolid Parameter description (if any) ... * @param array &$versions Parameter description (if any) ... * @param string $toolname Parameter description (if any) ... * @param integer $exclude_dev Parameter description (if any) ... * @return array Return description (if any) ... */ public function getToolVersions($toolid, &$versions, $toolname = '', $exclude_dev = 0) { $objA = new \Components\Tools\Tables\Author($this->_db); $query = "SELECT v.*, d.* "; $query .= "FROM #__tool_version as v LEFT JOIN #__doi_mapping as d ON d.alias = v.toolname AND d.local_revision=v.revision "; if ($toolid) { $query .= "WHERE v.toolid = " . $this->_db->quote($toolid) . " "; } else { if ($toolname) { $query .= "WHERE v.toolname = " . $this->_db->quote($toolname) . " "; } } if (($toolname or $toolid) && $exclude_dev) { $query .= "AND v.state != '3'"; } $query .= " ORDER BY v.state DESC, v.revision DESC"; $this->_db->setQuery($query); $versions = $this->_db->loadObjectList(); if ($versions) { require_once PATH_CORE . DS . 'components' . DS . 'com_tools' . DS . 'models' . DS . 'tool.php'; foreach ($versions as $version) { // get list of authors if ($version->state != 3) { $version->authors = $objA->getToolAuthors($version->id); } else { $rid = \Components\Tools\Models\Tool::getResourceId($version->toolid); $version->authors = $objA->getToolAuthors('dev', $rid); } } } return $versions; }
/** * Display forms for editing/creating a reosurce * * @return void */ public function displayTask() { // Incoming $alias = Request::getVar('app', ''); $version = Request::getVar('editversion', 'dev'); $step = Request::getInt('step', 1); // Load the tool $obj = new \Components\Tools\Tables\Tool($this->database); $this->_toolid = $obj->getToolId($alias); if (!$this->_toolid) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=pipeline')); return; } // make sure user is authorized to go further if (!$this->_checkAccess($this->_toolid)) { App::abort(403, Lang::txt('COM_TOOLS_ALERTNOTAUTH')); return; } $nextstep = $step + 1; // get tool version (dev or current) information $obj->getToolStatus($this->_toolid, $this->_option, $status, $version); // get resource information $row = new \Components\Resources\Tables\Resource($this->database); $row->loadAlias($alias); $row->alias = $row->alias ? $row->alias : $alias; if (!$status['fulltxt']) { $status['fulltxt'] = $row->fulltxt; } // process first step if ($nextstep == 3 && (isset($_POST['nbtag']) || isset($_POST['fulltxt']))) { if (!isset($_POST['fulltxt']) || !trim($_POST['fulltxt'])) { $this->setError(Lang::txt('COM_TOOLS_REQUIRED_FIELD_CHECK', 'Abstract')); $step = 1; $nextstep--; } $hztv = \Components\Tools\Helpers\Version::getToolRevision($this->_toolid, $version); $objV = new \Components\Tools\Tables\Version($this->database); if (!$objV->bind($_POST)) { $this->setError($objV->getError()); return; } $body = $this->txtClean($_POST['fulltxt']); if (preg_match("/([\\<])([^\\>]{1,})*([\\>])/i", $body)) { // Do nothing $status['fulltxt'] = trim(stripslashes($body)); } else { // Wiki format will be used $status['fulltxt'] = Request::getVar('fulltxt', $status['fulltxt'], 'post'); } // Get custom areas, add wrapper tags, and compile into fulltxt $type = new \Components\Resources\Tables\Type($this->database); $type->load($row->type); include_once PATH_CORE . DS . 'components' . DS . 'com_resources' . DS . 'models' . DS . 'elements.php'; $elements = new \Components\Resources\Models\Elements(array(), $type->customFields); $schema = $elements->getSchema(); $fields = array(); foreach ($schema->fields as $field) { $fields[$field->name] = $field; } $nbtag = Request::getVar('nbtag', array()); $found = array(); foreach ($nbtag as $tagname => $tagcontent) { $f = ''; $status['fulltxt'] .= "\n" . '<nb:' . $tagname . '>'; if (is_array($tagcontent)) { $c = count($tagcontent); $num = 0; foreach ($tagcontent as $key => $val) { if (trim($val)) { $num++; } $status['fulltxt'] .= '<' . $key . '>' . trim($val) . '</' . $key . '>'; } if ($c == $num) { $f = 'found'; } } else { $f = trim($tagcontent); if ($f) { $status['fulltxt'] .= trim($tagcontent); } } $status['fulltxt'] .= '</nb:' . $tagname . '>' . "\n"; if (!$f && isset($fields[$tagname]) && $fields[$tagname]->required) { $this->setError(Lang::txt('COM_TOOLS_REQUIRED_FIELD_CHECK', $fields[$tagname]->label)); } $found[] = $tagname; } foreach ($fields as $field) { if (!in_array($field->name, $found) && $field->required) { $found[] = $field->name; $this->setError(Lang::txt('COM_TOOLS_REQUIRED_FIELD_CHECK', $field->label)); } } $hztv->fulltxt = $objV->fulltxt = $status['fulltxt']; $hztv->description = $objV->description = \Hubzero\Utility\String::truncate(Request::getVar('description', $status['description'], 'post'), 500); $hztv->title = $objV->title = \Hubzero\Utility\String::truncate(preg_replace('/\\s+/', ' ', Request::getVar('title', $status['title'], 'post')), 500); if (!$hztv->update()) { throw new Exception(Lang::txt('COM_TOOLS_ERROR_UPDATING_TOOL'), 500); } else { // get updated tool status $obj->getToolStatus($this->_toolid, $this->_option, $status, $version); } if ($version == 'dev') { // update resource page $this->updatePage($row->id, $status); } } // Group access //$accesses = array('Public', 'Registered', 'Special', 'Protected', 'Private'); //$lists = array(); //$lists['access'] = \Components\Tools\Helpers\Html::selectAccess($accesses, $row->access); //$groups = \Hubzero\User\Helper::getGroups(User::get('id'), 'members'); // get authors $objA = new \Components\Tools\Tables\Author($this->database); $authors = $version == 'current' ? $objA->getToolAuthors($version, $row->id, $status['toolname']) : array(); // Tags $tags = Request::getVar('tags', '', 'post'); $tagfa = Request::getVar('tagfa', '', 'post'); // Get any HUB focus areas // These are used where any resource is required to have one of these tags $tconfig = Component::params('com_tags'); $fa1 = $tconfig->get('focus_area_01'); $fa2 = $tconfig->get('focus_area_02'); $fa3 = $tconfig->get('focus_area_03'); $fa4 = $tconfig->get('focus_area_04'); $fa5 = $tconfig->get('focus_area_05'); $fa6 = $tconfig->get('focus_area_06'); $fa7 = $tconfig->get('focus_area_07'); $fa8 = $tconfig->get('focus_area_08'); $fa9 = $tconfig->get('focus_area_09'); $fa10 = $tconfig->get('focus_area_10'); // Instantiate our tag object $tagcloud = new \Components\Resources\Helpers\Tags($row->id); // Normalize the focus areas $tagfa1 = $tagcloud->normalize($fa1); $tagfa2 = $tagcloud->normalize($fa2); $tagfa3 = $tagcloud->normalize($fa3); $tagfa4 = $tagcloud->normalize($fa4); $tagfa5 = $tagcloud->normalize($fa5); $tagfa6 = $tagcloud->normalize($fa6); $tagfa7 = $tagcloud->normalize($fa7); $tagfa8 = $tagcloud->normalize($fa8); $tagfa9 = $tagcloud->normalize($fa9); $tagfa10 = $tagcloud->normalize($fa10); // process new tags if ($tags or $tagfa) { $newtags = ''; if ($tagfa) { $newtags = $tagfa . ', '; } if ($tags) { $newtags .= $tags; } $tagcloud->setTags($newtags, User::get('id')); } // Get all the tags on this resource $tags_men = $tagcloud->tags(); $mytagarray = array(); $fas = array($tagfa1, $tagfa2, $tagfa3, $tagfa4, $tagfa5, $tagfa6, $tagfa7, $tagfa8, $tagfa9, $tagfa10); $fats = array(); if ($fa1) { $fats[$fa1] = $tagfa1; } if ($fa2) { $fats[$fa2] = $tagfa2; } if ($fa3) { $fats[$fa3] = $tagfa3; } if ($fa4) { $fats[$fa4] = $tagfa4; } if ($fa5) { $fats[$fa5] = $tagfa5; } if ($fa6) { $fats[$fa6] = $tagfa6; } if ($fa7) { $fats[$fa7] = $tagfa7; } if ($fa8) { $fats[$fa8] = $tagfa8; } if ($fa9) { $fats[$fa9] = $tagfa9; } if ($fa10) { $fats[$fa10] = $tagfa10; } // Loop through all the tags and pull out the focus areas - those will be displayed differently foreach ($tags_men as $tag_men) { if (in_array($tag_men->get('tag'), $fas)) { $tagfa = $tag_men->get('tag'); } else { $mytagarray[] = $tag_men->get('raw_tag'); } } $tags = implode(', ', $mytagarray); // Set the document title $this->view->title = Lang::txt(strtoupper($this->_option)) . ': ' . Lang::txt('COM_TOOLS_EDIT_TOOL_PAGE') . ' (' . $status['toolname'] . ')'; Document::setTitle($this->view->title); // Set the document pathway (breadcrumbs) if (Pathway::count() <= 0) { Pathway::append(Lang::txt(strtoupper($this->_name)), 'index.php?option=' . $this->_option); } if (Pathway::count() <= 1) { Pathway::append(Lang::txt('COM_TOOLS_STATUS_FOR', $status['toolname']), 'index.php?option=' . $this->_option . '&controller=pipeline&task=status&app=' . $alias); Pathway::append(Lang::txt('COM_TOOLS_EDIT_TOOL_PAGE'), 'index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&app=' . $alias . '&task=start&step=1'); } $this->view->row = $row; $this->view->step = $step; $this->view->version = $version; $this->view->status = $status; $this->view->tags = $tags; $this->view->tagfa = $tagfa; $this->view->fats = $fats; $this->view->authors = $authors; // Pass error messages to the view foreach ($this->getErrors() as $error) { $this->view->setError($error); } // Output HTML $this->view->display(); }
/** * 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)); }
/** * Save a question * * @return void */ public function saveqTask() { // Check for request forgeries Request::checkToken(); // Login required if (User::isGuest()) { $this->setError(Lang::txt('COM_ANSWERS_PLEASE_LOGIN')); $this->loginTask(); return; } if (!User::authorise('core.edit', $this->_option) && !User::authorise('core.create', $this->_option) && !User::authorise('core.manage', $this->_option)) { throw new Exception(Lang::txt('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403); } // Incoming $fields = Request::getVar('fields', array(), 'post', 'none', 2); $tags = Request::getVar('tags', ''); if (!isset($fields['reward'])) { $fields['reward'] = 0; } // If offering a reward, do some checks if ($fields['reward']) { // Is it an actual number? if (!is_numeric($fields['reward'])) { throw new Exception(Lang::txt('COM_ANSWERS_REWARD_MUST_BE_NUMERIC'), 500); } // Are they offering more than they can afford? if ($fields['reward'] > $fields['funds']) { throw new Exception(Lang::txt('COM_ANSWERS_INSUFFICIENT_FUNDS'), 500); } } // clean input array_walk($fields, function (&$field, $key) { $field = \Hubzero\Utility\Sanitize::clean($field); }); // Initiate class and bind posted items to database fields $row = new Question($fields['id']); if (!$row->bind($fields)) { throw new Exception($row->getError(), 500); } if ($fields['reward'] && $this->config->get('banking')) { $row->set('reward', 1); } // Store new content if (!Request::checkHoneypot()) { $this->setError(Lang::txt('JLIB_APPLICATION_ERROR_INVALID_CONTENT')); $this->newTask($row); return; } // Ensure the user added a tag if (!$tags) { $this->setError(Lang::txt('COM_ANSWERS_QUESTION_MUST_HAVE_TAG')); $this->newTask($row); return; } // We need to temporarily set this so the store() method // has access to the tags string to be able to run it // through spam checkers and validation. $row->set('tags', $tags); // Store new content if (!$row->store(true)) { Request::setVar('tag', $tags); $this->setError($row->getError()); $this->newTask($row); return; } // Hold the reward for this question if we're banking if ($fields['reward'] && $this->config->get('banking')) { $BTL = new Teller($this->database, User::get('id')); $BTL->hold($fields['reward'], Lang::txt('COM_ANSWERS_HOLD_REWARD_FOR_BEST_ANSWER'), 'answers', $row->get('id')); } // Add the tags $row->tag($tags); // Get users who need to be notified on every question $apu = $this->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 = preg_split("/[,;]/", $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); foreach ($tags as $tag) { if ($tag == '') { continue; } if (preg_match('/tool:/', $tag)) { $toolname = preg_replace('/tool:/', '', $tag); if (trim($toolname)) { $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'); $message = array(); // Plain text message $eview = new \Hubzero\Mail\View(array('name' => 'emails', 'layout' => 'question_plaintext')); $eview->option = $this->_option; $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, $this->_option))) { $this->setError(Lang::txt('COM_ANSWERS_MESSAGE_FAILED')); } } // Redirect to the question App::redirect(Route::url('index.php?option=' . $this->_option . '&task=question&id=' . $row->get('id')), Lang::txt('COM_ANSWERS_NOTICE_QUESTION_POSTED_THANKS')); }