2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class Syj_Model_PendingMapper
8 protected $_tableInfos = array('name' => 'pending_actions');
10 public function getDbTable() {
11 if (null === $this->_dbTable) {
12 $this->_dbTable = new Zend_Db_Table($this->_tableInfos);
14 return $this->_dbTable;
17 public function fetchByHash($hash) {
18 $table = $this->getDbTable();
19 $result = $table->fetchAll(array('hash = ?' => (string)$hash));
21 if (1 !== count($result)) {
24 $row = $result->current();
26 $entry = $this->pendingFactory($row->action);
30 $this->_itemFromRow($entry, $row);
34 public function find($id, Syj_Model_Pending $pending) {
35 $result = $this->getDbTable()->find((int)$id);
36 if (0 == count($result)) {
39 $row = $result->current();
40 $this->_itemFromRow($pending,$row);
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()));
52 $resultSet = $table->fetchAll($select);
54 foreach ($resultSet as $row) {
55 $entry = $this->pendingFactory($row->action);
59 $entry->setUser($user);
60 $entries[] = $this->_itemFromRow($entry, $row);
65 public function fetchAll() {
66 $table = $this->getDbTable();
67 $select = $table->select();
68 $select->union($this->_selects());
70 $resultSet = $table->fetchAll($select);
72 foreach ($resultSet as $row) {
73 $entry = $this->pendingFactory($row->action);
77 $entries[] = $this->_itemFromRow($entry, $row);
82 protected function pendingFactory($action) {
84 case 'validate_creation':
85 return new Syj_Model_Pending_ValidateCreation();
87 case 'reset_password':
88 return new Syj_Model_Pending_ResetPassword();
96 protected function _selects() {
98 $table = $this->getDbTable();
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);
105 $select->where('creation_time < (NOW() - interval ?)', $item);
107 if ($item === end($action)) {
108 $select->where('notifications_number >= ?', $number);
110 $select->where('notifications_number = ?', $number);
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);
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);
136 public function save (Syj_Model_Pending $pending) {
138 'userid'=> (string) $pending->user->id,
139 'action'=> (string)$pending->action,
141 if (isset($pending->hash)) {
142 $data['hash'] = (string) $pending->hash;
144 if (isset($pending->notificationsNumber)) {
145 $data['notifications_number'] = (string) $pending->notificationsNumber;
147 if (null === ($id = $pending->getId())) {
148 $id = $this->getDbTable()->insert($data);
149 $this->find($id, $pending);
151 $this->getDbTable()->update($data, array('id = ?' => $id));
152 $this->find($id, $pending);
156 public function delete (Syj_Model_Pending $pending) {
157 if (null !== ($id = $pending->getId())) {
158 $this->getDbTable()->delete(array('id = ?' => $id));