/** * Processing object before save data * * @return $this */ protected function _beforeSave() { if (!$this->getRuleId() && $this->_rule instanceof \Magento\SalesRule\Model\Rule) { $this->setRuleId($this->_rule->getId()); } return parent::_beforeSave(); }
/** * Check order id * * @return $this */ public function _beforeSave() { if (null == $this->getOrderId()) { throw new \Exception(__('Order id cannot be null')); } return parent::_beforeSave(); }
/** * @return $this */ protected function _beforeSave() { parent::_beforeSave(); if ($this->getAddress()) { $this->setAddressId($this->getAddress()->getId()); } return $this; }
/** * Prevent blocks recursion * * @return \Magento\Framework\Model\AbstractModel * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave() { $needle = 'block_id="' . $this->getBlockId() . '"'; if (false == strstr($this->getContent(), $needle)) { return parent::_beforeSave(); } throw new \Magento\Framework\Model\Exception(__('Make sure that static block content does not reference the block itself.')); }
/** * Before save prepare process * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productFactory->create(); $product->load($this->getProductId()); $typeId = $product->getTypeId() ? $product->getTypeId() : $this->getTypeId(); $isQty = $this->stockItemService->isQty($typeId); if ($isQty) { if (!$this->getId()) { $this->processIsInStock(); } if ($this->getManageStock() && !$this->verifyStock()) { $this->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true); } // if qty is below notify qty, update the low stock date to today date otherwise set null $this->setLowStockDate(null); if ($this->verifyNotification()) { $this->setLowStockDate($this->_localeDate->date(null, null, null, false)->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT)); } $this->setStockStatusChangedAuto(0); if ($this->hasStockStatusChangedAutomaticallyFlag()) { $this->setStockStatusChangedAuto((int) $this->getStockStatusChangedAutomaticallyFlag()); } } else { $this->setQty(0); } return $this; }
/** * Initialize item identifier before save data * * @return $this */ protected function _beforeSave() { if ($this->getItem()) { $this->setWishlistItemId($this->getItem()->getId()); } return parent::_beforeSave(); }
/** * Serelaize old and new data arrays before saving * * @return $this */ protected function _beforeSave() { $newData = $this->getNewData(false); $this->setNewData(serialize($newData)); if (!$this->hasCreatedAt()) { $this->setCreatedAt($this->dateTime->formatDate(time(), true)); } return parent::_beforeSave(); }
/** * Stop saving process if file with same report date, account ID and last modified date was already ferched * * @return \Magento\Framework\Model\AbstractModel */ protected function _beforeSave() { $this->_dataSaveAllowed = true; if ($this->getId()) { if ($this->getLastModified() == $this->getReportLastModified()) { $this->_dataSaveAllowed = false; } } $this->setLastModified($this->getReportLastModified()); return parent::_beforeSave(); }
/** * Validate data before save data * * @throws \Magento\Framework\Model\Exception * @return $this */ protected function _beforeSave() { if (!$this->getTypeId()) { throw new \Magento\Framework\Model\Exception(__('Invalid form type.')); } if (!$this->getStoreId() && $this->getLabel()) { $this->setStoreLabel($this->getStoreId(), $this->getLabel()); } return parent::_beforeSave(); }
/** * Verify data required for saving * * @return $this */ protected function _beforeSave() { // set parent id $this->_verifyPaymentObject(); if (!$this->getId()) { // We need to set order and payment ids only for new transactions if (null !== $this->_paymentObject) { $this->setPaymentId($this->_paymentObject->getId()); } if (null !== $this->_order) { $this->setOrderId($this->_order->getId()); } $this->setCreatedAt($this->_dateFactory->create()->gmtDate()); } return parent::_beforeSave(); }
/** * Processing object before save data * * @return $this */ protected function _beforeSave() { if ($this->getContentHeight() == 0) { $this->setContentHeight(''); //converting zero Content-Height } if ($this->getContentHeight() && !preg_match('/(' . implode("|", $this->allowedCssUnits) . ')/', $this->getContentHeight())) { $contentHeight = $this->getContentHeight() . 'px'; //setting default units for Content-Height $this->setContentHeight($contentHeight); } return parent::_beforeSave(); }
/** * Before Event save process * * @return $this */ protected function _beforeSave() { $date = $this->_dateFactory->create(); $this->setLoggedAt($date->gmtDate()); return parent::_beforeSave(); }
/** * Before save unlock attributes * * @return \Magento\Catalog\Model\AbstractModel */ protected function _beforeSave() { $this->unlockAttributes(); return parent::_beforeSave(); }
/** * Prepare customer/visitor, store data before save * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); if (!$this->hasVisitorId()) { $this->setVisitorId($this->getVisitorId()); } if (!$this->hasCustomerId()) { $this->setCustomerId($this->getCustomerId()); } if (!$this->hasStoreId()) { $this->setStoreId($this->getStoreId()); } if (!$this->hasAddedAt()) { $this->setAddedAt($this->dateTime->now()); } return $this; }
/** * Prepare data before save * * @return $this */ protected function _beforeSave() { $this->_prepareData(); return parent::_beforeSave(); }
/** * @return $this */ protected function _beforeSave() { parent::_beforeSave(); $this->getRegion(); return $this; }
/** * Serialize info for Resource Model to save * For new model check and set available cookie key * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); // Setting info $info = array(); foreach ($this->getData() as $index => $value) { if (!in_array($index, $this->_unserializableFields)) { $info[$index] = $value; } } $this->setInfo($this->_coreData->jsonEncode($info)); if ($this->isObjectNew()) { $this->setWebsiteId($this->_storeManager->getStore()->getWebsiteId()); // Setting cookie key do { $this->setKey($this->mathRandom->getRandomString(self::KEY_LENGTH)); } while (!$this->getResource()->isKeyAllowed($this->getKey())); } return $this; }
/** * Prepare data before saving * * @return $this * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave() { // Check if discount amount not negative if ($this->hasDiscountAmount()) { if ((int) $this->getDiscountAmount() < 0) { throw new \Magento\Framework\Model\Exception(__('Invalid discount amount.')); } } // Serialize conditions if ($this->getConditions()) { $this->setConditionsSerialized(serialize($this->getConditions()->asArray())); $this->unsConditions(); } // Serialize actions if ($this->getActions()) { $this->setActionsSerialized(serialize($this->getActions()->asArray())); $this->unsActions(); } /** * 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; }
/** * Before save actions * * @return $this */ protected function _beforeSave() { if ($this->isObjectNew() && null === $this->getCreatedAt()) { $this->setCreatedAt($this->_dateTime->now()); } parent::_beforeSave(); return $this; }
/** * Specify parent item id before saving data * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); if ($this->getParentItem()) { $this->setParentItemId($this->getParentItem()->getId()); } return $this; }
/** * Set date of last update for wishlist * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); $this->setUpdatedAt($this->_date->gmtDate()); return $this; }
/** * Check required data * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); // validate required item data $this->validate(); // set current store id if it is not defined if (is_null($this->getStoreId())) { $this->setStoreId($this->_storeManager->getStore()->getId()); } // set current date if added at data is not defined if (is_null($this->getAddedAt())) { $this->setAddedAt($this->_date->gmtDate()); } return $this; }
/** * Prepare data before save * * @return $this */ protected function _beforeSave() { /** * Currency logic * * global - currency which is set for default in backend * base - currency which is set for current website. all attributes that * have 'base_' prefix saved in this currency * quote/order - currency which was selected by customer or configured by * admin for current store. currency in which customer sees * price thought all checkout. * * Rates: * base_to_global & base_to_quote/base_to_order */ $globalCurrencyCode = $this->_config->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default'); $baseCurrency = $this->getStore()->getBaseCurrency(); if ($this->hasForcedCurrency()) { $quoteCurrency = $this->getForcedCurrency(); } else { $quoteCurrency = $this->getStore()->getCurrentCurrency(); } $this->setGlobalCurrencyCode($globalCurrencyCode); $this->setBaseCurrencyCode($baseCurrency->getCode()); $this->setStoreCurrencyCode($baseCurrency->getCode()); $this->setQuoteCurrencyCode($quoteCurrency->getCode()); $this->setBaseToGlobalRate($baseCurrency->getRate($globalCurrencyCode)); $this->setBaseToQuoteRate($baseCurrency->getRate($quoteCurrency)); if (!$this->hasChangedFlag() || $this->getChangedFlag() == true) { $this->setIsChanged(1); } else { $this->setIsChanged(0); } if ($this->_customer) { $this->setCustomerId($this->_customer->getId()); } parent::_beforeSave(); }
/** * Prepare location settings and tax postcode before save rate * * @return \Magento\Tax\Model\Calculation\Rate * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave() { $isWrongRange = $this->getZipIsRange() && ($this->getZipFrom() === '' || $this->getZipTo() === ''); $isEmptyValues = $this->getCode() === '' || $this->getTaxCountryId() === '' || $this->getRate() === '' || $this->getTaxPostcode() === '' && !$this->getZipIsRange(); if ($isEmptyValues || $isWrongRange) { throw new \Magento\Framework\Model\Exception(__('Please fill all required fields with valid information.')); } if (!is_numeric($this->getRate()) || $this->getRate() < 0) { throw new \Magento\Framework\Model\Exception(__('Rate Percent should be a positive number.')); } if ($this->getZipIsRange()) { $zipFrom = $this->getZipFrom(); $zipTo = $this->getZipTo(); if (strlen($zipFrom) > 9 || strlen($zipTo) > 9) { throw new \Magento\Framework\Model\Exception(__('Maximum zip code length is 9.')); } if (!is_numeric($zipFrom) || !is_numeric($zipTo) || $zipFrom < 0 || $zipTo < 0) { throw new \Magento\Framework\Model\Exception(__('Zip code should not contain characters other than digits.')); } if ($zipFrom > $zipTo) { throw new \Magento\Framework\Model\Exception(__('Range To should be equal or greater than Range From.')); } $this->setTaxPostcode($zipFrom . '-' . $zipTo); } else { $taxPostCode = $this->getTaxPostcode(); if (strlen($taxPostCode) > 10) { $taxPostCode = substr($taxPostCode, 0, 10); } $this->setTaxPostcode($taxPostCode)->setZipIsRange(null)->setZipFrom(null)->setZipTo(null); } parent::_beforeSave(); $country = $this->getTaxCountryId(); $region = $this->getTaxRegionId(); /** @var $regionModel \Magento\Directory\Model\Region */ $regionModel = $this->_regionFactory->create(); $regionModel->load($region); if ($regionModel->getCountryId() != $country) { $this->setTaxRegionId('*'); } return $this; }
/** * Check sitemap file location and permissions * * @return \Magento\Framework\Model\AbstractModel * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave() { $path = $this->getSitemapPath(); /** * Check path is allow */ if ($path && preg_match('#\\.\\.[\\\\/]#', $path)) { throw new \Magento\Framework\Model\Exception(__('Please define a correct path.')); } /** * Check exists and writable path */ if (!$this->_directory->isExist($path)) { throw new \Magento\Framework\Model\Exception(__('Please create the specified folder "%1" before saving the sitemap.', $this->_escaper->escapeHtml($this->getSitemapPath()))); } if (!$this->_directory->isWritable($path)) { throw new \Magento\Framework\Model\Exception(__('Please make sure that "%1" is writable by the web-server.', $this->getSitemapPath())); } /** * Check allow filename */ if (!preg_match('#^[a-zA-Z0-9_\\.]+$#', $this->getSitemapFilename())) { throw new \Magento\Framework\Model\Exception(__('Please use only letters (a-z or A-Z), numbers (0-9) or underscores (_) in the filename. No spaces or other characters are allowed.')); } if (!preg_match('#\\.xml$#', $this->getSitemapFilename())) { $this->setSitemapFilename($this->getSitemapFilename() . '.xml'); } $this->setSitemapPath(rtrim(str_replace(str_replace('\\', '/', $this->_getBaseDir()), '', $path), '/') . '/'); return parent::_beforeSave(); }
/** * Prepare data to be saved to database * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); if ($this->isObjectNew()) { $this->setCreatedAt($this->_dateTime->formatDate(true)); } $this->setUpdatedAt($this->_dateTime->formatDate(true)); return $this; }
/** * Set current store before save * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); if (!$this->hasStoreId()) { $this->setStoreId($this->_storeManager->getStore()->getId()); } return $this; }
/** * Before theme save * * @return $this */ protected function _beforeSave() { $this->_validate(); return parent::_beforeSave(); }
/** * Validate data before save data * * @throws \Magento\Framework\Model\Exception * @return $this */ protected function _beforeSave() { if (!$this->getTypeId()) { throw new Exception(__('Invalid form type.')); } if (!$this->getAttributeId()) { throw new Exception(__('Invalid EAV attribute')); } return parent::_beforeSave(); }
/** * Processing data before model save * * @return $this */ protected function _beforeSave() { $data = array('firstname' => $this->getFirstname(), 'lastname' => $this->getLastname(), 'email' => $this->getEmail(), 'modified' => $this->dateTime->now(), 'extra' => serialize($this->getExtra())); if ($this->getId() > 0) { $data['user_id'] = $this->getId(); } if ($this->getUsername()) { $data['username'] = $this->getUsername(); } if ($this->_willSavePassword()) { $data['password'] = $this->_getEncodedPassword($this->getPassword()); } if (!is_null($this->getIsActive())) { $data['is_active'] = intval($this->getIsActive()); } $this->addData($data); return parent::_beforeSave(); }