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_System_Convert_ProfileController extends Mage_Adminhtml_Controller_Action
35: {
36: protected function _initProfile($idFieldName = 'id')
37: {
38: $this->_title($this->__('System'))
39: ->_title($this->__('Import and Export'))
40: ->_title($this->__('Profiles'));
41:
42: $profileId = (int) $this->getRequest()->getParam($idFieldName);
43: $profile = Mage::getModel('dataflow/profile');
44:
45: if ($profileId) {
46: $profile->load($profileId);
47: if (!$profile->getId()) {
48: Mage::getSingleton('adminhtml/session')->addError(
49: $this->__('The profile you are trying to save no longer exists'));
50: $this->_redirect('*/*');
51: return false;
52: }
53: }
54:
55: Mage::register('current_convert_profile', $profile);
56:
57: return $this;
58: }
59:
60: 61: 62:
63: public function indexAction()
64: {
65: $this->_title($this->__('System'))
66: ->_title($this->__('Import and Export'))
67: ->_title($this->__('Advanced Profiles'));
68:
69: if ($this->getRequest()->getQuery('ajax')) {
70: $this->_forward('grid');
71: return;
72: }
73: $this->loadLayout();
74:
75: 76: 77:
78: $this->_setActiveMenu('system/convert');
79:
80: 81: 82:
83: $this->_addContent(
84: $this->getLayout()->createBlock('adminhtml/system_convert_profile', 'convert_profile')
85: );
86:
87: 88: 89:
90: $this->_addBreadcrumb(
91: Mage::helper('adminhtml')->__('Import/Export'),
92: Mage::helper('adminhtml')->__('Import/Export Advanced'));
93: $this->_addBreadcrumb(
94: Mage::helper('adminhtml')->__('Advanced Profiles'),
95: Mage::helper('adminhtml')->__('Advanced Profiles'));
96:
97: $this->renderLayout();
98: }
99:
100: public function gridAction()
101: {
102: $this->getResponse()->setBody(
103: $this->getLayout()->createBlock('adminhtml/system_convert_profile_grid')->toHtml()
104: );
105: }
106:
107: 108: 109:
110: public function editAction()
111: {
112: $this->_initProfile();
113: $this->loadLayout();
114:
115: $profile = Mage::registry('current_convert_profile');
116:
117:
118: $data = Mage::getSingleton('adminhtml/session')->getConvertProfileData(true);
119:
120: if (!empty($data)) {
121: $profile->addData($data);
122: }
123:
124: $this->_title($profile->getId() ? $profile->getName() : $this->__('New Profile'));
125:
126: $this->_setActiveMenu('system/convert');
127:
128: $this->_addContent(
129: $this->getLayout()->createBlock('adminhtml/system_convert_profile_edit')
130: );
131:
132: 133: 134:
135: $this->_addLeft($this->getLayout()->createBlock('adminhtml/system_convert_profile_edit_tabs'));
136:
137: $this->renderLayout();
138: }
139:
140: 141: 142:
143: public function newAction()
144: {
145: $this->_forward('edit');
146: }
147:
148: 149: 150:
151: public function deleteAction()
152: {
153: $this->_initProfile();
154: $profile = Mage::registry('current_convert_profile');
155: if ($profile->getId()) {
156: try {
157: $profile->delete();
158: Mage::getSingleton('adminhtml/session')->addSuccess(
159: Mage::helper('adminhtml')->__('The profile has been deleted.'));
160: } catch (Exception $e){
161: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
162: }
163: }
164: $this->_redirect('*/*');
165: }
166:
167: 168: 169:
170: public function saveAction()
171: {
172: if ($data = $this->getRequest()->getPost()) {
173: if (!$this->_initProfile('profile_id')) {
174: return;
175: }
176: $profile = Mage::registry('current_convert_profile');
177:
178:
179: if (isset($data)) {
180: $profile->addData($data);
181: }
182:
183: try {
184: $profile->save();
185:
186: Mage::getSingleton('adminhtml/session')->addSuccess(
187: Mage::helper('adminhtml')->__('The profile has been saved.'));
188: } catch (Exception $e){
189: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
190: Mage::getSingleton('adminhtml/session')->setConvertProfileData($data);
191: $this->getResponse()->setRedirect($this->getUrl('*/*/edit', array('id' => $profile->getId())));
192: return;
193: }
194: if ($this->getRequest()->getParam('continue')) {
195: $this->_redirect('*/*/edit', array('id' => $profile->getId()));
196: } else {
197: $this->_redirect('*/*');
198: }
199: } else {
200: Mage::getSingleton('adminhtml/session')->addError(
201: $this->__('Invalid POST data (please check post_max_size and upload_max_filesize settings in your php.ini file).')
202: );
203: $this->_redirect('*/*');
204: }
205: }
206:
207: public function runAction()
208: {
209: $this->_initProfile();
210: $this->loadLayout();
211: $this->renderLayout();
212: }
213:
214: public function batchRunAction()
215: {
216: if ($this->getRequest()->isPost()) {
217: $batchId = $this->getRequest()->getPost('batch_id', 0);
218: $rowIds = $this->getRequest()->getPost('rows');
219:
220:
221: $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
222:
223: if (!$batchModel->getId()) {
224: return;
225: }
226: if (!is_array($rowIds) || count($rowIds) < 1) {
227: return;
228: }
229: if (!$batchModel->getAdapter()) {
230: return;
231: }
232:
233: $batchImportModel = $batchModel->getBatchImportModel();
234: $importIds = $batchImportModel->getIdCollection();
235:
236: $adapter = Mage::getModel($batchModel->getAdapter());
237: $adapter->setBatchParams($batchModel->getParams());
238:
239: $errors = array();
240: $saved = 0;
241: foreach ($rowIds as $importId) {
242: $batchImportModel->load($importId);
243: if (!$batchImportModel->getId()) {
244: $errors[] = Mage::helper('dataflow')->__('Skip undefined row.');
245: continue;
246: }
247:
248: try {
249: $importData = $batchImportModel->getBatchData();
250: $adapter->saveRow($importData);
251: } catch (Exception $e) {
252: $errors[] = $e->getMessage();
253: continue;
254: }
255: $saved ++;
256: }
257:
258: if (method_exists($adapter, 'getEventPrefix')) {
259: 260: 261:
262: Mage::dispatchEvent($adapter->getEventPrefix() . '_finish_before', array(
263: 'adapter' => $adapter
264: ));
265:
266: 267: 268:
269: $adapter->clearAffectedEntityIds();
270: }
271:
272: $result = array(
273: 'savedRows' => $saved,
274: 'errors' => $errors
275: );
276: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
277: }
278: }
279:
280: public function batchFinishAction()
281: {
282: $batchId = $this->getRequest()->getParam('id');
283: if ($batchId) {
284: $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
285:
286:
287: if ($batchModel->getId()) {
288: $result = array();
289: try {
290: $batchModel->beforeFinish();
291: } catch (Mage_Core_Exception $e) {
292: $result['error'] = $e->getMessage();
293: } catch (Exception $e) {
294: $result['error'] = Mage::helper('adminhtml')->__('An error occurred while finishing process. Please refresh the cache');
295: }
296: $batchModel->delete();
297: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
298: }
299: }
300: }
301:
302: 303: 304: 305:
306: public function historyAction() {
307: $this->_initProfile();
308: $this->getResponse()->setBody(
309: $this->getLayout()->createBlock('adminhtml/system_convert_profile_edit_tab_history')->toHtml()
310: );
311: }
312:
313: protected function _isAllowed()
314: {
315: return Mage::getSingleton('admin/session')->isAllowed('admin/system/convert/profiles');
316: }
317: }
318: