]> dev.renevier.net Git - syj.git/blob - application/controllers/AccountController.php
5f20cc73849b863932ed287f6a8676abaf044945
[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         $this->_helper->SyjMedias->addScripts('account');
11
12         $this->view->headLink()->appendStylesheet('css/generic.css', 'all');
13         $this->view->headLink()->appendStylesheet('css/account.css', 'all');
14         $this->view->headTitle($this->view->translate("my account"));
15     }
16
17     public function indexAction() {
18         $user = $this->_helper->SyjSession->user();
19         $request = $this->getRequest();
20
21         $form = new Syj_Form_Account(array('name' => 'accountform'));
22         $formData = $request->getPost();
23
24         $valid = false;
25         if (!empty($formData) and $form->isValid($formData)) {
26             $valid = true;
27             if ($user->password != sha1($formData['account_password_current'])) {
28                 $valid = false;
29                 $form->account_password_current->addError(__("Wrong password"));
30             }
31             $user->email = $formData['account_email'];
32             $user->password = sha1($formData['account_password']);
33             $userMapper = new Syj_Model_UserMapper();
34
35             try {
36                 $userMapper->save ($user);
37             } catch(Zend_Db_Statement_Exception $e) {
38                 if ($e->getCode() == 23505) { // 23505: Unique violation throw new Syj_Exception_Request();
39                     $message = $e->getMessage();
40                     if (strpos($message, 'users_email_key') !== false) {
41                         $valid = false;
42                         $form->account_email->addError(__("an user is already registered with this email"));
43                     } else {
44                         throw $e;
45                     }
46                 } else {
47                     throw $e;
48                 }
49             }
50         }
51
52         if ($valid) {
53             $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
54             return;
55         }
56
57         if (empty($formData)) {
58             $form->account_email->setValue($user->email);
59         } else {
60             $form->account_email->setValue($formData['account_email']);
61         }
62
63         $this->_jsLocaleStrings();
64         $this->view->form = $form;
65     }
66
67     protected function _jsLocaleStrings() {
68         $this->view->jslocales = array(
69             'notEmptyField' => __("Value is required"),
70             'passwordNoMatchWarn' => __("Password do not match"),
71             'passwordLenghtWarn' => array(__("At least %d characters"), 6),
72             'nochangeWarn' => __("You have made no change"),
73             );
74     }
75 }