]> dev.renevier.net Git - syj.git/blob - application/models/Pending.php
version 0.1
[syj.git] / application / models / Pending.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 abstract class Syj_Model_Pending extends Syj_Model_Generic
6 {
7     protected $_id;
8     protected $_user;
9     protected $_action;
10     protected $_hash;
11     protected $_notifications_number;
12     protected $_creation_time;
13
14     protected $_baseUrl;
15
16     public static $rules = array('validate_creation' => array(null, '6d', '7d'),
17                             'reset_password' => array(null, '2d'));
18
19     public function setId($id) {
20         $this->_id = (int) $id;
21         return $this;
22     }
23
24     public function getId() {
25         return $this->_id;
26     }
27
28     public function setUser(Syj_Model_User $user) {
29         $this->_user = $user;
30         return $this;
31     }
32
33     public function getUser() {
34         return $this->_user;
35     }
36
37     public function setAction($action) {
38         $possibleactions = array("validate_creation", "reset_password");
39         if (!in_array($action, $possibleactions)) {
40             throw new Zend_Exception((string)$action . ': Invalid value for Pending::action');
41         }
42         $this->_action = (string)$action;
43         return $this;
44     }
45
46     public function getAction() {
47         return $this->_action;
48     }
49
50     public function setHash($hash) {
51         $this->_hash = (string)$hash;
52         return $this;
53     }
54
55     public function getHash() {
56         return $this->_hash;
57     }
58
59     public function setNotificationsNumber($number) {
60         $this->_notifications_number = (int)$number;
61         return $this;
62     }
63
64     public function getNotificationsNumber() {
65         return $this->_notifications_number;
66     }
67
68     public function setCreationTime($timestamp) {
69         $this->_creation_time = date_create($timestamp);
70         return $this;
71     }
72
73     public function getCreationTime() {
74         return $this->_creation_time;
75     }
76
77     protected function getHashUrl() {
78         $rooturl = Zend_Controller_Front::getInstance()->getParam('rooturl');
79         return $rooturl . "pending/" . $this->_hash;
80     }
81
82     protected function getContactUrl() {
83         $rooturl = Zend_Controller_Front::getInstance()->getParam('rooturl');
84         return $rooturl . "contact";
85     }
86
87     protected function _sendMail($subject, $text) {
88         $mail = new Zend_Mail('utf-8');
89         $mail->addTo($this->_user->email)
90              ->setSubject($subject)
91              ->setBodyText($text);
92
93         try {
94             $mail->send();
95         } catch(Exception $e) {
96             return false;
97         }
98         return true;
99     }
100
101     public function run() {
102         if ($this->_run() === false) {
103             return false;
104         }
105         $mapper = new Syj_Model_PendingMapper();
106         $mapper->delete($this);
107         return true;
108     }
109     abstract protected function _run();
110
111     public function notify() {
112         $mapper = new Syj_Model_PendingMapper();
113         if (!$this->_hash) {
114             $mapper->save($this);
115         }
116
117         if ($this->_notify() === false) {
118             return false;
119         }
120
121         $this->_notifications_number++;
122
123         if ($this->_notifications_number >= count(self::$rules[$this->_action])) {
124             $this->cancel();
125         } else {
126             $mapper->save($this);
127         }
128         return true;
129     }
130     abstract protected function _notify();
131
132     public function cancel() {
133         if ($this->_cancel() === false) {
134             return false;
135         }
136         $mapper = new Syj_Model_PendingMapper();
137         $mapper->delete($this);
138         return true;
139     }
140     abstract protected function _cancel();
141
142 }