/** * Add/edit file attachment * * * @return boolean or error */ public function addAttachment($filePath, $pub, $configs, $uid, $elementId, $element, $ordering = 1) { // Need to check against allowed types if ($configs->check) { if (!$this->checkAllowed(array($filePath), $element->typeParams->allowed_ext)) { return false; } } // Get latest Git hash $vcs_hash = $this->_git->gitLog($filePath, '', 'hash'); $new = 0; $update = 0; $objPA = new \Components\Publications\Tables\Attachment($this->_parent->_db); if ($objPA->loadElementAttachment($pub->version_id, array('path' => $filePath), $elementId, $this->_name, $element->role)) { // Update if new hash if ($vcs_hash && $vcs_hash != $objPA->vcs_hash) { $objPA->vcs_hash = $vcs_hash; $objPA->modified_by = $uid; $objPA->modified = Date::toSql(); $update = 1; // Copy file again (new version) // Reflect the update in curation record $this->_parent->set('_update', 1); } } else { $new = 1; $objPA->publication_id = $pub->id; $objPA->publication_version_id = $pub->version_id; $objPA->path = $filePath; $objPA->type = $this->_name; $objPA->vcs_hash = $vcs_hash; $objPA->created_by = $uid; $objPA->created = Date::toSql(); $objPA->role = $element->role; // Reflect the update in curation record $this->_parent->set('_update', 1); } $objPA->element_id = $elementId; $objPA->ordering = $ordering; // Copy file from project repo into publication directory if ($objPA->store()) { // Check for conflict in file name if ($new == 1) { $suffix = $this->checkForDuplicate($configs->path . DS . $filePath, $objPA, $configs); if ($suffix) { $pa = new \Components\Publications\Tables\Attachment($this->_parent->_db); $pa->saveParam($objPA, 'suffix', $suffix); } } // Copy file over to where to belongs if (!$this->publishAttachment($objPA, $pub, $configs, $update)) { return false; } // Make default image (if applicable) if ($configs->handler && $configs->handler->getName() == 'imageviewer') { $currentDefault = new \Components\Publications\Tables\Attachment($this->_parent->_db); if (!$currentDefault->getDefault($pub->version_id)) { $configs->handler->makeDefault($objPA, $pub, $configs); } } return true; } return false; }
/** * Add/edit attachment * * * @return boolean or error */ public function addAttachment($database_name, $pub, $configs, $uid, $elementId, $element, $ordering = 1) { // Get database object and load record $objData = new \Components\Projects\Tables\Database($this->_parent->_db); $objData->loadRecord($database_name); $dbVersion = NULL; $objPA = new \Components\Publications\Tables\Attachment($this->_parent->_db); if ($objPA->loadElementAttachment($pub->version_id, array('object_name' => $database_name), $elementId, $this->_name, $element->role)) { // Already attached $new = 0; if (!$objData->id) { // Original got deleted, can't do much return true; } } else { if (!$objData->id) { // Original database not found $this->setError(Lang::txt('Oups! Cannot attach selected database: database not found')); return false; } $objPA->publication_id = $pub->id; $objPA->publication_version_id = $pub->version_id; $objPA->type = $this->_name; $objPA->created_by = $uid; $objPA->created = Date::toSql(); $objPA->role = $element->role; $new = 1; // Reflect the update in curation record $this->_parent->set('_update', 1); } if ($new) { $result = Event::trigger('projects.clone_database', array($database_name, $pub->_project, $configs->servePath)); $dbVersion = $result && isset($result[0]) ? $result[0] : NULL; } else { $rtime = $objPA->modified ? strtotime($objPA->modified) : NULL; if ($objPA->object_id != $objData->id || strtotime($objData->updated) > $rtime) { // New database instance - need to clone again and get a new version number $result = Event::trigger('projects.clone_database', array($database_name, $pub->_project, $configs->servePath)); $dbVersion = $result && isset($result[0]) ? $result[0] : NULL; $objPA->modified_by = $uid; $objPA->modified = Date::toSql(); } else { // No changes $dbVersion = $objPA->object_revision; } } // Failed to clone if (!$dbVersion) { $this->_parent->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_FAILED_DB_CLONE')); return false; } $objPA->object_id = $objData->id; $objPA->object_name = $database_name; $objPA->object_revision = $dbVersion; $objPA->element_id = $elementId; $objPA->ordering = $ordering; $objPA->title = $objPA->title ? $objPA->title : $objData->title; // Build link path $objPA->path = 'dataviewer' . DS . 'view' . DS . 'publication:dsl' . DS . $database_name . DS . '?v=' . $dbVersion; if (!$objPA->store()) { $this->_parent->setError(Lang::txt('There was a problem attaching the database')); return false; } // Determine accompanying files and copy them in the right location $this->publishDataFiles($objData, $configs); return true; }
/** * Add/edit file attachment * * @param integer $id * @param string $title * @param object $pub * @param object $configs * @param integer $uid * @param integer $elementId * @param object $element * @param integer $ordering * @return boolean */ public function addAttachment($id, $title, $pub, $configs, $uid, $elementId, $element, $ordering = 1) { // Need to check against allowed types $accept = isset($element->typeParams->accept) ? $element->typeParams->accept : NULL; if ($configs->check) { if (!$this->checkAllowed($id, $accept)) { return false; } } $path = rtrim(Request::base(), '/') . '/' . ltrim(Route::url('index.php?option=com_publications&id=' . $id), '/'); $objPA = new \Components\Publications\Tables\Attachment($this->_parent->_db); if ($objPA->loadElementAttachment($pub->version_id, array('path' => $path), $elementId, $this->_name, $element->role)) { // Link already attached $this->setError(Lang::txt('The publication is already attached')); return true; } else { $objPA->publication_id = $pub->id; $objPA->publication_version_id = $pub->version_id; $objPA->path = $path; $objPA->type = $this->_name; $objPA->created_by = $uid; $objPA->created = Date::toSql(); $objPA->role = $element->role; $objPA->title = $title; $objPA->object_id = $id; $objPA->object_name = $this->_name; // Reflect the update in curation record $this->_parent->set('_update', 1); } $objPA->element_id = $elementId; $objPA->ordering = $ordering; if (!$objPA->store()) { $this->setError(Lang::txt('There was a problem attaching the publication')); return false; } return true; }