/** * 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; } }
/** * 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; }