]> dev.renevier.net Git - syj.git/blob - application/controllers/NewpwdController.php
version 0.1
[syj.git] / application / controllers / NewpwdController.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 NewpwdController extends Zend_Controller_Action
6 {
7
8     public function init() {
9         $this->view->headScript()->appendFile('js/prototype.js');
10         $this->view->headScript()->appendFile('js/newpwd.js');
11         $this->view->headLink()->appendStylesheet('css/generic.css');
12         $this->view->headLink()->appendStylesheet('css/newpwd.css');
13     }
14
15     public function indexAction() {
16         $form = new Syj_Form_Newpwd(array('name' => 'newpwdform'));
17         $request = $this->getRequest();
18         $formData = $request->getPost();
19         $this->view->form = $form;
20         $httprequest = $request->isXmlHttpRequest();
21
22         if (!$httprequest) {
23             $this->_jsLocaleStrings();
24         }
25
26         if (empty($formData)) {
27             $loggeduser = $this->view->loggedUser();
28             if ($loggeduser) {
29                 $form->newpwd_email->setValue($loggeduser->email)
30                                     ->setAttrib('readonly', 'true');
31             }
32         }
33
34         if (empty ($formData) or !$form->isValid($formData)) {
35             if ($httprequest) {
36                 throw new Syj_Exception_Request();
37             } else {
38                 return;
39             }
40         }
41
42         /* form has been filled */
43         $userMapper = new Syj_Model_UserMapper();
44         $user = new Syj_Model_User();
45         if ($userMapper->findByEmail($formData['newpwd_email'], $user)) {
46             // if no user exist with posted email, pretend everything went correct
47             $loggeduser = isset($loggeduser) ? $loggeduser: $this->view->loggedUser();
48             if ($loggeduser and ($loggeduser != $user)) {
49                 throw new Syj_Exception_Request();
50             }
51             $pending = new Syj_Model_Pending_ResetPassword();
52             $pending->setUser($user);
53             if (!$pending->notify()) {
54                 throw new Zend_Exception();
55             }
56         }
57
58         if ($httprequest) {
59             $api = $this->_helper->SyjApi->setCode(200);
60         } else {
61             $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
62         }
63
64     }
65
66     protected function _jsLocaleStrings() {
67         $this->view->jslocales = array(
68             'notEmptyField' => __("Value is required and can't be empty"),
69             );
70     }
71 }