]> dev.renevier.net Git - syj.git/blob - application/controllers/AccountController.php
redirection after path creation is decided by server (php), not guessed by client...
[syj.git] / application / controllers / AccountController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class AccountController extends Zend_Controller_Action
6 {
7
8     public function init() {
9         $this->_helper->SyjSession->needsLogin();
10
11         $this->view->headScript()->appendFile('js/prototype.js');
12         $this->view->headScript()->appendFile('js/utils.js');
13         $this->view->headScript()->appendFile('js/account.js');
14         $this->view->headLink()->appendStylesheet('css/generic.css');
15         $this->view->headLink()->appendStylesheet('css/account.css');
16         $this->view->headTitle($this->view->translate("my account"));
17     }
18
19     public function indexAction() {
20         $user = $this->_helper->SyjSession->user();
21         $request = $this->getRequest();
22
23         $form = new Syj_Form_Account(array('name' => 'accountform'));
24         $formData = $request->getPost();
25
26         $valid = false;
27         if (!empty($formData) and $form->isValid($formData)) {
28             $valid = true;
29             if ($user->password != sha1($formData['account_password_current'])) {
30                 $valid = false;
31                 $form->account_password_current->addError(__("Wrong password"));
32             }
33             $user->email = $formData['account_email'];
34             $user->password = sha1($formData['account_password']);
35             $userMapper = new Syj_Model_UserMapper();
36
37             try {
38                 $userMapper->save ($user);
39             } catch(Zend_Db_Statement_Exception $e) {
40                 if ($e->getCode() == 23505) { // 23505: Unique violation throw new Syj_Exception_Request();
41                     $message = $e->getMessage();
42                     if (strpos($message, 'users_email_key') !== false) {
43                         $valid = false;
44                         $form->account_email->addError(__("an user is already registered with this email"));
45                     } else {
46                         throw $e;
47                     }
48                 } else {
49                     throw $e;
50                 }
51             }
52         }
53
54         if ($valid) {
55             $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
56             return;
57         }
58
59         if (empty($formData)) {
60             $form->account_email->setValue($user->email);
61         } else {
62             $form->account_email->setValue($formData['account_email']);
63         }
64
65         $this->_jsLocaleStrings();
66         $this->view->form = $form;
67     }
68
69     protected function _jsLocaleStrings() {
70         $this->view->jslocales = array(
71             'notEmptyField' => __("Value is required and can't be empty"),
72             'passwordNoMatchWarn' => __("Password do not match"),
73             'passwordLenghtWarn' => array(__("At least %d characters"), 6),
74             'nochangeWarn' => __("You have made no change"),
75             );
76     }
77 }