/** * 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; }
/** * Make image default for publication * * @return void */ public function makeDefault($row, $pub, $configs) { // Make sure we got config if (!$this->_config) { $this->getConfig(); } // TBD - to come from component configs $defaultMasterName = 'master.png'; $defaultThumbName = 'thumb.gif'; $path = $this->getFilePath($row->path, $row->id, $configs, $row->params); // No file found if (!is_file($path)) { return false; } // Check if image if (!getimagesize($path)) { return false; } $copyToThumb = $configs->pubBase . DS . $defaultThumbName; $copyToMaster = $configs->pubBase . DS . $defaultMasterName; $thumbName = \Components\Publications\Helpers\Html::createThumbName(basename($path), $this->_config->params->thumbSuffix, $this->_config->params->thumbFormat); $thumbPath = dirname($path) . DS . $thumbName; // Copy to master if (is_file($path)) { Filesystem::copy($path, $copyToMaster); // Create/update thumb Filesystem::copy($path, $copyToThumb); $hi = new \Hubzero\Image\Processor($copyToThumb); if (count($hi->getErrors()) == 0) { $hi->resize(100, false, true, true); $hi->save($copyToThumb); } } else { return false; } // Get current default $currentDefault = new \Components\Publications\Tables\Attachment($this->_parent->_db); $currentDefault->getDefault($row->publication_version_id); // Unmark as default if ($currentDefault->id) { $currentDefault->saveParam($currentDefault, 'pubThumb', ''); } // Mark this image as default $currentDefault->saveParam($row, 'pubThumb', '1'); return true; }