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_Model_Export_Entity_Customer extends Mage_ImportExport_Model_Export_Entity_Abstract
35: {
36: 37: 38: 39: 40: 41:
42: const COL_EMAIL = 'email';
43: const COL_WEBSITE = '_website';
44: const COL_STORE = '_store';
45:
46: 47: 48: 49: 50:
51: protected $_attributeOverrides = array(
52: 'created_at' => array('backend_type' => 'datetime'),
53: 'reward_update_notification' => array('source_model' => 'eav/entity_attribute_source_boolean'),
54: 'reward_warning_notification' => array('source_model' => 'eav/entity_attribute_source_boolean')
55: );
56:
57: 58: 59: 60: 61:
62: protected $_disabledAttrs = array('default_billing', 'default_shipping');
63:
64: 65: 66: 67: 68:
69: protected $_indexValueAttributes = array('group_id', 'website_id', 'store_id');
70:
71: 72: 73: 74: 75:
76: protected $_permanentAttributes = array(self::COL_EMAIL, self::COL_WEBSITE, self::COL_STORE);
77:
78: 79: 80: 81: 82:
83: protected $_storeIdToCode = array();
84:
85: 86: 87: 88: 89:
90: protected $_websiteIdToCode = array();
91:
92: 93: 94: 95: 96:
97: public function __construct()
98: {
99: parent::__construct();
100:
101: $this->_initAttrValues()
102: ->_initStores()
103: ->_initWebsites();
104: }
105:
106: 107: 108: 109: 110:
111: protected function _initWebsites()
112: {
113:
114: foreach (Mage::app()->getWebsites(true) as $website) {
115: $this->_websiteIdToCode[$website->getId()] = $website->getCode();
116: }
117: return $this;
118: }
119:
120: 121: 122: 123: 124: 125:
126: protected function _prepareEntityCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection)
127: {
128:
129: return parent::_prepareEntityCollection($collection)->addAttributeToSelect(
130: Mage_ImportExport_Model_Import_Entity_Customer_Address::getDefaultAddressAttrMapping()
131: );
132: }
133:
134: 135: 136: 137: 138:
139: public function export()
140: {
141: $collection = $this->_prepareEntityCollection(Mage::getResourceModel('customer/customer_collection'));
142: $validAttrCodes = $this->_getExportAttrCodes();
143: $writer = $this->getWriter();
144: $defaultAddrMap = Mage_ImportExport_Model_Import_Entity_Customer_Address::getDefaultAddressAttrMapping();
145:
146:
147: $addrAttributes = array();
148: $addrColNames = array();
149: $customerAddrs = array();
150:
151: foreach (Mage::getResourceModel('customer/address_attribute_collection')
152: ->addSystemHiddenFilter()
153: ->addExcludeHiddenFrontendFilter() as $attribute) {
154: $options = array();
155: $attrCode = $attribute->getAttributeCode();
156:
157: if ($attribute->usesSource() && 'country_id' != $attrCode) {
158: foreach ($attribute->getSource()->getAllOptions(false) as $option) {
159: foreach (is_array($option['value']) ? $option['value'] : array($option) as $innerOption) {
160: if (strlen($innerOption['value'])) {
161: $options[$innerOption['value']] = $innerOption['label'];
162: }
163: }
164: }
165: }
166: $addrAttributes[$attrCode] = $options;
167: $addrColNames[] = Mage_ImportExport_Model_Import_Entity_Customer_Address::getColNameForAttrCode($attrCode);
168: }
169: foreach (Mage::getResourceModel('customer/address_collection')->addAttributeToSelect('*') as $address) {
170: $addrRow = array();
171:
172: foreach ($addrAttributes as $attrCode => $attrValues) {
173: if (null !== $address->getData($attrCode)) {
174: $value = $address->getData($attrCode);
175:
176: if ($attrValues) {
177: $value = $attrValues[$value];
178: }
179: $column = Mage_ImportExport_Model_Import_Entity_Customer_Address::getColNameForAttrCode($attrCode);
180: $addrRow[$column] = $value;
181: }
182: }
183: $customerAddrs[$address['parent_id']][$address->getId()] = $addrRow;
184: }
185:
186:
187: $writer->setHeaderCols(array_merge(
188: $this->_permanentAttributes, $validAttrCodes,
189: array('password'), $addrColNames,
190: array_keys($defaultAddrMap)
191: ));
192: foreach ($collection as $itemId => $item) {
193: $row = array();
194:
195:
196: foreach ($validAttrCodes as $attrCode) {
197: $attrValue = $item->getData($attrCode);
198:
199: if (isset($this->_attributeValues[$attrCode])
200: && isset($this->_attributeValues[$attrCode][$attrValue])
201: ) {
202: $attrValue = $this->_attributeValues[$attrCode][$attrValue];
203: }
204: if (null !== $attrValue) {
205: $row[$attrCode] = $attrValue;
206: }
207: }
208: $row[self::COL_WEBSITE] = $this->_websiteIdToCode[$item['website_id']];
209: $row[self::COL_STORE] = $this->_storeIdToCode[$item['store_id']];
210:
211:
212: $defaultAddrs = array();
213:
214: foreach ($defaultAddrMap as $colName => $addrAttrCode) {
215: if (!empty($item[$addrAttrCode])) {
216: $defaultAddrs[$item[$addrAttrCode]][] = $colName;
217: }
218: }
219: if (isset($customerAddrs[$itemId])) {
220: while (($addrRow = each($customerAddrs[$itemId]))) {
221: if (isset($defaultAddrs[$addrRow['key']])) {
222: foreach ($defaultAddrs[$addrRow['key']] as $colName) {
223: $row[$colName] = 1;
224: }
225: }
226: $writer->writeRow(array_merge($row, $addrRow['value']));
227:
228: $row = array();
229: }
230: } else {
231: $writer->writeRow($row);
232: }
233: }
234: return $writer->getContents();
235: }
236:
237: 238: 239: 240: 241: 242:
243: public function filterAttributeCollection(Mage_Eav_Model_Resource_Entity_Attribute_Collection $collection)
244: {
245: foreach (parent::filterAttributeCollection($collection) as $attribute) {
246: if (!empty($this->_attributeOverrides[$attribute->getAttributeCode()])) {
247: $data = $this->_attributeOverrides[$attribute->getAttributeCode()];
248:
249: if (isset($data['options_method']) && method_exists($this, $data['options_method'])) {
250: $data['filter_options'] = $this->$data['options_method']();
251: }
252: $attribute->addData($data);
253: }
254: }
255: return $collection;
256: }
257:
258: 259: 260: 261: 262:
263: public function getAttributeCollection()
264: {
265: return Mage::getResourceModel('customer/attribute_collection');
266: }
267:
268: 269: 270: 271: 272:
273: public function getEntityTypeCode()
274: {
275: return 'customer';
276: }
277: }
278: