PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Eav/Model/Convert/Adapter/Entity.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 354 lines | 253 code | 29 blank | 72 comment | 35 complexity | 0ade841607bab74e7a483d455b9f52f9 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Eav
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Eav_Model_Convert_Adapter_Entity
  27. extends Mage_Dataflow_Model_Convert_Adapter_Abstract
  28. {
  29. /**
  30. * Current store model
  31. *
  32. * @var Mage_Core_Model_Store
  33. */
  34. protected $_store;
  35. protected $_filter = array();
  36. protected $_joinFilter = array();
  37. protected $_joinAttr = array();
  38. protected $_attrToDb;
  39. protected $_joinField = array();
  40. /**
  41. * Retrieve store Id
  42. *
  43. * @return int
  44. */
  45. public function getStoreId()
  46. {
  47. if (is_null($this->_store)) {
  48. try {
  49. $this->_store = Mage::app()->getStore($this->getVar('store'));
  50. }
  51. catch (Exception $e) {
  52. $message = Mage::helper('eav')->__('Invalid store specified');
  53. $this->addException($message, Varien_Convert_Exception::FATAL);
  54. throw $e;
  55. }
  56. }
  57. return $this->_store->getId();
  58. }
  59. /**
  60. * @param $attrFilter - $attrArray['attrDB'] = ['like','eq','fromTo','dateFromTo]
  61. * @param $attrToDb - attribute name to DB field
  62. * @return Mage_Eav_Model_Convert_Adapter_Entity
  63. */
  64. protected function _parseVars()
  65. {
  66. $varFilters = $this->getVars();
  67. $filters = array();
  68. foreach ($varFilters as $key => $val) {
  69. if (substr($key,0,6) === 'filter') {
  70. $keys = explode('/', $key, 2);
  71. $filters[$keys[1]] = $val;
  72. }
  73. }
  74. return $filters;
  75. }
  76. public function setFilter($attrFilterArray, $attrToDb = null, $bind = null, $joinType = null)
  77. {
  78. if (is_null($bind)) {
  79. $defBind = 'entity_id';
  80. }
  81. if (is_null($joinType)) {
  82. $joinType = 'LEFT';
  83. }
  84. $this->_attrToDb=$attrToDb;
  85. $filters = $this->_parseVars();
  86. foreach ($attrFilterArray as $key => $type) {
  87. if (is_array($type)) {
  88. if (isset($type['bind'])) {
  89. $bind = $type['bind'];
  90. } else {
  91. $bind = $defBind;
  92. }
  93. $type = $type['type'];
  94. }
  95. if ($type == 'dateFromTo' || $type == 'datetimeFromTo') {
  96. foreach ($filters as $k => $v) {
  97. if (strpos($k, $key . '/') === 0) {
  98. $split = explode('/', $k);
  99. $filters[$key][$split[1]] = $v;
  100. }
  101. }
  102. }
  103. $keyDB = (isset($this->_attrToDb[$key])) ? $this->_attrToDb[$key] : $key;
  104. $exp = explode('/',$key);
  105. if(isset($exp[1])){
  106. if(isset($filters[$exp[1]])){
  107. $val = $filters[$exp[1]];
  108. $this->setJoinAttr(array(
  109. 'attribute' => $keyDB,
  110. 'bind' => $bind,
  111. 'joinType' => $joinType
  112. ));
  113. } else {
  114. $val = null;
  115. }
  116. $keyDB = str_replace('/','_',$keyDB);
  117. } else {
  118. $val = isset($filters[$key]) ? $filters[$key] : null;
  119. }
  120. if (is_null($val)) {
  121. continue;
  122. }
  123. $attr = array();
  124. switch ($type){
  125. case 'eq':
  126. $attr = array(
  127. 'attribute' => $keyDB,
  128. 'eq' => $val
  129. );
  130. break;
  131. case 'like':
  132. $attr = array(
  133. 'attribute' => $keyDB,
  134. 'like' => '%'.$val.'%'
  135. );
  136. break;
  137. case 'startsWith':
  138. $attr = array(
  139. 'attribute' => $keyDB,
  140. 'like' => $val.'%'
  141. );
  142. break;
  143. case 'fromTo':
  144. $attr = array(
  145. 'attribute' => $keyDB,
  146. 'from' => $val['from'],
  147. 'to' => $val['to']
  148. );
  149. break;
  150. case 'dateFromTo':
  151. $attr = array(
  152. 'attribute' => $keyDB,
  153. 'from' => $val['from'],
  154. 'to' => $val['to'],
  155. 'date' => true
  156. );
  157. break;
  158. case 'datetimeFromTo':
  159. $attr = array(
  160. 'attribute' => $keyDB,
  161. 'from' => isset($val['from']) ? $val['from'] : null,
  162. 'to' => isset($val['to']) ? $val['to'] : null,
  163. 'datetime' => true
  164. );
  165. break;
  166. default:
  167. break;
  168. }
  169. $this->_filter[] = $attr;
  170. }
  171. return $this;
  172. }
  173. public function getFilter()
  174. {
  175. return $this->_filter;
  176. }
  177. protected function getFieldValue($fields = array(), $name)
  178. {
  179. $result = array();
  180. if ($fields && $name) {
  181. foreach($fields as $index => $value) {
  182. $exp = explode('/', $index);
  183. if (isset($exp[1]) && $exp[0] == $name) {
  184. $result[$exp[1]] = $value;
  185. }
  186. }
  187. if ($result) return $result;
  188. }
  189. return false;
  190. }
  191. public function setJoinAttr($joinAttr)
  192. {
  193. if(is_array($joinAttr)){
  194. $joinArrAttr = array();
  195. $joinArrAttr['attribute'] = isset($joinAttr['attribute']) ? $joinAttr['attribute'] : null;
  196. $joinArrAttr['alias'] = isset($joinAttr['attribute']) ? str_replace('/','_',$joinAttr['attribute']):null;
  197. $joinArrAttr['bind'] = isset($joinAttr['bind']) ? $joinAttr['bind'] : null;
  198. $joinArrAttr['joinType'] = isset($joinAttr['joinType']) ? $joinAttr['joinType'] : null;
  199. $joinArrAttr['storeId'] = isset($joinAttr['storeId']) ? $joinAttr['storeId'] : $this->getStoreId();
  200. $this->_joinAttr[] = $joinArrAttr;
  201. }
  202. }
  203. /**
  204. * Add join field
  205. *
  206. * @param array $joinField Variable should be have view:
  207. * Example:
  208. * array(
  209. * 'alias' => 'alias_table',
  210. * 'attribute' => 'table_name', //table name, must be used path of table like 'module/table_name'
  211. * 'field' => 'field_name', //selected field name (optional)
  212. * //bind main condition
  213. * //left field use for joined table
  214. * //and right field use for main table of collection
  215. * //NOTE: around '=' cannot be used ' ' (space) because on the exploding not use space trimming
  216. * 'bind' => 'self_item_id=other_id',
  217. * 'cond' => 'alias_table.entity_id = e.entity_id', //additional condition (optional)
  218. * 'joinType' => 'LEFT'
  219. * )
  220. * NOTE: Optional key must be have NULL at least
  221. * @return void
  222. */
  223. public function setJoinField($joinField)
  224. {
  225. if (is_array($joinField)) {
  226. $this->_joinField[] = $joinField;
  227. }
  228. }
  229. public function load()
  230. {
  231. if (!($entityType = $this->getVar('entity_type'))
  232. || !(Mage::getResourceSingleton($entityType) instanceof Mage_Eav_Model_Entity_Interface)) {
  233. $this->addException(Mage::helper('eav')->__('Invalid entity specified'), Varien_Convert_Exception::FATAL);
  234. }
  235. try {
  236. $collection = $this->_getCollectionForLoad($entityType);
  237. if (isset($this->_joinAttr) && is_array($this->_joinAttr)) {
  238. foreach ($this->_joinAttr as $val) {
  239. // print_r($val);
  240. $collection->joinAttribute(
  241. $val['alias'],
  242. $val['attribute'],
  243. $val['bind'],
  244. null,
  245. strtolower($val['joinType']),
  246. $val['storeId']
  247. );
  248. }
  249. }
  250. $filterQuery = $this->getFilter();
  251. if (is_array($filterQuery)) {
  252. foreach ($filterQuery as $val) {
  253. $collection->addFieldToFilter(array($val));
  254. }
  255. }
  256. $joinFields = $this->_joinField;
  257. if (isset($joinFields) && is_array($joinFields)) {
  258. foreach ($joinFields as $field) {
  259. // print_r($field);
  260. $collection->joinField(
  261. $field['alias'],
  262. $field['attribute'],
  263. $field['field'],
  264. $field['bind'],
  265. $field['cond'],
  266. $field['joinType']);
  267. }
  268. }
  269. /**
  270. * Load collection ids
  271. */
  272. $entityIds = $collection->getAllIds();
  273. $message = Mage::helper('eav')->__("Loaded %d records", count($entityIds));
  274. $this->addException($message);
  275. }
  276. catch (Varien_Convert_Exception $e) {
  277. throw $e;
  278. }
  279. catch (Exception $e) {
  280. $message = Mage::helper('eav')->__('Problem loading the collection, aborting. Error: %s', $e->getMessage());
  281. $this->addException($message, Varien_Convert_Exception::FATAL);
  282. }
  283. /**
  284. * Set collection ids
  285. */
  286. $this->setData($entityIds);
  287. return $this;
  288. }
  289. /**
  290. * Retrieve collection for load
  291. *
  292. * @return Mage_Eav_Model_Entity_Collection
  293. */
  294. protected function _getCollectionForLoad($entityType)
  295. {
  296. return Mage::getResourceModel($entityType.'_collection');
  297. }
  298. public function save()
  299. {
  300. $collection = $this->getData();
  301. if ($collection instanceof Mage_Eav_Model_Entity_Collection_Abstract) {
  302. $this->addException(Mage::helper('eav')->__('Entity collections expected.'), Varien_Convert_Exception::FATAL);
  303. }
  304. $this->addException($collection->getSize().' records found.');
  305. if (!$collection instanceof Mage_Eav_Model_Entity_Collection_Abstract) {
  306. $this->addException(Mage::helper('eav')->__('Entity collection expected.'), Varien_Convert_Exception::FATAL);
  307. }
  308. try {
  309. $i = 0;
  310. foreach ($collection->getIterator() as $model) {
  311. $model->save();
  312. $i++;
  313. }
  314. $this->addException(Mage::helper('eav')->__("Saved %d record(s).", $i));
  315. }
  316. catch (Varien_Convert_Exception $e) {
  317. throw $e;
  318. }
  319. catch (Exception $e) {
  320. $this->addException(Mage::helper('eav')->__('Problem saving the collection, aborting. Error: %s', $e->getMessage()),
  321. Varien_Convert_Exception::FATAL);
  322. }
  323. return $this;
  324. }
  325. }