Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractModel.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Rule\Model;
7 
10 
19 {
25  protected $_conditions;
26 
32  protected $_actions;
33 
39  protected $_form;
40 
46  protected $_isDeleteable = true;
47 
53  protected $_isReadonly = false;
54 
59  protected $serializer;
60 
66  abstract public function getConditionsInstance();
67 
73  abstract public function getActionsInstance();
74 
80  protected $_formFactory;
81 
87  protected $_localeDate;
88 
104  public function __construct(
105  \Magento\Framework\Model\Context $context,
106  \Magento\Framework\Registry $registry,
107  \Magento\Framework\Data\FormFactory $formFactory,
108  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
109  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
110  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
111  array $data = [],
112  ExtensionAttributesFactory $extensionFactory = null,
114  \Magento\Framework\Serialize\Serializer\Json $serializer = null
115  ) {
116  $this->_formFactory = $formFactory;
117  $this->_localeDate = $localeDate;
119  \Magento\Framework\Serialize\Serializer\Json::class
120  );
121  parent::__construct(
122  $context,
123  $registry,
124  $extensionFactory ?: $this->getExtensionFactory(),
125  $customAttributeFactory ?: $this->getCustomAttributeFactory(),
126  $resource,
127  $resourceCollection,
128  $data
129  );
130  }
131 
139  public function beforeSave()
140  {
141  // Check if discount amount not negative
142  if ($this->hasDiscountAmount()) {
143  if ((int)$this->getDiscountAmount() < 0) {
144  throw new \Magento\Framework\Exception\LocalizedException(__('Please choose a valid discount amount.'));
145  }
146  }
147 
148  // Serialize conditions
149  if ($this->getConditions()) {
150  $this->setConditionsSerialized($this->serializer->serialize($this->getConditions()->asArray()));
151  $this->_conditions = null;
152  }
153 
154  // Serialize actions
155  if ($this->getActions()) {
156  $this->setActionsSerialized($this->serializer->serialize($this->getActions()->asArray()));
157  $this->_actions = null;
158  }
159 
164  if ($this->hasWebsiteIds()) {
165  $websiteIds = $this->getWebsiteIds();
166  if (is_string($websiteIds) && !empty($websiteIds)) {
167  $this->setWebsiteIds(explode(',', $websiteIds));
168  }
169  }
170 
175  if ($this->hasCustomerGroupIds()) {
176  $groupIds = $this->getCustomerGroupIds();
177  if (is_string($groupIds) && !empty($groupIds)) {
178  $this->setCustomerGroupIds(explode(',', $groupIds));
179  }
180  }
181 
182  parent::beforeSave();
183  return $this;
184  }
185 
192  public function setConditions($conditions)
193  {
194  $this->_conditions = $conditions;
195  return $this;
196  }
197 
203  public function getConditions()
204  {
205  if (empty($this->_conditions)) {
206  $this->_resetConditions();
207  }
208 
209  // Load rule conditions if it is applicable
210  if ($this->hasConditionsSerialized()) {
211  $conditions = $this->getConditionsSerialized();
212  if (!empty($conditions)) {
213  $conditions = $this->serializer->unserialize($conditions);
214  if (is_array($conditions) && !empty($conditions)) {
215  $this->_conditions->loadArray($conditions);
216  }
217  }
218  $this->unsConditionsSerialized();
219  }
220 
221  return $this->_conditions;
222  }
223 
230  public function setActions($actions)
231  {
232  $this->_actions = $actions;
233  return $this;
234  }
235 
241  public function getActions()
242  {
243  if (!$this->_actions) {
244  $this->_resetActions();
245  }
246 
247  // Load rule actions if it is applicable
248  if ($this->hasActionsSerialized()) {
249  $actions = $this->getActionsSerialized();
250  if (!empty($actions)) {
251  $actions = $this->serializer->unserialize($actions);
252  if (is_array($actions) && !empty($actions)) {
253  $this->_actions->loadArray($actions);
254  }
255  }
256  $this->unsActionsSerialized();
257  }
258 
259  return $this->_actions;
260  }
261 
268  protected function _resetConditions($conditions = null)
269  {
270  if (null === $conditions) {
271  $conditions = $this->getConditionsInstance();
272  }
273  $conditions->setRule($this)->setId('1')->setPrefix('conditions');
274  $this->setConditions($conditions);
275 
276  return $this;
277  }
278 
285  protected function _resetActions($actions = null)
286  {
287  if (null === $actions) {
288  $actions = $this->getActionsInstance();
289  }
290  $actions->setRule($this)->setId('1')->setPrefix('actions');
291  $this->setActions($actions);
292 
293  return $this;
294  }
295 
301  public function getForm()
302  {
303  if (!$this->_form) {
304  $this->_form = $this->_formFactory->create();
305  }
306  return $this->_form;
307  }
308 
315  public function loadPost(array $data)
316  {
317  $arr = $this->_convertFlatToRecursive($data);
318  if (isset($arr['conditions'])) {
319  $this->getConditions()->setConditions([])->loadArray($arr['conditions'][1]);
320  }
321  if (isset($arr['actions'])) {
322  $this->getActions()->setActions([])->loadArray($arr['actions'][1], 'actions');
323  }
324 
325  return $this;
326  }
327 
337  protected function _convertFlatToRecursive(array $data)
338  {
339  $arr = [];
340  foreach ($data as $key => $value) {
341  if (($key === 'conditions' || $key === 'actions') && is_array($value)) {
342  foreach ($value as $id => $data) {
343  $path = explode('--', $id);
344  $node = & $arr;
345  for ($i = 0, $l = sizeof($path); $i < $l; $i++) {
346  if (!isset($node[$key][$path[$i]])) {
347  $node[$key][$path[$i]] = [];
348  }
349  $node = & $node[$key][$path[$i]];
350  }
351  foreach ($data as $k => $v) {
352  $node[$k] = $v;
353  }
354  }
355  } else {
359  if (in_array($key, ['from_date', 'to_date'], true) && $value) {
360  $value = new \DateTime($value);
361  }
362  $this->setData($key, $value);
363  }
364  }
365 
366  return $arr;
367  }
368 
375  public function validate(\Magento\Framework\DataObject $object)
376  {
377  return $this->getConditions()->validate($object);
378  }
379 
388  public function validateData(\Magento\Framework\DataObject $dataObject)
389  {
390  $result = [];
391  $fromDate = $toDate = null;
392 
393  if ($dataObject->hasFromDate() && $dataObject->hasToDate()) {
394  $fromDate = $dataObject->getFromDate();
395  $toDate = $dataObject->getToDate();
396  }
397 
398  if ($fromDate && $toDate) {
399  $fromDate = new \DateTime($fromDate);
400  $toDate = new \DateTime($toDate);
401 
402  if ($fromDate > $toDate) {
403  $result[] = __('End Date must follow Start Date.');
404  }
405  }
406 
407  if ($dataObject->hasWebsiteIds()) {
408  $websiteIds = $dataObject->getWebsiteIds();
409  if (empty($websiteIds)) {
410  $result[] = __('Please specify a website.');
411  }
412  }
413  if ($dataObject->hasCustomerGroupIds()) {
414  $customerGroupIds = $dataObject->getCustomerGroupIds();
415  if (empty($customerGroupIds)) {
416  $result[] = __('Please specify Customer Groups.');
417  }
418  }
419 
420  return !empty($result) ? $result : true;
421  }
422 
429  public function isDeleteable()
430  {
431  return $this->_isDeleteable;
432  }
433 
441  public function setIsDeleteable($value)
442  {
443  $this->_isDeleteable = (bool)$value;
444  return $this;
445  }
446 
453  public function isReadonly()
454  {
455  return $this->_isReadonly;
456  }
457 
465  public function setIsReadonly($value)
466  {
467  $this->_isReadonly = (bool)$value;
468  return $this;
469  }
470 
476  public function getWebsiteIds()
477  {
478  if (!$this->hasWebsiteIds()) {
479  $websiteIds = $this->_getResource()->getWebsiteIds($this->getId());
480  $this->setData('website_ids', (array)$websiteIds);
481  }
482  return $this->_getData('website_ids');
483  }
484 
489  private function getExtensionFactory()
490  {
491  return \Magento\Framework\App\ObjectManager::getInstance()
492  ->get(\Magento\Framework\Api\ExtensionAttributesFactory::class);
493  }
494 
499  private function getCustomAttributeFactory()
500  {
501  return \Magento\Framework\App\ObjectManager::getInstance()
502  ->get(\Magento\Framework\Api\AttributeValueFactory::class);
503  }
504 }
__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)
validateData(\Magento\Framework\DataObject $dataObject)
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
validate(\Magento\Framework\DataObject $object)
if(isset($opts->o)) if(! $usingStdout) $l
$i
Definition: gallery.phtml:31