*/ class Mage_Adminhtml_Block_Media_Uploader extends Mage_Adminhtml_Block_Widget { protected $_config; public function __construct() { parent::__construct(); $this->setId($this->getId() . '_Uploader'); $this->setTemplate('media/uploader.phtml'); $this->getConfig()->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()->getUrl('*/*/upload')); $this->getConfig()->setParams(array('form_key' => $this->getFormKey())); $this->getConfig()->setFileField('file'); $this->getConfig()->setFilters(array( 'images' => array( 'label' => Mage::helper('adminhtml')->__('Images (.gif, .jpg, .png)'), 'files' => array('*.gif', '*.jpg', '*.png') ), 'media' => array( 'label' => Mage::helper('adminhtml')->__('Media (.avi, .flv, .swf)'), 'files' => array('*.avi', '*.flv', '*.swf') ), 'all' => array( 'label' => Mage::helper('adminhtml')->__('All Files'), 'files' => array('*.*') ) )); } protected function _prepareLayout() { $this->setChild( 'browse_button', $this->getLayout()->createBlock('adminhtml/widget_button') ->addData(array( 'id' => $this->_getButtonId('browse'), 'label' => Mage::helper('adminhtml')->__('Browse Files...'), 'type' => 'button', 'onclick' => $this->getJsObjectName() . '.browse()' )) ); $this->setChild( 'upload_button', $this->getLayout()->createBlock('adminhtml/widget_button') ->addData(array( 'id' => $this->_getButtonId('upload'), 'label' => Mage::helper('adminhtml')->__('Upload Files'), 'type' => 'button', 'onclick' => $this->getJsObjectName() . '.upload()' )) ); $this->setChild( 'delete_button', $this->getLayout()->createBlock('adminhtml/widget_button') ->addData(array( 'id' => '{{id}}-delete', 'class' => 'delete', 'type' => 'button', 'label' => Mage::helper('adminhtml')->__('Remove'), 'onclick' => $this->getJsObjectName() . '.removeFile(\'{{fileId}}\')' )) ); return parent::_prepareLayout(); } protected function _getButtonId($buttonName) { return $this->getHtmlId() . '-' . $buttonName; } public function getBrowseButtonHtml() { return $this->getChildHtml('browse_button'); } public function getUploadButtonHtml() { return $this->getChildHtml('upload_button'); } public function getDeleteButtonHtml() { return $this->getChildHtml('delete_button'); } /** * Retrive uploader js object name * * @return string */ public function getJsObjectName() { return $this->getHtmlId() . 'JsObject'; } /** * Retrive config json * * @return string */ public function getConfigJson() { return Mage::helper('core')->jsonEncode($this->getConfig()->getData()); } /** * Retrive config object * * @return Varien_Config */ public function getConfig() { if(is_null($this->_config)) { $this->_config = new Varien_Object(); } return $this->_config; } public function getPostMaxSize() { return ini_get('post_max_size'); } public function getUploadMaxSize() { return ini_get('upload_max_filesize'); } public function getDataMaxSize() { return min($this->getPostMaxSize(), $this->getUploadMaxSize()); } public function getDataMaxSizeInBytes() { $iniSize = $this->getDataMaxSize(); $size = substr($iniSize, 0, strlen($iniSize)-1); $parsedSize = 0; switch (strtolower(substr($iniSize, strlen($iniSize)-1))) { case 't': $parsedSize = $size*(1024*1024*1024*1024); break; case 'g': $parsedSize = $size*(1024*1024*1024); break; case 'm': $parsedSize = $size*(1024*1024); break; case 'k': $parsedSize = $size*1024; break; case 'b': default: $parsedSize = $size; break; } return $parsedSize; } /** * Retrieve full uploader SWF's file URL * Implemented to solve problem with cross domain SWFs * Now uploader can be only in the same URL where backend located * * @param string $url url to uploader in current theme * * @return string full URL */ public function getUploaderUrl($url) { if (!is_string($url)) { $url = ''; } $design = Mage::getDesign(); $theme = $design->getTheme('skin'); if (empty($url) || !$design->validateFile($url, array('_type' => 'skin', '_theme' => $theme))) { $theme = $design->getDefaultTheme(); } return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) . 'skin/' . $design->getArea() . '/' . $design->getPackageName() . '/' . $theme . '/' . $url; } }