00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Adminhtml_Block_Media_Uploader extends Mage_Adminhtml_Block_Widget
00035 {
00036
00037 protected $_config;
00038
00039 public function __construct()
00040 {
00041 parent::__construct();
00042 $this->setId($this->getId() . '_Uploader');
00043 $this->setTemplate('media/uploader.phtml');
00044 $this->getConfig()->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()->getUrl('*/*/upload'));
00045 $this->getConfig()->setParams(array('form_key' => $this->getFormKey()));
00046 $this->getConfig()->setFileField('file');
00047 $this->getConfig()->setFilters(array(
00048 'images' => array(
00049 'label' => Mage::helper('adminhtml')->__('Images (.gif, .jpg, .png)'),
00050 'files' => array('*.gif', '*.jpg', '*.png')
00051 ),
00052 'media' => array(
00053 'label' => Mage::helper('adminhtml')->__('Media (.avi, .flv, .swf)'),
00054 'files' => array('*.avi', '*.flv', '*.swf')
00055 ),
00056 'all' => array(
00057 'label' => Mage::helper('adminhtml')->__('All Files'),
00058 'files' => array('*.*')
00059 )
00060 ));
00061 }
00062
00063 protected function _prepareLayout()
00064 {
00065 $this->setChild(
00066 'browse_button',
00067 $this->getLayout()->createBlock('adminhtml/widget_button')
00068 ->addData(array(
00069 'id' => $this->_getButtonId('browse'),
00070 'label' => Mage::helper('adminhtml')->__('Browse Files...'),
00071 'type' => 'button',
00072 'onclick' => $this->getJsObjectName() . '.browse()'
00073 ))
00074 );
00075
00076 $this->setChild(
00077 'upload_button',
00078 $this->getLayout()->createBlock('adminhtml/widget_button')
00079 ->addData(array(
00080 'id' => $this->_getButtonId('upload'),
00081 'label' => Mage::helper('adminhtml')->__('Upload Files'),
00082 'type' => 'button',
00083 'onclick' => $this->getJsObjectName() . '.upload()'
00084 ))
00085 );
00086
00087 $this->setChild(
00088 'delete_button',
00089 $this->getLayout()->createBlock('adminhtml/widget_button')
00090 ->addData(array(
00091 'id' => '{{id}}-delete',
00092 'class' => 'delete',
00093 'type' => 'button',
00094 'label' => Mage::helper('adminhtml')->__('Remove'),
00095 'onclick' => $this->getJsObjectName() . '.removeFile(\'{{fileId}}\')'
00096 ))
00097 );
00098
00099 return parent::_prepareLayout();
00100 }
00101
00102 protected function _getButtonId($buttonName)
00103 {
00104 return $this->getHtmlId() . '-' . $buttonName;
00105 }
00106
00107 public function getBrowseButtonHtml()
00108 {
00109 return $this->getChildHtml('browse_button');
00110 }
00111
00112 public function getUploadButtonHtml()
00113 {
00114 return $this->getChildHtml('upload_button');
00115 }
00116
00117 public function getDeleteButtonHtml()
00118 {
00119 return $this->getChildHtml('delete_button');
00120 }
00121
00122
00123
00124
00125
00126
00127 public function getJsObjectName()
00128 {
00129 return $this->getHtmlId() . 'JsObject';
00130 }
00131
00132
00133
00134
00135
00136
00137 public function getConfigJson()
00138 {
00139 return Zend_Json::encode($this->getConfig()->getData());
00140 }
00141
00142
00143
00144
00145
00146
00147 public function getConfig()
00148 {
00149 if(is_null($this->_config)) {
00150 $this->_config = new Varien_Object();
00151 }
00152
00153 return $this->_config;
00154 }
00155
00156 public function getPostMaxSize()
00157 {
00158 return ini_get('post_max_size');
00159 }
00160
00161 public function getUploadMaxSize()
00162 {
00163 return ini_get('upload_max_filesize');
00164 }
00165
00166 public function getDataMaxSize()
00167 {
00168 return min($this->getPostMaxSize(), $this->getUploadMaxSize());
00169 }
00170
00171 public function getDataMaxSizeInBytes()
00172 {
00173 $iniSize = $this->getDataMaxSize();
00174 $size = substr($iniSize, 0, strlen($iniSize)-1);
00175 $parsedSize = 0;
00176 switch (strtolower(substr($iniSize, strlen($iniSize)-1))) {
00177 case 't':
00178 $parsedSize = $size*(1024*1024*1024*1024);
00179 break;
00180 case 'g':
00181 $parsedSize = $size*(1024*1024*1024);
00182 break;
00183 case 'm':
00184 $parsedSize = $size*(1024*1024);
00185 break;
00186 case 'k':
00187 $parsedSize = $size*1024;
00188 break;
00189 case 'b':
00190 default:
00191 $parsedSize = $size;
00192 break;
00193 }
00194 return $parsedSize;
00195 }
00196
00197 }