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

/app/code/core/Enterprise/ImportExport/controllers/Adminhtml/Scheduled/OperationController.php

https://bitbucket.org/kdms/sh-magento
PHP | 346 lines | 216 code | 26 blank | 104 comment | 21 complexity | f9a2b94ba7192f54480bd58968451052 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_ImportExport
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Operation controller
  28. *
  29. * @category Enterprise
  30. * @package Enterprise_ImportExport
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Enterprise_ImportExport_Adminhtml_Scheduled_OperationController extends Mage_Adminhtml_Controller_Action
  34. {
  35. /**
  36. * Custom constructor.
  37. *
  38. * @return void
  39. */
  40. protected function _construct()
  41. {
  42. // Define module dependent translate
  43. $this->setUsedModuleName('Enterprise_ImportExport');
  44. }
  45. /**
  46. * Initialize layout.
  47. *
  48. * @return Enterprise_ImportExport_Adminhtml_ImportController
  49. */
  50. protected function _initAction()
  51. {
  52. try {
  53. $this->_title($this->__('Scheduled Imports/Exports'))
  54. ->loadLayout()
  55. ->_setActiveMenu('system/enterprise_importexport');
  56. } catch (Mage_Core_Exception $e) {
  57. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  58. $this->_redirect('*/scheduled_operation/index');
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Check access (in the ACL) for current user.
  64. *
  65. * @return bool
  66. */
  67. protected function _isAllowed()
  68. {
  69. return Mage::getSingleton('admin/session')->isAllowed('system/convert/enterprise_scheduled_operation');
  70. }
  71. /**
  72. * Index action.
  73. *
  74. * @return void
  75. */
  76. public function indexAction()
  77. {
  78. $this->_initAction();
  79. $this->renderLayout();
  80. }
  81. /**
  82. * Create new operation action.
  83. *
  84. * @return void
  85. */
  86. public function newAction()
  87. {
  88. $operationType = $this->getRequest()->getParam('type');
  89. $this->_initAction()
  90. ->_title(Mage::helper('enterprise_importexport')->getOperationHeaderText($operationType, 'new'));
  91. $this->renderLayout();
  92. }
  93. /**
  94. * Edit operation action.
  95. *
  96. * @return void
  97. */
  98. public function editAction()
  99. {
  100. $this->_initAction();
  101. $operationType = Mage::registry('current_operation')->getOperationType();
  102. $this->_title(Mage::helper('enterprise_importexport')->getOperationHeaderText($operationType, 'edit'));
  103. $this->renderLayout();
  104. }
  105. /**
  106. * Save operation action
  107. *
  108. * @return void
  109. */
  110. public function saveAction()
  111. {
  112. $request = $this->getRequest();
  113. if ($request->isPost()) {
  114. $data = $request->getPost();
  115. if (isset($data['id']) && !is_numeric($data['id'])
  116. || !isset($data['id']) && (!isset($data['operation_type']) || empty($data['operation_type']))
  117. || !is_array($data['start_time'])
  118. ) {
  119. Mage::getSingleton('adminhtml/session')->addError($this->__('Unable to save scheduled operation'));
  120. return $this->_redirect('*/*/*', array('_current' => true));
  121. }
  122. $data['start_time'] = join(':', $data['start_time']);
  123. if (isset($data['export_filter']) && is_array($data['export_filter'])) {
  124. $data['entity_attributes']['export_filter'] = $data['export_filter'];
  125. if (isset($data['skip_attr']) && is_array($data['skip_attr'])) {
  126. $data['entity_attributes']['skip_attr'] = array_filter($data['skip_attr'], 'intval');
  127. }
  128. }
  129. try {
  130. $operation = Mage::getModel('enterprise_importexport/scheduled_operation')->setData($data);
  131. $operation->save();
  132. Mage::getSingleton('adminhtml/session')->addSuccess(
  133. Mage::helper('enterprise_importexport')->getSuccessSaveMessage($operation->getOperationType())
  134. );
  135. } catch (Mage_Core_Exception $e) {
  136. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  137. } catch (Exception $e) {
  138. Mage::logException($e);
  139. Mage::getSingleton('adminhtml/session')->addError(
  140. $this->__('Unable to save scheduled operation')
  141. );
  142. }
  143. }
  144. $this->_redirect('*/scheduled_operation/index');
  145. }
  146. /**
  147. * Delete operation action
  148. *
  149. * @return void
  150. */
  151. public function deleteAction()
  152. {
  153. $request = $this->getRequest();
  154. $id = (int)$request->getParam('id');
  155. if ($id) {
  156. try {
  157. Mage::getModel('enterprise_importexport/scheduled_operation')->setId($id)->delete();
  158. Mage::getSingleton('adminhtml/session')->addSuccess(
  159. Mage::helper('enterprise_importexport')->getSuccessDeleteMessage($request->getParam('type'))
  160. );
  161. } catch (Mage_Core_Exception $e) {
  162. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  163. } catch (Exception $e) {
  164. Mage::logException($e);
  165. Mage::getSingleton('adminhtml/session')->addError(
  166. $this->__('Unable to delete scheduled operation')
  167. );
  168. }
  169. }
  170. $this->_redirect('*/scheduled_operation/index');
  171. }
  172. /**
  173. * Ajax grid action
  174. *
  175. * @return void
  176. */
  177. public function gridAction()
  178. {
  179. $this->loadLayout();
  180. $this->getResponse()->setBody(
  181. $this->getLayout()->createBlock('enterprise_importexport/adminhtml_scheduled_operation_grid')->toHtml()
  182. );
  183. }
  184. /**
  185. * Batch delete action
  186. *
  187. * @return void
  188. */
  189. public function massDeleteAction()
  190. {
  191. $request = $this->getRequest();
  192. $ids = $request->getParam('operation');
  193. if (is_array($ids)) {
  194. $ids = array_filter($ids, 'intval');
  195. try {
  196. $operations = Mage::getResourceModel('enterprise_importexport/scheduled_operation_collection');
  197. $operations->addFieldToFilter($operations->getResource()->getIdFieldName(), array('in' => $ids));
  198. foreach ($operations as $operation) {
  199. $operation->delete();
  200. }
  201. Mage::getSingleton('adminhtml/session')->addSuccess(
  202. $this->__('Total of %s record(s) have been deleted', count($operations))
  203. );
  204. } catch (Mage_Core_Exception $e) {
  205. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  206. } catch (Exception $e) {
  207. Mage::logException($e);
  208. Mage::getSingleton('adminhtml/session')->addError($this->__('Can not delete all items'));
  209. }
  210. }
  211. $this->_redirect('*/scheduled_operation/index');
  212. }
  213. /**
  214. * Batch change status action
  215. *
  216. * @return void
  217. */
  218. public function massChangeStatusAction()
  219. {
  220. $request = $this->getRequest();
  221. $ids = $request->getParam('operation');
  222. $status = (bool)$request->getParam('status');
  223. if (is_array($ids)) {
  224. $ids = array_filter($ids, 'intval');
  225. try {
  226. $operations = Mage::getResourceModel('enterprise_importexport/scheduled_operation_collection');
  227. $operations->addFieldToFilter($operations->getResource()->getIdFieldName(), array('in' => $ids));
  228. foreach ($operations as $operation) {
  229. $operation->setStatus($status)
  230. ->save();
  231. }
  232. Mage::getSingleton('adminhtml/session')->addSuccess(
  233. $this->__('Total of %s record(s) have been updated', count($operations))
  234. );
  235. } catch (Mage_Core_Exception $e) {
  236. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  237. } catch (Exception $e) {
  238. Mage::logException($e);
  239. Mage::getSingleton('adminhtml/session')->addError($this->__('Can not change status for all items'));
  240. }
  241. }
  242. $this->_redirect('*/scheduled_operation/index');
  243. }
  244. /**
  245. * Get grid-filter of entity attributes action.
  246. *
  247. * @return void
  248. */
  249. public function getFilterAction()
  250. {
  251. $data = $this->getRequest()->getParams();
  252. if ($this->getRequest()->isXmlHttpRequest() && $data) {
  253. try {
  254. $this->loadLayout();
  255. /** @var $export Enterprise_ImportExport_Model_Export */
  256. $export = Mage::getModel('enterprise_importexport/export')->setData($data);
  257. /** @var $attrFilterBlock Enterprise_ImportExport_Block_Adminhtml_Export_Filter */
  258. $attrFilterBlock = $this->getLayout()->getBlock('export.filter')
  259. ->setOperation($export);
  260. $export->filterAttributeCollection(
  261. $attrFilterBlock->prepareCollection(
  262. $export->getEntityAttributeCollection()
  263. )
  264. );
  265. $this->renderLayout();
  266. return;
  267. } catch (Exception $e) {
  268. $this->_getSession()->addError($e->getMessage());
  269. }
  270. } else {
  271. $this->_getSession()->addError($this->__('No valid data sent'));
  272. }
  273. $this->_redirect('*/*/index');
  274. }
  275. /**
  276. * Run task through http request.
  277. *
  278. * @return void
  279. */
  280. public function cronAction()
  281. {
  282. try {
  283. $operationId = (int)$this->getRequest()->getParam('operation');
  284. $schedule = new Varien_Object();
  285. $schedule->setJobCode(
  286. Enterprise_ImportExport_Model_Scheduled_Operation::CRON_JOB_NAME_PREFIX . $operationId
  287. );
  288. $result = false;
  289. $result = Mage::getModel('enterprise_importexport/observer')->processScheduledOperation($schedule, true);
  290. } catch (Exception $e) {
  291. $this->_getSession()->addError($e->getMessage());
  292. }
  293. if ($result) {
  294. $this->_getSession()
  295. ->addSuccess(Mage::helper('enterprise_importexport')->__('Operation has been successfully run'));
  296. } else {
  297. $this->_getSession()
  298. ->addError(Mage::helper('enterprise_importexport')->__('Unable to run operation'));
  299. }
  300. $this->_redirect('*/*/index');
  301. }
  302. /**
  303. * Run log cleaning through http request.
  304. *
  305. * @return void
  306. */
  307. public function logCleanAction()
  308. {
  309. $schedule = new Varien_Object();
  310. $result = Mage::getModel('enterprise_importexport/observer')->scheduledLogClean($schedule, true);
  311. if ($result) {
  312. $this->_getSession()
  313. ->addSuccess(Mage::helper('enterprise_importexport')->__('History files have been deleted'));
  314. } else {
  315. $this->_getSession()
  316. ->addError(Mage::helper('enterprise_importexport')->__('Unable to delete history files'));
  317. }
  318. $this->_redirect('*/system_config/edit', array('section' => $this->getRequest()->getParam('section')));
  319. }
  320. }