Exemple #1
0
 /**
  * Update attachment record
  *
  * @return  void
  */
 public function saveItem($manifest, $blockId, $pub, $actor = 0, $elementId = 0, $aid = 0)
 {
     $aid = $aid ? $aid : Request::getInt('aid', 0);
     // Load classes
     $row = new \Components\Publications\Tables\Author($this->_parent->_db);
     $objO = new \Components\Projects\Tables\Owner($this->_parent->_db);
     // We need attachment record
     if (!$aid || !$row->load($aid) || $row->publication_version_id != $pub->version_id) {
         $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_CONTENT_ERROR_LOAD_AUTHOR'));
         return false;
     }
     // Instantiate a new registration object
     include_once PATH_CORE . DS . 'components' . DS . 'com_members' . DS . 'models' . DS . 'registration.php';
     $xregistration = new \Components\Members\Models\Registration();
     // Get current owners
     $owners = $objO->getIds($pub->_project->get('id'), 'all', 1);
     $email = Request::getVar('email', '', 'post');
     $firstName = Request::getVar('firstName', '', 'post');
     $lastName = Request::getVar('lastName', '', 'post');
     $org = Request::getVar('organization', '', 'post');
     $credit = Request::getVar('credit', '', 'post');
     $sendInvite = 0;
     $code = \Components\Projects\Helpers\Html::generateCode();
     $uid = Request::getInt('uid', 0, 'post');
     $regex = '/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/';
     $email = preg_match($regex, $email) ? $email : '';
     if (!$firstName || !$lastName) {
         $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_MISSING_REQUIRED'));
         return false;
     }
     $row->organization = $org;
     $row->firstName = $firstName;
     $row->lastName = $lastName;
     $row->name = $row->firstName . ' ' . $row->lastName;
     $row->credit = $credit;
     $row->modified_by = $actor;
     $row->modified = Date::toSql();
     // Check that profile exists
     if ($uid) {
         $profile = User::getInstance($uid);
         $uid = $profile->get('id') ? $uid : 0;
     }
     // Tying author to a user account?
     if ($uid && !$row->user_id) {
         // Do we have an owner with this user id?
         $owner = $objO->getOwnerId($pub->_project->get('id'), $uid);
         if ($owner) {
             // Update owner assoc
             $row->project_owner_id = $owner;
         } else {
             // Update associated project owner account
             if ($objO->load($row->project_owner_id) && !$objO->userid) {
                 $objO->userid = $uid;
                 $objO->status = 1;
                 $objO->store();
             }
         }
     }
     $row->user_id = $uid;
     if ($row->store()) {
         $this->set('_message', Lang::txt('Author record saved'));
         // Reflect the update in curation record
         $this->_parent->set('_update', 1);
     } else {
         $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_AUTHORS_ERROR_SAVING_AUTHOR_INFO'));
         return false;
     }
     // Update project owner (invited)
     if ($email && !$row->user_id && $objO->load($row->project_owner_id)) {
         $invitee = $objO->checkInvited($pub->_project->get('id'), $email);
         // Do we have a registered user with this email?
         $user = $xregistration->getEmailId($email);
         if ($invitee && $invitee != $row->project_owner_id) {
             // Stop, must have owner record
         } elseif (in_array($user, $owners)) {
             // Stop, already in team
         } elseif ($email != $objO->invited_email) {
             $objO->invited_email = $email;
             $objO->invited_name = $row->name;
             $objO->userid = $row->user_id;
             $objO->invited_code = $code;
             $objO->store();
             $sendInvite = 1;
         }
     }
     // (Re)send email invitation
     if ($sendInvite && $email) {
         // Get project model
         $project = new \Components\Projects\Models\Project($pub->_project->get('id'));
         // Plugin params
         $plugin_params = array(0, $email, $code, 2, $project, 'com_projects');
         // Send invite
         $output = Event::trigger('projects.sendInviteEmail', $plugin_params);
         $result = json_decode($output[0]);
     }
     return true;
 }
 /**
  * Change publication status
  *
  * @return     string
  */
 public function publishDraft()
 {
     // Incoming
     $pid = $this->_pid ? $this->_pid : Request::getInt('pid', 0);
     $confirm = Request::getInt('confirm', 0);
     $version = Request::getVar('version', 'dev');
     $agree = Request::getInt('agree', 0);
     $pubdate = Request::getVar('publish_date', '', 'post');
     $submitter = Request::getInt('submitter', $this->_uid, 'post');
     $notify = 1;
     $block = Request::getVar('section', '');
     $blockId = Request::getInt('step', 0);
     $element = Request::getInt('element', 0);
     // Check permission
     if (!$this->model->access('content')) {
         throw new Exception(Lang::txt('ALERTNOTAUTH'), 403);
         return;
     }
     // Load review step
     if (!$confirm && $this->_task != 'revert') {
         $this->_task = 'review';
         return $this->editDraft();
     }
     // Load publication model
     $pub = new \Components\Publications\Models\Publication($pid, $version);
     // Error loading publication record
     if (!$pub->exists()) {
         \Notify::message(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_NOT_FOUND'), 'error', 'projects');
         App::redirect(Route::url($pub->link('editbase')));
         return;
     }
     // Agreement to terms is required
     if ($confirm && !$agree) {
         \Notify::message(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_REVIEW_AGREE_TERMS_REQUIRED'), 'error', 'projects');
         App::redirect(Route::url($pub->link('editversion') . '&action=' . $this->_task));
         return;
     }
     // Check against quota
     if ($this->_overQuota()) {
         \Notify::message(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_NO_DISK_SPACE'), 'error', 'projects');
         App::redirect(Route::url($pub->link('editversion') . '&action=' . $this->_task));
         return;
     }
     // Set curation
     $pub->setCuration();
     // Require DOI?
     $requireDoi = isset($pub->_curationModel->_manifest->params->require_doi) ? $pub->_curationModel->_manifest->params->require_doi : 0;
     // Make sure the publication belongs to the project
     if (!$pub->belongsToProject($this->model->get('id'))) {
         Notify::message(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_PROJECT_ASSOC'), 'error', 'projects');
         App::redirect(Route::url($this->model->link('publications')));
         return;
     }
     // Check that version label was not published before
     $used_labels = $pub->version->getUsedLabels($pid, $version);
     if (!$pub->version->version_label || in_array($pub->version->version_label, $used_labels)) {
         $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_VERSION_LABEL_USED'));
     }
     // Is draft complete?
     if (!$pub->curation('complete') && $this->_task != 'revert') {
         $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_NOT_ALLOWED'));
     }
     // Is revert allowed?
     $revertAllowed = $this->_pubconfig->get('graceperiod', 0);
     if ($revertAllowed && $pub->version->state == 1 && $pub->version->accepted && $pub->version->accepted != '0000-00-00 00:00:00') {
         $monthFrom = Date::of($pub->version->accepted . '+1 month')->toSql();
         if (strtotime($monthFrom) < strtotime(Date::of())) {
             $revertAllowed = 0;
         }
     }
     // Embargo?
     if ($pubdate) {
         $pubdate = $this->_parseDate($pubdate);
         $tenYearsFromNow = Date::of(strtotime("+10 years"))->toSql();
         // Stop if more than 10 years from now
         if ($pubdate > $tenYearsFromNow) {
             $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_EMBARGO'));
         }
     }
     // Contact info is required for repositories
     if ($pub->config()->get('repository')) {
         $contact = Request::getVar('contact', array(), 'post');
         if (!$contact || empty($contact)) {
             $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_CONTACT_INFO_MISSING'));
         }
         $db = \App::get('db');
         foreach ($contact as $key) {
             $author = new \Components\Publications\Tables\Author($db);
             if (!$author->load($key)) {
                 $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_CONTACT_NOT_FOUND'));
                 continue;
             }
             $author->repository_contact = 1;
             if (!$author->store()) {
                 $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_CONTACT_NOT_SAVED'));
             }
         }
     }
     // Main version?
     $main = $this->_task == 'republish' ? $pub->version->main : 1;
     $main_vid = $pub->version->getMainVersionId($pid);
     // current default version
     // Save version before changes
     $originalStatus = $pub->version->state;
     // Checks
     if ($this->_task == 'republish' && $pub->version->state != 0) {
         // Can only re-publish unpublished version
         $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_CANNOT_REPUBLISH'));
     } elseif ($this->_task == 'revert' && $pub->version->state != 5 && !$revertAllowed) {
         // Can only revert a pending resource
         $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_CANNOT_REVERT'));
     }
     // On error
     if ($this->getError()) {
         \Notify::message($this->getError(), 'error', 'projects');
         App::redirect(Route::url($pub->link('editversion') . '&action=' . $this->_task));
         return;
     }
     // Determine state
     $state = 5;
     // Default - pending approval
     if ($this->_task == 'share' || $this->_task == 'revert') {
         $state = 4;
         // No approval needed
     } elseif ($this->_task == 'republish') {
         $state = 1;
         // No approval needed
     } else {
         $pub->version->set('submitted', Date::toSql());
         // Save submitter
         $pa = new \Components\Publications\Tables\Author($this->_database);
         $pa->saveSubmitter($pub->version->id, $submitter, $this->model->get('id'));
         if ($this->_pubconfig->get('autoapprove') == 1) {
             $state = 1;
         } else {
             $apu = $this->_pubconfig->get('autoapproved_users');
             $apu = explode(',', $apu);
             $apu = array_map('trim', $apu);
             if (in_array(User::get('username'), $apu)) {
                 // Set status to published
                 $state = 1;
             } else {
                 // Set status to pending review (submitted)
                 $state = 5;
             }
         }
     }
     // Save state
     $pub->version->set('state', $state);
     $pub->version->set('main', $main);
     if ($this->_task != 'revert') {
         $publishedUp = $this->_task == 'republish' ? $pub->version->published_up : Date::toSql();
         $publishedUp = $pubdate ? $pubdate : $publishedUp;
         $pub->version->set('rating', '0.0');
         $pub->version->set('published_up', $publishedUp);
         $pub->version->set('published_down', '');
     }
     $pub->version->set('modified', Date::toSql());
     $pub->version->set('modified_by', $this->_uid);
     // Issue DOI
     if ($requireDoi > 0 && $this->_task == 'publish' && !$pub->version->doi) {
         // Get DOI service
         $doiService = new \Components\Publications\Models\Doi($pub);
         $extended = $state == 5 ? false : true;
         $doi = $doiService->register($extended, $state == 5 ? 'reserved' : 'public');
         // Store DOI
         if ($doi) {
             $pub->version->set('doi', $doi);
         }
         // Can't proceed without a valid DOI
         if (!$doi || $doiService->getError()) {
             $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_DOI') . ' ' . $doiService->getError());
         }
     }
     // Proceed if no error
     if (!$this->getError()) {
         if ($state == 1) {
             // Get and save manifest and its version
             $versionNumber = $pub->_curationModel->checkCurationVersion();
             $pub->version->set('curation', json_encode($pub->_curationModel->_manifest));
             $pub->version->set('curation_version_id', $versionNumber);
         }
         // Save data
         if (!$pub->version->store()) {
             throw new Exception(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_FAILED'), 403);
             return;
         }
         // Remove main flag from previous default version
         if ($main && $main_vid && $main_vid != $pub->version->get('id')) {
             $pub->version->removeMainFlag($main_vid);
         }
     }
     // OnAfterPublish
     $this->onAfterChangeState($pub, $originalStatus);
     // Redirect
     App::redirect(Route::url($pub->link('editversion')));
     return;
 }