/**
  * Check completion status
  *
  * @return  object
  */
 public function getStatus($manifest, $pub = NULL)
 {
     $status = new \Components\Publications\Models\Status();
     // Get requirements to check against
     $field = $manifest->params->field;
     $required = $manifest->params->required;
     $key = $manifest->params->aliasmap;
     $default = isset($manifest->params->default) ? $manifest->params->default : NULL;
     $value = isset($pub->{$key}) ? $pub->{$key} : NULL;
     $incomplete = 0;
     // Parse data in metadata field
     $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]);
         }
     }
     // Metadata field (special treatment)
     if ($field == 'metadata') {
         $value = isset($data[$key]) ? $data[$key] : NULL;
     }
     // Default value not replaced?
     if ($default && $value) {
         if ($default == $value || preg_match('/' . $default . ' (\\(.*\\))/', $value, $matches)) {
             $status->setError(Lang::txt('Default value needs to be replaced'));
         }
     }
     // Required value not filled?
     if ($required && !$value) {
         $status->setError(Lang::txt('Missing ' . $key));
     } elseif (!$required && !$value) {
         $incomplete = 1;
     }
     $status->status = $status->getError() ? 0 : 1;
     $status->status = $incomplete ? 2 : $status->status;
     return $status;
 }
 /**
  * Get the content of nbtag in metadata field
  *
  * @return     mixed String or Integer
  */
 public function getNbtag($aliasmap = '')
 {
     $data = array();
     // Parse data
     preg_match_all("#<nb:(.*?)>(.*?)</nb:(.*?)>#s", $this->get('metadata', ''), $matches, PREG_SET_ORDER);
     if (count($matches) > 0) {
         foreach ($matches as $match) {
             $data[$match[1]] = Helpers\Html::_txtUnpee($match[2]);
         }
     }
     $value = isset($data[$aliasmap]) ? $data[$aliasmap] : NULL;
     return $value;
 }
 /**
  * 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;
 }