00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Eav_Model_Convert_Adapter_Entity
00029 extends Mage_Dataflow_Model_Convert_Adapter_Abstract
00030 {
00031
00032
00033
00034
00035
00036 protected $_store;
00037
00038 protected $_filter = array();
00039 protected $_joinFilter = array();
00040 protected $_joinAttr = array();
00041 protected $_attrToDb;
00042 protected $_joinField = array();
00043
00044
00045
00046
00047
00048
00049 public function getStoreId()
00050 {
00051 if (is_null($this->_store)) {
00052 try {
00053 $this->_store = Mage::app()->getStore($this->getVar('store'));
00054 }
00055 catch (Exception $e) {
00056 $message = Mage::helper('eav')->__('Invalid store specified');
00057 $this->addException($message, Varien_Convert_Exception::FATAL);
00058 throw $e;
00059 }
00060 }
00061 return $this->_store->getId();
00062 }
00063
00064
00065
00066
00067
00068
00069 protected function _parseVars()
00070 {
00071 $varFilters = $this->getVars();
00072 $filters = array();
00073 foreach ($varFilters as $key => $val) {
00074 if (substr($key,0,6) === 'filter') {
00075 $keys = explode('/', $key, 2);
00076 $filters[$keys[1]] = $val;
00077 }
00078 }
00079 return $filters;
00080 }
00081
00082 public function setFilter($attrFilterArray, $attrToDb = null, $bind = null, $joinType = null)
00083 {
00084 if (is_null($bind)) {
00085 $defBind = 'entity_id';
00086 }
00087 if (is_null($joinType)) {
00088 $joinType = 'LEFT';
00089 }
00090
00091 $this->_attrToDb=$attrToDb;
00092 $filters = $this->_parseVars();
00093
00094 foreach ($attrFilterArray as $key => $type) {
00095 if (is_array($type)) {
00096 if (isset($type['bind'])) {
00097 $bind = $type['bind'];
00098 } else {
00099 $bind = $defBind;
00100 }
00101 $type = $type['type'];
00102 }
00103
00104 if ($type == 'dateFromTo' || $type == 'datetimeFromTo') {
00105 foreach ($filters as $k => $v) {
00106 if (strpos($k, $key . '/') === 0) {
00107 $split = split('/', $k);
00108 $filters[$key][$split[1]] = $v;
00109 }
00110 }
00111 }
00112
00113 $keyDB = (isset($this->_attrToDb[$key])) ? $this->_attrToDb[$key] : $key;
00114
00115 $exp = explode('/',$key);
00116
00117 if(isset($exp[1])){
00118 if(isset($filters[$exp[1]])){
00119 $val = $filters[$exp[1]];
00120 $this->setJoinAttr(array(
00121 'attribute' => $keyDB,
00122 'bind' => $bind,
00123 'joinType' => $joinType
00124 ));
00125 } else {
00126 $val = null;
00127 }
00128 $keyDB = str_replace('/','_',$keyDB);
00129 } else {
00130 $val = isset($filters[$key]) ? $filters[$key] : null;
00131 }
00132 if (is_null($val)) {
00133 continue;
00134 }
00135 $attr = array();
00136 switch ($type){
00137 case 'eq':
00138 $attr = array(
00139 'attribute' => $keyDB,
00140 'eq' => $val
00141 );
00142 break;
00143 case 'like':
00144 $attr = array(
00145 'attribute' => $keyDB,
00146 'like' => '%'.$val.'%'
00147 );
00148 break;
00149 case 'fromTo':
00150 $attr = array(
00151 'attribute' => $keyDB,
00152 'from' => $val['from'],
00153 'to' => $val['to']
00154 );
00155 break;
00156 case 'dateFromTo':
00157 $attr = array(
00158 'attribute' => $keyDB,
00159 'from' => $val['from'],
00160 'to' => $val['to'],
00161 'date' => true
00162 );
00163 break;
00164 case 'datetimeFromTo':
00165 $attr = array(
00166 'attribute' => $keyDB,
00167 'from' => $val['from'],
00168 'to' => $val['to'],
00169 'datetime' => true
00170 );
00171 break;
00172 default:
00173 break;
00174 }
00175 $this->_filter[] = $attr;
00176 }
00177
00178 return $this;
00179 }
00180
00181 public function getFilter()
00182 {
00183 return $this->_filter;
00184 }
00185
00186 protected function getFieldValue($fields = array(), $name)
00187 {
00188 $result = array();
00189 if ($fields && $name) {
00190 foreach($fields as $index => $value) {
00191 $exp = explode('/', $index);
00192 if (isset($exp[1]) && $exp[0] == $name) {
00193 $result[$exp[1]] = $value;
00194 }
00195 }
00196 if ($result) return $result;
00197 }
00198 return false;
00199 }
00200
00201 public function setJoinAttr($joinAttr)
00202 {
00203 if(is_array($joinAttr)){
00204 $joinArrAttr = array();
00205 $joinArrAttr['attribute'] = isset($joinAttr['attribute']) ? $joinAttr['attribute'] : null;
00206 $joinArrAttr['alias'] = isset($joinAttr['attribute']) ? str_replace('/','_',$joinAttr['attribute']):null;
00207 $joinArrAttr['bind'] = isset($joinAttr['bind']) ? $joinAttr['bind'] : null;
00208 $joinArrAttr['joinType'] = isset($joinAttr['joinType']) ? $joinAttr['joinType'] : null;
00209 $joinArrAttr['storeId'] = isset($joinAttr['storeId']) ? $joinAttr['storeId'] : $this->getStoreId();
00210 $this->_joinAttr[] = $joinArrAttr;
00211 }
00212
00213 }
00214
00215 public function setJoinField($joinField)
00216 {
00217 if (is_array($joinField)) {
00218 $this->_joinField[] = $joinField;
00219 }
00220 }
00221 public function load()
00222 {
00223 if (!($entityType = $this->getVar('entity_type'))
00224 || !(Mage::getResourceSingleton($entityType) instanceof Mage_Eav_Model_Entity_Interface)) {
00225 $this->addException(Mage::helper('eav')->__('Invalid entity specified'), Varien_Convert_Exception::FATAL);
00226 }
00227 try {
00228 $collection = $this->_getCollectionForLoad($entityType);
00229
00230 if (isset($this->_joinAttr) && is_array($this->_joinAttr)) {
00231 foreach ($this->_joinAttr as $val) {
00232
00233 $collection->joinAttribute(
00234 $val['alias'],
00235 $val['attribute'],
00236 $val['bind'],
00237 null,
00238 strtolower($val['joinType']),
00239 $val['storeId']
00240 );
00241 }
00242 }
00243
00244 $filterQuery = $this->getFilter();
00245 if (is_array($filterQuery)) {
00246 foreach ($filterQuery as $val) {
00247 $collection->addFieldToFilter(array($val));
00248 }
00249 }
00250
00251 $joinFields = $this->_joinField;
00252 if (isset($joinFields) && is_array($joinFields)) {
00253 foreach ($joinFields as $field) {
00254
00255 $collection->joinField(
00256 $field['alias'],
00257 $field['attribute'],
00258 $field['field'],
00259 $field['bind'],
00260 $field['cond'],
00261 $field['joinType']);
00262 }
00263 }
00264
00265
00266
00267
00268 $entityIds = $collection->getAllIds();
00269
00270 $message = Mage::helper('eav')->__("Loaded %d records", count($entityIds));
00271 $this->addException($message);
00272 }
00273 catch (Varien_Convert_Exception $e) {
00274 throw $e;
00275 }
00276 catch (Exception $e) {
00277 $message = Mage::helper('eav')->__('Problem loading the collection, aborting. Error: %s', $e->getMessage());
00278 $this->addException($message, Varien_Convert_Exception::FATAL);
00279 }
00280
00281
00282
00283
00284 $this->setData($entityIds);
00285 return $this;
00286 }
00287
00288
00289
00290
00291
00292
00293 protected function _getCollectionForLoad($entityType)
00294 {
00295 return Mage::getResourceModel($entityType.'_collection');
00296 }
00297
00298 public function save()
00299 {
00300 $collection = $this->getData();
00301 if ($collection instanceof Mage_Eav_Model_Entity_Collection_Abstract) {
00302 $this->addException(Mage::helper('eav')->__('Entity collections expected'), Varien_Convert_Exception::FATAL);
00303 }
00304
00305 $this->addException($collection->getSize().' records found.');
00306
00307 if (!$collection instanceof Mage_Eav_Model_Entity_Collection_Abstract) {
00308 $this->addException(Mage::helper('eav')->__('Entity collection expected'), Varien_Convert_Exception::FATAL);
00309 }
00310 try {
00311 $i = 0;
00312 foreach ($collection->getIterator() as $model) {
00313 $model->save();
00314 $i++;
00315 }
00316 $this->addException(Mage::helper('eav')->__("Saved ".$i." record(s)"));
00317 }
00318 catch (Varien_Convert_Exception $e) {
00319 throw $e;
00320 }
00321 catch (Exception $e) {
00322 $this->addException(Mage::helper('eav')->__('Problem saving the collection, aborting. Error: %s', $e->getMessage()),
00323 Varien_Convert_Exception::FATAL);
00324 }
00325 return $this;
00326 }
00327 }