]> dev.renevier.net Git - syj.git/blob - application/controllers/AccountController.php
highlight for input on errors
[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->view->headScript()->appendFile('js/prototype.js');
10         $this->view->headScript()->appendFile('js/forms.js');
11         $this->view->headScript()->appendFile('js/highlight.js');
12         $this->view->headScript()->appendFile('js/account.js');
13         $this->view->headLink()->appendStylesheet('css/generic.css');
14         $this->view->headLink()->appendStylesheet('css/account.css');
15         $this->view->headTitle($this->view->translate("my account"));
16     }
17
18     public function indexAction() {
19         $user = $this->view->loggedUser();
20         $request = $this->getRequest();
21
22         if ($user === null) {
23             $encodeduri = implode('/', array_map('urlencode', explode('/', $request->getRequestUri())));
24             $loginurl = $this->view->addParamToUrl($this->view->baseUrl() . '/' . 'login', 'redirect', $encodeduri);
25             $translator = Zend_Registry::get('Zend_Translate');
26             $lang = Zend_Controller_Front::getInstance()->getRequest()->getQuery('lang');
27             if ($lang) {
28                 $adapter = $translator->getAdapter();
29                 if ($adapter->isAvailable($lang)) {
30                     $loginurl = $this->view->addParamToUrl($loginurl, 'lang', $lang);
31                 }
32             }
33             $this->_helper->Redirector->gotoURL($loginurl, array('prependBase' => false));
34         }
35
36         $form = new Syj_Form_Account(array('name' => 'accountform'));
37         $formData = $request->getPost();
38
39         $valid = false;
40         if (!empty($formData) and $form->isValid($formData)) {
41             $valid = true;
42             if ($user->password != sha1($formData['account_password_current'])) {
43                 $valid = false;
44                 $form->account_password_current->addError(__("Wrong password"));
45             }
46             $user->email = $formData['account_email'];
47             $user->password = sha1($formData['account_password']);
48             $userMapper = new Syj_Model_UserMapper();
49
50             try {
51                 $userMapper->save ($user);
52             } catch(Zend_Db_Statement_Exception $e) {
53                 if ($e->getCode() == 23505) { // 23505: Unique violation throw new Syj_Exception_Request();
54                     $message = $e->getMessage();
55                     if (strpos($message, 'users_email_key') !== false) {
56                         $valid = false;
57                         $form->account_email->addError(__("an user is already registered with this email"));
58                     } else {
59                         throw $e;
60                     }
61                 } else {
62                     throw $e;
63                 }
64             }
65         }
66
67         if ($valid) {
68             $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
69             return;
70         }
71
72         if (empty($formData)) {
73             $form->account_email->setValue($user->email);
74         } else {
75             $form->account_email->setValue($formData['account_email']);
76         }
77
78         $this->_jsLocaleStrings();
79         $this->view->form = $form;
80     }
81
82     protected function _jsLocaleStrings() {
83         $this->view->jslocales = array(
84             'notEmptyField' => __("Value is required and can't be empty"),
85             'passwordNoMatchWarn' => __("Password do not match"),
86             'passwordLenghtWarn' => array(__("At least %d characters"), 6),
87             'nochangeWarn' => __("You have made no change"),
88             );
89     }
90 }