/app/code/core/Mage/Adminhtml/controllers/System/Convert/ProfileController.php

https://bitbucket.org/acidel/buykoala · PHP · 317 lines · 203 code · 42 blank · 72 comment · 20 complexity · 67edf2691fe427f1f190b03972935d2a MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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 Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Convert Advanced admin controller
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_System_Convert_ProfileController extends Mage_Adminhtml_Controller_Action
  34. {
  35. protected function _initProfile($idFieldName = 'id')
  36. {
  37. $this->_title($this->__('System'))
  38. ->_title($this->__('Import and Export'))
  39. ->_title($this->__('Profiles'));
  40. $profileId = (int) $this->getRequest()->getParam($idFieldName);
  41. $profile = Mage::getModel('dataflow/profile');
  42. if ($profileId) {
  43. $profile->load($profileId);
  44. if (!$profile->getId()) {
  45. Mage::getSingleton('adminhtml/session')->addError(
  46. $this->__('The profile you are trying to save no longer exists'));
  47. $this->_redirect('*/*');
  48. return false;
  49. }
  50. }
  51. Mage::register('current_convert_profile', $profile);
  52. return $this;
  53. }
  54. /**
  55. * Profiles list action
  56. */
  57. public function indexAction()
  58. {
  59. $this->_title($this->__('System'))
  60. ->_title($this->__('Import and Export'))
  61. ->_title($this->__('Advanced Profiles'));
  62. if ($this->getRequest()->getQuery('ajax')) {
  63. $this->_forward('grid');
  64. return;
  65. }
  66. $this->loadLayout();
  67. /**
  68. * Set active menu item
  69. */
  70. $this->_setActiveMenu('system/convert');
  71. /**
  72. * Append profiles block to content
  73. */
  74. $this->_addContent(
  75. $this->getLayout()->createBlock('adminhtml/system_convert_profile', 'convert_profile')
  76. );
  77. /**
  78. * Add breadcrumb item
  79. */
  80. $this->_addBreadcrumb(
  81. Mage::helper('adminhtml')->__('Import/Export'),
  82. Mage::helper('adminhtml')->__('Import/Export Advanced'));
  83. $this->_addBreadcrumb(
  84. Mage::helper('adminhtml')->__('Advanced Profiles'),
  85. Mage::helper('adminhtml')->__('Advanced Profiles'));
  86. $this->renderLayout();
  87. }
  88. public function gridAction()
  89. {
  90. $this->getResponse()->setBody(
  91. $this->getLayout()->createBlock('adminhtml/system_convert_profile_grid')->toHtml()
  92. );
  93. }
  94. /**
  95. * Profile edit action
  96. */
  97. public function editAction()
  98. {
  99. $this->_initProfile();
  100. $this->loadLayout();
  101. $profile = Mage::registry('current_convert_profile');
  102. // set entered data if was error when we do save
  103. $data = Mage::getSingleton('adminhtml/session')->getConvertProfileData(true);
  104. if (!empty($data)) {
  105. $profile->addData($data);
  106. }
  107. $this->_title($profile->getId() ? $profile->getName() : $this->__('New Profile'));
  108. $this->_setActiveMenu('system/convert');
  109. $this->_addContent(
  110. $this->getLayout()->createBlock('adminhtml/system_convert_profile_edit')
  111. );
  112. /**
  113. * Append edit tabs to left block
  114. */
  115. $this->_addLeft($this->getLayout()->createBlock('adminhtml/system_convert_profile_edit_tabs'));
  116. $this->renderLayout();
  117. }
  118. /**
  119. * Create new profile action
  120. */
  121. public function newAction()
  122. {
  123. $this->_forward('edit');
  124. }
  125. /**
  126. * Delete profile action
  127. */
  128. public function deleteAction()
  129. {
  130. $this->_initProfile();
  131. $profile = Mage::registry('current_convert_profile');
  132. if ($profile->getId()) {
  133. try {
  134. $profile->delete();
  135. Mage::getSingleton('adminhtml/session')->addSuccess(
  136. Mage::helper('adminhtml')->__('The profile has been deleted.'));
  137. } catch (Exception $e){
  138. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  139. }
  140. }
  141. $this->_redirect('*/*');
  142. }
  143. /**
  144. * Save profile action
  145. */
  146. public function saveAction()
  147. {
  148. if ($data = $this->getRequest()->getPost()) {
  149. if (!$this->_initProfile('profile_id')) {
  150. return;
  151. }
  152. $profile = Mage::registry('current_convert_profile');
  153. // Prepare profile saving data
  154. if (isset($data)) {
  155. $profile->addData($data);
  156. }
  157. try {
  158. $profile->save();
  159. Mage::getSingleton('adminhtml/session')->addSuccess(
  160. Mage::helper('adminhtml')->__('The profile has been saved.'));
  161. } catch (Exception $e){
  162. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  163. Mage::getSingleton('adminhtml/session')->setConvertProfileData($data);
  164. $this->getResponse()->setRedirect($this->getUrl('*/*/edit', array('id' => $profile->getId())));
  165. return;
  166. }
  167. if ($this->getRequest()->getParam('continue')) {
  168. $this->_redirect('*/*/edit', array('id' => $profile->getId()));
  169. } else {
  170. $this->_redirect('*/*');
  171. }
  172. } else {
  173. Mage::getSingleton('adminhtml/session')->addError(
  174. $this->__('Invalid POST data (please check post_max_size and upload_max_filesize settings in your php.ini file).')
  175. );
  176. $this->_redirect('*/*');
  177. }
  178. }
  179. public function runAction()
  180. {
  181. $this->_initProfile();
  182. $this->loadLayout();
  183. $this->renderLayout();
  184. }
  185. public function batchRunAction()
  186. {
  187. if ($this->getRequest()->isPost()) {
  188. $batchId = $this->getRequest()->getPost('batch_id', 0);
  189. $rowIds = $this->getRequest()->getPost('rows');
  190. /* @var $batchModel Mage_Dataflow_Model_Batch */
  191. $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
  192. if (!$batchModel->getId()) {
  193. return;
  194. }
  195. if (!is_array($rowIds) || count($rowIds) < 1) {
  196. return;
  197. }
  198. if (!$batchModel->getAdapter()) {
  199. return;
  200. }
  201. $batchImportModel = $batchModel->getBatchImportModel();
  202. $importIds = $batchImportModel->getIdCollection();
  203. $adapter = Mage::getModel($batchModel->getAdapter());
  204. $adapter->setBatchParams($batchModel->getParams());
  205. $errors = array();
  206. $saved = 0;
  207. foreach ($rowIds as $importId) {
  208. $batchImportModel->load($importId);
  209. if (!$batchImportModel->getId()) {
  210. $errors[] = Mage::helper('dataflow')->__('Skip undefined row.');
  211. continue;
  212. }
  213. try {
  214. $importData = $batchImportModel->getBatchData();
  215. $adapter->saveRow($importData);
  216. } catch (Exception $e) {
  217. $errors[] = $e->getMessage();
  218. continue;
  219. }
  220. $saved ++;
  221. }
  222. if (method_exists($adapter, 'getEventPrefix')) {
  223. /**
  224. * Event for process rules relations after products import
  225. */
  226. Mage::dispatchEvent($adapter->getEventPrefix() . '_finish_before', array(
  227. 'adapter' => $adapter
  228. ));
  229. /**
  230. * Clear affected ids for adapter possible reuse
  231. */
  232. $adapter->clearAffectedEntityIds();
  233. }
  234. $result = array(
  235. 'savedRows' => $saved,
  236. 'errors' => $errors
  237. );
  238. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  239. }
  240. }
  241. public function batchFinishAction()
  242. {
  243. $batchId = $this->getRequest()->getParam('id');
  244. if ($batchId) {
  245. $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
  246. /* @var $batchModel Mage_Dataflow_Model_Batch */
  247. if ($batchModel->getId()) {
  248. $result = array();
  249. try {
  250. $batchModel->beforeFinish();
  251. } catch (Mage_Core_Exception $e) {
  252. $result['error'] = $e->getMessage();
  253. } catch (Exception $e) {
  254. $result['error'] = Mage::helper('adminhtml')->__('An error occurred while finishing process. Please refresh the cache');
  255. }
  256. $batchModel->delete();
  257. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  258. }
  259. }
  260. }
  261. /**
  262. * Customer orders grid
  263. *
  264. */
  265. public function historyAction() {
  266. $this->_initProfile();
  267. $this->getResponse()->setBody(
  268. $this->getLayout()->createBlock('adminhtml/system_convert_profile_edit_tab_history')->toHtml()
  269. );
  270. }
  271. protected function _isAllowed()
  272. {
  273. return Mage::getSingleton('admin/session')->isAllowed('admin/system/convert/profiles');
  274. }
  275. }