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: class Mage_Eav_Model_Convert_Adapter_Entity
29: extends Mage_Dataflow_Model_Convert_Adapter_Abstract
30: {
31: 32: 33: 34: 35:
36: protected $_store;
37:
38: protected $_filter = array();
39: protected $_joinFilter = array();
40: protected $_joinAttr = array();
41: protected $_attrToDb;
42: protected $_joinField = array();
43:
44: 45: 46: 47: 48:
49: public function getStoreId()
50: {
51: if (is_null($this->_store)) {
52: try {
53: $this->_store = Mage::app()->getStore($this->getVar('store'));
54: }
55: catch (Exception $e) {
56: $message = Mage::helper('eav')->__('Invalid store specified');
57: $this->addException($message, Varien_Convert_Exception::FATAL);
58: throw $e;
59: }
60: }
61: return $this->_store->getId();
62: }
63:
64: 65: 66: 67: 68:
69: protected function _parseVars()
70: {
71: $varFilters = $this->getVars();
72: $filters = array();
73: foreach ($varFilters as $key => $val) {
74: if (substr($key,0,6) === 'filter') {
75: $keys = explode('/', $key, 2);
76: $filters[$keys[1]] = $val;
77: }
78: }
79: return $filters;
80: }
81:
82: public function setFilter($attrFilterArray, $attrToDb = null, $bind = null, $joinType = null)
83: {
84: if (is_null($bind)) {
85: $defBind = 'entity_id';
86: }
87: if (is_null($joinType)) {
88: $joinType = 'LEFT';
89: }
90:
91: $this->_attrToDb=$attrToDb;
92: $filters = $this->_parseVars();
93:
94: foreach ($attrFilterArray as $key => $type) {
95: if (is_array($type)) {
96: if (isset($type['bind'])) {
97: $bind = $type['bind'];
98: } else {
99: $bind = $defBind;
100: }
101: $type = $type['type'];
102: }
103:
104: if ($type == 'dateFromTo' || $type == 'datetimeFromTo') {
105: foreach ($filters as $k => $v) {
106: if (strpos($k, $key . '/') === 0) {
107: $split = explode('/', $k);
108: $filters[$key][$split[1]] = $v;
109: }
110: }
111: }
112:
113: $keyDB = (isset($this->_attrToDb[$key])) ? $this->_attrToDb[$key] : $key;
114:
115: $exp = explode('/',$key);
116:
117: if(isset($exp[1])){
118: if(isset($filters[$exp[1]])){
119: $val = $filters[$exp[1]];
120: $this->setJoinAttr(array(
121: 'attribute' => $keyDB,
122: 'bind' => $bind,
123: 'joinType' => $joinType
124: ));
125: } else {
126: $val = null;
127: }
128: $keyDB = str_replace('/','_',$keyDB);
129: } else {
130: $val = isset($filters[$key]) ? $filters[$key] : null;
131: }
132: if (is_null($val)) {
133: continue;
134: }
135: $attr = array();
136: switch ($type){
137: case 'eq':
138: $attr = array(
139: 'attribute' => $keyDB,
140: 'eq' => $val
141: );
142: break;
143: case 'like':
144: $attr = array(
145: 'attribute' => $keyDB,
146: 'like' => '%'.$val.'%'
147: );
148: break;
149: case 'startsWith':
150: $attr = array(
151: 'attribute' => $keyDB,
152: 'like' => $val.'%'
153: );
154: break;
155: case 'fromTo':
156: $attr = array(
157: 'attribute' => $keyDB,
158: 'from' => $val['from'],
159: 'to' => $val['to']
160: );
161: break;
162: case 'dateFromTo':
163: $attr = array(
164: 'attribute' => $keyDB,
165: 'from' => $val['from'],
166: 'to' => $val['to'],
167: 'date' => true
168: );
169: break;
170: case 'datetimeFromTo':
171: $attr = array(
172: 'attribute' => $keyDB,
173: 'from' => isset($val['from']) ? $val['from'] : null,
174: 'to' => isset($val['to']) ? $val['to'] : null,
175: 'datetime' => true
176: );
177: break;
178: default:
179: break;
180: }
181: $this->_filter[] = $attr;
182: }
183:
184: return $this;
185: }
186:
187: public function getFilter()
188: {
189: return $this->_filter;
190: }
191:
192: protected function getFieldValue($fields = array(), $name)
193: {
194: $result = array();
195: if ($fields && $name) {
196: foreach($fields as $index => $value) {
197: $exp = explode('/', $index);
198: if (isset($exp[1]) && $exp[0] == $name) {
199: $result[$exp[1]] = $value;
200: }
201: }
202: if ($result) return $result;
203: }
204: return false;
205: }
206:
207: public function setJoinAttr($joinAttr)
208: {
209: if(is_array($joinAttr)){
210: $joinArrAttr = array();
211: $joinArrAttr['attribute'] = isset($joinAttr['attribute']) ? $joinAttr['attribute'] : null;
212: $joinArrAttr['alias'] = isset($joinAttr['attribute']) ? str_replace('/','_',$joinAttr['attribute']):null;
213: $joinArrAttr['bind'] = isset($joinAttr['bind']) ? $joinAttr['bind'] : null;
214: $joinArrAttr['joinType'] = isset($joinAttr['joinType']) ? $joinAttr['joinType'] : null;
215: $joinArrAttr['storeId'] = isset($joinAttr['storeId']) ? $joinAttr['storeId'] : $this->getStoreId();
216: $this->_joinAttr[] = $joinArrAttr;
217: }
218:
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240:
241: public function setJoinField($joinField)
242: {
243: if (is_array($joinField)) {
244: $this->_joinField[] = $joinField;
245: }
246: }
247:
248: public function load()
249: {
250: if (!($entityType = $this->getVar('entity_type'))
251: || !(Mage::getResourceSingleton($entityType) instanceof Mage_Eav_Model_Entity_Interface)) {
252: $this->addException(Mage::helper('eav')->__('Invalid entity specified'), Varien_Convert_Exception::FATAL);
253: }
254: try {
255: $collection = $this->_getCollectionForLoad($entityType);
256:
257: if (isset($this->_joinAttr) && is_array($this->_joinAttr)) {
258: foreach ($this->_joinAttr as $val) {
259:
260: $collection->joinAttribute(
261: $val['alias'],
262: $val['attribute'],
263: $val['bind'],
264: null,
265: strtolower($val['joinType']),
266: $val['storeId']
267: );
268: }
269: }
270:
271: $filterQuery = $this->getFilter();
272: if (is_array($filterQuery)) {
273: foreach ($filterQuery as $val) {
274: $collection->addFieldToFilter(array($val));
275: }
276: }
277:
278: $joinFields = $this->_joinField;
279: if (isset($joinFields) && is_array($joinFields)) {
280: foreach ($joinFields as $field) {
281:
282: $collection->joinField(
283: $field['alias'],
284: $field['attribute'],
285: $field['field'],
286: $field['bind'],
287: $field['cond'],
288: $field['joinType']);
289: }
290: }
291:
292: 293: 294:
295: $entityIds = $collection->getAllIds();
296:
297: $message = Mage::helper('eav')->__("Loaded %d records", count($entityIds));
298: $this->addException($message);
299: }
300: catch (Varien_Convert_Exception $e) {
301: throw $e;
302: }
303: catch (Exception $e) {
304: $message = Mage::helper('eav')->__('Problem loading the collection, aborting. Error: %s', $e->getMessage());
305: $this->addException($message, Varien_Convert_Exception::FATAL);
306: }
307:
308: 309: 310:
311: $this->setData($entityIds);
312: return $this;
313: }
314:
315: 316: 317: 318: 319:
320: protected function _getCollectionForLoad($entityType)
321: {
322: return Mage::getResourceModel($entityType.'_collection');
323: }
324:
325: public function save()
326: {
327: $collection = $this->getData();
328: if ($collection instanceof Mage_Eav_Model_Entity_Collection_Abstract) {
329: $this->addException(Mage::helper('eav')->__('Entity collections expected.'), Varien_Convert_Exception::FATAL);
330: }
331:
332: $this->addException($collection->getSize().' records found.');
333:
334: if (!$collection instanceof Mage_Eav_Model_Entity_Collection_Abstract) {
335: $this->addException(Mage::helper('eav')->__('Entity collection expected.'), Varien_Convert_Exception::FATAL);
336: }
337: try {
338: $i = 0;
339: foreach ($collection->getIterator() as $model) {
340: $model->save();
341: $i++;
342: }
343: $this->addException(Mage::helper('eav')->__("Saved %d record(s).", $i));
344: }
345: catch (Varien_Convert_Exception $e) {
346: throw $e;
347: }
348: catch (Exception $e) {
349: $this->addException(Mage::helper('eav')->__('Problem saving the collection, aborting. Error: %s', $e->getMessage()),
350: Varien_Convert_Exception::FATAL);
351: }
352: return $this;
353: }
354: }
355: