Esempio n. 1
0
 /**
  * Check customer scope, email and confirmation key before saving
  *
  * @param \Magento\Framework\Object $customer
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _beforeSave(\Magento\Framework\Object $customer)
 {
     /** @var \Magento\Customer\Model\Customer $customer */
     parent::_beforeSave($customer);
     if (!$customer->getEmail()) {
         throw new ValidatorException(__('Customer email is required'));
     }
     $adapter = $this->_getWriteAdapter();
     $bind = ['email' => $customer->getEmail()];
     $select = $adapter->select()->from($this->getEntityTable(), [$this->getEntityIdField()])->where('email = :email');
     if ($customer->getSharingConfig()->isWebsiteScope()) {
         $bind['website_id'] = (int) $customer->getWebsiteId();
         $select->where('website_id = :website_id');
     }
     if ($customer->getId()) {
         $bind['entity_id'] = (int) $customer->getId();
         $select->where('entity_id != :entity_id');
     }
     $result = $adapter->fetchOne($select, $bind);
     if ($result) {
         throw new AlreadyExistsException(__('Customer with the same email already exists in associated website.'));
     }
     // set confirmation key logic
     if ($customer->getForceConfirmed()) {
         $customer->setConfirmation(null);
     } elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
         $customer->setConfirmation($customer->getRandomConfirmationKey());
     }
     // remove customer confirmation key from database, if empty
     if (!$customer->getConfirmation()) {
         $customer->setConfirmation(null);
     }
     $this->_validate($customer);
     return $this;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 protected function _beforeSave(DataObject $category)
 {
     /** @var \Mirasvit\Blog\Model\Category $category */
     parent::_beforeSave($category);
     if (!$category->getChildrenCount()) {
         $category->setChildrenCount(0);
     }
     if (!$category->getData('url_key')) {
         $category->setData('url_key', $this->filter->translitUrl($category->getName()));
     }
     if ($category->isObjectNew()) {
         if (!$category->hasParentId()) {
             $category->setParentId(1);
         }
         /** @var \Mirasvit\Blog\Model\Category $parent */
         $parent = ObjectManager::getInstance()->create('Mirasvit\\Blog\\Model\\Category')->load($category->getParentId());
         $category->setPath($parent->getPath());
         if ($category->getPosition() === null) {
             $category->setPosition($this->getMaxPosition($category->getPath()) + 1);
         }
         $path = explode('/', $category->getPath());
         $level = count($path) - ($category->getId() ? 1 : 0);
         $toUpdateChild = array_diff($path, [$category->getId()]);
         if (!$category->hasPosition()) {
             $category->setPosition($this->getMaxPosition(implode('/', $toUpdateChild)) + 1);
         }
         if (!$category->hasLevel()) {
             $category->setLevel($level);
         }
         if (!$category->getId() && $category->getPath()) {
             $category->setPath($category->getPath() . '/');
         }
         $this->getConnection()->update($this->getEntityTable(), ['children_count' => new \Zend_Db_Expr('children_count+1')], ['entity_id IN(?)' => $toUpdateChild]);
     }
     return $this;
 }
Esempio n. 3
0
 /**
  * Check customer address before saving
  *
  * @param \Magento\Framework\Object $address
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Object $address)
 {
     parent::_beforeSave($address);
     $this->_validate($address);
     return $this;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 protected function _beforeSave(DataObject $post)
 {
     /** @var \Mirasvit\Blog\Model\Post $post */
     if (!$post->hasData('type')) {
         $post->setData('type', \Mirasvit\Blog\Model\Post::TYPE_POST);
     }
     if (!$post->getData('url_key')) {
         $post->setData('url_key', $this->filter->translitUrl($post->getName()));
     }
     $this->saveImage($post);
     return parent::_beforeSave($post);
 }