public function savepostAction() { if ($data = $this->getRequest()->getPost()) { $user = new Backoffice_Model_User(); $check_email_user = new Backoffice_Model_User(); try { if (!empty($data['user_id'])) { $user->find($data['user_id']); if (!$user->getId()) { throw new Exception($this->_('An error occurred while saving your account. Please try again later.')); } } if (empty($data['email'])) { throw new Exception($this->_('The email is required')); } $isNew = (bool) (!$user->getId()); $check_email_user->find($data['email'], 'email'); if ($check_email_user->getId() and $check_email_user->getId() != $user->getId()) { throw new Exception($this->_('This email address is already used')); } if (isset($data['password'])) { if ($data['password'] != $data['confirm_password']) { throw new Exception($this->_('Your password does not match the entered password.')); } if (!empty($data['old_password']) and !$user->isSamePassword($data['old_password'])) { throw new Exception($this->_("The old password does not match the entered password.")); } if (!empty($data['password'])) { $user->setPassword($data['password']); unset($data['password']); } } else { if ($isNew) { throw new Exception($this->_('The password is required')); } } $user->addData($data)->save(); $this->getSession()->addSuccess($this->_('The account has been successfully saved')); $this->_redirect('backoffice/account/list'); } catch (Exception $e) { $this->getSession()->addError($e->getMessage()); if ($user->getId()) { $this->_redirect('backoffice/account/edit', array('user_id' => $user->getId())); } else { $this->_redirect('backoffice/account/new'); } } } }
public function saveAction() { if ($data = Zend_Json::decode($this->getRequest()->getRawBody())) { try { if (!Zend_Validate::is($data["email"], "emailAddress")) { throw new Exception($this->_("Please, enter a correct email address.")); } $user = new Backoffice_Model_User(); $dummy = new Backoffice_Model_User(); $dummy->find($data["email"], "email"); $isNew = true; if (!empty($data["id"])) { $user->find($data["id"]); $isNew = !$user->getId(); } $user->addData($data); if ($dummy->getEmail() == $user->getEmail() and $dummy->getId() != $user->getId()) { throw new Exception($this->_("We are sorry but this email address already exists.")); } if ($isNew and empty($data["password"])) { throw new Exception($this->_("Please, enter a password.")); } if (!empty($data["password"]) and $data["password"] != $data["confirm_password"]) { throw new Exception($this->_("Passwords don't match")); } if (!empty($data["password"])) { $user->setPassword($data["password"]); } $user->save(); $data = array("success" => 1, "message" => $this->_("User successfully saved")); } catch (Exception $e) { $data = array("error" => 1, "message" => $e->getMessage()); } $this->_sendHtml($data); } }