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