PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Enterprise/TargetRule/Model/Actions/Condition/Product/Attributes.php

https://bitbucket.org/kdms/sh-magento
PHP | 381 lines | 231 code | 27 blank | 123 comment | 49 complexity | cc35959647608da38f95cae3e635e639 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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 Enterprise
  22. * @package Enterprise_TargetRule
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * TargetRule Action Product Attributes Condition Model
  28. *
  29. * @category Enterprise
  30. * @package Enterprise_TargetRule
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Enterprise_TargetRule_Model_Actions_Condition_Product_Attributes
  34. extends Enterprise_TargetRule_Model_Rule_Condition_Product_Attributes
  35. {
  36. /**
  37. * Value type values constants
  38. *
  39. */
  40. const VALUE_TYPE_CONSTANT = 'constant';
  41. const VALUE_TYPE_SAME_AS = 'same_as';
  42. const VALUE_TYPE_CHILD_OF = 'child_of';
  43. /**
  44. * Define action type and default value
  45. *
  46. */
  47. public function __construct()
  48. {
  49. parent::__construct();
  50. $this->setType('enterprise_targetrule/actions_condition_product_attributes');
  51. $this->setValue(null);
  52. $this->setValueType(self::VALUE_TYPE_SAME_AS);
  53. }
  54. /**
  55. * Add special action product attributes
  56. *
  57. * @param array $attributes
  58. */
  59. protected function _addSpecialAttributes(array &$attributes)
  60. {
  61. parent::_addSpecialAttributes($attributes);
  62. $attributes['type_id'] = Mage::helper('catalogrule')->__('Type');
  63. }
  64. /**
  65. * Retrieve value by option
  66. * Rewrite for Retrieve options by Product Type attribute
  67. *
  68. * @param mixed $option
  69. * @return string
  70. */
  71. public function getValueOption($option = null)
  72. {
  73. if (!$this->getData('value_option') && $this->getAttribute() == 'type_id') {
  74. $options = Mage::getSingleton('catalog/product_type')->getAllOption();
  75. $this->setData('value_option', $options);
  76. }
  77. return parent::getValueOption($option);
  78. }
  79. /**
  80. * Retrieve select option values
  81. * Rewrite Rewrite for Retrieve options by Product Type attribute
  82. *
  83. * @return array
  84. */
  85. public function getValueSelectOptions()
  86. {
  87. if (!$this->getData('value_select_options') && $this->getAttribute() == 'type_id') {
  88. $options = Mage::getSingleton('catalog/product_type')->getAllOptions();
  89. $this->setData('value_select_options', $options);
  90. }
  91. return parent::getValueSelectOptions();
  92. }
  93. /**
  94. * Retrieve input type
  95. * Rewrite for define input type for Product Type attribute
  96. *
  97. * @return string
  98. */
  99. public function getInputType()
  100. {
  101. $attributeCode = $this->getAttribute();
  102. if ($attributeCode == 'type_id') {
  103. return 'select';
  104. }
  105. return parent::getInputType();
  106. }
  107. /**
  108. * Retrieve value element type
  109. * Rewrite for define value element type for Product Type attribute
  110. *
  111. * @return string
  112. */
  113. public function getValueElementType()
  114. {
  115. $attributeCode = $this->getAttribute();
  116. if ($attributeCode == 'type_id') {
  117. return 'select';
  118. }
  119. return parent::getValueElementType();
  120. }
  121. /**
  122. * Retrieve model content as HTML
  123. * Rewrite for add value type chooser
  124. *
  125. * @return string
  126. */
  127. public function asHtml()
  128. {
  129. return Mage::helper('enterprise_targetrule')->__('Product %s%s%s%s%s%s%s', $this->getTypeElementHtml(), $this->getAttributeElementHtml(), $this->getOperatorElementHtml(), $this->getValueTypeElementHtml(), $this->getValueElementHtml(), $this->getRemoveLinkHtml(), $this->getChooserContainerHtml());
  130. }
  131. /**
  132. * Returns options for value type select box
  133. *
  134. * @return array
  135. */
  136. public function getValueTypeOptions()
  137. {
  138. $options = array(
  139. array(
  140. 'value' => self::VALUE_TYPE_CONSTANT,
  141. 'label' => Mage::helper('enterprise_targetrule')->__('Constant Value')
  142. )
  143. );
  144. if ($this->getAttribute() == 'category_ids') {
  145. $options[] = array(
  146. 'value' => self::VALUE_TYPE_SAME_AS,
  147. 'label' => Mage::helper('enterprise_targetrule')->__('the Same as Matched Product Categories')
  148. );
  149. $options[] = array(
  150. 'value' => self::VALUE_TYPE_CHILD_OF,
  151. 'label' => Mage::helper('enterprise_targetrule')->__('the Child of the Matched Product Categories')
  152. );
  153. } else {
  154. $options[] = array(
  155. 'value' => self::VALUE_TYPE_SAME_AS,
  156. 'label' => Mage::helper('enterprise_targetrule')->__('Matched Product %s', $this->getAttributeName())
  157. );
  158. }
  159. return $options;
  160. }
  161. /**
  162. * Retrieve Value Type display name
  163. *
  164. * @return string
  165. */
  166. public function getValueTypeName()
  167. {
  168. $options = $this->getValueTypeOptions();
  169. foreach ($options as $option) {
  170. if ($option['value'] == $this->getValueType()) {
  171. return $option['label'];
  172. }
  173. }
  174. return '...';
  175. }
  176. /**
  177. * Retrieve Value Type Select Element
  178. *
  179. * @return Varien_Data_Form_Element_Abstract
  180. */
  181. public function getValueTypeElement()
  182. {
  183. $elementId = $this->getPrefix().'__'.$this->getId().'__value_type';
  184. $element = $this->getForm()->addField($elementId, 'select', array(
  185. 'name' => 'rule['.$this->getPrefix().']['.$this->getId().'][value_type]',
  186. 'values' => $this->getValueTypeOptions(),
  187. 'value' => $this->getValueType(),
  188. 'value_name' => $this->getValueTypeName(),
  189. 'class' => 'value-type-chooser',
  190. ))->setRenderer(Mage::getBlockSingleton('rule/editable'));
  191. return $element;
  192. }
  193. /**
  194. * Retrieve value type element HTML code
  195. *
  196. * @return string
  197. */
  198. public function getValueTypeElementHtml()
  199. {
  200. $element = $this->getValueTypeElement();
  201. return $element->getHtml();
  202. }
  203. /**
  204. * Load attribute property from array
  205. *
  206. * @param array $array
  207. * @return Enterprise_TargetRule_Model_Actions_Condition_Product_Attributes
  208. */
  209. public function loadArray($array)
  210. {
  211. parent::loadArray($array);
  212. if (isset($array['value_type'])) {
  213. $this->setValueType($array['value_type']);
  214. }
  215. return $this;
  216. }
  217. /**
  218. * Retrieve condition data as array
  219. *
  220. * @param array $arrAttributes
  221. * @return array
  222. */
  223. public function asArray(array $arrAttributes = array())
  224. {
  225. $array = parent::asArray($arrAttributes);
  226. $array['value_type'] = $this->getValueType();
  227. return $array;
  228. }
  229. /**
  230. * Retrieve condition data as string
  231. *
  232. * @param string $format
  233. * @return string
  234. */
  235. public function asString($format = '')
  236. {
  237. if (!$format) {
  238. $format = ' %s %s %s %s';
  239. }
  240. return sprintf(Mage::helper('enterprise_targetrule')->__('Target Product ') . $format,
  241. $this->getAttributeName(),
  242. $this->getOperatorName(),
  243. $this->getValueTypeName(),
  244. $this->getValueName()
  245. );
  246. }
  247. /**
  248. * Retrieve SELECT WHERE condition for product collection
  249. *
  250. * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
  251. * @param Enterprise_TargetRule_Model_Index $object
  252. * @param array $bind
  253. * @return Zend_Db_Expr
  254. */
  255. public function getConditionForCollection($collection, $object, &$bind)
  256. {
  257. /* @var $resource Enterprise_TargetRule_Model_Mysql4_Index */
  258. $attributeCode = $this->getAttribute();
  259. $valueType = $this->getValueType();
  260. $operator = $this->getOperator();
  261. $resource = $object->getResource();
  262. if ($attributeCode == 'category_ids') {
  263. $select = $object->select()
  264. ->from($resource->getTable('catalog/category_product'), 'COUNT(*)')
  265. ->where('product_id=e.entity_id');
  266. if ($valueType == self::VALUE_TYPE_SAME_AS) {
  267. $operator = ('!{}' == $operator) ? '!()' : '()';
  268. $where = $resource->getOperatorBindCondition('category_id', 'category_ids', $operator, $bind,
  269. array('bindArrayOfIds'));
  270. $select->where($where);
  271. } else if ($valueType == self::VALUE_TYPE_CHILD_OF) {
  272. $concatenated = $resource->getReadConnection()->getConcatSql(array('tp.path', "'/%'"));
  273. $subSelect = $resource->select()
  274. ->from(array('tc' => $resource->getTable('catalog/category')), 'entity_id')
  275. ->join(
  276. array('tp' => $resource->getTable('catalog/category')),
  277. "tc.path ".($operator == '!()' ? 'NOT ' : '')."LIKE {$concatenated}",
  278. array())
  279. ->where($resource->getOperatorBindCondition('tp.entity_id', 'category_ids', '()', $bind,
  280. array('bindArrayOfIds')));
  281. $select->where('category_id IN(?)', $subSelect);
  282. } else { //self::VALUE_TYPE_CONSTANT
  283. $operator = ($operator == '==') ? '' : 'NOT';
  284. $value = $resource->bindArrayOfIds($this->getValue());
  285. $where = "category_id {$operator} IN(" . implode(',', $value) . ")";
  286. $select->where($where);
  287. }
  288. return new Zend_Db_Expr(sprintf('(%s) > 0', $select->assemble()));
  289. }
  290. if ($valueType == self::VALUE_TYPE_CONSTANT) {
  291. $useBind = false;
  292. $value = $this->getValue();
  293. // split value by commas into array for operators with multiple operands
  294. if (($operator == '()' || $operator == '!()') && is_string($value) && trim($value) != '') {
  295. $value = preg_split('/\s*,\s*/', trim($value), -1, PREG_SPLIT_NO_EMPTY);
  296. }
  297. } else { //self::VALUE_TYPE_SAME_AS
  298. $useBind = true;
  299. }
  300. $attribute = $this->getAttributeObject();
  301. if (!$attribute) {
  302. return false;
  303. }
  304. if ($attribute->isStatic()) {
  305. $field = "e.{$attributeCode}";
  306. if ($useBind) {
  307. $where = $resource->getOperatorBindCondition($field, $attributeCode, $operator, $bind);
  308. } else {
  309. $where = $resource->getOperatorCondition($field, $operator, $value);
  310. }
  311. $where = sprintf('(%s)', $where);
  312. } else if ($attribute->isScopeGlobal()) {
  313. $table = $attribute->getBackendTable();
  314. $select = $object->select()
  315. ->from(array('table' => $table), 'COUNT(*)')
  316. ->where('table.entity_id = e.entity_id')
  317. ->where('table.attribute_id=?', $attribute->getId())
  318. ->where('table.store_id=?', 0);
  319. if ($useBind) {
  320. $select->where($resource->getOperatorBindCondition('table.value', $attributeCode, $operator, $bind));
  321. } else {
  322. $select->where($resource->getOperatorCondition('table.value', $operator, $value));
  323. }
  324. $select = $resource->getReadConnection()->getIfNullSql($select);
  325. $where = sprintf('(%s) > 0', $select);
  326. } else { //scope store and website
  327. $valueExpr = $resource->getReadConnection()->getCheckSql(
  328. 'attr_s.value_id > 0',
  329. 'attr_s.value',
  330. 'attr_d.value'
  331. );
  332. $table = $attribute->getBackendTable();
  333. $select = $object->select()
  334. ->from(array('attr_d' => $table), 'COUNT(*)')
  335. ->joinLeft(
  336. array('attr_s' => $table),
  337. $resource->getReadConnection()->quoteInto(
  338. 'attr_s.entity_id = attr_d.entity_id AND attr_s.attribute_id = attr_d.attribute_id'
  339. . ' AND attr_s.store_id=?', $object->getStoreId()
  340. ),
  341. array())
  342. ->where('attr_d.entity_id = e.entity_id')
  343. ->where('attr_d.attribute_id=?', $attribute->getId())
  344. ->where('attr_d.store_id=?', 0);
  345. if ($useBind) {
  346. $select->where($resource->getOperatorBindCondition($valueExpr, $attributeCode, $operator, $bind));
  347. } else {
  348. $select->where($resource->getOperatorCondition($valueExpr, $operator, $value));
  349. }
  350. $where = sprintf('(%s) > 0', $select);
  351. }
  352. return new Zend_Db_Expr($where);
  353. }
  354. }