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