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