Пример #1
0
 /**
  * settingsAction: users edit their information
  * accessible only to users who are logged in
  *
  * @author tuomas valtanen
  * @author joel peltonen
  */
 function settingsAction()
 {
     // get authentication instance
     $auth = Zend_Auth::getInstance();
     // if user has identity
     if ($auth->hasIdentity()) {
         // retrieve user id from the identity
         $identity = $auth->getIdentity();
         $id = $identity->user_id;
         // generate the form for user settings
         $form = new Default_Form_AccountSettingsForm();
         // send form to view
         try {
             $this->view->form = $form;
         } catch (Zend_Exception $e) {
             // this should be replaced by throwing a general 500 error
             echo '<pre>Unknown server error occurred! Please try later </pre>';
         }
         // get user data
         $userInfos = new Default_Model_UserProfiles();
         $settingsData = $userInfos->getUserInfoById($id);
         // get user email and push to settingsData
         $userModel = new Default_Model_User($id);
         $email = $userModel->getUserEmail($id);
         $settingsData['gravatar'] = $userModel->getGravatarStatus($id);
         $settingsData['email'] = $email;
         $settingsData['confirm_email'] = $email;
         $settingsData['username'] = $identity->username;
         // Get users email notifications and push to settingsdata in correct form
         $notificationsModel = new Default_Model_Notifications();
         $notifications = $notificationsModel->getNotificationsById($id);
         $settingsData['notifications'] = array();
         foreach ($notifications as $id_ntf => $notification) {
             array_push($settingsData['notifications'], $id_ntf);
         }
         // populate form
         if (isset($settingsData)) {
             //echo '<pre>'; var_dump($settingsData);
             $form->populate($settingsData);
         }
         // If request is post
         //$request = $this->getRequest();
         if ($this->_request->isPost()) {
             // get form data
             $formdata = $this->_request->getPost();
             if ($form->isValid($formdata)) {
                 // if form is valid
                 // Updates checked notifications
                 //echo "<pre>"; var_dump($formdata);
                 $notificationsModel->setUserNotifications($id, $formdata['notifications']);
                 $userProfile = new Default_Model_UserProfiles();
                 $userProfile->setProfileData($id, $formdata);
                 $user = new Default_Model_User($id);
                 // Updates email
                 if (strlen($formdata['email']) != 0) {
                     $user->changeUserEmail($id, $formdata['email']);
                 }
                 // Updates the password
                 if (strlen($formdata['password']) != 0) {
                     $user->changeUserPassword($id, $formdata['password']);
                 }
                 // Redirects the user to a user page
                 $redirect = $this->_urlHelper->url(array('controller' => 'account', 'action' => 'view', 'user' => $identity->username, 'language' => $this->view->language), 'lang_default', true);
                 $this->_redirect($redirect);
             } else {
                 // Formdata is not valid, do nothing -- here for possible debugging
                 // echo $form->getErrors();
                 // echo $form->getMessages();
             }
         } else {
             // request is not post, do nothing
         }
     } else {
         // user has no identity -- Get url helper and redirect away
         $urlHelper = $this->_helper->getHelper('url');
         $target = $urlHelper->url(array('controller' => 'index', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
         $this->_redirect($target);
     }
 }