Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_ImportExport_Adminhtml_ExportController
  • Mage_ImportExport_Adminhtml_ImportController
  • Mage_ImportExport_Block_Adminhtml_Export_Edit
  • Mage_ImportExport_Block_Adminhtml_Export_Edit_Form
  • Mage_ImportExport_Block_Adminhtml_Export_Filter
  • Mage_ImportExport_Block_Adminhtml_Import_Edit
  • Mage_ImportExport_Block_Adminhtml_Import_Edit_Form
  • Mage_ImportExport_Block_Adminhtml_Import_Frame_Result
  • Mage_ImportExport_Helper_Data
  • Mage_ImportExport_Model_Abstract
  • Mage_ImportExport_Model_Config
  • Mage_ImportExport_Model_Export
  • Mage_ImportExport_Model_Export_Adapter_Abstract
  • Mage_ImportExport_Model_Export_Adapter_Csv
  • Mage_ImportExport_Model_Export_Entity_Abstract
  • Mage_ImportExport_Model_Export_Entity_Customer
  • Mage_ImportExport_Model_Export_Entity_Product
  • Mage_ImportExport_Model_Export_Entity_Product_Type_Abstract
  • Mage_ImportExport_Model_Export_Entity_Product_Type_Configurable
  • Mage_ImportExport_Model_Export_Entity_Product_Type_Grouped
  • Mage_ImportExport_Model_Export_Entity_Product_Type_Simple
  • Mage_ImportExport_Model_Import
  • Mage_ImportExport_Model_Import_Adapter
  • Mage_ImportExport_Model_Import_Adapter_Abstract
  • Mage_ImportExport_Model_Import_Adapter_Csv
  • Mage_ImportExport_Model_Import_Entity_Abstract
  • Mage_ImportExport_Model_Import_Entity_Customer
  • Mage_ImportExport_Model_Import_Entity_Customer_Address
  • Mage_ImportExport_Model_Import_Entity_Product
  • Mage_ImportExport_Model_Import_Entity_Product_Type_Abstract
  • Mage_ImportExport_Model_Import_Entity_Product_Type_Configurable
  • Mage_ImportExport_Model_Import_Entity_Product_Type_Grouped
  • Mage_ImportExport_Model_Import_Entity_Product_Type_Simple
  • Mage_ImportExport_Model_Import_Proxy_Product
  • Mage_ImportExport_Model_Import_Proxy_Product_Resource
  • Mage_ImportExport_Model_Import_Uploader
  • Mage_ImportExport_Model_Mysql4_Import_Data
  • Mage_ImportExport_Model_Mysql4_Setup
  • Mage_ImportExport_Model_Resource_Import_Data
  • Mage_ImportExport_Model_Resource_Setup
  • Mage_ImportExport_Model_Source_Export_Entity
  • Mage_ImportExport_Model_Source_Export_Format
  • Mage_ImportExport_Model_Source_Import_Behavior
  • Mage_ImportExport_Model_Source_Import_Entity
  • Overview
  • Package
  • Class
  • Tree
  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_ImportExport
 23:  * @copyright   Copyright (c) 2012 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: /**
 28:  * Export entity abstract model
 29:  *
 30:  * @category    Mage
 31:  * @package     Mage_ImportExport
 32:  * @author      Magento Core Team <core@magentocommerce.com>
 33:  */
 34: abstract class Mage_ImportExport_Model_Export_Entity_Abstract
 35: {
 36:     /**
 37:      * Attribute code to its values. Only attributes with options and only default store values used.
 38:      *
 39:      * @var array
 40:      */
 41:     protected $_attributeValues = array();
 42: 
 43: 
 44:     /**
 45:      * Attribute code to its values. Only attributes with options and only default store values used.
 46:      *
 47:      * @var array
 48:      */
 49:     protected static $attrCodes = null;
 50: 
 51:     /**
 52:      * DB connection.
 53:      *
 54:      * @var Varien_Db_Adapter_Pdo_Mysql
 55:      */
 56:     protected $_connection;
 57: 
 58:     /**
 59:      * Array of attributes codes which are disabled for export.
 60:      *
 61:      * @var array
 62:      */
 63:     protected $_disabledAttrs = array();
 64: 
 65:     /**
 66:      * Entity type id.
 67:      *
 68:      * @var int
 69:      */
 70:     protected $_entityTypeId;
 71: 
 72:     /**
 73:      * Error codes with arrays of corresponding row numbers.
 74:      *
 75:      * @var array
 76:      */
 77:     protected $_errors = array();
 78: 
 79:     /**
 80:      * Error counter.
 81:      *
 82:      * @var int
 83:      */
 84:     protected $_errorsCount = 0;
 85: 
 86:     /**
 87:      * Limit of errors after which pre-processing will exit.
 88:      *
 89:      * @var int
 90:      */
 91:     protected $_errorsLimit = 100;
 92: 
 93:     /**
 94:      * Export filter data.
 95:      *
 96:      * @var array
 97:      */
 98:     protected $_filter = array();
 99: 
100:     /**
101:      * Attributes with index (not label) value.
102:      *
103:      * @var array
104:      */
105:     protected $_indexValueAttributes = array();
106: 
107:     /**
108:      * Validation failure message template definitions.
109:      *
110:      * @var array
111:      */
112:     protected $_messageTemplates = array();
113: 
114:     /**
115:      * Parameters.
116:      *
117:      * @var array
118:      */
119:     protected $_parameters = array();
120: 
121:     /**
122:      * Column names that holds values with particular meaning.
123:      *
124:      * @var array
125:      */
126:     protected $_particularAttributes = array();
127: 
128:     /**
129:      * Permanent entity columns.
130:      *
131:      * @var array
132:      */
133:     protected $_permanentAttributes = array();
134: 
135:     /**
136:      * Number of entities processed by validation.
137:      *
138:      * @var int
139:      */
140:     protected $_processedEntitiesCount = 0;
141: 
142:     /**
143:      * Number of rows processed by validation.
144:      *
145:      * @var int
146:      */
147:     protected $_processedRowsCount = 0;
148: 
149:     /**
150:      * Source model.
151:      *
152:      * @var Mage_ImportExport_Model_Export_Adapter_Abstract
153:      */
154:     protected $_writer;
155: 
156:     /**
157:      * Constructor.
158:      *
159:      * @return void
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:      * Initialize stores hash.
170:      *
171:      * @return Mage_ImportExport_Model_Export_Entity_Abstract
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); // to ensure that 'admin' store (ID is zero) goes first
179: 
180:         return $this;
181:     }
182: 
183:     /**
184:      * Get attributes codes which are appropriate for export.
185:      *
186:      * @return array
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:      * Initialize attribute option values.
212:      *
213:      * @return Mage_ImportExport_Model_Export_Entity_Abstract
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:      * Apply filter to collection and add not skipped attributes to select.
225:      *
226:      * @param Mage_Eav_Model_Entity_Collection_Abstract $collection
227:      * @return Mage_Eav_Model_Entity_Collection_Abstract
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:             // filter applying
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:      * Add error with corresponding current data source row number.
291:      *
292:      * @param string $errorCode Error code or simply column name
293:      * @param int $errorRowNum Row number.
294:      * @return Mage_ImportExport_Model_Import_Adapter_Abstract
295:      */
296:     public function addRowError($errorCode, $errorRowNum)
297:     {
298:         $this->_errors[$errorCode][] = $errorRowNum + 1; // one added for human readability
299:         $this->_invalidRows[$errorRowNum] = true;
300:         $this->_errorsCount ++;
301: 
302:         return $this;
303:     }
304: 
305:     /**
306:      * Add message template for specific error code from outside.
307:      *
308:      * @param string $errorCode Error code
309:      * @param string $message Message template
310:      * @return Mage_ImportExport_Model_Import_Entity_Abstract
311:      */
312:     public function addMessageTemplate($errorCode, $message)
313:     {
314:         $this->_messageTemplates[$errorCode] = $message;
315: 
316:         return $this;
317:     }
318: 
319:     /**
320:      * Export process.
321:      *
322:      * @return string
323:      */
324:     abstract public function export();
325: 
326:     /**
327:      * Clean up attribute collection.
328:      *
329:      * @param Mage_Eav_Model_Resource_Entity_Attribute_Collection $collection
330:      * @return Mage_Eav_Model_Resource_Entity_Attribute_Collection
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:      * Entity attributes collection getter.
346:      *
347:      * @return Mage_Eav_Model_Resource_Entity_Attribute_Collection
348:      */
349:     abstract public function getAttributeCollection();
350: 
351:     /**
352:      * Returns attributes all values in label-value or value-value pairs form. Labels are lower-cased.
353:      *
354:      * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
355:      * @return array
356:      */
357:     public function getAttributeOptions(Mage_Eav_Model_Entity_Attribute_Abstract $attribute)
358:     {
359:         $options = array();
360: 
361:         if ($attribute->usesSource()) {
362:             // should attribute has index (option value) instead of a label?
363:             $index = in_array($attribute->getAttributeCode(), $this->_indexValueAttributes) ? 'value' : 'label';
364: 
365:             // only default (admin) store values used
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'])) { // skip ' -- Please Select -- ' option
372:                             $options[$innerOption['value']] = $innerOption[$index];
373:                         }
374:                     }
375:                 }
376:             } catch (Exception $e) {
377:                 // ignore exceptions connected with source models
378:             }
379:         }
380:         return $options;
381:     }
382: 
383:     /**
384:      * EAV entity type code getter.
385:      *
386:      * @abstract
387:      * @return string
388:      */
389:     abstract public function getEntityTypeCode();
390: 
391:     /**
392:      * Entity type ID getter.
393:      *
394:      * @return int
395:      */
396:     public function getEntityTypeId()
397:     {
398:         return $this->_entityTypeId;
399:     }
400: 
401:     /**
402:      * Returns error information.
403:      *
404:      * @return array
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:      * Returns error counter value.
424:      *
425:      * @return int
426:      */
427:     public function getErrorsCount()
428:     {
429:         return $this->_errorsCount;
430:     }
431: 
432:     /**
433:      * Returns invalid rows count.
434:      *
435:      * @return int
436:      */
437:     public function getInvalidRowsCount()
438:     {
439:         return count($this->_invalidRows);
440:     }
441: 
442:     /**
443:      * Returns number of checked entities.
444:      *
445:      * @return int
446:      */
447:     public function getProcessedEntitiesCount()
448:     {
449:         return $this->_processedEntitiesCount;
450:     }
451: 
452:     /**
453:      * Returns number of checked rows.
454:      *
455:      * @return int
456:      */
457:     public function getProcessedRowsCount()
458:     {
459:         return $this->_processedRowsCount;
460:     }
461: 
462:     /**
463:      * Inner writer object getter.
464:      *
465:      * @throws Exception
466:      * @return Mage_ImportExport_Model_Export_Adapter_Abstract
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:      * Set parameters.
478:      *
479:      * @param array $parameters
480:      * @return Mage_ImportExport_Model_Export_Entity_Abstract
481:      */
482:     public function setParameters(array $parameters)
483:     {
484:         $this->_parameters = $parameters;
485: 
486:         return $this;
487:     }
488: 
489:     /**
490:      * Writer model setter.
491:      *
492:      * @param Mage_ImportExport_Model_Export_Adapter_Abstract $writer
493:      * @return Mage_ImportExport_Model_Export_Entity_Abstract
494:      */
495:     public function setWriter(Mage_ImportExport_Model_Export_Adapter_Abstract $writer)
496:     {
497:         $this->_writer = $writer;
498: 
499:         return $this;
500:     }
501: }
502: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0