/** * 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')); }
/** * Reorder the list of authors * * @return void */ public function reorderTask() { // Incoming $id = Request::getInt('id', 0); $pid = Request::getInt('pid', 0); $move = 'order' . Request::getVar('move', 'down'); // Ensure we have an ID to work with if (!$id) { $this->setError(Lang::txt('COM_CONTRIBUTE_NO_CHILD_ID')); $this->displayTask($pid); return; } // Ensure we have a parent ID to work with if (!$pid) { $this->setError(Lang::txt('COM_CONTRIBUTE_NO_ID')); $this->displayTask($pid); return; } // Get the element moving down - item 1 $author1 = new Contributor($this->database); $author1->loadAssociation($id, $pid, 'resources'); // Get the element directly after it in ordering - item 2 $author2 = clone $author1; $author2->getNeighbor($move); switch ($move) { case 'orderup': // Switch places: give item 1 the position of item 2, vice versa $orderup = $author2->ordering; $orderdn = $author1->ordering; $author1->ordering = $orderup; $author2->ordering = $orderdn; break; case 'orderdown': // Switch places: give item 1 the position of item 2, vice versa $orderup = $author1->ordering; $orderdn = $author2->ordering; $author1->ordering = $orderdn; $author2->ordering = $orderup; break; } // Save changes $author1->updateAssociation(); $author2->updateAssociation(); // Push through to the attachments view $this->displayTask($pid); }
/** * 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)); }