/** * 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); } }
/** * Gets the full name of a user from their ID # * * @return string */ public function authorTask() { $this->view->id = Request::getVar('u', ''); $this->view->role = Request::getVar('role', ''); $rid = Request::getInt('rid', 0); // Get the member's info $profile = new \Hubzero\User\Profile(); $profile->load($this->view->id); if (!is_object($profile) || !$profile->get('uidNumber')) { $this->database->setQuery("SELECT id FROM `#__users` WHERE `name`=" . $this->database->Quote($this->view->id)); if ($id = $this->database->loadResult()) { $profile->load($id); } } if (is_object($profile) && $profile->get('uidNumber')) { if (!$profile->get('name')) { $this->view->name = $profile->get('givenName') . ' '; $this->view->name .= $profile->get('middleName') ? $profile->get('middleName') . ' ' : ''; $this->view->name .= $profile->get('surname'); } else { $this->view->name = $profile->get('name'); } $this->view->org = $profile->get('organization'); $this->view->id = $profile->get('uidNumber'); } else { $this->view->name = null; include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'contributor.php'; $rcc = new Contributor($this->database); if (is_numeric($this->view->id)) { $this->database->setQuery("SELECT name, organization FROM `#__author_assoc` WHERE authorid=" . $this->database->Quote($this->view->id) . " LIMIT 1"); $author = $this->database->loadObject(); if (is_object($author) && $author->name) { $this->view->name = $author->name; $this->view->org = $author->organization; } } if (!$this->view->name) { $this->view->org = ''; $this->view->name = str_replace('_', ' ', $this->view->id); $this->view->id = $rcc->getUserId($this->view->name); } } $row = new Resource($this->database); $row->load($rid); $rt = new Type($this->database); $this->view->roles = $rt->getRolesForType($row->type); $this->view->display(); }