123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Rule\Model;
- use Magento\Framework\Api\AttributeValueFactory;
- use Magento\Framework\Api\ExtensionAttributesFactory;
- /**
- * Abstract Rule entity data model
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @api
- * @since 100.0.2
- */
- abstract class AbstractModel extends \Magento\Framework\Model\AbstractExtensibleModel
- {
- /**
- * Store rule combine conditions model
- *
- * @var \Magento\Rule\Model\Condition\Combine
- */
- protected $_conditions;
- /**
- * Store rule actions model
- *
- * @var \Magento\Rule\Model\Action\Collection
- */
- protected $_actions;
- /**
- * Store rule form instance
- *
- * @var \Magento\Framework\Data\Form
- */
- protected $_form;
- /**
- * Is model can be deleted flag
- *
- * @var bool
- */
- protected $_isDeleteable = true;
- /**
- * Is model readonly
- *
- * @var bool
- */
- protected $_isReadonly = false;
- /**
- * @var \Magento\Framework\Serialize\Serializer\Json
- * @since 100.2.0
- */
- protected $serializer;
- /**
- * Getter for rule combine conditions instance
- *
- * @return \Magento\Rule\Model\Condition\Combine
- */
- abstract public function getConditionsInstance();
- /**
- * Getter for rule actions collection instance
- *
- * @return \Magento\Rule\Model\Action\Collection
- */
- abstract public function getActionsInstance();
- /**
- * Form factory
- *
- * @var \Magento\Framework\Data\FormFactory
- */
- protected $_formFactory;
- /**
- * Timezone instance
- *
- * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
- */
- protected $_localeDate;
- /**
- * AbstractModel constructor
- *
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Framework\Data\FormFactory $formFactory
- * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
- * @param array $data
- * @param ExtensionAttributesFactory|null $extensionFactory
- * @param AttributeValueFactory|null $customAttributeFactory
- * @param \Magento\Framework\Serialize\Serializer\Json $serializer
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\Model\Context $context,
- \Magento\Framework\Registry $registry,
- \Magento\Framework\Data\FormFactory $formFactory,
- \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
- \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- array $data = [],
- ExtensionAttributesFactory $extensionFactory = null,
- AttributeValueFactory $customAttributeFactory = null,
- \Magento\Framework\Serialize\Serializer\Json $serializer = null
- ) {
- $this->_formFactory = $formFactory;
- $this->_localeDate = $localeDate;
- $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
- \Magento\Framework\Serialize\Serializer\Json::class
- );
- parent::__construct(
- $context,
- $registry,
- $extensionFactory ?: $this->getExtensionFactory(),
- $customAttributeFactory ?: $this->getCustomAttributeFactory(),
- $resource,
- $resourceCollection,
- $data
- );
- }
- /**
- * Prepare data before saving
- *
- * @return $this
- * @throws \Magento\Framework\Exception\LocalizedException
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function beforeSave()
- {
- // Check if discount amount not negative
- if ($this->hasDiscountAmount()) {
- if ((int)$this->getDiscountAmount() < 0) {
- throw new \Magento\Framework\Exception\LocalizedException(__('Please choose a valid discount amount.'));
- }
- }
- // Serialize conditions
- if ($this->getConditions()) {
- $this->setConditionsSerialized($this->serializer->serialize($this->getConditions()->asArray()));
- $this->_conditions = null;
- }
- // Serialize actions
- if ($this->getActions()) {
- $this->setActionsSerialized($this->serializer->serialize($this->getActions()->asArray()));
- $this->_actions = null;
- }
- /**
- * Prepare website Ids if applicable and if they were set as string in comma separated format.
- * Backwards compatibility.
- */
- if ($this->hasWebsiteIds()) {
- $websiteIds = $this->getWebsiteIds();
- if (is_string($websiteIds) && !empty($websiteIds)) {
- $this->setWebsiteIds(explode(',', $websiteIds));
- }
- }
- /**
- * Prepare customer group Ids if applicable and if they were set as string in comma separated format.
- * Backwards compatibility.
- */
- if ($this->hasCustomerGroupIds()) {
- $groupIds = $this->getCustomerGroupIds();
- if (is_string($groupIds) && !empty($groupIds)) {
- $this->setCustomerGroupIds(explode(',', $groupIds));
- }
- }
- parent::beforeSave();
- return $this;
- }
- /**
- * Set rule combine conditions model
- *
- * @param \Magento\Rule\Model\Condition\Combine $conditions
- * @return $this
- */
- public function setConditions($conditions)
- {
- $this->_conditions = $conditions;
- return $this;
- }
- /**
- * Retrieve rule combine conditions model
- *
- * @return \Magento\Rule\Model\Condition\Combine
- */
- public function getConditions()
- {
- if (empty($this->_conditions)) {
- $this->_resetConditions();
- }
- // Load rule conditions if it is applicable
- if ($this->hasConditionsSerialized()) {
- $conditions = $this->getConditionsSerialized();
- if (!empty($conditions)) {
- $conditions = $this->serializer->unserialize($conditions);
- if (is_array($conditions) && !empty($conditions)) {
- $this->_conditions->loadArray($conditions);
- }
- }
- $this->unsConditionsSerialized();
- }
- return $this->_conditions;
- }
- /**
- * Set rule actions model
- *
- * @param \Magento\Rule\Model\Action\Collection $actions
- * @return $this
- */
- public function setActions($actions)
- {
- $this->_actions = $actions;
- return $this;
- }
- /**
- * Retrieve rule actions model
- *
- * @return \Magento\Rule\Model\Action\Collection
- */
- public function getActions()
- {
- if (!$this->_actions) {
- $this->_resetActions();
- }
- // Load rule actions if it is applicable
- if ($this->hasActionsSerialized()) {
- $actions = $this->getActionsSerialized();
- if (!empty($actions)) {
- $actions = $this->serializer->unserialize($actions);
- if (is_array($actions) && !empty($actions)) {
- $this->_actions->loadArray($actions);
- }
- }
- $this->unsActionsSerialized();
- }
- return $this->_actions;
- }
- /**
- * Reset rule combine conditions
- *
- * @param null|\Magento\Rule\Model\Condition\Combine $conditions
- * @return $this
- */
- protected function _resetConditions($conditions = null)
- {
- if (null === $conditions) {
- $conditions = $this->getConditionsInstance();
- }
- $conditions->setRule($this)->setId('1')->setPrefix('conditions');
- $this->setConditions($conditions);
- return $this;
- }
- /**
- * Reset rule actions
- *
- * @param null|\Magento\Rule\Model\Action\Collection $actions
- * @return $this
- */
- protected function _resetActions($actions = null)
- {
- if (null === $actions) {
- $actions = $this->getActionsInstance();
- }
- $actions->setRule($this)->setId('1')->setPrefix('actions');
- $this->setActions($actions);
- return $this;
- }
- /**
- * Rule form getter
- *
- * @return \Magento\Framework\Data\Form
- */
- public function getForm()
- {
- if (!$this->_form) {
- $this->_form = $this->_formFactory->create();
- }
- return $this->_form;
- }
- /**
- * Initialize rule model data from array
- *
- * @param array $data
- * @return $this
- */
- public function loadPost(array $data)
- {
- $arr = $this->_convertFlatToRecursive($data);
- if (isset($arr['conditions'])) {
- $this->getConditions()->setConditions([])->loadArray($arr['conditions'][1]);
- }
- if (isset($arr['actions'])) {
- $this->getActions()->setActions([])->loadArray($arr['actions'][1], 'actions');
- }
- return $this;
- }
- /**
- * Set specified data to current rule.
- * Set conditions and actions recursively.
- * Convert dates into \DateTime.
- *
- * @param array $data
- * @return array
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- protected function _convertFlatToRecursive(array $data)
- {
- $arr = [];
- foreach ($data as $key => $value) {
- if (($key === 'conditions' || $key === 'actions') && is_array($value)) {
- foreach ($value as $id => $data) {
- $path = explode('--', $id);
- $node = & $arr;
- for ($i = 0, $l = sizeof($path); $i < $l; $i++) {
- if (!isset($node[$key][$path[$i]])) {
- $node[$key][$path[$i]] = [];
- }
- $node = & $node[$key][$path[$i]];
- }
- foreach ($data as $k => $v) {
- $node[$k] = $v;
- }
- }
- } else {
- /**
- * Convert dates into \DateTime
- */
- if (in_array($key, ['from_date', 'to_date'], true) && $value) {
- $value = new \DateTime($value);
- }
- $this->setData($key, $value);
- }
- }
- return $arr;
- }
- /**
- * Validate rule conditions to determine if rule can run
- *
- * @param \Magento\Framework\DataObject $object
- * @return bool
- */
- public function validate(\Magento\Framework\DataObject $object)
- {
- return $this->getConditions()->validate($object);
- }
- /**
- * Validate rule data
- *
- * @param \Magento\Framework\DataObject $dataObject
- * @return bool|string[] - return true if validation passed successfully. Array with errors description otherwise
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function validateData(\Magento\Framework\DataObject $dataObject)
- {
- $result = [];
- $fromDate = $toDate = null;
- if ($dataObject->hasFromDate() && $dataObject->hasToDate()) {
- $fromDate = $dataObject->getFromDate();
- $toDate = $dataObject->getToDate();
- }
- if ($fromDate && $toDate) {
- $fromDate = new \DateTime($fromDate);
- $toDate = new \DateTime($toDate);
- if ($fromDate > $toDate) {
- $result[] = __('End Date must follow Start Date.');
- }
- }
- if ($dataObject->hasWebsiteIds()) {
- $websiteIds = $dataObject->getWebsiteIds();
- if (empty($websiteIds)) {
- $result[] = __('Please specify a website.');
- }
- }
- if ($dataObject->hasCustomerGroupIds()) {
- $customerGroupIds = $dataObject->getCustomerGroupIds();
- if (empty($customerGroupIds)) {
- $result[] = __('Please specify Customer Groups.');
- }
- }
- return !empty($result) ? $result : true;
- }
- /**
- * Check availability to delete rule
- *
- * @return bool
- * @codeCoverageIgnore
- */
- public function isDeleteable()
- {
- return $this->_isDeleteable;
- }
- /**
- * Set is rule can be deleted flag
- *
- * @param bool $value
- * @return $this
- * @codeCoverageIgnore
- */
- public function setIsDeleteable($value)
- {
- $this->_isDeleteable = (bool)$value;
- return $this;
- }
- /**
- * Check if rule is readonly
- *
- * @return bool
- * @codeCoverageIgnore
- */
- public function isReadonly()
- {
- return $this->_isReadonly;
- }
- /**
- * Set is readonly flag to rule
- *
- * @param bool $value
- * @return $this
- * @codeCoverageIgnore
- */
- public function setIsReadonly($value)
- {
- $this->_isReadonly = (bool)$value;
- return $this;
- }
- /**
- * Get rule associated website Ids
- *
- * @return array
- */
- public function getWebsiteIds()
- {
- if (!$this->hasWebsiteIds()) {
- $websiteIds = $this->_getResource()->getWebsiteIds($this->getId());
- $this->setData('website_ids', (array)$websiteIds);
- }
- return $this->_getData('website_ids');
- }
- /**
- * @return \Magento\Framework\Api\ExtensionAttributesFactory
- * @deprecated 100.1.0
- */
- private function getExtensionFactory()
- {
- return \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\Framework\Api\ExtensionAttributesFactory::class);
- }
- /**
- * @return \Magento\Framework\Api\AttributeValueFactory
- * @deprecated 100.1.0
- */
- private function getCustomAttributeFactory()
- {
- return \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\Framework\Api\AttributeValueFactory::class);
- }
- }
|