PageRenderTime 82ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Enterprise/CatalogEvent/controllers/Adminhtml/Catalog/EventController.php

https://bitbucket.org/kdms/sh-magento
PHP | 284 lines | 176 code | 29 blank | 79 comment | 21 complexity | a69a43e37b3e5849382f341c7c0d4284 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Enterprise
  22. * @package Enterprise_CatalogEvent
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Catalog Events Adminhtml controller
  28. *
  29. * @category Enterprise
  30. * @package Enterprise_CatalogEvent
  31. */
  32. class Enterprise_CatalogEvent_Adminhtml_Catalog_EventController extends Mage_Adminhtml_Controller_Action
  33. {
  34. /**
  35. * Check is enabled module in config
  36. *
  37. * @return Enterprise_CatalogEvent_Adminhtml_Catalog_EventController
  38. */
  39. public function preDispatch()
  40. {
  41. parent::preDispatch();
  42. if (!Mage::helper('enterprise_catalogevent')->isEnabled()) {
  43. if ($this->getRequest()->getActionName() != 'noroute') {
  44. $this->_forward('noroute');
  45. }
  46. }
  47. return $this;
  48. }
  49. /**
  50. * Init action breadcrumbs and active menu
  51. *
  52. * @return Enterprise_CatalogEvent_IndexController
  53. */
  54. public function _initAction()
  55. {
  56. $this->loadLayout()
  57. ->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog'))
  58. ->_addBreadcrumb(Mage::helper('enterprise_catalogevent')->__('Events'), Mage::helper('enterprise_catalogevent')->__('Events'))
  59. ->_setActiveMenu('catalog/enterprise_catelogevent');
  60. return $this;
  61. }
  62. /**
  63. * Events list action
  64. *
  65. * @return void
  66. */
  67. public function indexAction()
  68. {
  69. $this->_title($this->__('Catalog'))
  70. ->_title($this->__('Categories'))
  71. ->_title($this->__('Catalog Events'));
  72. $this->_initAction();
  73. $this->renderLayout();
  74. }
  75. /**
  76. * New event action
  77. *
  78. * @return void
  79. */
  80. public function newAction()
  81. {
  82. $this->_forward('edit');
  83. }
  84. /**
  85. * Edit event action
  86. */
  87. public function editAction()
  88. {
  89. $this->_title($this->__('Catalog'))
  90. ->_title($this->__('Categories'))
  91. ->_title($this->__('Catalog Events'));
  92. $event = Mage::getModel('enterprise_catalogevent/event')
  93. ->setStoreId($this->getRequest()->getParam('store', 0));
  94. if ($eventId = $this->getRequest()->getParam('id', false)) {
  95. $event->load($eventId);
  96. } else {
  97. $event->setCategoryId($this->getRequest()->getParam('category_id'));
  98. }
  99. $this->_title($event->getId() ? sprintf("#%s", $event->getId()) : $this->__('New Event'));
  100. $sessionData = Mage::getSingleton('adminhtml/session')->getEventData(true);
  101. if (!empty($sessionData)) {
  102. $event->addData($sessionData);
  103. }
  104. Mage::register('enterprise_catalogevent_event', $event);
  105. $this->_initAction();
  106. $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
  107. if (($switchBlock = $this->getLayout()->getBlock('store_switcher'))) {
  108. if (!$event->getId() || Mage::app()->isSingleStoreMode()) {
  109. $switchBlock->getParentBlock()->unsetChild('store_switcher');
  110. } else {
  111. $switchBlock->setDefaultStoreName(Mage::helper('enterprise_catalogevent')->__('Default Values'))
  112. ->setSwitchUrl($this->getUrl('*/*/*', array('_current' => true, 'store' => null)));
  113. }
  114. }
  115. $this->renderLayout();
  116. }
  117. /**
  118. * Save action
  119. *
  120. * @return void
  121. */
  122. public function saveAction()
  123. {
  124. $event = Mage::getModel('enterprise_catalogevent/event')
  125. ->setStoreId($this->getRequest()->getParam('store', 0));
  126. /* @var $event Enterprise_CatalogEvent_Model_Event */
  127. if ($eventId = $this->getRequest()->getParam('id', false)) {
  128. $event->load($eventId);
  129. } else {
  130. $event->setCategoryId($this->getRequest()->getParam('category_id'));
  131. }
  132. $postData = $this->_filterPostData($this->getRequest()->getPost());
  133. if (!isset($postData['catalogevent'])) {
  134. $this->_getSession()->addError(
  135. Mage::helper('enterprise_catalogevent')->__('An error occurred while saving this event.')
  136. );
  137. $this->_redirect('*/*/edit', array('_current' => true));
  138. return;
  139. }
  140. $data = new Varien_Object($postData['catalogevent']);
  141. $event->setDisplayState($data->getDisplayState())
  142. ->setStoreDateStart($data->getDateStart())
  143. ->setStoreDateEnd($data->getDateEnd())
  144. ->setSortOrder($data->getSortOrder());
  145. $isUploaded = true;
  146. try {
  147. $uploader = new Mage_Core_Model_File_Uploader('image');
  148. $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
  149. $uploader->setAllowRenameFiles(true);
  150. $uploader->setAllowCreateFolders(true);
  151. $uploader->setFilesDispersion(false);
  152. } catch (Exception $e) {
  153. $isUploaded = false;
  154. }
  155. $validateResult = $event->validate();
  156. if ($validateResult !== true) {
  157. foreach ($validateResult as $errorMessage) {
  158. $this->_getSession()->addError($errorMessage);
  159. }
  160. $this->_getSession()->setEventData($event->getData());
  161. $this->_redirect('*/*/edit', array('_current' => true));
  162. return;
  163. }
  164. try {
  165. if ($data->getData('image/is_default')) {
  166. $event->setImage(null);
  167. } elseif ($data->getData('image/delete')) {
  168. $event->setImage('');
  169. } elseif ($isUploaded) {
  170. try {
  171. $event->setImage($uploader);
  172. } catch (Exception $e) {
  173. Mage::throwException(
  174. Mage::helper('enterprise_catalogevent')->__('Image was not uploaded.')
  175. );
  176. }
  177. }
  178. $event->save();
  179. $this->_getSession()->addSuccess(
  180. Mage::helper('enterprise_catalogevent')->__('Event has been saved.')
  181. );
  182. if ($this->getRequest()->getParam('_continue')) {
  183. $this->_redirect('*/*/edit', array('_current' => true, 'id' => $event->getId()));
  184. } else {
  185. $this->_redirect('*/*/');
  186. }
  187. } catch (Exception $e) {
  188. $this->_getSession()->addError($e->getMessage());
  189. $this->_getSession()->setEventData($event->getData());
  190. $this->_redirect('*/*/edit', array('_current' => true));
  191. }
  192. }
  193. /**
  194. * Delete action
  195. *
  196. * @return void
  197. */
  198. public function deleteAction()
  199. {
  200. $event = Mage::getModel('enterprise_catalogevent/event');
  201. $event->load($this->getRequest()->getParam('id', false));
  202. if ($event->getId()) {
  203. try {
  204. $event->delete();
  205. $this->_getSession()->addSuccess(
  206. Mage::helper('enterprise_catalogevent')->__('Event has been deleted.')
  207. );
  208. if ($this->getRequest()->getParam('category')) {
  209. $this->_redirect('*/catalog_category/edit', array('id' => $event->getCategoryId(), 'clear' => 1));
  210. } else {
  211. $this->_redirect('*/*/');
  212. }
  213. } catch (Exception $e) {
  214. $this->_getSession()->addError($e->getMessage());
  215. $this->_redirect('*/*/edit', array('_current' => true));
  216. }
  217. }
  218. }
  219. /**
  220. * Ajax categories tree loader action
  221. *
  222. */
  223. public function categoriesJsonAction()
  224. {
  225. $id = $this->getRequest()->getParam('id', null);
  226. $this->getResponse()->setBody(
  227. $this->getLayout()->createBlock('enterprise_catalogevent/adminhtml_event_edit_category')
  228. ->getTreeArray($id, true, 1)
  229. );
  230. }
  231. /**
  232. * Acl check for admin
  233. *
  234. * @return boolean
  235. */
  236. protected function _isAllowed()
  237. {
  238. return Mage::getSingleton('admin/session')->isAllowed('catalog/events');
  239. }
  240. /**
  241. * Filtering posted data. Converting localized data if needed
  242. *
  243. * @param array
  244. * @return array
  245. */
  246. protected function _filterPostData($data)
  247. {
  248. if(isset($data['catalogevent'])) {
  249. $_data = $data['catalogevent'];
  250. $_data = $this->_filterDateTime($_data, array('date_start', 'date_end'));
  251. $data['catalogevent'] = $_data;
  252. }
  253. return $data;
  254. }
  255. }