/** * 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; }
/** * Remove databases used in a draft publications * * @param integer $identifier Database ID or name * @param object $project Project object * @param integer $version Database version * @return bool */ public function remove_database($identifier = 0, $project = NULL, $version = NULL) { if (!$identifier || $project == NULL) { $this->setError(Lang::txt('PLG_PROJECTS_DATABASES_ERROR_MISSING_ID')); return false; } if ($version === NULL || trim($version) == '') { $this->setError(Lang::txt('PLG_PROJECTS_DATABASES_ERROR_INVALID_VERSION')); return false; } $db = App::get('db'); $ds_db = $this->get_ds_db($project->get('id')); // Load database record $objPD = new \Components\Projects\Tables\Database($db); if (!$objPD->loadRecord($identifier)) { $this->setError(Lang::txt('PLG_PROJECTS_DATABASES_ERROR_LOAD_RECORD')); return false; } // Remove record from database versions table $sql = 'DELETE FROM #__project_database_versions WHERE' . ' database_name = ' . $db->quote($objPD->database_name) . ' AND version = ' . $db->quote($version); $db->setQuery($sql); $db->query(); // Remove database table $table_name = $objPD->database_name . '_' . $version; $sql = 'DROP TABLE ' . $table_name; $ds_db->setQuery($sql); return $ds_db->query(); }