Beispiel #1
0
 /**
  * Parse loaded data for further processing
  *
  * @access public
  * @return string
  */
 public function parse($dryRun = 1, $output = NULL)
 {
     // Set common props
     $this->_uid = User::get('id');
     // No errors. Let's rewind and parse
     $this->reader = new \XMLReader();
     $this->reader->XML($this->data);
     // Load classes
     $objCat = new Tables\Category($this->database);
     $objL = new Tables\License($this->database);
     // Get base type
     $base = Request::getVar('base', 'files');
     // Determine publication master type
     $mt = new Tables\MasterType($this->database);
     $choices = $mt->getTypes('alias', 1);
     $mastertype = in_array($base, $choices) ? $base : 'files';
     // Get type params
     $mType = $mt->getType($mastertype);
     // Get curation model for the type
     $curationModel = new \Components\Publications\Models\Curation($mType->curation);
     // Get defaults from manifest
     $title = $curationModel && isset($curationModel->_manifest->params->default_title) ? $curationModel->_manifest->params->default_title : 'Untitled Draft';
     $title = $title ? $title : 'Untitled Draft';
     $cat = isset($curationModel->_manifest->params->default_category) ? $curationModel->_manifest->params->default_category : 1;
     $this->curationModel = $curationModel;
     // Get element IDs
     $elementPrimeId = 1;
     $elementGalleryId = 2;
     $elementSupportId = 3;
     if ($this->curationModel) {
         $elements1 = $this->curationModel->getElements(1);
         $elements2 = $this->curationModel->getElements(2);
         $elements3 = $this->curationModel->getElements(3);
         $elementPrimeId = !empty($elements1) ? $elements1[0]->id : $elementPrimeId;
         $elementGalleryId = !empty($elements3) ? $elements3[0]->id : $elementGalleryId;
         $elementSupportId = !empty($elements2) ? $elements2[0]->id : $elementSupportId;
     }
     // Get project repo path
     $this->projectPath = $this->project->repo()->get('path');
     // Parse data
     $items = array();
     while ($this->reader->read()) {
         if ($this->reader->name === 'publication') {
             $node = new \SimpleXMLElement($this->reader->readOuterXML());
             // Check that category exists
             $category = isset($node->cat) ? $node->cat : 'dataset';
             $catId = $objCat->getCatId($category);
             $item['category'] = $category;
             $item['type'] = $mastertype;
             $item['errors'] = array();
             $item['tags'] = array();
             $item['authors'] = array();
             // Publication properties
             $item['publication'] = new Tables\Publication($this->database);
             $item['publication']->master_type = $mType->id;
             $item['publication']->category = $catId ? $catId : $cat;
             $item['publication']->project_id = $this->project->get('id');
             $item['publication']->created_by = $this->_uid;
             $item['publication']->created = Date::toSql();
             $item['publication']->access = 0;
             // Version properties
             $item['version'] = new Tables\Version($this->database);
             $item['version']->title = isset($node->title) && trim($node->title) ? trim($node->title) : $title;
             $item['version']->abstract = isset($node->synopsis) ? trim($node->synopsis) : '';
             $item['version']->description = isset($node->abstract) ? trim($node->abstract) : '';
             $item['version']->version_label = isset($node->version) ? trim($node->version) : '1.0';
             $item['version']->release_notes = isset($node->notes) ? trim($node->notes) : '';
             // Check license
             $license = isset($node->license) ? $node->license : '';
             $item['license'] = $objL->getLicenseByTitle($license);
             if (!$item['license']) {
                 $item['errors'][] = Lang::txt('COM_PUBLICATIONS_BATCH_ITEM_ERROR_LICENSE');
             } else {
                 $item['version']->license_type = $item['license']->id;
             }
             // Pick up files
             $item['files'] = array();
             if ($node->content) {
                 $i = 1;
                 foreach ($node->content->file as $file) {
                     $this->collectFileData($file, 1, $i, $item, $elementPrimeId);
                     $i++;
                 }
             }
             // Supporting docs
             if ($node->supportingmaterials) {
                 $i = 1;
                 foreach ($node->supportingmaterials->file as $file) {
                     $this->collectFileData($file, 2, $i, $item, $elementSupportId);
                     $i++;
                 }
             }
             // Gallery
             if ($node->gallery) {
                 $i = 1;
                 foreach ($node->gallery->file as $file) {
                     $this->collectFileData($file, 3, $i, $item, $elementGalleryId);
                     $i++;
                 }
             }
             // Tags
             if ($node->tags) {
                 foreach ($node->tags->tag as $tag) {
                     if (trim($tag)) {
                         $item['tags'][] = $tag;
                     }
                 }
             }
             // Authors
             if ($node->authors) {
                 $i = 1;
                 foreach ($node->authors->author as $author) {
                     $attributes = $author->attributes();
                     $uid = $attributes['uid'];
                     $this->collectAuthorData($author, $i, $uid, $item);
                     $i++;
                 }
             }
             // Set general process error
             if (count($item['errors']) > 0) {
                 $this->setError(Lang::txt('COM_PUBLICATIONS_BATCH_ERROR_MISSING_OR_INVALID'));
             }
             $items[] = $item;
             $this->reader->next();
         }
     }
     // Show what you'll get
     if ($dryRun == 1) {
         $eview = new \Hubzero\Component\View(array('name' => 'batchcreate', 'layout' => 'dryrun'));
         $eview->option = $this->_option;
         $eview->items = $items;
         $output .= $eview->loadTemplate();
     } elseif ($dryRun == 2) {
         // Get hub config
         $this->site = trim(Request::base(), DS);
         // Process batch
         $out = NULL;
         $i = 0;
         foreach ($items as $item) {
             if ($this->processRecord($item, $out)) {
                 $i++;
             }
         }
         if ($i > 0) {
             $output = '<p class="success">' . Lang::txt('COM_PUBLICATIONS_BATCH_SUCCESS_CREATED') . ' ' . $i . ' ' . Lang::txt('COM_PUBLICATIONS_BATCH_RECORDS_S') . '</p>';
         }
         if ($i != count($items)) {
             $output = '<p class="error">' . Lang::txt('COM_PUBLICATIONS_BATCH_FAILED_CREATED') . ' ' . (count($items) - $i) . ' ' . Lang::txt('COM_PUBLICATIONS_BATCH_RECORDS_S') . '</p>';
         }
         $output .= $out;
     }
     $this->reader->close();
     return $output;
 }
Beispiel #2
0
 /**
  * Produce publication package
  *
  * @return     boolean
  */
 public function package()
 {
     if (empty($this->_pub)) {
         return false;
     }
     // Get elements
     $prime = $this->getElements(1);
     $second = $this->getElements(2);
     $gallery = $this->getElements(3);
     $elements = array_merge($prime, $second, $gallery);
     // Do we have items to package?
     if (!$elements) {
         return false;
     }
     if (!is_dir($this->_pub->path('base', true))) {
         return false;
     }
     // Set archival properties
     $bundleDir = $this->_pub->title;
     $tarname = $this->getBundleName();
     $tarpath = $this->_pub->path('base', true) . DS . $tarname;
     $licFile = $this->_pub->path('base', true) . DS . 'LICENSE.txt';
     $readmeFile = $this->_pub->path('base', true) . DS . 'README.txt';
     // Get attachment type model
     $attModel = new Attachments($this->_db);
     // Start README
     $readme = $this->_pub->title . "\n ";
     $readme .= 'Version ' . $this->_pub->version_label . "\n ";
     // List authors
     if (isset($this->_pub->_authors) && $this->_pub->_authors) {
         $readme .= 'Authors: ' . "\n ";
         foreach ($this->_pub->_authors as $author) {
             $readme .= $author->name ? $author->name : $author->p_name;
             $org = $author->organization ? $author->organization : $author->p_organization;
             if ($org) {
                 $readme .= ', ' . $org;
             }
             $readme .= "\n ";
         }
     }
     // Add DOI if available
     if ($this->_pub->doi) {
         $readme .= 'doi:' . $this->_pub->doi . "\n ";
     }
     // Add license information
     $objL = new Tables\License($this->_db);
     if ($objL->loadLicense($this->_pub->license_type) && $objL->id) {
         $readme .= "\n " . "\n ";
         $readme .= 'License: ' . "\n ";
         $readme .= $objL->title . "\n ";
         // Custom license text?
         if ($this->_pub->license_text) {
             $readme .= $this->_pub->license_text . "\n ";
             // Create license file
             $handle = fopen($licFile, 'w');
             fwrite($handle, $this->_pub->license_text);
             fclose($handle);
         } elseif ($objL->text) {
             $readme .= $objL->text . "\n ";
         }
     }
     $readme .= "\n ";
     $readme .= '#####################################' . "\n ";
     $readme .= 'Included Publication Materials:' . "\n ";
     $readme .= '#####################################' . "\n ";
     // Create bundle
     $zip = new ZipArchive();
     if ($zip->open($tarpath, ZipArchive::OVERWRITE) === TRUE) {
         // Bundle file attachments
         $attModel->bundleItems($zip, $elements, $this->_pub, $readme, $bundleDir);
         // Add license file
         if (file_exists($licFile)) {
             $where = $bundleDir . DS . basename($licFile);
             $zip->addFile($licFile, $where);
             $readme .= "\n" . 'License File: ' . "\n";
             $readme .= '>>> ' . basename($licFile) . "\n";
         }
         // Add readme
         if ($readme) {
             $where = $bundleDir . DS . basename($readmeFile);
             $readme .= "\n" . 'Archival Info:' . "\n";
             $readme .= '>>> ' . basename($readmeFile) . "\n";
             $readme .= "\n ";
             $readme .= "\n ";
             $readme .= '--------------------------------------------' . "\n ";
             $readme .= 'Archival package produced ' . Date::toSql();
             $handle = fopen($readmeFile, 'w');
             fwrite($handle, $readme);
             fclose($handle);
             $zip->addFile($readmeFile, $where);
         }
         $zip->close();
     } else {
         return false;
     }
     return true;
 }