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_ImportExport_Adminhtml_ImportController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected function _construct()
42: {
43:
44: $this->setUsedModuleName('Mage_ImportExport');
45: }
46:
47: 48: 49: 50: 51:
52: protected function _initAction()
53: {
54: $this->_title($this->__('Import/Export'))
55: ->loadLayout()
56: ->_setActiveMenu('system/importexport');
57:
58: return $this;
59: }
60:
61: 62: 63: 64: 65:
66: protected function _isAllowed()
67: {
68: return Mage::getSingleton('admin/session')->isAllowed('system/convert/import');
69: }
70:
71: 72: 73: 74: 75:
76: public function indexAction()
77: {
78: $maxUploadSize = Mage::helper('importexport')->getMaxUploadSize();
79: $this->_getSession()->addNotice(
80: $this->__('Total size of uploadable files must not exceed %s', $maxUploadSize)
81: );
82: $this->_initAction()
83: ->_title($this->__('Import'))
84: ->_addBreadcrumb($this->__('Import'), $this->__('Import'));
85:
86: $this->renderLayout();
87: }
88:
89: 90: 91: 92: 93:
94: public function startAction()
95: {
96: $data = $this->getRequest()->getPost();
97: if ($data) {
98: $this->loadLayout(false);
99:
100:
101: $resultBlock = $this->getLayout()->getBlock('import.frame.result');
102:
103: $importModel = Mage::getModel('importexport/import');
104:
105: try {
106: $importModel->importSource();
107: $importModel->invalidateIndex();
108: $resultBlock->addAction('show', 'import_validation_container')
109: ->addAction('innerHTML', 'import_validation_container_header', $this->__('Status'));
110: } catch (Exception $e) {
111: $resultBlock->addError($e->getMessage());
112: $this->renderLayout();
113: return;
114: }
115: $resultBlock->addAction('hide', array('edit_form', 'upload_button', 'messages'))
116: ->addSuccess($this->__('Import successfully done.'));
117: $this->renderLayout();
118: } else {
119: $this->_redirect('*/*/index');
120: }
121: }
122:
123: 124: 125: 126: 127:
128: public function validateAction()
129: {
130: $data = $this->getRequest()->getPost();
131: if ($data) {
132: $this->loadLayout(false);
133:
134: $resultBlock = $this->getLayout()->getBlock('import.frame.result');
135:
136: $resultBlock->addAction('show', 'import_validation_container')
137: ->addAction('clear', array(
138: Mage_ImportExport_Model_Import::FIELD_NAME_SOURCE_FILE,
139: Mage_ImportExport_Model_Import::FIELD_NAME_IMG_ARCHIVE_FILE)
140: );
141:
142: try {
143:
144: $import = Mage::getModel('importexport/import');
145: $validationResult = $import->validateSource($import->setData($data)->uploadSource());
146:
147: if (!$import->getProcessedRowsCount()) {
148: $resultBlock->addError($this->__('File does not contain data. Please upload another one'));
149: } else {
150: if (!$validationResult) {
151: if ($import->getProcessedRowsCount() == $import->getInvalidRowsCount()) {
152: $resultBlock->addNotice(
153: $this->__('File is totally invalid. Please fix errors and re-upload file')
154: );
155: } elseif ($import->getErrorsCount() >= $import->getErrorsLimit()) {
156: $resultBlock->addNotice(
157: $this->__(
158: 'Errors limit (%d) reached. Please fix errors and re-upload file',
159: $import->getErrorsLimit()
160: )
161: );
162: } else {
163: if ($import->isImportAllowed()) {
164: $resultBlock->addNotice(
165: $this->__('Please fix errors and re-upload file or simply press "Import" button to skip rows with errors'),
166: true
167: );
168: } else {
169: $resultBlock->addNotice(
170: $this->__('File is partially valid, but import is not possible'), false
171: );
172: }
173: }
174:
175: foreach ($import->getErrors() as $errorCode => $rows) {
176: $error = $errorCode . ' ' . $this->__('in rows:') . ' ' . implode(', ', $rows);
177: $resultBlock->addError($error);
178: }
179: } else {
180: if ($import->isImportAllowed()) {
181: $resultBlock->addSuccess(
182: $this->__('File is valid! To start import process press "Import" button'), true
183: );
184: } else {
185: $resultBlock->addError(
186: $this->__('File is valid, but import is not possible'), false
187: );
188: }
189: }
190: $resultBlock->addNotice($import->getNotices());
191: $resultBlock->addNotice(
192: $this->__(
193: 'Checked rows: %d, checked entities: %d, invalid rows: %d, total errors: %d',
194: $import->getProcessedRowsCount(), $import->getProcessedEntitiesCount(),
195: $import->getInvalidRowsCount(), $import->getErrorsCount()
196: )
197: );
198: }
199: } catch (Exception $e) {
200: $resultBlock->addNotice($this->__('Please fix errors and re-upload file'))
201: ->addError($e->getMessage());
202: }
203: $this->renderLayout();
204: } elseif ($this->getRequest()->isPost() && empty($_FILES)) {
205: $this->loadLayout(false);
206: $resultBlock = $this->getLayout()->getBlock('import.frame.result');
207: $resultBlock->addError($this->__('File was not uploaded'));
208: $this->renderLayout();
209: } else {
210: $this->_getSession()->addError($this->__('Data is invalid or file is not uploaded'));
211: $this->_redirect('*/*/index');
212: }
213: }
214: }
215: