2 /* This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3 and is published under the AGPL license. */
5 abstract class Syj_Model_Pending extends Syj_Model_Generic
11 protected $_notifications_number;
12 protected $_creation_time;
16 public static $rules = array('validate_creation' => array(null, '6d', '7d'),
17 'reset_password' => array(null, '2d'));
19 public function setId($id) {
20 $this->_id = (int) $id;
24 public function getId() {
28 public function setUser(Syj_Model_User $user) {
33 public function getUser() {
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');
42 $this->_action = (string)$action;
46 public function getAction() {
47 return $this->_action;
50 public function setHash($hash) {
51 $this->_hash = (string)$hash;
55 public function getHash() {
59 public function setNotificationsNumber($number) {
60 $this->_notifications_number = (int)$number;
64 public function getNotificationsNumber() {
65 return $this->_notifications_number;
68 public function setCreationTime($timestamp) {
69 $this->_creation_time = date_create($timestamp);
73 public function getCreationTime() {
74 return $this->_creation_time;
77 protected function getHashUrl() {
78 $rooturl = Zend_Controller_Front::getInstance()->getParam('rooturl');
79 return $rooturl . "pending/" . $this->_hash;
82 protected function getContactUrl() {
83 $rooturl = Zend_Controller_Front::getInstance()->getParam('rooturl');
84 return $rooturl . "contact";
87 protected function _sendMail($subject, $text) {
88 $mail = new Zend_Mail('utf-8');
89 $mail->addTo($this->_user->email)
90 ->setSubject($subject)
95 } catch(Exception $e) {
101 public function run() {
102 if ($this->_run() === false) {
105 $mapper = new Syj_Model_PendingMapper();
106 $mapper->delete($this);
109 abstract protected function _run();
111 public function notify() {
112 $mapper = new Syj_Model_PendingMapper();
114 $mapper->save($this);
117 if ($this->_notify() === false) {
121 $this->_notifications_number++;
123 if ($this->_notifications_number >= count(self::$rules[$this->_action])) {
126 $mapper->save($this);
130 abstract protected function _notify();
132 public function cancel() {
133 if ($this->_cancel() === false) {
136 $mapper = new Syj_Model_PendingMapper();
137 $mapper->delete($this);
140 abstract protected function _cancel();