]> dev.renevier.net Git - syj.git/blob - application/models/PendingMapper.php
version 0.1
[syj.git] / application / models / PendingMapper.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 Syj_Model_PendingMapper
6 {
7     protected $_dbTable;
8     protected $_tableInfos = array('name' => 'pending_actions');
9
10     public function getDbTable() {
11         if (null === $this->_dbTable) {
12             $this->_dbTable = new Zend_Db_Table($this->_tableInfos);
13         }
14         return $this->_dbTable;
15     }
16
17     public function fetchByHash($hash) {
18         $table = $this->getDbTable();
19         $result = $table->fetchAll(array('hash = ?' => (string)$hash));
20
21         if (1 !== count($result)) {
22             return null;
23         }
24         $row = $result->current();
25
26         $entry = $this->pendingFactory($row->action);
27         if (!isset($entry)) {
28             continue;
29         }
30         $this->_itemFromRow($entry, $row);
31         return $entry;
32     }
33
34     public function find($id, Syj_Model_Pending $pending) {
35         $result = $this->getDbTable()->find((int)$id);
36         if (0 == count($result)) {
37             return false;
38         }
39         $row = $result->current();
40         $this->_itemFromRow($pending,$row);
41         return true;
42     }
43
44
45     public function fetchForUser(Syj_Model_User $user) {
46         $table = $this->getDbTable();
47         $select = $table->select();
48         $select->union(array_map(function($select) use (&$user) {
49             return $select->where('userid = ?', $user->id);
50         }, $this->_selects()));
51
52         $resultSet = $table->fetchAll($select);
53         $entries   = array();
54         foreach ($resultSet as $row) {
55             $entry = $this->pendingFactory($row->action);
56             if (!isset($entry)) {
57                 continue;
58             }
59             $entry->setUser($user);
60             $entries[] = $this->_itemFromRow($entry, $row);
61         }
62         return $entries;
63     }
64
65     public function fetchAll() {
66         $table = $this->getDbTable();
67         $select = $table->select();
68         $select->union($this->_selects());
69
70         $resultSet = $table->fetchAll($select);
71         $entries   = array();
72         foreach ($resultSet as $row) {
73             $entry = $this->pendingFactory($row->action);
74             if (!isset($entry)) {
75                 continue;
76             }
77             $entries[] = $this->_itemFromRow($entry, $row);
78         }
79         return $entries;
80     }
81
82     protected function pendingFactory($action) {
83         switch ($action) {
84             case 'validate_creation':
85                 return new Syj_Model_Pending_ValidateCreation();
86             break;
87             case 'reset_password':
88                 return new Syj_Model_Pending_ResetPassword();
89             break;
90             default:
91                 return null;
92             break;
93         }
94     }
95
96     protected function _selects() {
97         $res = array();
98         $table = $this->getDbTable();
99
100         foreach (Syj_Model_Pending::$rules as $action_name => $action) {
101             foreach ($action as $number => $item) {
102                 $select = $table->select();
103                 $select->where('action = ?', $action_name);
104                 if (isset($item)) {
105                     $select->where('creation_time < (NOW() - interval ?)', $item);
106                 }
107                 if ($item === end($action)) {
108                     $select->where('notifications_number >= ?', $number);
109                 } else {
110                     $select->where('notifications_number = ?', $number);
111                 }
112                 $res[] = $select;
113             }
114         }
115
116         return $res;
117     }
118
119     protected function _itemFromRow(Syj_Model_Pending $item, Zend_Db_Table_Row $row) {
120         $item->setId($row->id)
121              ->setHash($row->hash)
122              ->setNotificationsNumber($row->notifications_number)
123              ->setCreationTime($row->creation_time);
124
125         if (!$item->getUser()) {
126             $user = new Syj_Model_User();
127             $userMapper = new Syj_Model_UserMapper();
128             if ($userMapper->find($row->userid, $user)) {
129                 $item->setUser($user);
130             }
131         }
132
133         return $item;
134     }
135
136     public function save (Syj_Model_Pending $pending) {
137         $data = array(
138             'userid'=> (string) $pending->user->id,
139             'action'=> (string)$pending->action,
140         );
141         if (isset($pending->hash)) {
142             $data['hash'] = (string) $pending->hash;
143         }
144         if (isset($pending->notificationsNumber)) {
145             $data['notifications_number'] = (string) $pending->notificationsNumber;
146         }
147          if (null === ($id = $pending->getId())) {
148             $id = $this->getDbTable()->insert($data);
149             $this->find($id, $pending);
150         } else {
151             $this->getDbTable()->update($data, array('id = ?' => $id));
152             $this->find($id, $pending);
153         }
154     }
155
156     public function delete (Syj_Model_Pending $pending) {
157          if (null !== ($id = $pending->getId())) {
158             $this->getDbTable()->delete(array('id = ?' => $id));
159          }
160     }
161 }