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: abstract class Mage_ImportExport_Model_Export_Entity_Abstract
35: {
36: 37: 38: 39: 40:
41: protected $_attributeValues = array();
42:
43:
44: 45: 46: 47: 48:
49: protected static $attrCodes = null;
50:
51: 52: 53: 54: 55:
56: protected $_connection;
57:
58: 59: 60: 61: 62:
63: protected $_disabledAttrs = array();
64:
65: 66: 67: 68: 69:
70: protected $_entityTypeId;
71:
72: 73: 74: 75: 76:
77: protected $_errors = array();
78:
79: 80: 81: 82: 83:
84: protected $_errorsCount = 0;
85:
86: 87: 88: 89: 90:
91: protected $_errorsLimit = 100;
92:
93: 94: 95: 96: 97:
98: protected $_filter = array();
99:
100: 101: 102: 103: 104:
105: protected $_indexValueAttributes = array();
106:
107: 108: 109: 110: 111:
112: protected $_messageTemplates = array();
113:
114: 115: 116: 117: 118:
119: protected $_parameters = array();
120:
121: 122: 123: 124: 125:
126: protected $_particularAttributes = array();
127:
128: 129: 130: 131: 132:
133: protected $_permanentAttributes = array();
134:
135: 136: 137: 138: 139:
140: protected $_processedEntitiesCount = 0;
141:
142: 143: 144: 145: 146:
147: protected $_processedRowsCount = 0;
148:
149: 150: 151: 152: 153:
154: protected $_writer;
155:
156: 157: 158: 159: 160:
161: public function __construct()
162: {
163: $entityCode = $this->getEntityTypeCode();
164: $this->_entityTypeId = Mage::getSingleton('eav/config')->getEntityType($entityCode)->getEntityTypeId();
165: $this->_connection = Mage::getSingleton('core/resource')->getConnection('write');
166: }
167:
168: 169: 170: 171: 172:
173: protected function _initStores()
174: {
175: foreach (Mage::app()->getStores(true) as $store) {
176: $this->_storeIdToCode[$store->getId()] = $store->getCode();
177: }
178: ksort($this->_storeIdToCode);
179:
180: return $this;
181: }
182:
183: 184: 185: 186: 187:
188: protected function _getExportAttrCodes()
189: {
190: if (null === self::$attrCodes) {
191: if (!empty($this->_parameters[Mage_ImportExport_Model_Export::FILTER_ELEMENT_SKIP])
192: && is_array($this->_parameters[Mage_ImportExport_Model_Export::FILTER_ELEMENT_SKIP])) {
193: $skipAttr = array_flip($this->_parameters[Mage_ImportExport_Model_Export::FILTER_ELEMENT_SKIP]);
194: } else {
195: $skipAttr = array();
196: }
197: $attrCodes = array();
198:
199: foreach ($this->filterAttributeCollection($this->getAttributeCollection()) as $attribute) {
200: if (!isset($skipAttr[$attribute->getAttributeId()])
201: || in_array($attribute->getAttributeCode(), $this->_permanentAttributes)) {
202: $attrCodes[] = $attribute->getAttributeCode();
203: }
204: }
205: self::$attrCodes = $attrCodes;
206: }
207: return self::$attrCodes;
208: }
209:
210: 211: 212: 213: 214:
215: protected function _initAttrValues()
216: {
217: foreach ($this->getAttributeCollection() as $attribute) {
218: $this->_attributeValues[$attribute->getAttributeCode()] = $this->getAttributeOptions($attribute);
219: }
220: return $this;
221: }
222:
223: 224: 225: 226: 227: 228:
229: protected function _prepareEntityCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
230: {
231: if (!isset($this->_parameters[Mage_ImportExport_Model_Export::FILTER_ELEMENT_GROUP])
232: || !is_array($this->_parameters[Mage_ImportExport_Model_Export::FILTER_ELEMENT_GROUP])) {
233: $exportFilter = array();
234: } else {
235: $exportFilter = $this->_parameters[Mage_ImportExport_Model_Export::FILTER_ELEMENT_GROUP];
236: }
237: $exportAttrCodes = $this->_getExportAttrCodes();
238:
239: foreach ($this->filterAttributeCollection($this->getAttributeCollection()) as $attribute) {
240: $attrCode = $attribute->getAttributeCode();
241:
242:
243: if (isset($exportFilter[$attrCode])) {
244: $attrFilterType = Mage_ImportExport_Model_Export::getAttributeFilterType($attribute);
245:
246: if (Mage_ImportExport_Model_Export::FILTER_TYPE_SELECT == $attrFilterType) {
247: if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) {
248: $collection->addAttributeToFilter($attrCode, array('eq' => $exportFilter[$attrCode]));
249: }
250: } elseif (Mage_ImportExport_Model_Export::FILTER_TYPE_INPUT == $attrFilterType) {
251: if (is_scalar($exportFilter[$attrCode]) && trim($exportFilter[$attrCode])) {
252: $collection->addAttributeToFilter($attrCode, array('like' => "%{$exportFilter[$attrCode]}%"));
253: }
254: } elseif (Mage_ImportExport_Model_Export::FILTER_TYPE_DATE == $attrFilterType) {
255: if (is_array($exportFilter[$attrCode]) && count($exportFilter[$attrCode]) == 2) {
256: $from = array_shift($exportFilter[$attrCode]);
257: $to = array_shift($exportFilter[$attrCode]);
258:
259: if (is_scalar($from) && !empty($from)) {
260: $date = Mage::app()->getLocale()->date($from,null,null,false)->toString('MM/dd/YYYY');
261: $collection->addAttributeToFilter($attrCode, array('from' => $date, 'date' => true));
262: }
263: if (is_scalar($to) && !empty($to)) {
264: $date = Mage::app()->getLocale()->date($to,null,null,false)->toString('MM/dd/YYYY');
265: $collection->addAttributeToFilter($attrCode, array('to' => $date, 'date' => true));
266: }
267: }
268: } elseif (Mage_ImportExport_Model_Export::FILTER_TYPE_NUMBER == $attrFilterType) {
269: if (is_array($exportFilter[$attrCode]) && count($exportFilter[$attrCode]) == 2) {
270: $from = array_shift($exportFilter[$attrCode]);
271: $to = array_shift($exportFilter[$attrCode]);
272:
273: if (is_numeric($from)) {
274: $collection->addAttributeToFilter($attrCode, array('from' => $from));
275: }
276: if (is_numeric($to)) {
277: $collection->addAttributeToFilter($attrCode, array('to' => $to));
278: }
279: }
280: }
281: }
282: if (in_array($attrCode, $exportAttrCodes)) {
283: $collection->addAttributeToSelect($attrCode);
284: }
285: }
286: return $collection;
287: }
288:
289: 290: 291: 292: 293: 294: 295:
296: public function addRowError($errorCode, $errorRowNum)
297: {
298: $this->_errors[$errorCode][] = $errorRowNum + 1;
299: $this->_invalidRows[$errorRowNum] = true;
300: $this->_errorsCount ++;
301:
302: return $this;
303: }
304:
305: 306: 307: 308: 309: 310: 311:
312: public function addMessageTemplate($errorCode, $message)
313: {
314: $this->_messageTemplates[$errorCode] = $message;
315:
316: return $this;
317: }
318:
319: 320: 321: 322: 323:
324: abstract public function export();
325:
326: 327: 328: 329: 330: 331:
332: public function filterAttributeCollection(Mage_Eav_Model_Resource_Entity_Attribute_Collection $collection)
333: {
334: $collection->load();
335:
336: foreach ($collection as $attribute) {
337: if (in_array($attribute->getAttributeCode(), $this->_disabledAttrs)) {
338: $collection->removeItemByKey($attribute->getId());
339: }
340: }
341: return $collection;
342: }
343:
344: 345: 346: 347: 348:
349: abstract public function getAttributeCollection();
350:
351: 352: 353: 354: 355: 356:
357: public function getAttributeOptions(Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
358: {
359: $options = array();
360:
361: if ($attribute->usesSource()) {
362:
363: $index = in_array($attribute->getAttributeCode(), $this->_indexValueAttributes) ? 'value' : 'label';
364:
365:
366: $attribute->setStoreId(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
367:
368: try {
369: foreach ($attribute->getSource()->getAllOptions(false) as $option) {
370: foreach (is_array($option['value']) ? $option['value'] : array($option) as $innerOption) {
371: if (strlen($innerOption['value'])) {
372: $options[$innerOption['value']] = $innerOption[$index];
373: }
374: }
375: }
376: } catch (Exception $e) {
377:
378: }
379: }
380: return $options;
381: }
382:
383: 384: 385: 386: 387: 388:
389: abstract public function getEntityTypeCode();
390:
391: 392: 393: 394: 395:
396: public function getEntityTypeId()
397: {
398: return $this->_entityTypeId;
399: }
400:
401: 402: 403: 404: 405:
406: public function getErrorMessages()
407: {
408: $translator = Mage::helper('importexport');
409: $messages = array();
410:
411: foreach ($this->_errors as $errorCode => $errorRows) {
412: if (isset($this->_messageTemplates[$errorCode])) {
413: $message = $translator->__($this->_messageTemplates[$errorCode]);
414: } else {
415: $message = $translator->__("Invalid value for '%s' column", $errorCode);
416: }
417: $messages[$message] = $errorRows;
418: }
419: return $messages;
420: }
421:
422: 423: 424: 425: 426:
427: public function getErrorsCount()
428: {
429: return $this->_errorsCount;
430: }
431:
432: 433: 434: 435: 436:
437: public function getInvalidRowsCount()
438: {
439: return count($this->_invalidRows);
440: }
441:
442: 443: 444: 445: 446:
447: public function getProcessedEntitiesCount()
448: {
449: return $this->_processedEntitiesCount;
450: }
451:
452: 453: 454: 455: 456:
457: public function getProcessedRowsCount()
458: {
459: return $this->_processedRowsCount;
460: }
461:
462: 463: 464: 465: 466: 467:
468: public function getWriter()
469: {
470: if (!$this->_writer) {
471: Mage::throwException(Mage::helper('importexport')->__('No writer specified'));
472: }
473: return $this->_writer;
474: }
475:
476: 477: 478: 479: 480: 481:
482: public function setParameters(array $parameters)
483: {
484: $this->_parameters = $parameters;
485:
486: return $this;
487: }
488:
489: 490: 491: 492: 493: 494:
495: public function setWriter(Mage_ImportExport_Model_Export_Adapter_Abstract $writer)
496: {
497: $this->_writer = $writer;
498:
499: return $this;
500: }
501: }
502: