/** * Saves a resource * Redirects to main listing * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Initiate extended database class $row = new Resource($this->database); if (!$row->bind($_POST)) { throw new Exception($row->getError(), 400); } $isNew = 0; if ($row->id < 1) { $isNew = 1; } if ($isNew) { // New entry $row->created = $row->created ? $row->created : Date::toSql(); $row->created_by = $row->created_by ? $row->created_by : User::get('id'); $row->access = 0; } else { $old = new Resource($this->database); $old->load($row->id); $created_by_id = Request::getInt('created_by_id', 0); // Updating entry $row->modified = Date::toSql(); $row->modified_by = User::get('id'); if ($created_by_id) { $row->created_by = $row->created_by ? $row->created_by : $created_by_id; } else { $row->created_by = $row->created_by ? $row->created_by : User::get('id'); } } // publish up $row->publish_up = Date::of($row->publish_up, Config::get('offset'))->toSql(); // publish down if (!$row->publish_down || trim($row->publish_down) == '0000-00-00 00:00:00' || trim($row->publish_down) == 'Never') { $row->publish_down = '0000-00-00 00:00:00'; } else { $row->publish_down = Date::of($row->publish_down, Config::get('offset'))->toSql(); } // Get parameters $params = Request::getVar('params', array(), 'post'); if (is_array($params)) { $txt = new \Hubzero\Config\Registry(''); foreach ($params as $k => $v) { $txt->set($k, $v); } $row->params = $txt->toString(); } // Get attributes $attribs = Request::getVar('attrib', array(), 'post'); if (is_array($attribs)) { $txta = new \Hubzero\Config\Registry(''); foreach ($attribs as $k => $v) { if ($k == 'timeof') { if (strtotime(trim($v)) === false) { $v = NULL; } $v = trim($v) ? Date::of($v, Config::get('offset'))->toSql() : NULL; } $txta->set($k, $v); } $row->attribs = $txta->toString(); } // Get custom areas, add wrappers, and compile into fulltxt if (isset($_POST['nbtag'])) { $type = new 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 = $_POST['nbtag']; $found = array(); foreach ($nbtag as $tagname => $tagcontent) { $f = ''; $row->fulltxt .= "\n" . '<nb:' . $tagname . '>'; if (is_array($tagcontent)) { $c = count($tagcontent); $num = 0; foreach ($tagcontent as $key => $val) { if (trim($val)) { $num++; } $row->fulltxt .= '<' . $key . '>' . trim($val) . '</' . $key . '>'; } if ($c == $num) { $f = 'found'; } } else { $f = trim($tagcontent); if ($f) { $row->fulltxt .= trim($tagcontent); } } $row->fulltxt .= '</nb:' . $tagname . '>' . "\n"; if (!$tagcontent && isset($fields[$tagname]) && $fields[$tagname]->required) { throw new Exception(Lang::txt('RESOURCES_REQUIRED_FIELD_CHECK', $fields[$tagname]->label), 500); } $found[] = $tagname; } foreach ($fields as $field) { if (!in_array($field->name, $found) && $field->required) { $found[] = $field->name; $this->setError(Lang::txt('COM_CONTRIBUTE_REQUIRED_FIELD_CHECK', $field->label)); } } } // Code cleaner for xhtml transitional compliance if ($row->type != 7) { $row->introtext = str_replace('<br>', '<br />', $row->introtext); $row->fulltxt = str_replace('<br>', '<br />', $row->fulltxt); } // Check content if (!$row->check()) { throw new Exception($row->getError(), 500); } // Store content if (!$row->store()) { throw new Exception($row->getError(), 500); } // Checkin resource $row->checkin(); // Rename the temporary upload directory if it exist $tmpid = Request::getInt('tmpid', 0, 'post'); if ($tmpid != Html::niceidformat($row->id)) { // Build the full paths $path = Html::dateToPath($row->created); $dir_id = Html::niceidformat($row->id); $tmppath = Utilities::buildUploadPath($path . DS . $tmpid); $newpath = Utilities::buildUploadPath($path . DS . $dir_id); // Attempt to rename the temp directory if (\Filesystem::exists($tmppath)) { $result = \Filesystem::move($tmppath, $newpath); if ($result !== true) { $this->setError($result); } } $row->path = str_replace($tmpid, Html::niceidformat($row->id), $row->path); $row->store(); } // Incoming tags $tags = Request::getVar('tags', '', 'post'); // Save the tags $rt = new Tags($row->id); $rt->setTags($tags, User::get('id'), 1, 1); // Incoming authors if ($row->type != 7) { $authorsOldstr = Request::getVar('old_authors', '', 'post'); $authorsNewstr = Request::getVar('new_authors', '', 'post'); if (!$authorsNewstr) { $authorsNewstr = $authorsOldstr; } include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'contributor.php'; $authorsNew = explode(',', $authorsNewstr); $authorsOld = explode(',', $authorsOldstr); // We have either a new ordering or new authors or both if ($authorsNewstr) { for ($i = 0, $n = count($authorsNew); $i < $n; $i++) { $rc = new Contributor($this->database); $rc->subtable = 'resources'; $rc->subid = $row->id; if (is_numeric($authorsNew[$i])) { $rc->authorid = $authorsNew[$i]; } else { $rc->authorid = $rc->getUserId($authorsNew[$i]); } $rc->ordering = $i; $rc->role = trim(Request::getVar($authorsNew[$i] . '_role', '')); $rc->name = trim(Request::getVar($authorsNew[$i] . '_name', '')); $rc->organization = trim(Request::getVar($authorsNew[$i] . '_organization', '')); $authorsNew[$i] = $rc->authorid; if (in_array($authorsNew[$i], $authorsOld)) { //echo 'update: ' . $rc->authorid . ', ' . $rc->role . ', ' . $rc->name . ', ' . $rc->organization . '<br />'; // Updating record $rc->updateAssociation(); } else { //echo 'create: ' . $rc->authorid . ', ' . $rc->role . ', ' . $rc->name . ', ' . $rc->organization . '<br />'; // New record $rc->createAssociation(); } } } // Run through previous author list and check to see if any IDs had been dropped if ($authorsOldstr) { $rc = new Contributor($this->database); for ($i = 0, $n = count($authorsOld); $i < $n; $i++) { if (!in_array($authorsOld[$i], $authorsNew)) { $rc->deleteAssociation($authorsOld[$i], $row->id, 'resources'); } } } } // If this is a child, add parent/child association $pid = Request::getInt('pid', 0, 'post'); if ($isNew && $pid) { $this->_attachChild($row->id, $pid); } // Is this a standalone resource and we need to email approved submissions? if ($row->standalone == 1 && $this->config->get('email_when_approved')) { // If the state went from pending to published if ($row->published == 1 && $old->published == 3) { $this->_emailContributors($row, $this->database); } } // Redirect App::redirect($this->buildRedirectURL($pid), Lang::txt('COM_RESOURCES_ITEM_SAVED')); }
/** * Save one or more authors * * @param integer $show Display author list when done? * @param integer $id Resource ID * @param array $authorsNew Authors to add * @return void */ public function saveTask($show = 1, $id = 0, $authorsNew = array()) { // Incoming resource ID if (!$id) { $id = Request::getInt('pid', 0); } if (!$id) { $this->setError(Lang::txt('CONTRIBUTE_NO_ID')); if ($show) { $this->displayTask($id); } return; } // Incoming authors $authid = Request::getInt('authid', 0, 'post'); $authorsNewstr = trim(Request::getVar('new_authors', '', 'post')); $role = Request::getVar('role', '', 'post'); // Turn the string into an array of usernames $authorsNew = empty($authorsNew) ? explode(',', $authorsNewstr) : $authorsNew; // Instantiate a resource/contributor association object $rc = new Contributor($this->database); $rc->subtable = 'resources'; $rc->subid = $id; // Get the last child in the ordering $order = $rc->getLastOrder($id, 'resources'); $order = $order + 1; // new items are always last if (!$authid && isset($_POST['author'])) { $this->database->setQuery('SELECT id FROM `#__users` WHERE username = '******'author'])); $authid = $this->database->loadResult(); } // Was there an ID? (this will come from the author <select>) if ($authid) { // Check if they're already linked to this resource $rc->loadAssociation($authid, $id, 'resources'); if ($rc->authorid) { $this->setError(Lang::txt('COM_CONTRIBUTE_USER_IS_ALREADY_AUTHOR', $rc->name)); } else { // Perform a check to see if they have a contributors page. If not, we'll need to make one $xprofile = new Profile(); $xprofile->load($authid); if ($xprofile) { $this->_authorCheck($authid); // New record $rc->authorid = $authid; $rc->ordering = $order; $rc->name = addslashes($xprofile->get('name')); $rc->role = addslashes($role); $rc->organization = addslashes($xprofile->get('organization')); $rc->createAssociation(); $order++; } } } $xprofile = null; // Do we have new authors? if (!empty($authorsNew)) { jimport('joomla.user.helper'); // loop through each one for ($i = 0, $n = count($authorsNew); $i < $n; $i++) { $cid = trim($authorsNew[$i]); if (is_numeric($cid)) { $uid = intval($cid); } else { // Find the user's account info $uid = \JUserHelper::getUserId(strtolower($cid)); if (!$uid) { $cid = addslashes(trim($cid)); // No account // This should mean we have an author that is not a site member $rcc = new Contributor($this->database); // Check to see if they're already an author $rcc->loadAssociation($cid, $id, 'resources'); if ($rcc->authorid) { $this->setError(Lang::txt('COM_CONTRIBUTE_USER_IS_ALREADY_AUTHOR', $cid)); continue; } // No name. Can't save record, so pass over it. if (!trim($cid)) { continue; } $rcc->subtable = 'resources'; $rcc->subid = $id; $rcc->authorid = $rcc->getUserId($cid); $rcc->ordering = $order; $rcc->name = $cid; $rcc->role = addslashes($role); $rcc->createAssociation(); //$this->setError(Lang::txt('COM_CONTRIBUTE_UNABLE_TO_FIND_USER_ACCOUNT', $cid)); $order++; continue; } } // We should only get to this part if the author is also a site member $user = User::getInstance($uid); if (!is_object($user)) { $this->setError(Lang::txt('COM_CONTRIBUTE_UNABLE_TO_FIND_USER_ACCOUNT', $cid)); continue; } $uid = $user->get('id'); if (!$uid) { $this->setError(Lang::txt('COM_CONTRIBUTE_UNABLE_TO_FIND_USER_ACCOUNT', $cid)); continue; } // Check if they're already linked to this resource $rcc = new Contributor($this->database); $rcc->loadAssociation($uid, $id, 'resources'); if ($rcc->authorid) { $this->setError(Lang::txt('COM_CONTRIBUTE_USER_IS_ALREADY_AUTHOR', $rcc->name)); continue; } $this->_authorCheck($uid); $xprofile = Profile::getInstance(User::get('id')); $rcc->subtable = 'resources'; $rcc->subid = $id; $rcc->authorid = $uid; $rcc->ordering = $order; $rcc->name = $xprofile->get('name'); $rcc->role = $role; $rcc->organization = $xprofile->get('organization'); if (!$rcc->createAssociation()) { $this->setError($rcc->getError()); } $order++; } } if ($show) { // Push through to the authors view $this->displayTask($id); } }
/** * Save an entry * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); // Incoming $fields = Request::getVar('fields', array(), 'post'); $authorid = Request::getVar('authorid', 0); $id = Request::getVar('id', 0); if (!$authorid) { App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false)); return; } $rows = array(); if (is_array($fields)) { foreach ($fields as $fieldset) { $rc = new Contributor($this->database); $rc->subtable = 'resources'; $rc->subid = trim($fieldset['subid']); $rc->authorid = $authorid; $rc->name = trim($fieldset['name']); $rc->organization = trim($fieldset['organization']); $rc->role = $fieldset['role']; $rc->ordering = $fieldset['ordering']; if ($authorid != $id) { if (!$rc->createAssociation()) { $this->setError($rc->getError()); } if (!$rc->deleteAssociation($id, $rc->subid, $rc->subtable)) { $this->setError($rc->getError()); } } else { if (!$rc->updateAssociation()) { $this->setError($rc->getError()); } } $rows[] = $rc; } } // Instantiate a resource/contributor association object $rc = new Contributor($this->database); if ($this->_task == 'apply') { return $this->editTask($rows); } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false)); }