예제 #1
0
 public function editAction()
 {
     $this->view->headTitle()->append($this->view->translate('Edit your profile'));
     $id = $this->view->id = (int) $this->getRequest()->getParam('id');
     $auth = Zend_Auth::getInstance();
     if (!$auth->getIdentity()->id) {
         $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
         $this->_redirect('/' . $this->view->lang . '/woeid/' . $this->location . '/give');
         return;
     }
     $model = new Model_User();
     $user = $model->fetchUser($id)->id;
     if ($auth->getIdentity()->id == $user) {
         //if is the user profile owner lets edit
         require_once APPLICATION_PATH . '/forms/UserEdit.php';
         $form = new Form_UserEdit();
         $form->submit->setLabel('Save profile');
         $this->view->form = $form;
         if ($this->getRequest()->isPost()) {
             $formData = $this->getRequest()->getPost();
             if ($form->isValid($formData)) {
                 //chekusername if exists, dont let change it
                 $checkuser = $model->checkUsername($form->getValue('username'));
                 if (!is_null($checkuser) and $checkuser['username'] != $auth->getIdentity()->username) {
                     $this->view = $this->initView();
                     $this->view->error = $this->view->translate('This username is taken. Please choose another one.');
                     return;
                 }
                 $data['id'] = $id;
                 $data['username'] = $form->getValue('username');
                 if ($form->getValue('password')) {
                     $data['password'] = md5(trim($form->getValue('password')));
                 }
                 $model->update($data);
                 //update the auth data stored
                 $auth = Zend_Auth::getInstance();
                 $auth->getStorage()->write((object) $data);
                 $this->_helper->_flashMessenger->addMessage($this->view->translate('Your profile was edited succesfully!'));
                 $this->_redirect('/' . $this->view->lang . '/woeid/' . $this->location . '/give');
                 return;
             } else {
                 $form->populate($formData);
             }
         } else {
             $id = $this->_getParam('id', 0);
             if ($id > 0) {
                 $user = new Model_User();
                 $form->populate($user->fetchUser($id)->toArray());
             }
         }
     } else {
         $this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));
         $this->_redirect('/' . $this->view->lang . '/woeid/' . $this->location . '/give');
         return;
     }
 }