PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/CatalogRule/Model/Rule/Condition/Product.php

https://github.com/gryzz/crystal_magento
PHP | 421 lines | 251 code | 45 blank | 125 comment | 51 complexity | c468980f5b636c811b14de242ec09cf5 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@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 Mage
  22. * @package Mage_CatalogRule
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_CatalogRule_Model_Rule_Condition_Product extends Mage_Rule_Model_Condition_Abstract
  27. {
  28. /**
  29. * All attribute values as array in form:
  30. * array(
  31. * [entity_id_1] => array(
  32. * [store_id_1] => store_value_1,
  33. * [store_id_2] => store_value_2,
  34. * ...
  35. * [store_id_n] => store_value_n
  36. * ),
  37. * ...
  38. * )
  39. *
  40. * Will be set only for not global scope attribute
  41. *
  42. * @var array
  43. */
  44. protected $_entityAttributeValues = null;
  45. /**
  46. * Attribute data key that indicates whether it should be used for rules
  47. *
  48. * @var string
  49. */
  50. protected $_isUsedForRuleProperty = 'is_used_for_promo_rules';
  51. /**
  52. * Retrieve attribute object
  53. *
  54. * @return Mage_Catalog_Model_Resource_Eav_Attribute
  55. */
  56. public function getAttributeObject()
  57. {
  58. try {
  59. $obj = Mage::getSingleton('eav/config')
  60. ->getAttribute('catalog_product', $this->getAttribute());
  61. }
  62. catch (Exception $e) {
  63. $obj = new Varien_Object();
  64. $obj->setEntity(Mage::getResourceSingleton('catalog/product'))
  65. ->setFrontendInput('text');
  66. }
  67. return $obj;
  68. }
  69. /**
  70. * Add special attributes
  71. *
  72. * @param array $attributes
  73. */
  74. protected function _addSpecialAttributes(array &$attributes)
  75. {
  76. $attributes['attribute_set_id'] = Mage::helper('catalogrule')->__('Attribute Set');
  77. $attributes['category_ids'] = Mage::helper('catalogrule')->__('Category');
  78. }
  79. /**
  80. * Load attribute options
  81. *
  82. * @return Mage_CatalogRule_Model_Rule_Condition_Product
  83. */
  84. public function loadAttributeOptions()
  85. {
  86. $productAttributes = Mage::getResourceSingleton('catalog/product')
  87. ->loadAllAttributes()
  88. ->getAttributesByCode();
  89. $attributes = array();
  90. foreach ($productAttributes as $attribute) {
  91. /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
  92. if (!$attribute->isAllowedForRuleCondition() || !$attribute->getDataUsingMethod($this->_isUsedForRuleProperty)) {
  93. continue;
  94. }
  95. $attributes[$attribute->getAttributeCode()] = $attribute->getFrontendLabel();
  96. }
  97. $this->_addSpecialAttributes($attributes);
  98. asort($attributes);
  99. $this->setAttributeOption($attributes);
  100. return $this;
  101. }
  102. /**
  103. * Retrieve value by option
  104. *
  105. * @param mixed $option
  106. * @return string
  107. */
  108. public function getValueOption($option=null)
  109. {
  110. if (!$this->getData('value_option')) {
  111. if ($this->getAttribute()==='attribute_set_id') {
  112. $entityTypeId = Mage::getSingleton('eav/config')
  113. ->getEntityType('catalog_product')->getId();
  114. $options = Mage::getResourceModel('eav/entity_attribute_set_collection')
  115. ->setEntityTypeFilter($entityTypeId)
  116. ->load()
  117. ->toOptionHash();
  118. $this->setData('value_option', $options);
  119. } elseif (is_object($this->getAttributeObject()) && $this->getAttributeObject()->usesSource()) {
  120. if ($this->getAttributeObject()->getFrontendInput() == 'multiselect') {
  121. $addEmptyOption = false;
  122. } else {
  123. $addEmptyOption = true;
  124. }
  125. $optionsArr = $this->getAttributeObject()->getSource()->getAllOptions($addEmptyOption);
  126. $options = array();
  127. foreach ($optionsArr as $o) {
  128. if (is_array($o['value'])) {
  129. } else {
  130. $options[$o['value']] = $o['label'];
  131. }
  132. }
  133. $this->setData('value_option', $options);
  134. }
  135. }
  136. return $this->getData('value_option'.(!is_null($option) ? '/'.$option : ''));
  137. }
  138. /**
  139. * Retrieve select option values
  140. *
  141. * @return array
  142. */
  143. public function getValueSelectOptions()
  144. {
  145. if (!$this->getData('value_select_options')) {
  146. if ($this->getAttribute()==='attribute_set_id') {
  147. $entityTypeId = Mage::getSingleton('eav/config')
  148. ->getEntityType('catalog_product')->getId();
  149. $options = Mage::getResourceModel('eav/entity_attribute_set_collection')
  150. ->setEntityTypeFilter($entityTypeId)
  151. ->load()->toOptionArray();
  152. $this->setData('value_select_options', $options);
  153. } elseif (is_object($this->getAttributeObject()) && $this->getAttributeObject()->usesSource()) {
  154. if ($this->getAttributeObject()->getFrontendInput() == 'multiselect') {
  155. $addEmptyOption = false;
  156. } else {
  157. $addEmptyOption = true;
  158. }
  159. $optionsArr = $this->getAttributeObject()->getSource()->getAllOptions($addEmptyOption);
  160. $this->setData('value_select_options', $optionsArr);
  161. }
  162. }
  163. return $this->getData('value_select_options');
  164. }
  165. /**
  166. * Retrieve after element HTML
  167. *
  168. * @return string
  169. */
  170. public function getValueAfterElementHtml()
  171. {
  172. $html = '';
  173. switch ($this->getAttribute()) {
  174. case 'sku': case 'category_ids':
  175. $image = Mage::getDesign()->getSkinUrl('images/rule_chooser_trigger.gif');
  176. break;
  177. }
  178. if (!empty($image)) {
  179. $html = '<a href="javascript:void(0)" class="rule-chooser-trigger"><img src="' . $image . '" alt="" class="v-middle rule-chooser-trigger" title="' . Mage::helper('rule')->__('Open Chooser') . '" /></a>';
  180. }
  181. return $html;
  182. }
  183. /**
  184. * Retrieve attribute element
  185. *
  186. * @return Varien_Form_Element_Abstract
  187. */
  188. public function getAttributeElement()
  189. {
  190. $element = parent::getAttributeElement();
  191. $element->setShowAsText(true);
  192. return $element;
  193. }
  194. /**
  195. * Collect validated attributes
  196. *
  197. * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $productCollection
  198. * @return Mage_CatalogRule_Model_Rule_Condition_Product
  199. */
  200. public function collectValidatedAttributes($productCollection)
  201. {
  202. $attribute = $this->getAttribute();
  203. if ('category_ids' != $attribute) {
  204. if ($this->getAttributeObject()->isScopeGlobal()) {
  205. $attributes = $this->getRule()->getCollectedAttributes();
  206. $attributes[$attribute] = true;
  207. $this->getRule()->setCollectedAttributes($attributes);
  208. $productCollection->addAttributeToSelect($attribute, 'left');
  209. } else {
  210. $this->_entityAttributeValues = $productCollection->getAllAttributeValues($attribute);
  211. }
  212. }
  213. return $this;
  214. }
  215. /**
  216. * Retrieve input type
  217. *
  218. * @return string
  219. */
  220. public function getInputType()
  221. {
  222. if ($this->getAttribute()==='attribute_set_id') {
  223. return 'select';
  224. }
  225. if (!is_object($this->getAttributeObject())) {
  226. return 'string';
  227. }
  228. switch ($this->getAttributeObject()->getFrontendInput()) {
  229. case 'select':
  230. return 'select';
  231. case 'multiselect':
  232. return 'multiselect';
  233. case 'date':
  234. return 'date';
  235. default:
  236. return 'string';
  237. }
  238. }
  239. /**
  240. * Retrieve value element type
  241. *
  242. * @return string
  243. */
  244. public function getValueElementType()
  245. {
  246. if ($this->getAttribute()==='attribute_set_id') {
  247. return 'select';
  248. }
  249. if (!is_object($this->getAttributeObject())) {
  250. return 'text';
  251. }
  252. switch ($this->getAttributeObject()->getFrontendInput()) {
  253. case 'select':
  254. return 'select';
  255. case 'multiselect':
  256. return 'multiselect';
  257. case 'date':
  258. return 'date';
  259. default:
  260. return 'text';
  261. }
  262. }
  263. /**
  264. * Retrieve value element
  265. *
  266. * @return Varien_Data_Form_Element_Abstract
  267. */
  268. public function getValueElement()
  269. {
  270. $element = parent::getValueElement();
  271. if (is_object($this->getAttributeObject())) {
  272. switch ($this->getAttributeObject()->getFrontendInput()) {
  273. case 'date':
  274. $element->setImage(Mage::getDesign()->getSkinUrl('images/grid-cal.gif'));
  275. break;
  276. }
  277. }
  278. return $element;
  279. }
  280. /**
  281. * Retrieve value element chooser URL
  282. *
  283. * @return string
  284. */
  285. public function getValueElementChooserUrl()
  286. {
  287. $url = false;
  288. switch ($this->getAttribute()) {
  289. case 'sku': case 'category_ids':
  290. $url = 'adminhtml/promo_widget/chooser'
  291. .'/attribute/'.$this->getAttribute();
  292. if ($this->getJsFormObject()) {
  293. $url .= '/form/'.$this->getJsFormObject();
  294. }
  295. break;
  296. }
  297. return $url!==false ? Mage::helper('adminhtml')->getUrl($url) : '';
  298. }
  299. /**
  300. * Retrieve Explicit Apply
  301. *
  302. * @return bool
  303. */
  304. public function getExplicitApply()
  305. {
  306. switch ($this->getAttribute()) {
  307. case 'sku': case 'category_ids':
  308. return true;
  309. }
  310. if (is_object($this->getAttributeObject())) {
  311. switch ($this->getAttributeObject()->getFrontendInput()) {
  312. case 'date':
  313. return true;
  314. }
  315. }
  316. return false;
  317. }
  318. /**
  319. * Load array
  320. *
  321. * @param array $arr
  322. * @return Mage_CatalogRule_Model_Rule_Condition_Product
  323. */
  324. public function loadArray($arr)
  325. {
  326. $this->setAttribute(isset($arr['attribute']) ? $arr['attribute'] : false);
  327. $attribute = $this->getAttributeObject();
  328. if ($attribute && $attribute->getBackendType() == 'decimal') {
  329. $arr['value'] = isset($arr['value']) ? Mage::app()->getLocale()->getNumber($arr['value']) : false;
  330. $arr['is_value_parsed'] = isset($arr['is_value_parsed']) ? Mage::app()->getLocale()->getNumber($arr['is_value_parsed']) : false;
  331. }
  332. return parent::loadArray($arr);
  333. }
  334. /**
  335. * Validate product attrbute value for condition
  336. *
  337. * @param Varien_Object $object
  338. * @return bool
  339. */
  340. public function validate(Varien_Object $object)
  341. {
  342. $attrCode = $this->getAttribute();
  343. if ('category_ids' == $attrCode) {
  344. return $this->validateAttribute($object->getAvailableInCategories());
  345. } elseif (! isset($this->_entityAttributeValues[$object->getId()])) {
  346. $attr = $object->getResource()->getAttribute($attrCode);
  347. if ($attr && $attr->getBackendType() == 'datetime' && ! is_int($this->getValue())) {
  348. $this->setValue(strtotime($this->getValue()));
  349. $value = strtotime($object->getData($attrCode));
  350. return $this->validateAttribute($value);
  351. }
  352. if ($attr && $attr->getFrontendInput() == 'multiselect') {
  353. $value = $object->getData($attrCode);
  354. $value = strlen($value) ? explode(',', $value) : array();
  355. return $this->validateAttribute($value);
  356. }
  357. return parent::validate($object);
  358. } else {
  359. $result = false; // any valid value will set it to TRUE
  360. $oldAttrValue = $object->hasData($attrCode) ? $object->getData($attrCode) : null; // remember old attribute state
  361. foreach ($this->_entityAttributeValues[$object->getId()] as $storeId => $value) {
  362. $object->setData($attrCode, $value);
  363. $result |= parent::validate($object);
  364. if ($result) {
  365. break;
  366. }
  367. }
  368. if (is_null($oldAttrValue)) {
  369. $object->unsetData($attrCode);
  370. } else {
  371. $object->setData($attrCode, $oldAttrValue);
  372. }
  373. return (bool) $result;
  374. }
  375. }
  376. }