/** * Save block content * * @return string HTML */ public function save($manifest = NULL, $blockId = 0, $pub = NULL, $actor = 0, $elementId = 0) { // Set block manifest if ($this->_manifest === NULL) { $this->_manifest = $manifest ? $manifest : self::getManifest(); } // Make sure changes are allowed if ($this->_parent->checkFreeze($this->_manifest->params, $pub)) { return false; } // Load publication version $row = new \Components\Publications\Tables\Version($this->_parent->_db); if (!$row->load($pub->version_id)) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_VERSION_NOT_FOUND')); return false; } $originalType = $row->license_type; $originalText = $row->license_text; // Load license class $objL = new \Components\Publications\Tables\License($this->_parent->_db); // Incoming - license screen agreements $license = Request::getInt('license', 0, 'post'); $text = \Hubzero\Utility\Sanitize::clean(Request::getVar('license_text', '', 'post')); $agree = Request::getInt('agree', 0, 'post'); $custom = Request::getVar('substitute', array(), 'request', 'array'); if ($license) { if (!$objL->load($license)) { $this->setError(Lang::txt('There was a problem saving license selection')); return false; } if ($objL->agreement == 1 && !$agree) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_LICENSE_NEED_AGREEMENT')); return false; } elseif ($objL->customizable == 1 && !$text) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_LICENSE_NEED_TEXT')); return false; } $row->license_type = $license; $text = preg_replace("/\r/", '', $text); $row->license_text = $text; // Pre-defined license text if ($objL->text && $objL->customizable == 0) { $row->license_text = $objL->text; // Do we have template items to replace? preg_match_all('/\\[([^\\]]*)\\]/', $objL->text, $substitutes); if (count($substitutes) > 1) { foreach ($substitutes[1] as $sub) { if (!isset($custom[$sub]) || !$custom[$sub]) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_LICENSE_NEED_CUSTOM')); return false; } else { $row->license_text = preg_replace('/\\[' . $sub . '\\]/', trim($custom[$sub]), $row->license_text); } } } } $row->store(); // Save agreement $row->saveParam($pub->version_id, 'licenseagreement', 1); // Save custom fields in version params foreach ($custom as $label => $value) { $row->saveParam($pub->version_id, 'licensecustom' . strtolower($label), trim($value)); } if ($license != $originalType || $text != $originalText) { $this->_parent->set('_update', 1); } // Check agreements return true; } // Incoming - selector screen $selections = Request::getVar('selecteditems', ''); $toAttach = explode(',', $selections); $i = 0; foreach ($toAttach as $license) { if (!trim($license)) { continue; } // Make sure license exists if ($objL->load($license)) { $row->license_type = $license; $i++; $row->store(); // Clear agreement if license is changed if ($originalType != $license) { // Save agreement $row->saveParam($pub->version_id, 'licenseagreement', 0); $this->_parent->set('_update', 1); } // Only one choice break; } } if ($i) { $this->set('_message', Lang::txt('License selection saved')); return true; } else { $this->setError(Lang::txt('There was a problem saving license selection')); return false; } }
/** * Start/save a new version * * @return string */ public function newVersion() { // Incoming $pid = $this->_pid ? $this->_pid : Request::getInt('pid', 0); $ajax = Request::getInt('ajax', 0); $label = trim(Request::getVar('version_label', '', 'post')); // Check permission if (!$this->model->access('content')) { throw new Exception(Lang::txt('ALERTNOTAUTH'), 403); return; } // Load default version $pub = new \Components\Publications\Models\Publication($pid, 'default'); if (!$pub->exists()) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_NOT_FOUND')); $this->_task = ''; return $this->browse(); } // 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; } // Set curation model $pub->setCuration(); // Check if dev version is already there if ($pub->version->checkVersion($pid, 'dev')) { // Redirect App::redirect(Route::url($pub->link('editdev'))); return; } // Can't start a new version if there is a finalized or submitted draft if ($pub->version->get('state') == 4 || $pub->version->get('state') == 5 || $pub->version->get('state') == 7) { // Determine redirect path App::redirect(Route::url($pub->link('editdefault'))); return; } // Saving new version if ($this->_task == 'savenew') { $used_labels = $pub->version->getUsedLabels($pid, 'dev'); if (!$label) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_VERSION_LABEL_NONE')); } elseif ($label && in_array($label, $used_labels)) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_VERSION_LABEL_USED')); } else { // Create new version $new = new \Components\Publications\Tables\Version($this->_database); // Copy from previous version $new->publication_id = $pub->get('publication_id'); $new->title = $pub->get('title'); $new->abstract = $pub->get('abstract'); $new->description = $pub->get('description'); $new->metadata = $pub->get('metadata'); $new->release_notes = $pub->get('release_notes'); $new->license_type = $pub->get('license_type'); $new->license_text = $pub->get('license_text'); $new->access = $pub->get('access'); $new->created = Date::toSql(); $new->created_by = $this->_uid; $new->modified = Date::toSql(); $new->modified_by = $this->_uid; $new->state = 3; $new->main = 0; $new->version_label = $label; $new->version_number = $pub->versionCount() + 1; $new->secret = strtolower(\Components\Projects\Helpers\Html::generateCode(10, 10, 0, 1, 1)); if ($new->store()) { // Transfer data $pub->_curationModel->transfer($pub, $pub->version, $new); // Set response message $this->set('_msg', Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_NEW_VERSION_STARTED')); // Set activity message $pubTitle = \Hubzero\Utility\String::truncate($new->title, 100); $action = Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ACTIVITY_STARTED_VERSION') . ' ' . $new->version_label . ' '; $action .= Lang::txt('PLG_PROJECTS_PUBLICATIONS_OF_PUBLICATION') . ' "' . $pubTitle . '"'; $this->set('_activity', $action); // Record action, notify team $pub->set('version_number', $new->version_number); $pub->set('version_label', $new->version_label); $this->onAfterSave($pub); } else { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_SAVING_NEW_VERSION')); } } } else { $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'publications', 'name' => 'newversion')); // Output HTML $view->option = $this->_option; $view->database = $this->_database; $view->project = $this->model; $view->uid = $this->_uid; $view->pid = $pid; $view->pub = $pub; $view->task = $this->_task; $view->config = $this->model->config(); $view->pubconfig = $this->_pubconfig; $view->ajax = $ajax; $view->title = $this->_area['title']; // Get messages and errors $view->msg = $this->_msg; if ($this->getError()) { $view->setError($this->getError()); } return $view->loadTemplate(); } // Pass success or error message if ($this->getError()) { \Notify::message($this->getError(), 'error', 'projects'); } elseif (!empty($this->_msg)) { \Notify::message($this->_msg, 'success', 'projects'); } // Redirect App::redirect(Route::url($pub->link('editdev'))); return; }
/** * Save block * * @return string HTML */ public function save($manifest = NULL, $blockId = 0, $pub = NULL, $actor = 0, $elementId = 0) { // Set block manifest if ($this->_manifest === NULL) { $this->_manifest = $manifest ? $manifest : self::getManifest(); } // Make sure changes are allowed if ($this->_parent->checkFreeze($this->_manifest->params, $pub)) { return false; } // Load publication version $row = new \Components\Publications\Tables\Version($this->_parent->_db); if (!$row->load($pub->version_id)) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_VERSION_NOT_FOUND')); return false; } // Track changes $changed = 0; $missed = 0; $collapse = $this->_manifest->params->collapse_elements == 0 ? 0 : 1; // Incoming $nbtags = Request::getVar('nbtag', array(), 'request', 'array'); // Parse metadata $data = array(); preg_match_all("#<nb:(.*?)>(.*?)</nb:(.*?)>#s", $pub->metadata, $matches, PREG_SET_ORDER); if (count($matches) > 0) { foreach ($matches as $match) { $data[$match[1]] = \Components\Publications\Helpers\Html::_txtUnpee($match[2]); } } // Save each element foreach ($this->_manifest->elements as $id => $element) { // Are we saving just one element? if ($elementId && $id != $elementId && $collapse) { continue; } $field = $element->params->field; $aliasmap = $element->params->aliasmap; $input = $element->params->input; $required = $element->params->required; if ($field == 'metadata') { $value = isset($nbtags[$aliasmap]) ? trim(stripslashes($nbtags[$aliasmap])) : NULL; if (!$value && $required) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_MISSING_REQUIRED')); } else { if ($value && !isset($data[$aliasmap]) || isset($data[$aliasmap]) && $data[$aliasmap] != $value) { $changed++; } // Replace data $data[$aliasmap] = $value; // Save all in one field $tagCollect = ''; foreach ($data as $tagname => $tagcontent) { $tagCollect .= "\n" . '<nb:' . $tagname . '>' . $tagcontent . '</nb:' . $tagname . '>' . "\n"; } $row->metadata = $tagCollect; } } else { $value = trim(Request::getVar($field, '', 'post', 'none', 2)); $value = $input == 'editor' ? stripslashes($value) : \Hubzero\Utility\Sanitize::clean($value); if (!$value && $required) { $missed++; } if ($row->{$field} != $value) { $lastRecord = $pub->_curationModel->getLastUpdate($id, $this->_name, $blockId); $changed++; // Record update time $data = new stdClass(); $data->updated = Date::toSql(); $data->updated_by = $actor; // Unmark as skipped if ($lastRecord && $lastRecord->review_status == 3) { $data->review_status = 0; $data->update = ''; } if ($value) { $data->update = ''; // remove dispute message if requirement satisfied } $pub->_curationModel->saveUpdate($data, $id, $this->_name, $pub, $blockId); } $row->{$field} = $value; } } // Update modified info if ($changed) { $row->modified = Date::toSql(); $row->modified_by = $actor; $this->_parent->set('_update', 1); } // Report error if ($missed && $collapse == 0) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_MISSING_REQUIRED')); } // Save if (!$row->store()) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_SAVE_PUBLICATION')); return false; } // Set success message $this->_parent->set('_message', $this->get('_message')); return true; }
/** * Archive publications beyond grace period * * @param object $job \Components\Cron\Models\Job * @return boolean */ public function runMkAip(\Components\Cron\Models\Job $job) { $database = \App::get('db'); $config = Component::params('com_publications'); require_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'helpers' . DS . 'utilities.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'tables' . DS . 'version.php'; require_once PATH_CORE . DS . 'components' . DS . 'com_projects' . DS . 'helpers' . DS . 'html.php'; // Check that mkAIP script exists if (!\Components\Publications\Helpers\Utilities::archiveOn()) { return; } // Check for grace period $gracePeriod = $config->get('graceperiod', 0); if (!$gracePeriod) { // If no grace period, this cron is unnecessary (archived as approval) return; } $aipBasePath = trim($config->get('aip_path', NULL), DS); $aipBasePath = $aipBasePath && is_dir(DS . $aipBasePath) ? DS . $aipBasePath : NULL; // Check for base path if (!$aipBasePath) { $this->setError('Missing archival base directory'); return; } // Get all unarchived publication versions $query = "SELECT V.*, C.id as id, V.id as version_id "; $query .= " FROM #__publication_versions as V, #__publications as C "; $query .= " WHERE C.id=V.publication_id AND V.state=1 "; $query .= " AND V.doi IS NOT NULL "; $query .= " AND V.accepted IS NOT NULL AND V.accepted !='0000-00-00 00:00:00' "; $query .= " AND (V.archived IS NULL OR V.archived ='0000-00-00 00:00:00') "; $database->setQuery($query); if (!($rows = $database->loadObjectList())) { return true; } // Start email message $subject = Lang::txt('Update on recently archived publications'); $body = Lang::txt('The following publications passed the grace period and were archived:') . "\n"; $aipGroup = $config->get('aip_group'); $counter = 0; foreach ($rows as $row) { // Grace period unexpired? $monthFrom = Date::of($row->accepted . '+1 month')->toSql(); if (strtotime($monthFrom) > strtotime(Date::of('now'))) { continue; } // Load version $pv = new \Components\Publications\Tables\Version($database); if (!$pv->load($row->version_id)) { continue; } // Create aip path $doiParts = explode('/', $row->doi); $aipName = count($doiParts) > 1 ? $doiParts[0] . '__' . $doiParts[1] : ''; // Archival package exists? if ($aipBasePath && $aipName && is_dir($aipBasePath . DS . $aipName)) { // Save approved date and archive date $pv->archived = $pv->accepted; $pv->store(); // Do not overwrite existing archives !! continue; } // Run mkAIP and save archived date if (\Components\Publications\Helpers\Utilities::mkAip($row)) { $pv->archived = Date::toSql(); $pv->store(); $counter++; $body .= $row->title . ' v.' . $row->version_label . ' (id #' . $row->id . ')' . "\n"; } } // Email update to admins if ($counter > 0 && $aipGroup) { // Set email config $from = array('name' => Config::get('fromname') . ' ' . Lang::txt('Publications'), 'email' => Config::get('mailfrom'), 'multipart' => md5(date('U'))); $admins = \Components\Projects\Helpers\Html::getGroupMembers($aipGroup); // Build message if (!empty($admins)) { foreach ($admins as $admin) { // Get the user's account $user = User::getInstance($admin); if (!$user->get('id')) { continue; } $message = new \Hubzero\Mail\Message(); $message->setSubject($subject)->addFrom($from['email'], $from['name'])->addTo($user->get('email'), $user->get('name'))->addHeader('X-Component', 'com_publications')->addHeader('X-Component-Object', 'publications'); $message->addPart($body, 'text/plain'); $message->send(); } } } // All done return true; }
/** * Save incoming * * @return boolean */ public function save($element, $elementId, $pub, $blockParams, $toAttach = array()) { $toAttach = $toAttach ? $toAttach : Request::getVar('url', '', 'post', 'array'); $titles = Request::getVar('title', '', 'post', 'array'); $desc = Request::getVar('desc', '', 'post', 'array'); // Incoming selections if (empty($toAttach)) { $toAttach = array($url); } // Get configs $configs = $this->getConfigs($element, $elementId, $pub, $blockParams); // Cannot make changes if ($configs->freeze) { return false; } // Nothing to change if (empty($toAttach) && !$configs->replace) { return false; } // Get existing attachments for the elemnt $attachments = $pub->_attachments; $attachments = isset($attachments['elements'][$elementId]) ? $attachments['elements'][$elementId] : NULL; // Sort out attachments for this element $attachments = $this->_parent->getElementAttachments($elementId, $attachments, $this->_name); // Counters $i = 0; $a = 0; // Default title for publication $defaultTitle = $pub->_curationModel->_manifest->params->default_title; // Attach/refresh each selected item foreach ($toAttach as $identifier) { if (!trim($identifier)) { continue; } $a++; $ordering = $i + 1; $title = isset($titles[$i]) ? $titles[$i] : NULL; $desc = isset($desc[$i]) ? $desc[$i] : NULL; if ($this->addAttachment($identifier, $title, $pub, $configs, User::get('id'), $elementId, $element, $ordering)) { // Do we also set draft title and metadata from the link? if ($i == 0 && $title && $element->role == 1 && stripos($pub->title, $defaultTitle) !== false) { // Load publication version $row = new \Components\Publications\Tables\Version($this->_parent->_db); if (!$row->load($pub->version_id)) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_VERSION_NOT_FOUND')); return false; } $row->title = $title; $description = \Hubzero\Utility\Sanitize::clean($desc); $row->description = $description; $row->abstract = \Hubzero\Utility\String::truncate($description, 255); $row->store(); } $i++; } } // Success if ($i > 0 && $i == $a) { $message = $this->get('_message') ? $this->get('_message') : Lang::txt('Selection successfully saved'); $this->set('_message', $message); } return true; }