PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Rule/Model/Condition/Combine.php

https://bitbucket.org/mengqing/magento-mirror
PHP | 339 lines | 226 code | 42 blank | 71 comment | 17 complexity | 1091973457986458f4d8abb7d3a05a40 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_Rule
  23. * @copyright Copyright (c) 2012 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_Rule_Model_Condition_Combine extends Mage_Rule_Model_Condition_Abstract
  27. {
  28. /**
  29. * Store all used condition models
  30. *
  31. * @var array
  32. */
  33. static protected $_conditionModels = array();
  34. /**
  35. * Retrieve new object for each requested model.
  36. * If model is requested first time, store it at static array.
  37. *
  38. * It's made by performance reasons to avoid initialization of same models each time when rules are being processed.
  39. *
  40. * @param string $modelClass
  41. * @return Mage_Rule_Model_Condition_Abstract|bool
  42. */
  43. protected function _getNewConditionModelInstance($modelClass)
  44. {
  45. if (empty($modelClass)) {
  46. return false;
  47. }
  48. if (!array_key_exists($modelClass, self::$_conditionModels)) {
  49. $model = Mage::getModel($modelClass);
  50. self::$_conditionModels[$modelClass] = $model;
  51. } else {
  52. $model = self::$_conditionModels[$modelClass];
  53. }
  54. if (!$model) {
  55. return false;
  56. }
  57. $newModel = clone $model;
  58. return $newModel;
  59. }
  60. public function __construct()
  61. {
  62. parent::__construct();
  63. $this->setType('rule/condition_combine')
  64. ->setAggregator('all')
  65. ->setValue(true)
  66. ->setConditions(array())
  67. ->setActions(array());
  68. $this->loadAggregatorOptions();
  69. if ($options = $this->getAggregatorOptions()) {
  70. foreach ($options as $aggregator=>$dummy) { $this->setAggregator($aggregator); break; }
  71. }
  72. }
  73. /* start aggregator methods */
  74. public function loadAggregatorOptions()
  75. {
  76. $this->setAggregatorOption(array(
  77. 'all' => Mage::helper('rule')->__('ALL'),
  78. 'any' => Mage::helper('rule')->__('ANY'),
  79. ));
  80. return $this;
  81. }
  82. public function getAggregatorSelectOptions()
  83. {
  84. $opt = array();
  85. foreach ($this->getAggregatorOption() as $k=>$v) {
  86. $opt[] = array('value'=>$k, 'label'=>$v);
  87. }
  88. return $opt;
  89. }
  90. public function getAggregatorName()
  91. {
  92. return $this->getAggregatorOption($this->getAggregator());
  93. }
  94. public function getAggregatorElement()
  95. {
  96. if (is_null($this->getAggregator())) {
  97. foreach ($this->getAggregatorOption() as $k=>$v) {
  98. $this->setAggregator($k);
  99. break;
  100. }
  101. }
  102. return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__aggregator', 'select', array(
  103. 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][aggregator]',
  104. 'values'=>$this->getAggregatorSelectOptions(),
  105. 'value'=>$this->getAggregator(),
  106. 'value_name'=>$this->getAggregatorName(),
  107. ))->setRenderer(Mage::getBlockSingleton('rule/editable'));
  108. }
  109. /* end aggregator methods */
  110. public function loadValueOptions()
  111. {
  112. $this->setValueOption(array(
  113. 1 => Mage::helper('rule')->__('TRUE'),
  114. 0 => Mage::helper('rule')->__('FALSE'),
  115. ));
  116. return $this;
  117. }
  118. public function addCondition($condition)
  119. {
  120. $condition->setRule($this->getRule());
  121. $condition->setObject($this->getObject());
  122. $condition->setPrefix($this->getPrefix());
  123. $conditions = $this->getConditions();
  124. $conditions[] = $condition;
  125. if (!$condition->getId()) {
  126. $condition->setId($this->getId().'--'.sizeof($conditions));
  127. }
  128. $this->setData($this->getPrefix(), $conditions);
  129. return $this;
  130. }
  131. public function getValueElementType()
  132. {
  133. return 'select';
  134. }
  135. /**
  136. * Returns array containing conditions in the collection
  137. *
  138. * Output example:
  139. * array(
  140. * 'type'=>'combine',
  141. * 'operator'=>'ALL',
  142. * 'value'=>'TRUE',
  143. * 'conditions'=>array(
  144. * {condition::asArray},
  145. * {combine::asArray},
  146. * {quote_item_combine::asArray}
  147. * )
  148. * )
  149. *
  150. * @return array
  151. */
  152. public function asArray(array $arrAttributes = array())
  153. {
  154. $out = parent::asArray();
  155. $out['aggregator'] = $this->getAggregator();
  156. foreach ($this->getConditions() as $condition) {
  157. $out['conditions'][] = $condition->asArray();
  158. }
  159. return $out;
  160. }
  161. public function asXml($containerKey='conditions', $itemKey='condition')
  162. {
  163. $xml = "<aggregator>".$this->getAggregator()."</aggregator>"
  164. ."<value>".$this->getValue()."</value>"
  165. ."<$containerKey>";
  166. foreach ($this->getConditions() as $condition) {
  167. $xml .= "<$itemKey>".$condition->asXml()."</$itemKey>";
  168. }
  169. $xml .= "</$containerKey>";
  170. return $xml;
  171. }
  172. public function loadArray($arr, $key='conditions')
  173. {
  174. $this->setAggregator(isset($arr['aggregator']) ? $arr['aggregator']
  175. : (isset($arr['attribute']) ? $arr['attribute'] : null))
  176. ->setValue(isset($arr['value']) ? $arr['value']
  177. : (isset($arr['operator']) ? $arr['operator'] : null));
  178. if (!empty($arr[$key]) && is_array($arr[$key])) {
  179. foreach ($arr[$key] as $condArr) {
  180. try {
  181. $cond = $this->_getNewConditionModelInstance($condArr['type']);
  182. if ($cond) {
  183. $this->addCondition($cond);
  184. $cond->loadArray($condArr, $key);
  185. }
  186. } catch (Exception $e) {
  187. Mage::logException($e);
  188. }
  189. }
  190. }
  191. return $this;
  192. }
  193. public function loadXml($xml)
  194. {
  195. if (is_string($xml)) {
  196. $xml = simplexml_load_string($xml);
  197. }
  198. $arr = parent::loadXml($xml);
  199. foreach ($xml->conditions->children() as $condition) {
  200. $arr['conditions'] = parent::loadXml($condition);
  201. }
  202. $this->loadArray($arr);
  203. return $this;
  204. }
  205. public function asHtml()
  206. {
  207. $html = $this->getTypeElement()->getHtml().
  208. Mage::helper('rule')->__('If %s of these conditions are %s:', $this->getAggregatorElement()->getHtml(), $this->getValueElement()->getHtml());
  209. if ($this->getId() != '1') {
  210. $html.= $this->getRemoveLinkHtml();
  211. }
  212. return $html;
  213. }
  214. public function getNewChildElement()
  215. {
  216. return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__new_child', 'select', array(
  217. 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][new_child]',
  218. 'values'=>$this->getNewChildSelectOptions(),
  219. 'value_name'=>$this->getNewChildName(),
  220. ))->setRenderer(Mage::getBlockSingleton('rule/newchild'));
  221. }
  222. public function asHtmlRecursive()
  223. {
  224. $html = $this->asHtml().'<ul id="'.$this->getPrefix().'__'.$this->getId().'__children" class="rule-param-children">';
  225. foreach ($this->getConditions() as $cond) {
  226. $html .= '<li>'.$cond->asHtmlRecursive().'</li>';
  227. }
  228. $html .= '<li>'.$this->getNewChildElement()->getHtml().'</li></ul>';
  229. return $html;
  230. }
  231. public function asString($format='')
  232. {
  233. $str = Mage::helper('rule')->__("If %s of these conditions are %s:", $this->getAggregatorName(), $this->getValueName());
  234. return $str;
  235. }
  236. public function asStringRecursive($level=0)
  237. {
  238. $str = parent::asStringRecursive($level);
  239. foreach ($this->getConditions() as $cond) {
  240. $str .= "\n".$cond->asStringRecursive($level+1);
  241. }
  242. return $str;
  243. }
  244. public function validate(Varien_Object $object)
  245. {
  246. if (!$this->getConditions()) {
  247. return true;
  248. }
  249. $all = $this->getAggregator() === 'all';
  250. $true = (bool)$this->getValue();
  251. foreach ($this->getConditions() as $cond) {
  252. $validated = $cond->validate($object);
  253. if ($all && $validated !== $true) {
  254. return false;
  255. } elseif (!$all && $validated === $true) {
  256. return true;
  257. }
  258. }
  259. return $all ? true : false;
  260. }
  261. public function setJsFormObject($form)
  262. {
  263. $this->setData('js_form_object', $form);
  264. foreach ($this->getConditions() as $condition) {
  265. $condition->setJsFormObject($form);
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Get conditions, if current prefix is undefined use 'conditions' key
  271. *
  272. * @return array
  273. */
  274. public function getConditions()
  275. {
  276. $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
  277. return $this->getData($key);
  278. }
  279. /**
  280. * Set conditions, if current prefix is undefined use 'conditions' key
  281. *
  282. * @param array $conditions
  283. * @return Mage_Rule_Model_Condition_Combine
  284. */
  285. public function setConditions($conditions)
  286. {
  287. $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
  288. return $this->setData($key, $conditions);
  289. }
  290. /**
  291. * Getter for "Conditions Combination" select option for recursive combines
  292. */
  293. protected function _getRecursiveChildSelectOption()
  294. {
  295. return array('value' => $this->getType(), 'label' => Mage::helper('rule')->__('Conditions Combination'));
  296. }
  297. }