protected function _beforeSave()
 {
     $oReq = Mage::app()->getFrontController()->getRequest();
     $data = $oReq->getPost('aitreg');
     if ($data && !Mage::registry('aitoc_customer_saved') && !Mage::registry('aitoc_customer_to_save')) {
         $oAttribute = Mage::getModel('aitcheckoutfields/aitcheckoutfields');
         foreach ($data as $sKey => $sVal) {
             $oAttribute->setCustomValue($sKey, $sVal, 'register');
         }
         Mage::register('aitoc_customer_to_save', true);
     }
     return parent::_beforeSave();
 }
Esempio n. 2
0
 protected function _beforeSave()
 {
     parent::_beforeSave();
     $storeId = $this->getStoreId();
     /**
     * ORIGINAL CODE
     *
      if (is_null($storeId)) {
         $this->setStoreId(Mage::app()->getStore()->getId());
     }
     */
     if (is_null($storeId) || !$storeId) {
         $this->setStoreId(Mage::app()->getStore(true)->getId());
     }
     $this->getGroupId();
     return $this;
 }
Esempio n. 3
0
 /**
  * Processing object before save data
  *
  * @return Mage_Customer_Model_Customer
  */
 protected function _beforeSave()
 {
     if (Mage::helper('fieldsmanager')->getStoredDatafor('enable')) {
         Mage::getSingleton('customer/session')->setRegistry('');
         $data = array();
         $fmedata = array();
         if (isset($_POST['fme_register'])) {
             $fmedata = $_POST['fme_register'];
         } elseif (isset($_POST['fme_account'])) {
             $fmedata = $_POST['fme_account'];
         }
         foreach ($fmedata as $key => $value) {
             if (substr($key, 0, 3) == 'fm_') {
                 $data[substr($key, 3)] = $value;
             }
         }
         if (is_array($data) and count($data) != 0) {
             Mage::getSingleton('customer/session')->setRegistry($data);
         }
     }
     return parent::_beforeSave();
 }
Esempio n. 4
0
 /**
  *
  * (override from an abstract parent)
  */
 protected function _beforeSave()
 {
     $oldId = $this->getId();
     if (!empty($oldId)) {
         $this->oldId = $oldId;
     }
     return parent::_beforeSave();
 }
Esempio n. 5
0
 protected function _beforeSave()
 {
     parent::_beforeSave();
     $checkUnique = array();
     $nameGroupAttribute = '';
     /**
      * Will detect which attributes are dates and files, and check unique attributes
      */
     $collection = Mage::getModel('customer/attribute')->getCollection();
     $alias = Mage::helper('amcustomerattr')->getProperAlias($collection->getSelect()->getPart('from'), 'eav_attribute');
     $collection->addFieldToFilter($alias . 'is_user_defined', 1);
     $collection->addFieldToFilter($alias . 'entity_type_id', Mage::getModel('eav/entity')->setType('customer')->getTypeId());
     $castDate = array();
     $filesFields = array();
     $filesRestrictions = array();
     foreach ($collection as $attribute) {
         if ('selectgroup' == $attribute->getTypeInternal()) {
             $nameGroupAttribute = $attribute->getAttributeCode();
         }
         if ('datetime' == $attribute->getBackendType()) {
             $castDate[] = $attribute->getAttributeCode();
         }
         if ('file' == $attribute->getTypeInternal()) {
             $this->_fileAttributes[$attribute->getId()] = $attribute->getAttributeCode();
             $filesFields[] = $attribute->getAttributeCode();
             $filesRestrictions[$attribute->getAttributeCode()]['size'] = 1048576 * $attribute->getFileSize();
             $filesRestrictions[$attribute->getAttributeCode()]['type'] = $attribute->getFileTypes();
             $filesRestrictions[$attribute->getAttributeCode()]['dimentions'] = $attribute->getFileDimentions();
             $this->_savedFileNames[$attribute->getAttributeCode()] = $this->getData($attribute->getAttributeCode());
         }
         if ($attribute->getIsUnique()) {
             $translations = $attribute->getStoreLabels();
             if (isset($translations[Mage::app()->getStore()->getId()])) {
                 $attributeLabel = $translations[Mage::app()->getStore()->getId()];
             } else {
                 $attributeLabel = $attribute->getFrontend()->getLabel();
             }
             $checkUnique[$attribute->getAttributeCode()] = $attributeLabel;
         }
     }
     // Need to change `Customer Group Selector` attribute, if admin change group for customer in the Backend
     if ($nameGroupAttribute && ($backendPost = Mage::app()->getRequest()->getPost('account')) && isset($backendPost['group_id'])) {
         $entityType = Mage::getSingleton('eav/config')->getEntityType('customer');
         $groupAttribute = Mage::getModel('customer/attribute')->loadByCode($entityType, $nameGroupAttribute);
         $storeIds = explode(',', $groupAttribute->getData('store_ids'));
         if (in_array($this->getStoreId(), $storeIds) || in_array(0, $storeIds)) {
             $options = $groupAttribute->getSource()->getAllOptions(true, true);
             foreach ($options as $option) {
                 $temp = Mage::getModel('eav/entity_attribute_option')->load($option['value']);
                 if ($temp && $temp->getGroupId() && $temp->getGroupId() == $backendPost['group_id']) {
                     $this->setData($nameGroupAttribute, $option['value']);
                 }
             }
         }
     }
     /**
      * Adding customer attributes to self data array
      */
     $customerAttributes = Mage::app()->getRequest()->getPost('amcustomerattr');
     if (!$customerAttributes && ('checkout' == Mage::app()->getRequest()->getModuleName() || 'sgps' == Mage::app()->getRequest()->getModuleName() || 'amscheckoutfront' == Mage::app()->getRequest()->getModuleName())) {
         $customerAttributes = Mage::getSingleton('checkout/session')->getAmcustomerattr();
     }
     if ($customerAttributes) {
         // set to session attributes except file attributes
         if ($filesFields) {
             $temp = $customerAttributes;
             foreach ($customerAttributes as $attributeCode => $attributeValue) {
                 if (in_array($attributeCode, $filesFields)) {
                     unset($temp[$attributeCode]);
                 }
             }
             Mage::getSingleton('customer/session')->setAmcustomerattr($temp);
         } else {
             Mage::getSingleton('customer/session')->setAmcustomerattr($customerAttributes);
         }
         $deleteFiles = Mage::app()->getRequest()->getPost('amcustomerattr_delete');
         $idGroupSelect = 0;
         foreach ($customerAttributes as $attributeCode => $attributeValue) {
             if ($attributeCode == $nameGroupAttribute) {
                 $idGroupSelect = $attributeValue;
             }
             if (in_array($attributeCode, $castDate)) {
                 if ($attributeValue) {
                     $temp = Mage::app()->getLocale()->date($attributeValue);
                     $attributeValue = Mage::getModel('core/date')->date('Y-m-d', $temp->getTimestamp());
                 }
             }
             if (in_array($attributeCode, $filesFields)) {
                 if (isset($_FILES['amcustomerattr_' . $attributeCode]['error']) && UPLOAD_ERR_OK == $_FILES['amcustomerattr_' . $attributeCode]['error']) {
                     // check if uploaded new file
                     // correct filename
                     $temp = explode('.', $_FILES['amcustomerattr_' . $attributeCode]['name']);
                     $ext = strtolower(array_pop($temp));
                     $fileName = Mage::helper('amcustomerattr')->getCorrectFileName($temp[0]);
                     $f1 = Mage::helper('amcustomerattr')->getFolderName($fileName[0]);
                     $f2 = Mage::helper('amcustomerattr')->getFolderName($fileName[1]);
                     $fileDestination = Mage::getBaseDir('media') . DS . 'customer' . DS . $f1 . DS . $f2 . DS;
                     if (file_exists($fileDestination . $fileName . '.' . $ext)) {
                         // check if exist file with the same name
                         $attributeValue = DS . $f1 . DS . $f2 . DS . uniqid(date('ihs')) . $fileName . '.' . $ext;
                     } else {
                         $attributeValue = DS . $f1 . DS . $f2 . DS . $fileName . '.' . $ext;
                     }
                     $_FILES['amcustomerattr_' . $attributeCode]['name'] = $attributeValue;
                 } elseif ($deleteFiles[$attributeCode] && $this->_savedFileNames[$attributeCode] === $deleteFiles[$attributeCode]) {
                     // check if file mark for delete
                     $attributeValue = '';
                 } else {
                     $attributeValue = $this->_savedFileNames[$attributeCode];
                 }
             }
             $this->setData($attributeCode, $attributeValue);
         }
         if ($idGroupSelect) {
             $option = Mage::getModel('eav/entity_attribute_option')->load($idGroupSelect);
             if ($option && $option->getGroupId()) {
                 $this->setGroupId($option->getGroupId());
             }
         } else {
             if (Mage::helper('amcustomerattr/group')->isAllowed()) {
                 if ($this->getData(Mage::helper('amcustomerattr/group')->getAttribute())) {
                     $this->setGroupId(Mage::helper('amcustomerattr/group')->getGroupId());
                 }
             }
         }
     }
     if ($checkUnique) {
         foreach ($checkUnique as $attributeCode => $attributeLabel) {
             //skip empty values
             if (!$this->getData($attributeCode)) {
                 continue;
             }
             $customerCollection = Mage::getResourceModel('customer/customer_collection');
             $customerCollection->addAttributeToFilter($attributeCode, array('eq' => $this->getData($attributeCode)));
             $mainAlias = false !== strpos($customerCollection->getSelect()->__toString(), 'AS `e') ? 'e' : 'main_table';
             $customerCollection->getSelect()->where($mainAlias . '.entity_id != ?', $this->getId());
             if ($customerCollection->getSize() > 0) {
                 $e = new Mage_Customer_Exception(Mage::helper('amcustomerattr')->__('Please specify different value for `%s` attribute. Customer with such value already exists.', $attributeLabel));
                 if (method_exists($e, 'setMessage')) {
                     $e->setMessage(Mage::helper('amcustomerattr')->__('Please specify different value for `%s` attribute. Customer with such value already exists.', $attributeLabel));
                 }
                 throw $e;
             }
         }
     }
     // check files
     $fileErrors = array();
     if ($filesFields) {
         foreach ($filesFields as $attributeCode) {
             if (isset($_FILES['amcustomerattr_' . $attributeCode]['error']) && UPLOAD_ERR_OK == $_FILES['amcustomerattr_' . $attributeCode]['error']) {
                 // check file size
                 if ($filesRestrictions[$attributeCode]['size'] && $filesRestrictions[$attributeCode]['size'] < $_FILES['amcustomerattr_' . $attributeCode]['size']) {
                     $fileErrors[] = Mage::helper('amcustomerattr')->__('File size restriction: %d bytes', $filesRestrictions[$attributeCode]['size']);
                 }
                 // check file ext
                 if ($filesRestrictions[$attributeCode]['type']) {
                     $temp = explode('.', $_FILES['amcustomerattr_' . $attributeCode]['name']);
                     $ext = strtolower(array_pop($temp));
                     if (!in_array($ext, explode(',', $filesRestrictions[$attributeCode]['type']))) {
                         $fileErrors[] = Mage::helper('amcustomerattr')->__('File ext restriction: %s', $filesRestrictions[$attributeCode]['type']);
                     }
                 }
                 // check type of file
                 if (substr_count($_FILES['amcustomerattr_' . $attributeCode]['type'], '/') > 1) {
                     // check double file type
                     $fileErrors[] = Mage::helper('amcustomerattr')->__('Not supported type of file: %s', $_FILES['amcustomerattr_' . $attributeCode]['type']);
                 }
                 // check content (MIME) type of file
                 if (Mage::getStoreConfig('amcustomerattr/general/check_file_type') && !$this->checkContentType($ext, $_FILES['amcustomerattr_' . $attributeCode]['type'])) {
                     $fileErrors[] = Mage::helper('amcustomerattr')->__('Not supported content type of file: %s', $_FILES['amcustomerattr_' . $attributeCode]['type']);
                 }
                 // check dimentions for image
                 if ($filesRestrictions[$attributeCode]['dimentions'] && false !== strpos($_FILES['amcustomerattr_' . $attributeCode]['type'], 'image/')) {
                     $dimentions = explode('/', $filesRestrictions[$attributeCode]['dimentions']);
                     try {
                         $imageInfo = getimagesize($_FILES['amcustomerattr_' . $attributeCode]['tmp_name']);
                     } catch (Exception $error) {
                         $e = new Mage_Customer_Exception(Mage::helper('amcustomerattr')->__('System error: %s', $error));
                         if (method_exists($e, 'setMessage')) {
                             $e->setMessage(Mage::helper('amcustomerattr')->__('System error: %s', $error));
                         }
                         throw $e;
                     }
                     $imageInfo = getimagesize($_FILES['amcustomerattr_' . $attributeCode]['tmp_name']);
                     if ($imageInfo[0] > $dimentions[0] || $imageInfo[1] > $dimentions[1]) {
                         $fileErrors[] = Mage::helper('amcustomerattr')->__('Image size restriction: %s pixels (width/height)', $filesRestrictions[$attributeCode]['dimentions']);
                     }
                 }
                 // errors
                 if ($fileErrors) {
                     $e = new Mage_Customer_Exception(Mage::helper('amcustomerattr')->__('File error: %s', implode('; ', $fileErrors)));
                     if (method_exists($e, 'setMessage')) {
                         $e->setMessage(Mage::helper('amcustomerattr')->__('File error: %s', implode('; ', $fileErrors)));
                     }
                     throw $e;
                 }
             }
         }
     }
     // account activation checkbox save
     $account_post = Mage::app()->getRequest()->getPost('account');
     if ($account_post) {
         //set activation status
         $this->setData('am_is_activated', isset($account_post['am_is_activated']) ? $account_post['am_is_activated'] : self::ACTIVATION_STATUS_PENDING);
     } else {
         if ($this->getOrigData() === null && Mage::getStoreConfig('amcustomerattr/activation/auto_activation', $this->getStoreId())) {
             // activate new users automatically
             if (Mage::getStoreConfig('amcustomerattr/activation/enabled_customer_groups', $this->getStoreId()) && $this->getGroupId()) {
                 // activate by group
                 $groups = explode(',', Mage::getStoreConfig('amcustomerattr/activation/customer_groups', $this->getStoreId()));
                 if (in_array($this->getGroupId(), $groups)) {
                     $this->setData('am_is_activated', self::ACTIVATION_STATUS_ACTIVATED);
                 }
             } else {
                 $this->setData('am_is_activated', self::ACTIVATION_STATUS_ACTIVATED);
             }
         }
     }
     return $this;
 }
 protected function _beforeSave()
 {
     parent::_beforeSave();
     $checkUnique = array();
     /**
      * Will detect which attributes are dates, and check unique attributes
      */
     $collection = Mage::getModel('customer/attribute')->getCollection();
     $collection->addFieldToFilter('is_user_defined', 1);
     $collection->addFieldToFilter('entity_type_id', Mage::getModel('eav/entity')->setType('customer')->getTypeId());
     $castDate = array();
     foreach ($collection as $attribute) {
         if ('datetime' == $attribute->getBackendType()) {
             $castDate[] = $attribute->getAttributeCode();
         }
         if ($attribute->getIsUnique()) {
             $translations = $attribute->getStoreLabels();
             if (isset($translations[Mage::app()->getStore()->getId()])) {
                 $attributeLabel = $translations[Mage::app()->getStore()->getId()];
             } else {
                 $attributeLabel = $attribute->getFrontend()->getLabel();
             }
             $checkUnique[$attribute->getAttributeCode()] = $attributeLabel;
         }
     }
     /**
      * Adding customer attributes to self data array
      */
     $customerAttributes = Mage::app()->getRequest()->getPost('amcustomerattr');
     if (!$customerAttributes && ('checkout' == Mage::app()->getRequest()->getModuleName() || 'sgps' == Mage::app()->getRequest()->getModuleName())) {
         $customerAttributes = Mage::getSingleton('checkout/session')->getAmcustomerattr();
     }
     if ($customerAttributes) {
         Mage::getSingleton('customer/session')->setAmcustomerattr($customerAttributes);
         foreach ($customerAttributes as $attributeCode => $attributeValue) {
             if (in_array($attributeCode, $castDate)) {
                 $attributeValue = date('Y-m-d', strtotime($attributeValue));
             }
             $this->setData($attributeCode, $attributeValue);
         }
     }
     if ($checkUnique) {
         foreach ($checkUnique as $attributeCode => $attributeLabel) {
             //skip empty values
             if (!$this->getData($attributeCode)) {
                 continue;
             }
             $customerCollection = Mage::getResourceModel('customer/customer_collection');
             $customerCollection->addAttributeToFilter($attributeCode, array('eq' => $this->getData($attributeCode)));
             $mainAlias = false !== strpos($customerCollection->getSelect()->__toString(), 'AS `e') ? 'e' : 'main_table';
             $customerCollection->getSelect()->where($mainAlias . '.entity_id != ?', $this->getId());
             if ($customerCollection->getSize() > 0) {
                 $e = new Mage_Customer_Exception(Mage::helper('amcustomerattr')->__('Please specify different value for "%s" attribute. Customer with such value already exists.', $attributeLabel));
                 if (method_exists($e, 'setMessage')) {
                     $e->setMessage(Mage::helper('amcustomerattr')->__('Please specify different value for "%s" attribute. Customer with such value already exists.', $attributeLabel));
                 }
                 throw $e;
             }
         }
     }
     return $this;
 }