/** * Sets the ID of this resource map. Do not supply with alias. */ public function setPaths() { $this->componentURL = Request::base() . 'publications/'; $this->resourceURL = $this->componentURL . $this->id; $database = \App::get('db'); $pub = new \Components\Publications\Tables\Publication($database); $publication = $pub->getPublication($this->id); $this->resourceSite = \Components\Publications\Helpers\Html::buildPubPath($this->id, $publication->version_id, '', $publication->secret, 1); }
/** * Get configs * * @return boolean */ public function getConfigs($element, $elementId, $pub, $blockParams) { $configs = new stdClass(); $typeParams = $element->typeParams; // Allow changes in non-draft version? $configs->freeze = isset($blockParams->published_editing) && $blockParams->published_editing == 0 && ($pub->state == 1 || $pub->state == 5) ? 1 : 0; // Log path $configs->logPath = \Components\Publications\Helpers\Html::buildPubPath($pub->id, $pub->version_id, '', 'logs', 0); // replace current attachments? $configs->replace = Request::getInt('replace_current', 0, 'post'); // Verify file type against allowed before attaching? $configs->check = isset($blockParams->verify_types) ? $blockParams->verify_types : 0; // Get default title $title = isset($element->title) ? str_replace('{pubtitle}', $pub->title, $element->title) : NULL; $configs->title = str_replace('{pubversion}', $pub->version_label, $title); // Fancy launcher? $configs->fancyLauncher = isset($typeParams->fancyLauncher) ? $typeParams->fancyLauncher : 0; return $configs; }
/** * Get configs * * @return boolean */ public function getConfigs($element, $elementId, $pub, $blockParams) { $configs = new stdClass(); $typeParams = $element->typeParams; // Allow changes in non-draft version? $configs->freeze = isset($blockParams->published_editing) && $blockParams->published_editing == 0 && ($pub->state == 1 || $pub->state == 5) ? 1 : 0; // Get project path $configs->path = $pub->_project->repo()->get('path'); $pubconfig = Component::params('com_publications'); $base = $pubconfig->get('webpath'); // Log path $configs->logPath = \Components\Publications\Helpers\Html::buildPubPath($pub->id, $pub->version_id, $base, 'logs', 0); // Get publication path $configs->pubBase = \Components\Publications\Helpers\Html::buildPubPath($pub->id, $pub->version_id, $base, '', 1); // Get default title $title = isset($element->title) ? str_replace('{pubtitle}', $pub->title, $element->title) : NULL; $configs->title = str_replace('{pubversion}', $pub->version_label, $title); // Fancy launcher? $configs->fancyLauncher = isset($typeParams->fancyLauncher) ? $typeParams->fancyLauncher : 0; return $configs; }
/** * Transfer files from one version to another * * @return boolean */ public function transferData($elementparams, $elementId, $pub, $blockParams, $attachments, $oldVersion, $newVersion) { // Get configs $configs = $this->getConfigs($elementparams, $elementId, $pub, $blockParams); $newConfigs = new stdClass(); $newConfigs->path = $configs->path; $newConfigs->dataPath = \Components\Publications\Helpers\Html::buildPubPath($pub->id, $newVersion->id, '', 'data', 1); $newConfigs->servePath = Route::url('index.php?option=com_publications&id=' . $pub->id) . '/?vid=' . $newVersion->id . '&task=serve'; // Loop through attachments foreach ($attachments as $att) { if ($att->type != $this->_name) { continue; } // Get database object and load record $objData = new \Components\Projects\Tables\Database($this->_parent->_db); $objData->loadRecord($att->object_name); $dbVersion = NULL; if (!$objData->id) { // Original database not found $this->_parent->setError(Lang::txt('Oups! Cannot attach selected database: database not found')); return false; } // Make new attachment record $pAttach = new \Components\Publications\Tables\Attachment($this->_parent->_db); if (!$pAttach->copyAttachment($att, $newVersion->id, $elementId, User::get('id'))) { continue; } // New database instance - need to clone again and get a new version number $result = Event::trigger('projects.clone_database', array($pAttach->object_name, $pub->_project, $newConfigs->servePath)); $dbVersion = $result && isset($result[0]) ? $result[0] : NULL; // Failed to clone if (!$dbVersion) { $this->_parent->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_FAILED_DB_CLONE')); $pAttach->delete(); return false; } $pAttach->modified_by = NULL; $pAttach->modified = NULL; $pAttach->object_revision = $dbVersion; $pAttach->path = 'dataviewer' . DS . 'view' . DS . 'publication:dsl' . DS . $pAttach->object_name . DS . '?v=' . $dbVersion; $pAttach->store(); } // Determine accompanying files and copy them in the right location $this->publishDataFiles($objData, $newConfigs); return true; }
/** * Get path to log file * * @return void */ public function getLogPath($pid = 0, $vid = 0) { if (!isset($this->_config)) { $this->_config = Component::params('com_publications'); } if (!$pid || !$vid) { return false; } // Build log path (access logs) $logPath = Html::buildPubPath($pid, $vid, $this->_config->get('webpath'), 'logs', 1); if (!is_dir($logPath)) { return false; } return $logPath; }
/** * Get path to archival bundle * * @return mixed */ public function bundlePath() { if (!$this->exists()) { return false; } if (!isset($this->_bundlePath)) { // Archival package $tarname = Lang::txt('Publication') . '_' . $this->get('id') . '.zip'; $this->_bundlePath = Helpers\Html::buildPubPath($this->get('id'), $this->get('version_id'), '', '', 1) . DS . $tarname; } return $this->_bundlePath; }
// List all content? $listAll = isset($this->publication->_curationModel->_manifest->params->list_all) ? $this->publication->_curationModel->_manifest->params->list_all : 0; $listLabel = isset($this->publication->_curationModel->_manifest->params->list_label) ? $this->publication->_curationModel->_manifest->params->list_label : Lang::txt('COM_PUBLICATIONS_CONTENT_LIST'); // Add plugin style \Hubzero\Document\Assets::addPluginStylesheet('publications', 'supportingdocs'); if ($listAll) { // Get elements in primary and supporting role $prime = $this->publication->_curationModel->getElements(1); $second = $this->publication->_curationModel->getElements(2); $elements = array_merge($prime, $second); // Get attachment type model $attModel = new \Components\Publications\Models\Attachments($this->database); if ($elements) { $append = NULL; // Get file path $path = \Components\Publications\Helpers\Html::buildPubPath($this->publication->id, $this->publication->version_id, $webpath, '', 1); $licFile = $path . DS . 'LICENSE.txt'; if (file_exists($licFile)) { $licenseUrl = Route::url('index.php?option=' . $this->option . '&id=' . $this->publication->id . '&task=license' . '&v=' . $this->publication->version_id); $append = '<li><a href="' . $licenseUrl . '" class="license-terms play" rel="external">' . Lang::txt('COM_PUBLICATIONS_LICENSE_TERMS') . '</a></li>'; } // Archival path $tarname = Lang::txt('Publication') . '_' . $this->publication->id . '.zip'; $archPath = $path . DS . $tarname; $showArchive = isset($this->publication->_curationModel->_manifest->params->show_archival) ? $this->publication->_curationModel->_manifest->params->show_archival : 0; $archiveUrl = Route::url('index.php?option=com_publications&id=' . $this->publication->id . '&task=serve&v=' . $this->publication->version_number . '&render=archive'); $showArchive = $showArchive && file_exists($archPath) ? true : false; // Draw list $list = $attModel->listItems($elements, $this->publication, $authorized, $append); ?> <h4 class="list-header"><?php
/** * Serve publication-related file (via public link) * * @param int $projectid * @return void */ public function serve($type = '', $projectid = 0, $query = '') { $this->_area = $this->onProjectAreas(); if ($type != $this->_area['name']) { return false; } $data = json_decode($query); if (!isset($data->pid) || !$projectid) { return false; } $disp = isset($data->disp) ? $data->disp : 'inline'; $type = isset($data->type) ? $data->type : 'file'; $folder = isset($data->folder) ? $data->folder : 'wikicontent'; $fpath = isset($data->path) ? $data->path : 'inline'; $limited = isset($data->limited) ? $data->limited : 0; if ($type != 'file') { return false; } $database = App::get('db'); // Instantiate a project $model = new \Components\Projects\Models\Project($projectid); if (!$model->exists() || $limited == 1 && !$model->access('member')) { // Throw error throw new Exception(Lang::txt('COM_PROJECTS_ERROR_ACTION_NOT_AUTHORIZED'), 403); return; } // Get referenced path $pubconfig = Component::params('com_publications'); $base_path = $pubconfig->get('webpath'); $pubPath = \Components\Publications\Helpers\Html::buildPubPath($data->pid, $data->vid, $base_path, $folder, $root = 0); $serve = PATH_APP . $pubPath . DS . $fpath; // Ensure the file exist if (!file_exists($serve)) { // Throw error throw new Exception(Lang::txt('COM_PROJECTS_FILE_NOT_FOUND'), 404); return; } // Initiate a new content server and serve up the file $server = new \Hubzero\Content\Server(); $server->filename($serve); $server->disposition($disp); $server->acceptranges(false); // @TODO fix byte range support $server->saveas(basename($fpath)); if (!$server->serve()) { // Should only get here on error throw new Exception(Lang::txt('COM_PUBLICATIONS_SERVER_ERROR'), 404); } else { exit; } return; }
/** * Transfer files from one version to another * * @return boolean */ public function transferData($elementparams, $elementId, $pub, $blockParams, $attachments, $oldVersion, $newVersion) { // Get configs $configs = $this->getConfigs($elementparams, $elementId, $pub, $blockParams); // Get configs for new version $typeParams = $elementparams->typeParams; $directory = isset($typeParams->directory) && $typeParams->directory ? $typeParams->directory : $newVersion->secret; $newConfigs = new stdClass(); // Directory path within pub folder $newConfigs->dirPath = $configs->subdir ? $directory . DS . $configs->subdir : $directory; // Build new path $newPath = \Components\Publications\Helpers\Html::buildPubPath($pub->id, $newVersion->id, '', $newConfigs->dirPath, 1); $newConfigs->pubPath = $newPath; $newConfigs->dirHierarchy = $configs->dirHierarchy; // Create new path if (!is_dir($newPath)) { Filesystem::makeDirectory($newPath, 0755, true, true); } // Loop through attachments foreach ($attachments as $att) { // Make new attachment record $pAttach = new \Components\Publications\Tables\Attachment($this->_parent->_db); if (!$pAttach->copyAttachment($att, $newVersion->id, $elementId, User::get('id'))) { continue; } // Get paths $copyFrom = $this->getFilePath($att->path, $att->id, $configs, $att->params); $copyTo = $this->getFilePath($pAttach->path, $pAttach->id, $newConfigs, $pAttach->params); if (!is_file($copyFrom)) { $pAttach->delete(); continue; } // Make sure we have subdirectories if (!is_dir(dirname($copyTo))) { Filesystem::makeDirectory(dirname($copyTo), 0755, true, true); } // Copy file if (!Filesystem::copy($copyFrom, $copyTo)) { $pAttach->delete(); } else { // Also make hash $md5hash = hash_file('sha256', $copyTo); $pAttach->content_hash = $md5hash; // Create hash file $hfile = $copyTo . '.hash'; if (!is_file($hfile)) { $handle = fopen($hfile, 'w'); fwrite($handle, $md5hash); fclose($handle); chmod($hfile, 0644); } $pAttach->store(); // Produce thumbnail (if applicable) if ($configs->handler && $configs->handler->getName() == 'imageviewer') { $configs->handler->makeThumbnail($pAttach, $pub, $newConfigs); } } } return true; }
/** * Download a file * * @return void */ public function downloadTask() { // Incoming $pid = Request::getInt('id', 0); $vid = Request::getInt('v', 0); $source = NULL; // Need pub and version ID if (!$pid || $pid == 0 || !$vid) { return; } // Get the file name $uri = Request::getVar('REQUEST_URI', '', 'server'); if (strstr($uri, 'Image:')) { $file = str_replace('Image:', '', strstr($uri, 'Image:')); } elseif (strstr($uri, 'File:')) { $file = str_replace('File:', '', strstr($uri, 'File:')); } //decode file name $file = urldecode($file); if (strtolower($file) == 'thumb') { // Get publication thumbnail $source = Helpers\Html::getThumb($pid, $vid, $this->config); } else { // Build publication path $path = Helpers\Html::buildPubPath($pid, $vid, $this->config->get('webpath')); if (strtolower($file) == 'master') { // Get master image $source = $path . DS . 'master.png'; // Default image if (!is_file(PATH_APP . DS . $source)) { // Grab first bigger image in gallery if (is_dir(PATH_APP . DS . $path . DS . 'gallery')) { $file_list = scandir(PATH_APP . DS . $path . DS . 'gallery'); foreach ($file_list as $file) { list($width, $height, $type, $attr) = getimagesize(PATH_APP . DS . $path . DS . 'gallery' . DS . $file); if ($width > 200) { $source = $path . DS . 'gallery' . DS . $file; break; } } } if (!is_file(PATH_APP . DS . $source)) { $source = PATH_CORE . DS . trim($this->config->get('masterimage', 'components/com_publications/site/assets/img/master.png'), DS); } } } else { // Load from gallery $source = PATH_APP . DS . $path . DS . 'gallery' . DS . $file; // Default image if (!is_file($source)) { $source = PATH_CORE . DS . trim($this->config->get('gallery_thumb', 'components/com_publications/site/assets/img/gallery_thumb.gif'), DS); } } } if (is_file($source)) { $server = new \Hubzero\Content\Server(); $server->filename($source); $server->serve_inline($source); exit; } return; }
/** * Return data on a resource view (this will be some form of HTML) * * @param object $publication Current publication * @param string $option Name of the component * @param array $areas Active area(s) * @param string $rtrn Data to be returned * @param string $version Version name * @param boolean $extended Whether or not to show panel * @return array */ public function onPublication($publication, $option, $areas, $rtrn = 'all', $version = 'default', $extended = true, $authorized = true) { $arr = array('html' => '', 'metadata' => ''); // Check if our area is in the array of areas we want to return results for if (is_array($areas)) { if (!array_intersect($areas, $this->onPublicationAreas($publication)) && !array_intersect($areas, array_keys($this->onPublicationAreas($publication)))) { // do nothing return $arr; } } if (!$publication->_category->_params->get('plg_supportingdocs')) { return $arr; } // Are we returning HTML? if ($rtrn == 'all' || $rtrn == 'html') { $database = App::get('db'); $config = Component::params($option); // Instantiate a view $view = new \Hubzero\Plugin\View(array('folder' => 'publications', 'element' => 'supportingdocs', 'name' => 'browse')); // Get docs $pContent = new \Components\Publications\Tables\Attachment($database); $view->docs = $pContent->getAttachments($publication->version_id, $filters = array('role' => array(1, 0, 2), 'order' => 'a.role DESC, a.ordering ASC')); // Get projects html helper require_once PATH_CORE . DS . 'components' . DS . 'com_projects' . DS . 'helpers' . DS . 'html.php'; // Build publication path $base_path = $config->get('webpath'); $view->path = \Components\Publications\Helpers\Html::buildPubPath($publication->id, $publication->version_id, $base_path, $publication->secret, $root = 1); // Pass the view some info $view->option = $option; $view->publication = $publication; $view->config = $config; $view->version = $version; $view->live_site = Request::base(); $view->authorized = $authorized; if ($this->getError()) { $view->setError($this->getError()); } // Return the output $arr['html'] = $view->loadTemplate(); } return $arr; }
/** * Deletes assoc with pub version * * @param integer $vid * @param integer $pid * @return void */ public function deleteVersionExistence($vid, $pid) { // Delete authors $pa = new Tables\Author($this->database); $authors = $pa->deleteAssociations($vid); // Delete attachments $pContent = new Tables\Attachment($this->database); $pContent->deleteAttachments($vid); // Delete screenshots $pScreenshot = new Tables\Screenshot($this->database); $pScreenshot->deleteScreenshots($vid); // Delete access accosiations $pAccess = new Tables\Access($this->database); $pAccess->deleteGroups($vid); // Delete audience $pAudience = new Tables\Audience($this->database); $pAudience->deleteAudience($vid); // Build publication path $path = Helpers\Html::buildPubPath($pid, $vid, $this->config->get('webpath'), '', 1); // Delete all files if (is_dir($path)) { Filesystem::deleteDirectory($path); } return true; }
/** * 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; }
/** * Show attachments in an image band (gallery) * * @return void */ public function showImageBand($pub) { // Get element manifest to deliver content as intended $elements = $pub->_curationModel->getElements(3); if (empty($elements)) { return false; } // Make sure we got config if (!$this->_config) { $this->getConfig(); } // Show first element $element = $elements[0]; $manifest = $element->manifest; $params = $manifest->params->typeParams; $dirHierarchy = isset($params->dirHierarchy) ? $params->dirHierarchy : 1; // Get files directory $directory = isset($params->directory) && $params->directory ? $params->directory : $pub->secret; $pubPath = \Components\Publications\Helpers\Html::buildPubPath($pub->id, $pub->version_id, '', $directory, 0); $configs = new stdClass(); $configs->dirHierarchy = $dirHierarchy; $configs->pubPath = $pubPath; // Do we have attachments? $attachments = isset($pub->_attachments['elements'][$element->id]) ? $pub->_attachments['elements'][$element->id] : NULL; if (!$attachments) { return false; } $html = ''; $i = 0; foreach ($attachments as $attach) { $fpath = $this->getFilePath($attach->path, $attach->id, $configs, $attach->params); $thumbName = \Components\Publications\Helpers\Html::createThumbName(basename($fpath), $this->_config->params->thumbSuffix, $this->_config->params->thumbFormat); $thumbPath = dirname($fpath) . DS . $thumbName; if (is_file(PATH_APP . DS . $fpath) && is_file(PATH_APP . DS . $thumbPath)) { // Get extentsion $ext = Filesystem::extension(PATH_APP . DS . $fpath); $title = $attach->title ? $attach->title : basename($attach->path); $link = Route::url($pub->link('versionid')) . '/Image:' . basename($fpath); $rel = $ext == 'swf' || $ext == 'mov' ? '' : ' rel="lightbox"'; $class = $ext == 'swf' || $ext == 'mov' ? ' class="video"' : ''; $html .= ' <a ' . $class . ' ' . $rel . ' href="' . $link . '" title="' . $title . '">'; $html .= '<img src="' . Route::url($pub->link('versionid')) . '/Image:' . $thumbName . '" alt="' . $title . '" class="thumbima" /></a>'; $i++; } } if ($i > 0) { $view = new \Hubzero\Component\View(array('base_path' => PATH_CORE . DS . 'components' . DS . 'com_publications' . DS . 'site', 'name' => 'view', 'layout' => '_gallery')); $view->content = $html; return $view->loadTemplate(); } return; }