Merge branch 'xorg/master' into xorg/f/xnet-accounts
[platal.git] / include / notifs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
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
22 abstract class WatchOperation
23 {
24 private static $false = null;
25
26 public function getTitle($count = 0)
27 {
28 if ($count == 1) {
29 return str_replace(array('$x', '$s'), '', $this->title);
30 } else {
31 return str_replace(array('$x', '$s'), array('x', 's'), $this->title);
32 }
33 }
34
35 public function getCondition(Watch $watch)
36 {
37 if (!$watch->user()->watchType($this->flag)) {
38 if (!self::$false) {
39 self::$false = new PFC_False();
40 }
41 return self::$false;
42 } else {
43 return $this->buildCondition($watch);
44 }
45 }
46
47 abstract protected function buildCondition(Watch $watch);
48 abstract public function getOrder();
49 abstract public function getDate(PlUser &$user);
50
51 public function publicationDate(PlUser &$user)
52 {
53 return $this->getDate($user);
54 }
55
56 public function seen(PlUser &$user, $last)
57 {
58 return strtotime($this->getDate($user)) > $last;
59 }
60
61 public function getData(PlUser &$user)
62 {
63 return null;
64 }
65 }
66
67 class WatchProfileUpdate extends WatchOperation
68 {
69 private static $order = null;
70
71 public $flag = 'profile';
72 public $title = 'Mise$s à jour de fiche';
73 private $date = null;
74
75 public static function register(Profile &$profile, $field)
76 {
77 XDB::execute('INSERT INTO watch_profile (pid, ts, field)
78 VALUES ({?}, NOW(), {?})
79 ON DUPLICATE KEY UPDATE ts = NOW()',
80 $profile->id(), $field);
81 }
82
83 protected function buildCondition(Watch $watch)
84 {
85 $this->date = $watch->date();
86 return new PFC_And(new UFC_ProfileUpdated('>', $watch->date()),
87 $watch->contactCondition());
88 }
89
90 public function getOrder()
91 {
92 if (!self::$order) {
93 self::$order = new UFO_ProfileUpdate();
94 }
95 return self::$order;
96 }
97
98 public function getDate(PlUser &$user)
99 {
100 return $user->profile()->last_change;
101 }
102
103 public function getData(PlUser &$user)
104 {
105 $data = XDB::fetchColumn("SELECT field
106 FROM watch_profile
107 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
108 ORDER BY ts",
109 $user->profile()->id(), $this->date);
110 if (count($data) == 0) {
111 return null;
112 } else {
113 $text = array();
114 foreach ($data as $f) {
115 $text[] = Profile::$descriptions[$f];
116 }
117 return $text;
118 }
119 }
120 }
121
122 class WatchRegistration extends WatchOperation
123 {
124 private static $order = null;
125
126 public $flag = 'registration';
127 public $title = 'Inscription$s';
128
129 protected function buildCondition(Watch $watch)
130 {
131 return new PFC_And(new UFC_Registered(false, '>', $watch->date()),
132 new PFC_Or($watch->contactCondition(),
133 $watch->promoCondition()));
134 }
135
136 public function getOrder()
137 {
138 if (!self::$order) {
139 self::$order = new UFO_Registration();
140 }
141 return self::$order;
142 }
143
144 public function getDate(PlUser &$user)
145 {
146 return $user->registration_date;
147 }
148 }
149
150 class WatchDeath extends WatchOperation
151 {
152 private static $order = null;
153
154 public $flag = 'death';
155 public $title = 'Décès';
156
157 protected function buildCondition(Watch $watch)
158 {
159 return new PFC_And(new UFC_Dead('>', $watch->date(), true),
160 new PFC_Or($watch->contactCondition(),
161 $watch->promoCondition()));
162 }
163
164 public function getOrder()
165 {
166 if (!self::$order) {
167 self::$order = new UFO_Death();
168 }
169 return self::$order;
170 }
171
172 public function getDate(PlUser &$user)
173 {
174 return $user->profile()->deathdate;
175 }
176
177 public function publicationDate(PlUser &$user)
178 {
179 return $user->profile()->deathdate_rec;
180 }
181
182 public function seen(PlUser &$user, $last)
183 {
184 return strtotime($user->profile()->deathdate_rec) > $last;
185 }
186 }
187
188 class WatchBirthday extends WatchOperation
189 {
190 const WATCH_LIMIT = 604800; // 1 week
191
192 private static $order = null;
193
194 public $flag = 'birthday';
195 public $title = 'Anniversaire$s';
196
197 protected function buildCondition(Watch $watch)
198 {
199 $select_date = new PFC_OR(new UFC_Birthday('=', time()),
200 new PFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
201 new UFC_Birthday('>', $watch->date() + self::WATCH_LIMIT)));
202 $profile = $watch->profile();
203 $cond = $watch->contactCondition();
204 if ($profile) {
205 $cond = new PFC_Or($cond,
206 new PFC_And($watch->promoCondition(),
207 new UFC_Promo('>=', $profile->mainGrade(), $profile->yearpromo() - 1),
208 new UFC_Promo('<=', $profile->mainGrade(), $profile->yearpromo() + 1)));
209 }
210 return new PFC_And($select_date, $cond);
211 }
212
213 public function getOrder()
214 {
215 if (!self::$order) {
216 self::$order = new UFO_Birthday();
217 }
218 return self::$order;
219 }
220
221 public function getDate(PlUser &$user)
222 {
223 return $user->profile()->next_birthday;
224 }
225
226 public function publicationDate(PlUser &$user)
227 {
228 return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
229 }
230
231 public function seen(PlUser &$user, $last)
232 {
233 $birthday = strtotime($user->profile()->next_birthday);
234 return $birthday > $last + self::WATCH_LIMIT
235 || date('Ymd', $birthday) == date('Ymd');
236 }
237 }
238
239 class Watch
240 {
241 private static $classes = array('WatchRegistration',
242 'WatchProfileUpdate',
243 'WatchDeath',
244 'WatchBirthday');
245 private static $events = array();
246
247 private $user = null;
248 private $date = null;
249 private $contactCond = null;
250 private $promoCond = null;
251
252 private $filters = array();
253
254 public function __construct(PlUser $user, $date = null)
255 {
256 $this->user = $user;
257 $this->date = self::getDate($user, $date);
258 }
259
260 public function user()
261 {
262 return $this->user;
263 }
264
265 public function profile()
266 {
267 return $this->user->profile();
268 }
269
270 public function date()
271 {
272 return $this->date;
273 }
274
275 public function contactCondition()
276 {
277 if (!$this->contactCond) {
278 $this->contactCond = new UFC_WatchContact($this->user);
279 }
280 return $this->contactCond;
281 }
282
283 public function promoCondition()
284 {
285 if (!$this->promoCond) {
286 $this->promoCond = new UFC_WatchPromo($this->user);
287 }
288 return $this->promoCond;
289 }
290
291 private function fetchEventWatch($class)
292 {
293 if (!isset(self::$events[$class])) {
294 self::$events[$class] = new $class();
295 }
296 return self::$events[$class];
297 }
298
299 private function fetchFilter($class)
300 {
301
302 if (!isset($this->filters[$class])) {
303 $event = $this->fetchEventWatch($class);
304 $this->filters[$class] = new UserFilter($event->getCondition($this),
305 array($event->getOrder(), new UFO_Name(Profile::DN_SORT)));
306 }
307 return $this->filters[$class];
308 }
309
310 public function count()
311 {
312 $count = 0;
313 foreach (self::$classes as $class) {
314 $uf = $this->fetchFilter($class);
315 $count += $uf->getTotalCount();
316 }
317 return $count;
318 }
319
320
321 private function fetchEvents($class)
322 {
323 $obj = $this->fetchEventWatch($class);
324 $uf = $this->fetchFilter($class);
325 $users = $uf->getUsers();
326 if (count($users) == 0) {
327 return null;
328 } else {
329 return array('type' => $obj->flag,
330 'operation' => $obj,
331 'title' => $obj->getTitle(count($users)),
332 'users' => $users);
333 }
334 }
335
336 public function events()
337 {
338 $events = array();
339 foreach (self::$classes as $class) {
340 $e = $this->fetchEvents($class);
341 if (!is_null($e)) {
342 $events[] = $e;
343 }
344 }
345 return $events;
346 }
347
348
349 private static function getDate(PlUser &$user, $date)
350 {
351 if (is_null($date)) {
352 $date = $user->watchLast();
353 $limit = time() - (7 * 86400);
354 if ($date < $limit) {
355 $date = $limit;
356 }
357 }
358 return $date;
359 }
360
361 public static function getCount(PlUser &$user, $date = null)
362 {
363 $watch = new Watch($user, $date);
364 return $watch->count();
365 }
366
367 public static function getEvents(PlUser &$user, $date = null)
368 {
369 $watch = new Watch($user, $date);
370 return $watch->events();
371 }
372 }
373
374 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
375 ?>