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_Cms_Wysiwyg_ImagesController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected function _initAction()
42: {
43: $this->getStorage();
44: return $this;
45: }
46:
47: public function indexAction()
48: {
49: $storeId = (int) $this->getRequest()->getParam('store');
50:
51: try {
52: Mage::helper('cms/wysiwyg_images')->getCurrentPath();
53: } catch (Exception $e) {
54: $this->_getSession()->addError($e->getMessage());
55: }
56: $this->_initAction()->loadLayout('overlay_popup');
57: $block = $this->getLayout()->getBlock('wysiwyg_images.js');
58: if ($block) {
59: $block->setStoreId($storeId);
60: }
61: $this->renderLayout();
62: }
63:
64: public function treeJsonAction()
65: {
66: try {
67: $this->_initAction();
68: $this->getResponse()->setBody(
69: $this->getLayout()->createBlock('adminhtml/cms_wysiwyg_images_tree')
70: ->getTreeJson()
71: );
72: } catch (Exception $e) {
73: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode(array()));
74: }
75: }
76:
77: public function contentsAction()
78: {
79: try {
80: $this->_initAction()->_saveSessionCurrentPath();
81: $this->loadLayout('empty');
82: $this->renderLayout();
83: } catch (Exception $e) {
84: $result = array('error' => true, 'message' => $e->getMessage());
85: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
86: }
87: }
88:
89: public function newFolderAction()
90: {
91: try {
92: $this->_initAction();
93: $name = $this->getRequest()->getPost('name');
94: $path = $this->getStorage()->getSession()->getCurrentPath();
95: $result = $this->getStorage()->createDirectory($name, $path);
96: } catch (Exception $e) {
97: $result = array('error' => true, 'message' => $e->getMessage());
98: }
99: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
100: }
101:
102: public function deleteFolderAction()
103: {
104: try {
105: $path = $this->getStorage()->getSession()->getCurrentPath();
106: $this->getStorage()->deleteDirectory($path);
107: } catch (Exception $e) {
108: $result = array('error' => true, 'message' => $e->getMessage());
109: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
110: }
111: }
112:
113: 114: 115: 116: 117:
118: public function deleteFilesAction()
119: {
120: try {
121: if (!$this->getRequest()->isPost()) {
122: throw new Exception ('Wrong request.');
123: }
124: $files = Mage::helper('core')->jsonDecode($this->getRequest()->getParam('files'));
125:
126:
127: $helper = Mage::helper('cms/wysiwyg_images');
128: $path = $this->getStorage()->getSession()->getCurrentPath();
129: foreach ($files as $file) {
130: $file = $helper->idDecode($file);
131: $_filePath = realpath($path . DS . $file);
132: if (strpos($_filePath, realpath($path)) === 0 &&
133: strpos($_filePath, realpath($helper->getStorageRoot())) === 0
134: ) {
135: $this->getStorage()->deleteFile($path . DS . $file);
136: }
137: }
138: } catch (Exception $e) {
139: $result = array('error' => true, 'message' => $e->getMessage());
140: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
141: }
142: }
143:
144: 145: 146:
147: public function uploadAction()
148: {
149: try {
150: $result = array();
151: $this->_initAction();
152: $targetPath = $this->getStorage()->getSession()->getCurrentPath();
153: $result = $this->getStorage()->uploadFile($targetPath, $this->getRequest()->getParam('type'));
154: } catch (Exception $e) {
155: $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode());
156: }
157: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
158:
159: }
160:
161: 162: 163:
164: public function onInsertAction()
165: {
166: $helper = Mage::helper('cms/wysiwyg_images');
167: $storeId = $this->getRequest()->getParam('store');
168:
169: $filename = $this->getRequest()->getParam('filename');
170: $filename = $helper->idDecode($filename);
171: $asIs = $this->getRequest()->getParam('as_is');
172:
173: Mage::helper('catalog')->setStoreId($storeId);
174: $helper->setStoreId($storeId);
175:
176: $image = $helper->getImageHtmlDeclaration($filename, $asIs);
177: $this->getResponse()->setBody($image);
178: }
179:
180: 181: 182:
183: public function thumbnailAction()
184: {
185: $file = $this->getRequest()->getParam('file');
186: $file = Mage::helper('cms/wysiwyg_images')->idDecode($file);
187: $thumb = $this->getStorage()->resizeOnTheFly($file);
188: if ($thumb !== false) {
189: $image = Varien_Image_Adapter::factory('GD2');
190: $image->open($thumb);
191: $image->display();
192: } else {
193:
194: }
195: }
196:
197: 198: 199: 200: 201:
202: public function getStorage()
203: {
204: if (!Mage::registry('storage')) {
205: $storage = Mage::getModel('cms/wysiwyg_images_storage');
206: Mage::register('storage', $storage);
207: }
208: return Mage::registry('storage');
209: }
210:
211: 212: 213: 214: 215:
216: protected function _saveSessionCurrentPath()
217: {
218: $this->getStorage()
219: ->getSession()
220: ->setCurrentPath(Mage::helper('cms/wysiwyg_images')->getCurrentPath());
221: return $this;
222: }
223:
224: 225: 226: 227: 228:
229: protected function _isAllowed()
230: {
231: return Mage::getSingleton('admin/session')->isAllowed('cms/media_gallery');
232: }
233: }
234: