Last active
August 29, 2015 14:06
-
-
Save Daniel-KM/b91f7f94182e50538149 to your computer and use it in GitHub Desktop.
Example to use to add one or multiple specific derivative types in Omeka, for example mini square thumbnails (see https://github.com/omeka/Omeka/pull/638).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Specific Derivative | |
* | |
* Example to use to add one or multiple specific derivative types in Omeka, for | |
* example mini square thumbnails. | |
* | |
* It creates the directory automatically. If the path exists, this plugin is | |
* useless, except the form to set constraints of the specific types. | |
* | |
* To use it, you need to install the patch presented in the page below and to | |
* add in "config.ini": | |
* | |
* fileDerivatives.paths.square_mini_thumbnail = "square_mini_thumbnails" | |
* | |
* @see https://github.com/omeka/Omeka/pull/638 | |
* | |
* @copyright Daniel Berthereau, 2014 | |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt | |
* @package SpecificDerivative | |
*/ | |
/** | |
* The Specific Derivative plugin. | |
* @package Omeka\Plugins\SpecificDerivative | |
*/ | |
class SpecificDerivativePlugin extends Omeka_Plugin_AbstractPlugin | |
{ | |
/** | |
* @var array Hooks for the plugin. | |
*/ | |
protected $_hooks = array( | |
'install', | |
'uninstall', | |
'appearance_settings_form', | |
); | |
/** | |
* @var array Options and their default values. | |
*/ | |
protected $_options = array( | |
'square_mini_thumbnail_constraint' => 32, | |
); | |
/** | |
* @var array Paths for specific types. | |
*/ | |
protected $_pathsByType = array( | |
'square_mini_thumbnail' => 'square_mini_thumbnails', | |
); | |
/** | |
* Installs the plugin. | |
*/ | |
public function hookInstall() | |
{ | |
try { | |
$this->_installPaths(); | |
} | |
catch (Exception $e) { | |
throw new Omeka_Plugin_Installer_Exception($e->getMessage()); | |
} | |
$this->_installOptions(); | |
} | |
/** | |
* Uninstalls the plugin. | |
*/ | |
public function hookUninstall() | |
{ | |
$this->_uninstallPaths(); | |
$this->_uninstallOptions(); | |
} | |
/** | |
* Add forms for apparence. | |
* | |
* To add a derivative type, it should be set in `config.ini` in option `fileDerivatives.paths.xxx`. | |
* The constraint is not obligatory and it can be anything. | |
* | |
* @return void | |
*/ | |
public function hookAppearanceSettingsForm($args) | |
{ | |
$form = $args['form']; | |
$form->addElement('text', 'square_mini_thumbnail_constraint', array( | |
'label' => __('My Square Mini Thumbnail Size'), | |
'description' => __('Maximum square mini thumbnail size constraint (in pixels).'), | |
'validators' => array('Digits'), | |
'required' => true, | |
'order' => 2, | |
)); | |
$form->setDefaults(array( | |
'square_mini_thumbnail_constraint' => get_option('square_mini_thumbnail_constraint'), | |
)); | |
} | |
/** | |
* Create paths for specific derivative types. | |
* | |
* @return void Exception if an error occurs. | |
*/ | |
protected function _installPaths() | |
{ | |
$fileDerivatives = Zend_Registry::get('bootstrap')->getResource('Config')->fileDerivatives; | |
if (empty($fileDerivatives) || empty($fileDerivatives->paths)) { | |
throw new Exception(__('"config.ini" does not contain paths for derivative files.')); | |
} | |
$configPathsByType = $fileDerivatives->paths->toArray(); | |
foreach ($this->_pathsByType as $type => $path) { | |
if (!isset($configPathsByType[$type])) { | |
throw new Exception(__('"config.ini" does not contain path for the derivative type "%s".', $type)); | |
} | |
$dirpath = FILES_DIR . DIRECTORY_SEPARATOR . $path; | |
if (!realpath($dirpath)) { | |
try { | |
$this->_createFolder($dirpath); | |
// For security reason, an index.html is added. | |
copy( | |
FILES_DIR . DIRECTORY_SEPARATOR . $configPathsByType['original'] . DIRECTORY_SEPARATOR . 'index.html', | |
$dirpath . DIRECTORY_SEPARATOR . 'index.html' | |
); | |
} catch (Omeka_Storage_Exception $e) { | |
throw new Exception($e->getMessage()); | |
} catch (Exception $e) { | |
throw new Exception(__('Error when creating folder "%s" for type "%s".', $path, $type)); | |
} | |
} | |
} | |
} | |
/** | |
* Remove paths for specific derivative types. | |
* | |
* @return void | |
*/ | |
protected function _uninstallPaths() | |
{ | |
foreach ($this->_pathsByType as $type => $path) { | |
$dirpath = FILES_DIR . DIRECTORY_SEPARATOR . $path; | |
$this->_removeFolder($dirpath); | |
} | |
} | |
/** | |
* Checks and creates a folder. | |
* | |
* @param string $path Full path of the folder to create. | |
* | |
* @return void Exception if an error occurs. | |
*/ | |
private function _createFolder($path) | |
{ | |
if ($path != '') { | |
if (file_exists($path)) { | |
if (is_dir($path)) { | |
if (is_writable($path)) { | |
return; | |
} | |
throw new Omeka_Storage_Exception(__('Error directory non writable: "%s".', $path)); | |
} | |
throw new Omeka_Storage_Exception(__('Failed to create folder "%s": a file with the same name exists...', $path)); | |
} | |
if (!@mkdir($path, 0755, true)) { | |
throw new Omeka_Storage_Exception(__('Error making directory: "%s".', $path)); | |
} | |
} | |
} | |
/** | |
* Checks and removes a folder. | |
* | |
* @param string $path Full path of the folder to remove. | |
* | |
* @return void. | |
*/ | |
private function _removeFolder($path) | |
{ | |
foreach(glob("{$path}/*") as $file) { | |
if (is_dir($file)) { | |
$this->_removeFolder($file); | |
} | |
else { | |
@unlink($file); | |
} | |
} | |
@rmdir($path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment