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