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