* @copyright 2007-2013 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) exit; class Watermark extends Module { private $_html = ''; private $_postErrors = array(); private $xaligns = array('left', 'middle', 'right'); private $yaligns = array('top', 'middle', 'bottom'); private $yAlign; private $xAlign; private $transparency; private $imageTypes = array(); private $watermarkTypes; public function __construct() { $this->name = 'watermark'; $this->tab = 'administration'; $this->version = '0.3'; $this->author = 'PrestaShop'; parent::__construct(); $config = Configuration::getMultiple(array('WATERMARK_TYPES', 'WATERMARK_Y_ALIGN', 'WATERMARK_X_ALIGN', 'WATERMARK_TRANSPARENCY')); if (!isset($config['WATERMARK_TYPES'])) $config['WATERMARK_TYPES'] = ''; $tmp = explode(',', $config['WATERMARK_TYPES']); foreach (ImageType::getImagesTypes('products') as $type) if (in_array($type['id_image_type'], $tmp)) $this->imageTypes[] = $type; $this->yAlign = isset($config['WATERMARK_Y_ALIGN']) ? $config['WATERMARK_Y_ALIGN'] : ''; $this->xAlign = isset($config['WATERMARK_X_ALIGN']) ? $config['WATERMARK_X_ALIGN'] : ''; $this->transparency = isset($config['WATERMARK_TRANSPARENCY']) ? $config['WATERMARK_TRANSPARENCY'] : 60; $this->displayName = $this->l('Watermark'); $this->description = $this->l('Protect image by watermark.'); $this->confirmUninstall = $this->l('Are you sure you want to delete your details ?'); if (!isset($this->transparency) || !isset($this->xAlign) || !isset($this->yAlign)) $this->warning = $this->l('Watermark image must be uploaded in order for this module to work correctly.'); } public function install() { if (!parent::install() || !$this->registerHook('watermark')) return false; Configuration::updateValue('WATERMARK_TRANSPARENCY', 60); Configuration::updateValue('WATERMARK_Y_ALIGN', 'bottom'); Configuration::updateValue('WATERMARK_X_ALIGN', 'right'); return true; } public function uninstall() { return (parent::uninstall() && Configuration::deleteByName('WATERMARK_TYPES') && Configuration::deleteByName('WATERMARK_TRANSPARENCY') && Configuration::deleteByName('WATERMARK_Y_ALIGN') && Configuration::deleteByName('WATERMARK_X_ALIGN')); } private function _postValidation() { $yalign = Tools::getValue('yalign'); $xalign = Tools::getValue('xalign'); $transparency = (int)(Tools::getValue('transparency')); $image_types = Tools::getValue('image_types'); if (empty($transparency)) $this->_postErrors[] = $this->l('Transparency required.'); elseif ($transparency < 0 || $transparency > 100) $this->_postErrors[] = $this->l('Transparency is not in allowed range.'); if (empty($yalign)) $this->_postErrors[] = $this->l('Y-Align is required.'); elseif (!in_array($yalign, $this->yaligns)) $this->_postErrors[] = $this->l('Y-Align is not in allowed range.'); if (empty($xalign)) $this->_postErrors[] = $this->l('X-Align is required.'); elseif (!in_array($xalign, $this->xaligns)) $this->_postErrors[] = $this->l('X-Align is not in allowed range.'); if (empty($image_types)) $this->_postErrors[] = $this->l('At least one image type is required.'); if (isset($_FILES['PS_WATERMARK']['tmp_name']) && !empty($_FILES['PS_WATERMARK']['tmp_name'])) { if (!ImageManager::isRealImage($_FILES['PS_WATERMARK']['tmp_name'], $_FILES['PS_WATERMARK']['type'], array('image/gif'))) $this->_postErrors[] = $this->l('Image must be in GIF format.'); } return !count($this->_postErrors) ? true : false; } private function _postProcess() { Configuration::updateValue('WATERMARK_TYPES', implode(',', Tools::getValue('image_types'))); Configuration::updateValue('WATERMARK_Y_ALIGN', Tools::getValue('yalign')); Configuration::updateValue('WATERMARK_X_ALIGN', Tools::getValue('xalign')); Configuration::updateValue('WATERMARK_TRANSPARENCY', Tools::getValue('transparency')); if (Shop::getContext() == Shop::CONTEXT_SHOP) $str_shop = '-'.(int)$this->context->shop->id; else $str_shop = ''; //submited watermark if (isset($_FILES['PS_WATERMARK']) && !empty($_FILES['PS_WATERMARK']['tmp_name'])) { /* Check watermark validity */ if ($error = ImageManager::validateUpload($_FILES['PS_WATERMARK'])) $this->_errors[] = $error; /* Copy new watermark */ elseif (!copy($_FILES['PS_WATERMARK']['tmp_name'], dirname(__FILE__).'/watermark'.$str_shop.'.gif')) $this->_errors[] = sprintf($this->l('An error occurred while uploading watermark: %1$s to %2$s'), $_FILES['PS_WATERMARK']['tmp_name'], dirname(__FILE__).'/watermark'.$str_shop.'.gif'); } if ($this->_errors) foreach ($this->_errors as $error) $this->_html .= '