Ejemplo n.º 1
0
 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."));
             }
             $admin = new Admin_Model_Admin();
             $dummy = new Admin_Model_Admin();
             $dummy->find($data["email"], "email");
             $isNew = true;
             $data["confirm_password"] = !empty($data["confirm_password"]) ? $data["confirm_password"] : "";
             if (!empty($data["id"])) {
                 $admin->find($data["id"]);
                 $isNew = !$admin->getId();
             }
             if ($isNew and empty($data["password"])) {
                 throw new Exception($this->_("Please, enter a password."));
             }
             if (empty($data["password"]) and empty($data["confirm_password"])) {
                 unset($data["password"]);
                 unset($data["confirm_password"]);
             }
             if (!empty($data["password"]) and $data["password"] != $data["confirm_password"]) {
                 throw new Exception($this->_("Passwords don't match"));
             }
             $admin->addData($data);
             if ($dummy->getEmail() == $admin->getEmail() and $dummy->getId() != $admin->getId()) {
                 throw new Exception($this->_("We are sorry but this email address already exists."));
             }
             if (!empty($data["password"])) {
                 $admin->setPassword($data["password"]);
             }
             if (!empty($data["publication_access_type"])) {
                 $admin->setPublicationAccessType($data["publication_access_type"]);
             }
             $admin->save();
             $data = array("success" => 1, "message" => $this->_("User successfully saved"));
         } catch (Exception $e) {
             $data = array("error" => 1, "message" => $e->getMessage());
         }
         $this->_sendHtml($data);
     }
 }
Ejemplo n.º 2
0
 public function updateAction()
 {
     if ($data = $this->getRequest()->getPost()) {
         try {
             if (isset($data["id"])) {
                 unset($data["id"]);
             }
             $admin = new Admin_Model_Admin();
             if (!empty($data["user_id"])) {
                 $admin->find($data["user_id"]);
                 if (!$admin->getId()) {
                     throw new Exception($this->_("This admin does not exist"));
                 }
             }
             if (!empty($data["email"])) {
                 $email_checker = new Admin_Model_Admin();
                 $email_checker->find($data['email'], 'email');
                 if ($email_checker->getId() and $email_checker->getId() != $admin->getId()) {
                     throw new Exception($this->_("This email address is already used"));
                 }
             }
             $admin->addData($data);
             if (isset($data['password'])) {
                 $admin->setPassword($data["password"]);
             }
             $admin->save();
             $data = array("success" => 1, "user_id" => $admin->getId(), "token" => $admin->getLoginToken());
         } catch (Exception $e) {
             $data = array('error' => 1, 'message' => $e->getMessage());
         }
         $this->_sendHtml($data);
     }
 }