00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Adminhtml_System_Convert_ProfileController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037 protected function _initProfile($idFieldName = 'id')
00038 {
00039 $profileId = (int) $this->getRequest()->getParam($idFieldName);
00040 $profile = Mage::getModel('dataflow/profile');
00041
00042 if ($profileId) {
00043 $profile->load($profileId);
00044 if (!$profile->getId()) {
00045 Mage::getSingleton('adminhtml/session')->addError('The profile you are trying to save no longer exists');
00046 $this->_redirect('*/*');
00047 return false;
00048 }
00049 }
00050
00051 Mage::register('current_convert_profile', $profile);
00052
00053 return $this;
00054 }
00055
00056
00057
00058
00059 public function indexAction()
00060 {
00061 if ($this->getRequest()->getQuery('ajax')) {
00062 $this->_forward('grid');
00063 return;
00064 }
00065 $this->loadLayout();
00066
00067
00068
00069
00070 $this->_setActiveMenu('system/convert');
00071
00072
00073
00074
00075 $this->_addContent(
00076 $this->getLayout()->createBlock('adminhtml/system_convert_profile', 'convert_profile')
00077 );
00078
00079
00080
00081
00082 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Import/Export'), Mage::helper('adminhtml')->__('Import/Export Advanced'));
00083 $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Advanced Profiles'), Mage::helper('adminhtml')->__('Advanced Profiles'));
00084
00085 $this->renderLayout();
00086 }
00087
00088 public function gridAction()
00089 {
00090 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/system_convert_profile_grid')->toHtml());
00091 }
00092
00093
00094
00095
00096 public function editAction()
00097 {
00098 $this->_initProfile();
00099 $this->loadLayout();
00100
00101 $profile = Mage::registry('current_convert_profile');
00102
00103
00104 $data = Mage::getSingleton('adminhtml/session')->getConvertProfileData(true);
00105
00106 if (!empty($data)) {
00107 $profile->addData($data);
00108 }
00109
00110 $this->_setActiveMenu('system/convert');
00111
00112 $this->_addContent(
00113 $this->getLayout()->createBlock('adminhtml/system_convert_profile_edit')
00114 );
00115
00116
00117
00118
00119 $this->_addLeft($this->getLayout()->createBlock('adminhtml/system_convert_profile_edit_tabs'));
00120
00121 $this->renderLayout();
00122 }
00123
00124
00125
00126
00127 public function newAction()
00128 {
00129 $this->_forward('edit');
00130 }
00131
00132
00133
00134
00135 public function deleteAction()
00136 {
00137 $this->_initProfile();
00138 $profile = Mage::registry('current_convert_profile');
00139 if ($profile->getId()) {
00140 try {
00141 $profile->delete();
00142 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Profile was deleted'));
00143 }
00144 catch (Exception $e){
00145 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00146 }
00147 }
00148 $this->_redirect('*/*');
00149 }
00150
00151
00152
00153
00154 public function saveAction()
00155 {
00156 if ($data = $this->getRequest()->getPost()) {
00157 if (!$this->_initProfile('profile_id')) {
00158 return ;
00159 }
00160 $profile = Mage::registry('current_convert_profile');
00161
00162
00163 if (isset($data)) {
00164 $profile->addData($data);
00165 }
00166
00167 try {
00168 $profile->save();
00169
00170 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Profile was successfully saved'));
00171 }
00172 catch (Exception $e){
00173 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00174 Mage::getSingleton('adminhtml/session')->setConvertProfileData($data);
00175 $this->getResponse()->setRedirect($this->getUrl('*/*/edit', array('id'=>$profile->getId())));
00176 return;
00177 }
00178 if ($this->getRequest()->getParam('continue')) {
00179 $this->_redirect('*/*/edit', array('id'=>$profile->getId()));
00180 } else {
00181 $this->_redirect('*/*');
00182 }
00183 }
00184 else {
00185 Mage::getSingleton('adminhtml/session')->addError($this->__('Invalid POST data (please check post_max_size and upload_max_filesize settings in you php.ini file)'));
00186 $this->_redirect('*/*');
00187 }
00188 }
00189
00190 public function runAction()
00191 {
00192 $this->_initProfile();
00193 #$this->loadLayout();
00194
00195 #$this->_setActiveMenu('system/convert');
00196
00197 #$this->_addContent(
00198 # $this->getLayout()->createBlock('adminhtml/system_convert_profile_run')
00199 #);
00200 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/system_convert_profile_run')->toHtml());
00201 $this->getResponse()->sendResponse();
00202
00203 #$this->renderLayout();
00204 }
00205
00206 public function batchRunAction()
00207 {
00208 if ($this->getRequest()->isPost()) {
00209 $batchId = $this->getRequest()->getPost('batch_id',0);
00210 $rowIds = $this->getRequest()->getPost('rows');
00211
00212 $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
00213
00214
00215 if (!$batchModel->getId()) {
00216
00217 return ;
00218 }
00219 if (!is_array($rowIds) || count($rowIds) < 1) {
00220
00221 return ;
00222 }
00223 if (!$batchModel->getAdapter()) {
00224
00225 return ;
00226 }
00227
00228 $batchImportModel = $batchModel->getBatchImportModel();
00229 $importIds = $batchImportModel->getIdCollection();
00230
00231 $adapter = Mage::getModel($batchModel->getAdapter());
00232 $adapter->setBatchParams($batchModel->getParams());
00233
00234 $errors = array();
00235 $saved = 0;
00236
00237 foreach ($rowIds as $importId) {
00238 $batchImportModel->load($importId);
00239 if (!$batchImportModel->getId()) {
00240 $errors[] = Mage::helper('dataflow')->__('Skip undefined row');
00241 continue;
00242 }
00243
00244 $importData = $batchImportModel->getBatchData();
00245 try {
00246 $adapter->saveRow($importData);
00247 }
00248 catch (Exception $e) {
00249 $errors[] = $e->getMessage();
00250 continue;
00251 }
00252 $saved ++;
00253 }
00254
00255 $result = array(
00256 'savedRows' => $saved,
00257 'errors' => $errors
00258 );
00259 $this->getResponse()->setBody(Zend_Json::encode($result));
00260 }
00261 }
00262
00263 public function batchFinishAction()
00264 {
00265 if ($batchId = $this->getRequest()->getParam('id')) {
00266 $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
00267
00268
00269 if ($batchModel->getId()) {
00270 $result = array();
00271 try {
00272 $batchModel->beforeFinish();
00273 }
00274 catch (Mage_Core_Exception $e) {
00275 $result['error'] = $e->getMessage();
00276 }
00277 catch (Exception $e) {
00278 $result['error'] = Mage::helper('adminhtml')->__('Error while finished process. Please refresh cache');
00279 }
00280 $batchModel->delete();
00281 $this->getResponse()->setBody(Zend_Json::encode($result));
00282 }
00283 }
00284 }
00285
00286
00287
00288
00289
00290 public function historyAction() {
00291 $this->_initProfile();
00292 $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/system_convert_profile_edit_tab_history')->toHtml());
00293 }
00294
00295 protected function _isAllowed()
00296 {
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 return Mage::getSingleton('admin/session')->isAllowed('admin/system/convert/profiles');
00313 }
00314 }