/**
  * Serve publication content
  * Determine how to render depending on master type, attachment type and user choice
  * Defaults to download
  *
  * @return  void
  */
 public function serveTask()
 {
     // Incoming
     $aid = Request::getInt('a', 0);
     // Attachment id
     $elementId = Request::getInt('el', 1);
     // Element id
     $render = Request::getVar('render', '');
     $vid = Request::getInt('vid', '');
     $file = Request::getVar('file', '');
     $disp = Request::getVar('disposition');
     $disp = in_array($disp, array('inline', 'attachment')) ? $disp : 'attachment';
     // Get our model and load publication data
     $this->model = new Models\Publication($this->_identifier, $this->_version, $vid);
     if (!$this->model->exists() || $this->model->isDeleted()) {
         App::redirect(Route::url('index.php?option=' . $this->_option), Lang::txt('COM_PUBLICATIONS_RESOURCE_NOT_FOUND'), 'error');
         return;
     }
     // Is the visitor authorized to view content?
     if (!$this->model->access('view-all')) {
         $this->_blockAccess();
         return true;
     }
     // Set curation
     $this->model->setCuration();
     // Bundle requested?
     if ($render == 'archive') {
         // Produce archival package
         if ($this->model->_curationModel->package()) {
             // Log access
             if ($this->model->isPublished()) {
                 $this->model->logAccess('primary');
             }
             $this->model->_curationModel->serveBundle();
             return;
         } else {
             throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
             return;
         }
     }
     // Bundle requested?
     if ($render == 'showcontents') {
         // Produce archival package
         if ($this->model->_curationModel->package()) {
             // Build the HTML of the "about" tab
             $view = new \Hubzero\Component\View(['name' => 'view', 'layout' => '_contents']);
             $view->model = $this->model;
             $view->option = $this->_option;
             $view->display();
             return;
         } else {
             throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
             return;
         }
     }
     // Serving data file (dataview)
     if ($file) {
         // Ensure the file exist
         if (!file_exists($this->model->path('data', true) . DS . trim($file))) {
             throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
             return;
         }
         // Initiate a new content server and serve up the file
         $server = new \Hubzero\Content\Server();
         $server->filename($this->model->path('data', true) . DS . trim($file));
         $server->disposition($disp);
         $server->acceptranges(true);
         $server->saveas(basename($file));
         if (!$server->serve()) {
             // Should only get here on error
             throw new Exception(Lang::txt('COM_PUBLICATIONS_SERVER_ERROR'), 404);
         } else {
             exit;
         }
     }
     $this->model->attachments();
     // Individual attachment is requested? Find element ID
     if ($aid) {
         $elementId = $this->model->_curationModel->getElementIdByAttachment($aid);
     }
     // We do need attachments
     if (!isset($this->model->_attachments['elements'][$elementId]) || empty($this->model->_attachments['elements'][$elementId])) {
         throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
         return;
     }
     // Get element manifest to deliver content as intended
     $curation = $this->model->_curationModel->getElementManifest($elementId);
     // We do need manifest!
     if (!$curation || !isset($curation->element) || !$curation->element) {
         return false;
     }
     // Get attachment type model
     $attModel = new Models\Attachments($this->database);
     // Log access
     if ($this->model->isPublished()) {
         $aType = $curation->element->params->role == 1 ? 'primary' : 'support';
         $this->model->logAccess($aType);
     }
     // Serve content
     $content = $attModel->serve($curation->element->params->type, $curation->element, $elementId, $this->model, $curation->block->params, $aid);
     // No content served
     if ($content === NULL || $content == false) {
         throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
     }
     // Do we need to redirect to content?
     if ($attModel->get('redirect')) {
         App::redirect($attModel->get('redirect'));
         return;
     }
     return $content;
 }
 /**
  * Conversion for publications created in a non-curated flow
  *
  * @param   object $pub
  * @return  boolean
  */
 public function convertToCuration($pub = NULL)
 {
     $pub = $pub ? $pub : $this->_pub;
     $oldFlow = false;
     // Load attachments
     $pub->attachments();
     if (!isset($pub->_attachments) || empty($pub->_attachments['elements'])) {
         // Nothing to convert
         return false;
     }
     // Get supporting docs element manifest
     $sElements = self::getElements(2);
     $sElement = $sElements ? $sElements[0] : NULL;
     // Loop through attachments
     foreach ($pub->_attachments['elements'] as $elementId => $elementAttachments) {
         if (empty($elementAttachments)) {
             continue;
         }
         // Check if any attachments are missing element id
         foreach ($elementAttachments as $elAttach) {
             if ($elAttach->element_id == 0) {
                 // Save elementid
                 $row = new Tables\Attachment($this->_db);
                 if ($row->load($elAttach->id)) {
                     $markId = $elAttach->role != 1 && $sElement ? $sElement->id : $elementId;
                     $row->element_id = $markId;
                     $row->store();
                 }
                 $oldFlow = true;
                 // will need to make further checks
             }
         }
     }
     if (!$oldFlow) {
         return false;
     }
     // Get gallery element manifest
     $elements = self::getElements(3);
     $element = $elements ? $elements[0] : NULL;
     // Retrieve screenshots
     $pScreenshot = new Tables\Screenshot($this->_db);
     $shots = $pScreenshot->getScreenshots($pub->version_id);
     // Transfer gallery files to the right location
     if ($element && $shots) {
         // Get attachment type model
         $attModel = new Attachments($this->_db);
         $fileAttach = $attModel->loadAttach('file');
         // Set configs
         $configs = $fileAttach->getConfigs($element->manifest->params, $element->id, $pub, $element->block);
         // Get gallery path
         $galleryPath = Helpers\Html::buildPubPath($pub->id, $pub->version_id, '', 'gallery', 1);
         if (is_dir($galleryPath)) {
             foreach ($shots as $shot) {
                 $objPA = new Tables\Attachment($this->_db);
                 if (is_file($galleryPath . DS . $shot->srcfile) && !$objPA->loadElementAttachment($pub->version_id, array('path' => $shot->filename), $element->id, 'file', $element->manifest->params->role)) {
                     $objPA = new Tables\Attachment($this->_db);
                     $objPA->publication_id = $pub->id;
                     $objPA->publication_version_id = $pub->version_id;
                     $objPA->path = $shot->filename;
                     $objPA->type = 'file';
                     $objPA->created_by = User::get('id');
                     $objPA->created = Date::toSql();
                     $objPA->role = $element->manifest->params->role;
                     $objPA->element_id = $element->id;
                     $objPA->ordering = $shot->ordering;
                     if (!$objPA->store()) {
                         continue;
                     }
                     // Check if names is already used
                     $suffix = $fileAttach->checkForDuplicate($configs->path . DS . $objPA->path, $objPA, $configs);
                     // Save params if applicable
                     if ($suffix) {
                         $objPA->params = 'suffix=' . $suffix . "\n";
                     }
                     // Copy file into the right spot
                     $configs->copyFrom = $galleryPath . DS . $shot->srcfile;
                     if (!$fileAttach->publishAttachment($objPA, $pub, $configs)) {
                         $objPA->delete();
                     }
                 }
             }
         }
     }
     // Check if published version has curation manifest saved
     $row = new Tables\Version($this->_db);
     if ($pub->state == 1 && !$pub->curation) {
         if ($row->load($pub->version_id)) {
             $row->curation = json_encode($this->_manifest);
             $row->store();
         }
     }
     // Mark as curated
     $row->saveParam($row->id, 'curated', 1);
     return true;
 }