self::MIN_HEIGHT, 'minwidth' => self::MIN_WIDTH, 'maxheight' => self::MAX_HEIGHT, 'maxwidth' => self::MAX_WIDTH, ); protected $_allowedExtensions = array('jpg', 'gif', 'png'); private $_attributeImageDeletePath = 'adminhtml/colorpicker_media/delete/index'; private $_attributeImageUploadPath = 'adminhtml/colorpicker_media/upload/index'; public function getAllowedImageExtensions() { return $this->_allowedExtensions; } public function getBaseUrl() { return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . self::MEDIA_PATH; } public static function getBaseDir() { return Mage::getBaseDir('media') . DS . str_replace("/", DS, self::MEDIA_PATH); } public function getDeleteUrl($optionId, $attribute_id) { /** * @var $sesParam Mage_Adminhtml_Model_Url */ $sesParam = Mage::getModel('adminhtml/url')->addSessionParam(); $url = $sesParam->getUrl( $this->_attributeImageDeletePath, array( 'attribute_id' => (int) $attribute_id, 'option_id' => (int) $optionId ) ); return $url; } public function getUploadPath() { return $this->_attributeImageUploadPath; } public function upload($attributeId, $optionId) { $path = $this->getBaseDir() . DS . $attributeId . DS; $url = $this->getBaseUrl() . '/' . $attributeId . '/'; try { $uploader = new Mage_Core_Model_File_Uploader("file"); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(false); $uploader->setAllowCreateFolders(true); $uploader->setAllowedExtensions(array('gif', 'jpg', 'png')); //server-side validation of extension $result = $uploader->save($path); /** * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS */ $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']); $result['path'] = str_replace(DS, "/", $result['path']); $result['url'] = $url . $result['file']; $result['html_id'] = Mage::app()->getRequest()->getPost('html_id'); if (isset($result['file'])) { $result = array_merge_recursive($this->delete($optionId), $result); $this->_saveFileNameToDb($optionId, $attributeId, $result['file']); $this->resizePack($result['file'], $attributeId); } } catch(Exception $e) { $result = array( "error" => $e->getMessage(), "errorCode" => $e->getCode(), "status" => "error" ); } return $result; } public function deleteAttributeDirectory($attributeId) { if (!empty($attributeId) && $this->removePack($attributeId, null, true)) { return true; } return false; } /** * @param $optionId * @return array */ public function delete($optionId) { if (empty($optionId)) { return array(); } $optionModel = Mage::getModel('orange35_colorpicker/option'); $optionModel->load($optionId); $imageName = $optionModel->getData('image'); if (empty($imageName)) { return array(); } $attributeId = (int) $optionModel->getData('attribute_id'); $path = self::getBaseDir() . DS . $attributeId . DS; $imagePath = rtrim($path, DS) . DS . ltrim($imageName, DS); if ($this->removePack($attributeId, $imageName)) { $optionModel->setData('image', ''); $optionModel->save(); $result = array("status" => "success"); } else { $result = array('status' => 'error', 'error' => 'can not remove image ' . $imagePath); } return $result; } public function getSwatchImageUrl($imageName, $attributeId) { return $this->_getImageUrl( $imageName, $attributeId, $this->_getProductSwatchWidth(), $this->_getProductSwatchHeight() ); } public function getListSwatchImageUrl($imageName, $attributeId) { return $this->_getImageUrl( $imageName, $attributeId, $this->_getProductListSwatchWidth(), $this->_getProductListSwatchHeight() ); } public function getPopupSwatchImageUrl($imageName, $attributeId) { return $this->_getImageUrl( $imageName, $attributeId, $this->_getProductPopupSwatchWidth(), $this->_getProductPopupSwatchHeight() ); } public function getMiniImageUrl($imageName, $attributeId) { return $this->_getImageUrl($imageName, $attributeId, $this->_widthMini, $this->_heightMini); } public function getAdminPreviewImageUrl($imageName, $attributeId) { return $this->_getImageUrl($imageName, $attributeId, $this->_widthAdminPreview, $this->_heightAdminPreview); } /** * @param $imageName * @param $attributeId * @return $this|bool */ public function resizePack($imageName, $attributeId) { if (!$imageName) { return false; } $this->resizeCropThumb( $imageName, $attributeId, $this->_getProductSwatchWidth(), $this->_getProductSwatchHeight() ); $this->resizeCropThumb( $imageName, $attributeId, $this->_getProductListSwatchWidth(), $this->_getProductListSwatchHeight() ); return $this; } /** * Removes folder with cached images * * @return boolean */ public function flushImagesCache() { //todo: викликати це чудо з конфігу $cacheDir = $this->getBaseDir() . DS . 'cache' . DS ; $io = new Varien_Io_File(); if ($io->fileExists($cacheDir, false) ) { return $io->rmdir($cacheDir, true); } return true; } /** * @param $imageName * @param $attributeId * @param $width * @param $height * @return bool */ protected function resizeCropThumb($imageName, $attributeId, $width, $height) { $width = (int) $width; $height = (int) $height; $size = $this->_getSizePrefix($width, $height); $cacheDir = $this->_getCacheDir($attributeId, $size); $io = new Varien_Io_File(); $io->checkAndCreateFolder($cacheDir); $io->open(array('path' => $cacheDir)); if ($io->fileExists($imageName)) { return true; } try { $image = new Varien_Image($this->getBaseDir() . DS . $attributeId . DS . $imageName); $image->constrainOnly(false); $image->keepFrame(true); $image->backgroundColor(array(255,255,255)); $image->keepAspectRatio(true); $image->resize($width, $height); $image->save($cacheDir . DS . $imageName); return true; } catch (Exception $e) { Mage::logException($e); return false; } } /** * Remove image by filePath * * @param string $path * @return bool */ private function remove($path, $onlyFile = true) { $io = new Varien_Io_File(); $io->open(array('path' => $this->getBaseDir())); if ($io->fileExists($path, $onlyFile)) { return $io->rm($path); } return false; } private function removePack($attributeId, $imageName = null, $removeDirectory = false) { $sizes = array( $this->_getSizePrefix($this->_getProductSwatchWidth(), $this->_getProductSwatchHeight()), $this->_getSizePrefix($this->_getProductListSwatchWidth(), $this->_getProductListSwatchHeight()) ); foreach ($sizes as $size) { if (!empty($imageName)) { $this->remove($this->_getCacheDir($attributeId, $size) . DS . $imageName); } if ($removeDirectory) { $this->remove($this->_getCacheDir($attributeId, $size)); } } return $this->remove($this->getBaseDir() . DS . $attributeId . DS . $imageName); } /** * @param $optionId * @param $attributeId * @param $fileName */ private function _saveFileNameToDb($optionId, $attributeId, $fileName) { $optionModel = Mage::getModel('orange35_colorpicker/option'); $optionModel->load($optionId); $optionModel->addData( array( 'option_id' => $optionId, 'attribute_id' => $attributeId, 'image' => $fileName ) ); $optionModel->save(); } /** * @param $width * @param $height * @return string */ protected function _getSizePrefix($width, $height) { return 'cache/' . $width . 'x' . $height; } /** * @return int */ protected function _getProductSwatchHeight() { return (int) Mage::getStoreConfig('colorpicker_section/product_view_group/product_swatch_height'); } /** * @return int */ protected function _getProductSwatchWidth() { return (int) Mage::getStoreConfig('colorpicker_section/product_view_group/product_swatch_width'); } /** * @return int */ protected function _getProductListSwatchHeight() { return (int) Mage::getStoreConfig('colorpicker_section/product_list_group/product_list_swatch_height'); } /** * @return int */ protected function _getProductListSwatchWidth() { return (int) Mage::getStoreConfig('colorpicker_section/product_list_group/product_list_swatch_width'); } /** * @return int */ protected function _getProductPopupSwatchHeight() { return (int) Mage::getStoreConfig('colorpicker_section/product_view_group/product_tooltip_height'); } /** * @return int */ protected function _getProductPopupSwatchWidth() { return (int) Mage::getStoreConfig('colorpicker_section/product_view_group/product_tooltip_width'); } protected function _getCacheDir($attributeId, $size) { return $this->getBaseDir() . DS . $size . DS . $attributeId; } protected function _getImageUrl($imageName, $attributeId, $width, $height) { $this->resizeCropThumb($imageName, $attributeId, $width, $height); return $this->getBaseUrl() . '/' . $this->_getSizePrefix($width, $height) . '/' . $attributeId . '/' . $imageName; } }