Exemplo n.º 1
0
 public function savepostAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         if (!($customer = $this->getSession()->getCustomer())) {
             $customer = new Customer_Model_Customer();
         }
         $isNew = !$customer->getId();
         $isMobile = APPLICATION_TYPE == 'mobile';
         try {
             if (!Zend_Validate::is($datas['email'], 'EmailAddress')) {
                 throw new Exception($this->_('Please enter a valid email address'));
             }
             $dummy = new Customer_Model_Customer();
             $dummy->find($datas['email'], 'email');
             if ($isNew and $dummy->getId()) {
                 throw new Exception($this->_('We are sorry but this address is already used.'));
             }
             if (!empty($datas['social_datas'])) {
                 $social_ids = array();
                 foreach ($datas['social_datas'] as $type => $data) {
                     if ($customer->findBySocialId($data['id'], $type)->getId()) {
                         throw new Exception($this->_('We are sorry but the %s account is already linked to one of our customers', ucfirst($type)));
                     }
                     $social_ids[$type] = array('id' => $data['id']);
                 }
             }
             $password = $customer->getPassword();
             if (empty($datas['show_in_social_gaming'])) {
                 $datas['show_in_social_gaming'] = 0;
             }
             $customer->setData($datas);
             $customer->setData('password', $password);
             if (isset($datas['id']) and $datas['id'] != $this->getSession()->getCustomer()->getId()) {
                 throw new Exception($this->_('An error occurred while saving. Please try again later.'));
             }
             $formated_name = Core_Model_Lib_String::format($customer->getName(), true);
             $base_logo_path = $customer->getBaseImagePath() . '/' . $formated_name;
             if ($customer->getSocialPicture()) {
                 $social_image = @file_get_contents($customer->getSocialPicture());
                 if ($social_image) {
                     if (!is_dir($customer->getBaseImagePath())) {
                         mkdir($customer->getBaseImagePath(), 0777);
                     }
                     $image_name = uniqid() . '.jpg';
                     $image = fopen($customer->getBaseImagePath() . '/' . $image_name, 'w');
                     fputs($image, $social_image);
                     fclose($image);
                     $customer->setImage('/' . $formated_name . '/' . $image_name);
                 } else {
                     $this->getSession()->addError($this->_('An error occurred while saving your picture. Please try againg later.'));
                 }
             }
             if (empty($datas['password']) and $isNew) {
                 throw new Exception($this->_('Please enter a password'));
             }
             if (!$isMobile and $datas['password'] != $datas['confirm_password']) {
                 throw new Exception($this->_('Your password does not match the entered password.'));
             }
             if ($isNew and !$isMobile and $datas['email'] != $datas['confirm_email']) {
                 throw new Exception($this->_("The old email address does not match the entered email address."));
             }
             if (!$isNew and !empty($datas['old_password']) and !$customer->isSamePassword($datas['old_password'])) {
                 throw new Exception($this->_("The old password does not match the entered password."));
             }
             if (!empty($datas['password'])) {
                 $customer->setPassword($datas['password']);
             }
             if (!empty($social_ids)) {
                 $customer->setSocialDatas($social_ids);
             }
             $customer->save();
             $this->getSession()->setCustomer($customer);
             if ($isNew) {
                 $this->_sendNewAccountEmail($customer, $datas['password']);
             }
             if (!$isMobile) {
                 $this->getSession()->addSuccess($this->_('Your account has been successfully saved'));
                 // Retour des données (redirection vers la page en cours)
                 $referer = !empty($datas['referer']) ? $datas['referer'] : $this->getRequest()->getHeader('referer');
                 $this->_redirect($referer);
                 return $this;
             }
             foreach ($this->getRequest()->getParam('add_to_session', array()) as $key => $value) {
                 $this->getSession()->{$key} = $value;
             }
             $html = array('success' => 1, 'customer_id' => $customer->getId());
         } catch (Exception $e) {
             $html = array('error' => 1, 'message' => $e->getMessage());
         }
         $this->_sendHtml($html);
     }
 }