Improvements.
[platal.git] / include / notifs.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
009b8ab7 22abstract class WatchOperation
612a2d8a 23{
009b8ab7
FB
24 public function getTitle($count = 0)
25 {
26 if ($count == 1) {
27 return str_replace(array('$x', '$s'), '', $this->title);
28 } else {
29 return str_replace(array('$x', '$s'), array('x', 's'), $this->title);
30 }
31 }
32
33 public function getCondition(PlUser &$user, $date)
34 {
35 if (!$user->watch($this->flag)) {
36 return new UFC_False();
37 } else {
38 return $this->buildCondition($user, $date);
39 }
40 }
41
42 abstract protected function buildCondition(PlUser &$user, $date);
43 abstract public function getOrder();
44 abstract public function getDate(PlUser &$user);
45
c350577b
FB
46 public function publicationDate(PlUser &$user)
47 {
48 return $this->getDate($user);
49 }
50
009b8ab7
FB
51 public function seen(PlUser &$user, $last)
52 {
53 return strtotime($this->getDate($user)) > $last;
54 }
c350577b
FB
55
56 public function getData(PlUser &$user)
57 {
58 return null;
59 }
009b8ab7
FB
60}
61
62class WatchProfileUpdate extends WatchOperation
63{
64 public $flag = 'profile';
65 public $title = 'Mise$s à jour de fiche';
a3a049fc 66
4e7bf1e0 67 public static function register(Profile &$profile, $field)
612a2d8a 68 {
4e7bf1e0
FB
69 XDB::execute('REPLACE INTO watch_profile (uid, ts, field)
70 VALUES ({?}, NOW(), {?})',
71 $profile->id(), $field);
0337d704 72 }
73
009b8ab7 74 protected function buildCondition(PlUser &$user, $date)
612a2d8a 75 {
009b8ab7
FB
76 return new UFC_And(new UFC_ProfileUpdated('>', $date),
77 new UFC_WatchContact($user));
78 }
79
80 public function getOrder()
81 {
82 return new UFO_ProfileUpdate();
83 }
84
85 public function getDate(PlUser &$user)
86 {
87 return $user->profile()->last_change;
0337d704 88 }
89}
90
009b8ab7 91class WatchRegistration extends WatchOperation
612a2d8a 92{
009b8ab7
FB
93 public $flag = 'registration';
94 public $title = 'Inscription$s';
95
96 protected function buildCondition(PlUser &$user, $date)
97 {
98 return new UFC_And(new UFC_Registered(false, '>', $date),
99 new UFC_Or(new UFC_WatchContact($user),
100 new UFC_WatchPromo($user)));
101 }
102
103 public function getOrder()
104 {
105 return new UFO_Registration();
106 }
0337d704 107
009b8ab7 108 public function getDate(PlUser &$user)
612a2d8a 109 {
009b8ab7 110 return $user->registration_date;
0337d704 111 }
112}
113
009b8ab7 114class WatchDeath extends WatchOperation
612a2d8a 115{
009b8ab7
FB
116 public $flag = 'death';
117 public $title = 'Décès';
118
119 protected function buildCondition(PlUser &$user, $date)
120 {
121 return new UFC_And(new UFC_Dead('>', $date, true),
122 new UFC_Or(new UFC_WatchPromo($user),
123 new UFC_WatchContact($user)));
124 }
0337d704 125
009b8ab7 126 public function getOrder()
612a2d8a 127 {
009b8ab7
FB
128 return new UFO_Death();
129 }
130
131 public function getDate(PlUser &$user)
132 {
133 return $user->profile()->deathdate;
134 }
135
c350577b
FB
136 public function publicationDate(PlUser &$user)
137 {
138 return $user->profile()->deathdate_rec;
139 }
140
009b8ab7
FB
141 public function seen(PlUser &$user, $last)
142 {
143 return strtotime($user->profile()->deathdate_rec) > $last;
0337d704 144 }
145}
146
009b8ab7 147class WatchBirthday extends WatchOperation
612a2d8a 148{
009b8ab7 149 const WATCH_LIMIT = 604800; // 1 week
0337d704 150
009b8ab7
FB
151 public $flag = 'birthday';
152 public $title = 'Anniversaire$s';
153
154 protected function buildCondition(PlUser &$user, $date)
612a2d8a 155 {
7e735012 156 return new UFC_And(new UFC_OR(new UFC_Birthday('=', time()),
009b8ab7
FB
157 new UFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
158 new UFC_Birthday('>', $date + self::WATCH_LIMIT))),
159 new UFC_Or(new UFC_WatchPromo($user),
160 new UFC_WatchContact($user)));
161 }
162
163 public function getOrder()
164 {
165 return new UFO_Birthday();
166 }
167
168 public function getDate(PlUser &$user)
169 {
170 return $user->profile()->next_birthday;
171 }
172
c350577b
FB
173 public function publicationDate(PlUser &$user)
174 {
175 return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
176 }
177
009b8ab7
FB
178 public function seen(PlUser &$user, $last)
179 {
180 $birthday = strtotime($user->profile()->next_birthday);
181 return $birthday > $last + self::WATCH_LIMIT
182 || date('Ymd', $birthday) == date('Ymd');
0337d704 183 }
184}
a3a049fc 185
7e735012
FB
186class Watch
187{
188 private static $classes = array('WatchRegistration',
189 'WatchProfileUpdate',
190 'WatchDeath',
191 'WatchBirthday');
192
009b8ab7 193 private static function fetchCount(PlUser &$user, $date, $class)
7e735012
FB
194 {
195 $obj = new $class();
009b8ab7 196 $uf = new UserFilter($obj->getCondition($user, $date));
7e735012
FB
197 return $uf->getTotalCount();
198 }
199
009b8ab7 200 public static function getCount(PlUser &$user, $date = null)
7e735012
FB
201 {
202 $count = 0;
009b8ab7
FB
203 if (is_null($date)) {
204 $date = $user->watchLast();
205 }
7e735012 206 foreach (self::$classes as $class) {
009b8ab7 207 $count += self::fetchCount($user, $date, $class);
7e735012
FB
208 }
209 return $count;
210 }
009b8ab7
FB
211
212
213 private static function fetchEvents(PlUser &$user, $date, $class)
214 {
215 $obj = new $class();
216 $uf = new UserFilter($obj->getCondition($user, $date),
217 array($obj->getOrder(), new UFO_Name(UserFilter::DN_SORT)));
218 $users = $uf->getUsers();
219 if (count($users) == 0) {
220 return null;
221 } else {
222 return array('operation' => $obj,
223 'title' => $obj->getTitle(count($users)),
224 'users' => $users);
225 }
226 }
227
228 public static function getEvents(PlUser &$user, $date = null)
229 {
230 if (is_null($date)) {
231 $date = $user->watchLast();
232 }
233 $events = array();
234 foreach (self::$classes as $class) {
235 $e = self::fetchEvents($user, $date, $class);
236 if (!is_null($e)) {
237 $events[] = $e;
238 }
239 }
240 return $events;
241 }
7e735012
FB
242}
243
244// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 245?>