![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Model/Rule/Condition/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Model\Rule\Condition; use Magento\Catalog\Model\ResourceModel\Eav\Attribute; use Magento\Framework\Data\Form\Element\AbstractElement; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Model\AbstractModel; use Magento\Rule\Model\Condition\Product\AbstractProduct; /** * Product rule condition data model * * @author Magento Core Team <[email protected]> */ class Product extends AbstractProduct { /** * Add special attributes * * @param array $attributes * @return void */ protected function _addSpecialAttributes(array &$attributes) { parent::_addSpecialAttributes($attributes); $attributes['quote_item_qty'] = __('Quantity in cart'); $attributes['quote_item_price'] = __('Price in cart'); $attributes['quote_item_row_total'] = __('Row total in cart'); $attributes['parent::category_ids'] = __('Category (Parent only)'); $attributes['children::category_ids'] = __('Category (Children Only)'); } /** * Retrieve attribute * * @return string */ public function getAttribute(): string { $attribute = $this->getData('attribute'); if (strpos($attribute, '::') !== false) { [, $attribute] = explode('::', $attribute); } return $attribute; } /** * @inheritdoc */ public function getAttributeName() { $attribute = $this->getAttribute(); if ($this->getAttributeScope()) { $attribute = $this->getAttributeScope() . '::' . $attribute; } return $this->getAttributeOption($attribute); } /** * @inheritdoc */ public function loadAttributeOptions() { $productAttributes = $this->_productResource->loadAllAttributes()->getAttributesByCode(); $attributes = []; foreach ($productAttributes as $attribute) { /* @var $attribute Attribute */ if (!$attribute->isAllowedForRuleCondition() || !$attribute->getDataUsingMethod($this->_isUsedForRuleProperty) ) { continue; } $frontLabel = $attribute->getFrontendLabel(); $attributes[$attribute->getAttributeCode()] = $frontLabel; $attributes['parent::' . $attribute->getAttributeCode()] = $frontLabel . __('(Parent Only)'); $attributes['children::' . $attribute->getAttributeCode()] = $frontLabel . __('(Children Only)'); } $this->_addSpecialAttributes($attributes); asort($attributes); $this->setAttributeOption($attributes); return $this; } /** * @inheritdoc */ public function getAttributeElementHtml() { return parent::getAttributeElementHtml() . $this->getAttributeScopeElement()->getHtml(); } /** * Retrieve form element for scope element * * @return AbstractElement */ private function getAttributeScopeElement(): AbstractElement { return $this->getForm()->addField( $this->getPrefix() . '__' . $this->getId() . '__attribute_scope', 'hidden', [ 'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][attribute_scope]', 'value' => $this->getAttributeScope(), 'no_span' => true, 'class' => 'hidden', 'data-form-part' => $this->getFormName(), ] ); } /** * Set attribute value * * @param string $value * @return void */ public function setAttribute(string $value) { if (strpos($value, '::') !== false) { [$scope, $attribute] = explode('::', $value); $this->setData('attribute_scope', $scope); $this->setData('attribute', $attribute); } else { $this->setData('attribute', $value); } } /** * @inheritdoc */ public function loadArray($arr) { parent::loadArray($arr); $this->setAttributeScope($arr['attribute_scope'] ?? null); return $this; } /** * @inheritdoc */ public function asArray(array $arrAttributes = []) { $out = parent::asArray($arrAttributes); $out['attribute_scope'] = $this->getAttributeScope(); return $out; } /** * Validate Product Rule Condition * * @param AbstractModel $model * * @return bool * @throws NoSuchEntityException */ public function validate(AbstractModel $model) { //@todo reimplement this method when is fixed MAGETWO-5713 /** @var \Magento\Catalog\Model\Product $product */ $product = $model->getProduct(); if (!$product instanceof \Magento\Catalog\Model\Product) { $product = $this->productRepository->getById($model->getProductId()); } $product->setQuoteItemQty( $model->getQty() )->setQuoteItemPrice( $model->getPrice() // possible bug: need to use $model->getBasePrice() )->setQuoteItemRowTotal( $model->getBaseRowTotal() ); $attrCode = $this->getAttribute(); if ($attrCode === 'category_ids') { return $this->validateAttribute($this->_getAvailableInCategories($product->getId())); } if ($attrCode === 'quote_item_price') { $numericOperations = $this->getDefaultOperatorInputByType()['numeric']; if (in_array($this->getOperator(), $numericOperations)) { $this->setData('value', $this->getFormattedPrice($this->getValue())); } } return $this->validateFixed($product); } /** * Retrieve value element chooser URL * * @return string */ public function getValueElementChooserUrl() { $url = false; switch ($this->getAttribute()) { case 'sku': case 'category_ids': $url = 'sales_rule/promo_widget/chooser/attribute/' . $this->getAttribute(); if ($this->getJsFormObject()) { $url .= '/form/' . $this->getJsFormObject(); } break; default: break; } return $url !== false ? $this->_backendData->getUrl($url) : ''; } /** * Get locale-based formatted price. * * @param string $value * @return float|null */ private function getFormattedPrice($value) { $value = preg_replace('/[^0-9^\^.,-]/m', '', $value); /** * If the comma is the third symbol in the number, we consider it to be a decimal separator */ $separatorComa = strpos($value, ','); $separatorDot = strpos($value, '.'); if ($separatorComa !== false && $separatorDot === false && preg_match('/,\d{3}$/m', $value) === 1) { $value .= '.00'; } return $this->_localeFormat->getNumber($value); } /** * Validate product attribute value for condition * * @param \Magento\Framework\Model\AbstractModel $model * @return bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function validateFixed(\Magento\Framework\Model\AbstractModel $model) { $attrCode = $this->getAttribute(); if ('category_ids' == $attrCode) { return parent::validate($model); } elseif (!isset($this->_entityAttributeValues[$model->getId()])) { if (!$model->getResource()) { return false; } $attr = $model->getResource()->getAttribute($attrCode); if ($attr && $attr->getBackendType() == 'datetime' && !is_int($this->getValue())) { $this->setValue(strtotime((string) $this->getValue())); $value = strtotime($model->getData($attrCode)); return $this->validateAttribute($value); } if ($attr && $attr->getFrontendInput() == 'multiselect') { $value = $model->getData($attrCode); $value = strlen($value??'') ? explode(',', $value) : []; return $this->validateAttribute($value); } return parent::validate($model); } else { $result = false; // any valid value will set it to TRUE // remember old attribute state $oldAttrValue = $model->hasData($attrCode) ? $model->getData($attrCode) : null; foreach ($this->_entityAttributeValues[$model->getId()] as $value) { $attr = $model->getResource()->getAttribute($attrCode); if ($attr && $attr->getBackendType() == 'datetime') { $value = strtotime($value); } elseif ($attr && $attr->getFrontendInput() == 'multiselect') { $value = strlen($value) ? explode(',', $value) : []; } $model->setData($attrCode, $value); $result |= parent::validate($model); if ($result) { break; } } if ($oldAttrValue === null) { $model->unsetData($attrCode); } else { $model->setData($attrCode, $oldAttrValue); } return (bool)$result; } } }