123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Rule\Model\Condition;
- /**
- * @api
- * @since 100.0.2
- */
- class Combine extends AbstractCondition
- {
- /**
- * @var \Magento\Rule\Model\ConditionFactory
- */
- protected $_conditionFactory;
- /**
- * @var \Psr\Log\LoggerInterface
- */
- protected $_logger;
- /**
- * @param Context $context
- * @param array $data
- */
- public function __construct(Context $context, array $data = [])
- {
- $this->_conditionFactory = $context->getConditionFactory();
- $this->_logger = $context->getLogger();
- parent::__construct($context, $data);
- $this->setType(
- \Magento\Rule\Model\Condition\Combine::class
- )->setAggregator(
- 'all'
- )->setValue(
- true
- )->setConditions(
- []
- )->setActions(
- []
- );
- $this->loadAggregatorOptions();
- $options = $this->getAggregatorOptions();
- if ($options) {
- reset($options);
- $this->setAggregator(key($options));
- }
- }
- /* start aggregator methods */
- /**
- * @return $this
- */
- public function loadAggregatorOptions()
- {
- $this->setAggregatorOption(['all' => __('ALL'), 'any' => __('ANY')]);
- return $this;
- }
- /**
- * @return array
- */
- public function getAggregatorSelectOptions()
- {
- $opt = [];
- foreach ($this->getAggregatorOption() as $key => $value) {
- $opt[] = ['value' => $key, 'label' => $value];
- }
- return $opt;
- }
- /**
- * @return string
- */
- public function getAggregatorName()
- {
- return $this->getAggregatorOption($this->getAggregator());
- }
- /**
- * @return object
- */
- public function getAggregatorElement()
- {
- if ($this->getAggregator() === null) {
- $options = $this->getAggregatorOption();
- if ($options) {
- reset($options);
- $this->setAggregator(key($options));
- }
- }
- return $this->getForm()->addField(
- $this->getPrefix() . '__' . $this->getId() . '__aggregator',
- 'select',
- [
- 'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][aggregator]',
- 'values' => $this->getAggregatorSelectOptions(),
- 'value' => $this->getAggregator(),
- 'value_name' => $this->getAggregatorName(),
- 'data-form-part' => $this->getFormName()
- ]
- )->setRenderer(
- $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
- );
- }
- /* end aggregator methods */
- /**
- * @return $this
- */
- public function loadValueOptions()
- {
- $this->setValueOption([1 => __('TRUE'), 0 => __('FALSE')]);
- return $this;
- }
- /**
- * @param object $condition
- * @return $this
- */
- public function addCondition($condition)
- {
- $condition->setRule($this->getRule());
- $condition->setObject($this->getObject());
- $condition->setPrefix($this->getPrefix());
- $conditions = $this->getConditions();
- $conditions[] = $condition;
- if (!$condition->getId()) {
- $condition->setId($this->getId() . '--' . sizeof($conditions));
- }
- $this->setData($this->getPrefix(), $conditions);
- return $this;
- }
- /**
- * @return string
- */
- public function getValueElementType()
- {
- return 'select';
- }
- /**
- * Returns array containing conditions in the collection
- *
- * Output example:
- * array(
- * 'type'=>'combine',
- * 'operator'=>'ALL',
- * 'value'=>'TRUE',
- * 'conditions'=>array(
- * {condition::asArray},
- * {combine::asArray},
- * {quote_item_combine::asArray}
- * )
- * )
- *
- * @param array $arrAttributes
- * @return array
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function asArray(array $arrAttributes = [])
- {
- $out = parent::asArray();
- $out['aggregator'] = $this->getAggregator();
- foreach ($this->getConditions() as $condition) {
- $out['conditions'][] = $condition->asArray();
- }
- return $out;
- }
- /**
- * @param string $containerKey
- * @param string $itemKey
- * @return string
- */
- public function asXml($containerKey = 'conditions', $itemKey = 'condition')
- {
- $xml = "<aggregator>" .
- $this->getAggregator() .
- "</aggregator>" .
- "<value>" .
- $this->getValue() .
- "</value>" .
- "<{$containerKey}>";
- foreach ($this->getConditions() as $condition) {
- $xml .= "<{$itemKey}>" . $condition->asXml() . "</{$itemKey}>";
- }
- $xml .= "</{$containerKey}>";
- return $xml;
- }
- /**
- * @param array $arr
- * @param string $key
- * @return $this
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function loadArray($arr, $key = 'conditions')
- {
- $this->setAggregator(
- isset($arr['aggregator']) ? $arr['aggregator'] : (isset($arr['attribute']) ? $arr['attribute'] : null)
- )->setValue(
- isset($arr['value']) ? $arr['value'] : (isset($arr['operator']) ? $arr['operator'] : null)
- );
- if (!empty($arr[$key]) && is_array($arr[$key])) {
- foreach ($arr[$key] as $conditionArr) {
- try {
- $condition = $this->_conditionFactory->create($conditionArr['type']);
- $this->addCondition($condition);
- $condition->loadArray($conditionArr, $key);
- } catch (\Exception $e) {
- $this->_logger->critical($e);
- }
- }
- }
- return $this;
- }
- /**
- * @param array|string $xml
- * @return $this
- */
- public function loadXml($xml)
- {
- if (is_string($xml)) {
- $xml = simplexml_load_string($xml);
- }
- $arr = parent::loadXml($xml);
- foreach ($xml->conditions->children() as $condition) {
- $arr['conditions'] = parent::loadXml($condition);
- }
- $this->loadArray($arr);
- return $this;
- }
- /**
- * @return string
- */
- public function asHtml()
- {
- $html = $this->getTypeElement()->getHtml() . __(
- 'If %1 of these conditions are %2:',
- $this->getAggregatorElement()->getHtml(),
- $this->getValueElement()->getHtml()
- );
- if ($this->getId() != '1') {
- $html .= $this->getRemoveLinkHtml();
- }
- return $html;
- }
- /**
- * @return $this
- */
- public function getNewChildElement()
- {
- return $this->getForm()->addField(
- $this->getPrefix() . '__' . $this->getId() . '__new_child',
- 'select',
- [
- 'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][new_child]',
- 'values' => $this->getNewChildSelectOptions(),
- 'value_name' => $this->getNewChildName(),
- 'data-form-part' => $this->getFormName()
- ]
- )->setRenderer(
- $this->_layout->getBlockSingleton(\Magento\Rule\Block\Newchild::class)
- );
- }
- /**
- * @return string
- */
- public function asHtmlRecursive()
- {
- $html = $this->asHtml() .
- '<ul id="' .
- $this->getPrefix() .
- '__' .
- $this->getId() .
- '__children" class="rule-param-children">';
- foreach ($this->getConditions() as $cond) {
- $html .= '<li>' . $cond->asHtmlRecursive() . '</li>';
- }
- $html .= '<li>' . $this->getNewChildElement()->getHtml() . '</li></ul>';
- return $html;
- }
- /**
- * @param string $format
- * @return string
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function asString($format = '')
- {
- $str = __("If %1 of these conditions are %2:", $this->getAggregatorName(), $this->getValueName());
- return $str;
- }
- /**
- * @param int $level
- * @return string
- */
- public function asStringRecursive($level = 0)
- {
- $str = parent::asStringRecursive($level);
- foreach ($this->getConditions() as $cond) {
- $str .= "\n" . $cond->asStringRecursive($level + 1);
- }
- return $str;
- }
- /**
- * @param \Magento\Framework\Model\AbstractModel $model
- * @return bool
- */
- public function validate(\Magento\Framework\Model\AbstractModel $model)
- {
- return $this->_isValid($model);
- }
- /**
- * Validate by entity ID
- *
- * @param int $entityId
- * @return mixed
- */
- public function validateByEntityId($entityId)
- {
- return $this->_isValid($entityId);
- }
- /**
- * Is entity valid
- *
- * @param int|\Magento\Framework\Model\AbstractModel $entity
- * @return bool
- */
- protected function _isValid($entity)
- {
- if (!$this->getConditions()) {
- return true;
- }
- $all = $this->getAggregator() === 'all';
- $true = (bool)$this->getValue();
- foreach ($this->getConditions() as $cond) {
- if ($entity instanceof \Magento\Framework\Model\AbstractModel) {
- $validated = $cond->validate($entity);
- } else {
- $validated = $cond->validateByEntityId($entity);
- }
- if ($all && $validated !== $true) {
- return false;
- } elseif (!$all && $validated === $true) {
- return true;
- }
- }
- return $all ? true : false;
- }
- /**
- * @param \Magento\Framework\Data\Form $form
- * @return $this
- */
- public function setJsFormObject($form)
- {
- $this->setData('js_form_object', $form);
- foreach ($this->getConditions() as $condition) {
- $condition->setJsFormObject($form);
- }
- return $this;
- }
- /**
- * Get conditions, if current prefix is undefined use 'conditions' key
- *
- * @return array
- */
- public function getConditions()
- {
- $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
- return $this->getData($key);
- }
- /**
- * Set conditions, if current prefix is undefined use 'conditions' key
- *
- * @param array $conditions
- * @return $this
- */
- public function setConditions($conditions)
- {
- $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
- return $this->setData($key, $conditions);
- }
- /**
- * Getter for "Conditions Combination" select option for recursive combines
- *
- * @return array
- */
- protected function _getRecursiveChildSelectOption()
- {
- return ['value' => $this->getType(), 'label' => __('Conditions Combination')];
- }
- }
|