/** * 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('COM_TOOLS_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 \Components\Resources\Tables\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 // 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('USER_IS_ALREADY_AUTHOR', $authid)); } else { // Perform a check to see if they have a contributors page. If not, we'll need to make one $xprofile = User::getInstance($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++; } } } // Do we have new authors? if (!empty($authorsNew)) { // 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 { $cid = strtolower($cid); // Find the user's account info $uid = User::oneByUsername($cid)->get('id'); if (!$uid) { $this->setError(Lang::txt('COM_CONTRIBUTE_UNABLE_TO_FIND_USER_ACCOUNT', $cid)); continue; } } $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 \Components\Resources\Tables\Contributor($this->database); $rcc->loadAssociation($uid, $id, 'resources'); if ($rcc->authorid) { $this->setError(Lang::txt('USER_IS_ALREADY_AUTHOR', $cid)); continue; } $this->_authorCheck($uid); $xprofile = User::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 a list of authors * * @param array $authors List of authors to add * @param string $version Tool version * @param integer $rid Resource ID * @param integer $revision Revision number * @param string $toolname Tool name * @return boolean False if errors, True if not */ public function saveAuthors($authors, $version = 'dev', $rid = 0, $revision = 0, $toolname = '') { if (!$rid) { return false; } if ($authors) { $authors = Utils::transform($authors, 'uidNumber'); } $dev_authors = $this->getToolAuthors('dev', $rid); $dev_authors = Utils::transform($dev_authors, 'uidNumber'); if ($dev_authors && $version == 'dev') { // update $to_delete = array_diff($current_authors, $authors); if ($to_delete) { foreach ($to_delete as $del) { $query = "DELETE FROM #__author_assoc WHERE authorid=" . $this->_db->quote($del) . " AND subid=" . $this->_db->quote($rid) . " AND subtable='resources'"; $this->_db->setQuery($query); $this->_db->query(); } } } // add new authors if ($version == 'dev') { // development version is updated $to_delete = array_diff($dev_authors, $authors); $rc = new \Components\Resources\Tables\Contributor($this->_db); $rc->subtable = 'resources'; $rc->subid = $rid; if ($to_delete) { foreach ($to_delete as $del) { $query = "DELETE FROM #__author_assoc WHERE authorid=" . $this->_db->quote($del) . " AND subid=" . $this->_db->quote($rid) . " AND subtable='resources'"; $this->_db->setQuery($query); $this->_db->query(); } } // Get the last child in the ordering $order = $rc->getLastOrder($rid, 'resources'); $order = $order + 1; // new items are always last foreach ($authors as $authid) { // Check if they're already linked to this resource $rc->loadAssociation($authid, $rid, 'resources'); if (!$rc->authorid) { $xprofile = User::getInstance($authid); // New record $rc->authorid = $authid; $rc->ordering = $order; $rc->name = addslashes($xprofile->get('name')); $rc->organization = addslashes($xprofile->get('organization')); $rc->createAssociation(); $order++; } } } else { if ($dev_authors) { // new version is being published, transfer data from author_assoc $i = 0; foreach ($dev_authors as $authid) { $name = ''; $organization = ''; // Check the author_assoc table first, as that's what gets updated when you edit the tool info $query = "SELECT name, organization FROM `#__author_assoc` "; $query .= "WHERE subid= " . $this->_db->quote($rid); $query .= " AND authorid=" . $this->_db->quote($authid); $query .= " AND subtable='resources'"; $this->_db->setQuery($query); $author = $this->_db->loadObject(); if ($author) { $name = $author->name; $organization = $author->organization; } if (!$name || !$organization) { // Do we have name/org info in previous version? $query = "SELECT name, organization FROM #__tool_authors "; $query .= "WHERE toolname=" . $this->_db->quote($toolname) . " AND uid=" . $this->_db->quote($authid) . " AND revision < " . $this->_db->quote($revision); $query .= " AND name IS NOT NULL AND organization IS NOT NULL "; $query .= " ORDER BY revision DESC LIMIT 1"; $this->_db->setQuery($query); $info = $this->_db->loadObjectList(); if ($info) { $name = $name ?: $info[0]->name; $organization = $organization ?: $info[0]->organization; } // If we still don't have it, try to grab it from the profile if (!$name || !$organization) { $xprofile = User::getInstance($authid); $name = $name ?: $xprofile->get('name'); $organization = $organization ?: $xprofile->get('organization'); } } $query = "INSERT INTO {$this->_tbl} (toolname, revision, uid, ordering, version_id, name, organization) VALUES ('" . $toolname . "','" . $revision . "','" . $authid . "','" . $i . "', '" . $version . "', '" . addslashes($name) . "', '" . addslashes($organization) . "')"; $this->_db->setQuery($query); if (!$this->_db->query()) { return false; } $i++; } } } return true; }