1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Adminhtml_Block_Media_Uploader extends Mage_Adminhtml_Block_Widget
35: {
36:
37: protected $_config;
38:
39: public function __construct()
40: {
41: parent::__construct();
42: $this->setId($this->getId() . '_Uploader');
43: $this->setTemplate('media/uploader.phtml');
44: $this->getConfig()->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()->getUrl('*/*/upload'));
45: $this->getConfig()->setParams(array('form_key' => $this->getFormKey()));
46: $this->getConfig()->setFileField('file');
47: $this->getConfig()->setFilters(array(
48: 'images' => array(
49: 'label' => Mage::helper('adminhtml')->__('Images (.gif, .jpg, .png)'),
50: 'files' => array('*.gif', '*.jpg', '*.png')
51: ),
52: 'media' => array(
53: 'label' => Mage::helper('adminhtml')->__('Media (.avi, .flv, .swf)'),
54: 'files' => array('*.avi', '*.flv', '*.swf')
55: ),
56: 'all' => array(
57: 'label' => Mage::helper('adminhtml')->__('All Files'),
58: 'files' => array('*.*')
59: )
60: ));
61: }
62:
63: protected function _prepareLayout()
64: {
65: $this->setChild(
66: 'browse_button',
67: $this->getLayout()->createBlock('adminhtml/widget_button')
68: ->addData(array(
69: 'id' => $this->_getButtonId('browse'),
70: 'label' => Mage::helper('adminhtml')->__('Browse Files...'),
71: 'type' => 'button',
72: 'onclick' => $this->getJsObjectName() . '.browse()'
73: ))
74: );
75:
76: $this->setChild(
77: 'upload_button',
78: $this->getLayout()->createBlock('adminhtml/widget_button')
79: ->addData(array(
80: 'id' => $this->_getButtonId('upload'),
81: 'label' => Mage::helper('adminhtml')->__('Upload Files'),
82: 'type' => 'button',
83: 'onclick' => $this->getJsObjectName() . '.upload()'
84: ))
85: );
86:
87: $this->setChild(
88: 'delete_button',
89: $this->getLayout()->createBlock('adminhtml/widget_button')
90: ->addData(array(
91: 'id' => '{{id}}-delete',
92: 'class' => 'delete',
93: 'type' => 'button',
94: 'label' => Mage::helper('adminhtml')->__('Remove'),
95: 'onclick' => $this->getJsObjectName() . '.removeFile(\'{{fileId}}\')'
96: ))
97: );
98:
99: return parent::_prepareLayout();
100: }
101:
102: protected function _getButtonId($buttonName)
103: {
104: return $this->getHtmlId() . '-' . $buttonName;
105: }
106:
107: public function getBrowseButtonHtml()
108: {
109: return $this->getChildHtml('browse_button');
110: }
111:
112: public function getUploadButtonHtml()
113: {
114: return $this->getChildHtml('upload_button');
115: }
116:
117: public function getDeleteButtonHtml()
118: {
119: return $this->getChildHtml('delete_button');
120: }
121:
122: 123: 124: 125: 126:
127: public function getJsObjectName()
128: {
129: return $this->getHtmlId() . 'JsObject';
130: }
131:
132: 133: 134: 135: 136:
137: public function getConfigJson()
138: {
139: return Mage::helper('core')->jsonEncode($this->getConfig()->getData());
140: }
141:
142: 143: 144: 145: 146:
147: public function getConfig()
148: {
149: if(is_null($this->_config)) {
150: $this->_config = new Varien_Object();
151: }
152:
153: return $this->_config;
154: }
155:
156: public function getPostMaxSize()
157: {
158: return ini_get('post_max_size');
159: }
160:
161: public function getUploadMaxSize()
162: {
163: return ini_get('upload_max_filesize');
164: }
165:
166: public function getDataMaxSize()
167: {
168: return min($this->getPostMaxSize(), $this->getUploadMaxSize());
169: }
170:
171: public function getDataMaxSizeInBytes()
172: {
173: $iniSize = $this->getDataMaxSize();
174: $size = substr($iniSize, 0, strlen($iniSize)-1);
175: $parsedSize = 0;
176: switch (strtolower(substr($iniSize, strlen($iniSize)-1))) {
177: case 't':
178: $parsedSize = $size*(1024*1024*1024*1024);
179: break;
180: case 'g':
181: $parsedSize = $size*(1024*1024*1024);
182: break;
183: case 'm':
184: $parsedSize = $size*(1024*1024);
185: break;
186: case 'k':
187: $parsedSize = $size*1024;
188: break;
189: case 'b':
190: default:
191: $parsedSize = $size;
192: break;
193: }
194: return $parsedSize;
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204: 205:
206: public function getUploaderUrl($url)
207: {
208: if (!is_string($url)) {
209: $url = '';
210: }
211: $design = Mage::getDesign();
212: $theme = $design->getTheme('skin');
213: if (empty($url) || !$design->validateFile($url, array('_type' => 'skin', '_theme' => $theme))) {
214: $theme = $design->getDefaultTheme();
215: }
216: return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) . 'skin/' .
217: $design->getArea() . '/' . $design->getPackageName() . '/' . $theme . '/' . $url;
218: }
219: }
220: