REPLACE INTO should only be used if the data deletion is wanted.
[platal.git] / include / notifs.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 static private $descriptions = array('search_names' => 'L\'un de ses noms',
104 'freetext' => 'Le texte libre',
105 'mobile' => 'Son numéro de téléphone portable',
106 'nationalite' => 'Sa nationalité',
107 'nationalite2' => 'Sa seconde nationalité',
108 'nationalite3' => 'Sa troisième nationalité',
109 'nick' => 'Son surnom',
110 'networking' => 'La liste de ses adresses de networking',
111 'edus' => 'Ses formations',
112 'addresses' => 'Ses adresses',
113 'section' => 'Sa section sportive',
114 'binets' => 'La liste de ses binets',
115 'medals' => 'Ses décorations',
116 'cv' => 'Son Curriculum Vitae',
117 'corps' => 'Son Corps d\'État',
118 'jobs' => 'Ses informations professionnelles',
119 'photo' => 'Sa photographie');
120 public function getData(PlUser &$user)
121 {
122 $data = XDB::fetchColumn("SELECT field
123 FROM watch_profile
124 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
125 ORDER BY ts",
126 $user->profile()->id(), $this->date);
127 if (count($data) == 0) {
128 return null;
129 } else {
130 $text = array();
131 foreach ($data as $f) {
132 $text[] = self::$descriptions[$f];
133 }
134 return $text;
135 }
136 }
137 }
138
139 class WatchRegistration extends WatchOperation
140 {
141 private static $order = null;
142
143 public $flag = 'registration';
144 public $title = 'Inscription$s';
145
146 protected function buildCondition(Watch $watch)
147 {
148 return new PFC_And(new UFC_Registered(false, '>', $watch->date()),
149 new PFC_Or($watch->contactCondition(),
150 $watch->promoCondition()));
151 }
152
153 public function getOrder()
154 {
155 if (!self::$order) {
156 self::$order = new UFO_Registration();
157 }
158 return self::$order;
159 }
160
161 public function getDate(PlUser &$user)
162 {
163 return $user->registration_date;
164 }
165 }
166
167 class WatchDeath extends WatchOperation
168 {
169 private static $order = null;
170
171 public $flag = 'death';
172 public $title = 'Décès';
173
174 protected function buildCondition(Watch $watch)
175 {
176 return new PFC_And(new UFC_Dead('>', $watch->date(), true),
177 new PFC_Or($watch->contactCondition(),
178 $watch->promoCondition()));
179 }
180
181 public function getOrder()
182 {
183 if (!self::$order) {
184 self::$order = new UFO_Death();
185 }
186 return self::$order;
187 }
188
189 public function getDate(PlUser &$user)
190 {
191 return $user->profile()->deathdate;
192 }
193
194 public function publicationDate(PlUser &$user)
195 {
196 return $user->profile()->deathdate_rec;
197 }
198
199 public function seen(PlUser &$user, $last)
200 {
201 return strtotime($user->profile()->deathdate_rec) > $last;
202 }
203 }
204
205 class WatchBirthday extends WatchOperation
206 {
207 const WATCH_LIMIT = 604800; // 1 week
208
209 private static $order = null;
210
211 public $flag = 'birthday';
212 public $title = 'Anniversaire$s';
213
214 protected function buildCondition(Watch $watch)
215 {
216 $select_date = new PFC_OR(new UFC_Birthday('=', time()),
217 new PFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
218 new UFC_Birthday('>', $watch->date() + self::WATCH_LIMIT)));
219 $profile = $watch->profile();
220 $cond = $watch->contactCondition();
221 if ($profile) {
222 $cond = new PFC_Or($cond,
223 new PFC_And($watch->promoCondition(),
224 new UFC_Promo('>=', $profile->mainGrade(), $profile->yearpromo() - 1),
225 new UFC_Promo('<=', $profile->mainGrade(), $profile->yearpromo() + 1)));
226 }
227 return new PFC_And($select_date, $cond);
228 }
229
230 public function getOrder()
231 {
232 if (!self::$order) {
233 self::$order = new UFO_Birthday();
234 }
235 return self::$order;
236 }
237
238 public function getDate(PlUser &$user)
239 {
240 return $user->profile()->next_birthday;
241 }
242
243 public function publicationDate(PlUser &$user)
244 {
245 return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
246 }
247
248 public function seen(PlUser &$user, $last)
249 {
250 $birthday = strtotime($user->profile()->next_birthday);
251 return $birthday > $last + self::WATCH_LIMIT
252 || date('Ymd', $birthday) == date('Ymd');
253 }
254 }
255
256 class Watch
257 {
258 private static $classes = array('WatchRegistration',
259 'WatchProfileUpdate',
260 'WatchDeath',
261 'WatchBirthday');
262 private static $events = array();
263
264 private $user = null;
265 private $date = null;
266 private $contactCond = null;
267 private $promoCond = null;
268
269 private $filters = array();
270
271 public function __construct(PlUser $user, $date = null)
272 {
273 $this->user = $user;
274 $this->date = self::getDate($user, $date);
275 }
276
277 public function user()
278 {
279 return $this->user;
280 }
281
282 public function profile()
283 {
284 return $this->user->profile();
285 }
286
287 public function date()
288 {
289 return $this->date;
290 }
291
292 public function contactCondition()
293 {
294 if (!$this->contactCond) {
295 $this->contactCond = new UFC_WatchContact($this->user);
296 }
297 return $this->contactCond;
298 }
299
300 public function promoCondition()
301 {
302 if (!$this->promoCond) {
303 $this->promoCond = new UFC_WatchPromo($this->user);
304 }
305 return $this->promoCond;
306 }
307
308 private function fetchEventWatch($class)
309 {
310 if (!isset(self::$events[$class])) {
311 self::$events[$class] = new $class();
312 }
313 return self::$events[$class];
314 }
315
316 private function fetchFilter($class)
317 {
318
319 if (!isset($this->filters[$class])) {
320 $event = $this->fetchEventWatch($class);
321 $this->filters[$class] = new UserFilter($event->getCondition($this),
322 array($event->getOrder(), new UFO_Name(Profile::DN_SORT)));
323 }
324 return $this->filters[$class];
325 }
326
327 public function count()
328 {
329 $count = 0;
330 foreach (self::$classes as $class) {
331 $uf = $this->fetchFilter($class);
332 $count += $uf->getTotalCount();
333 }
334 return $count;
335 }
336
337
338 private function fetchEvents($class)
339 {
340 $obj = $this->fetchEventWatch($class);
341 $uf = $this->fetchFilter($class);
342 $users = $uf->getUsers();
343 if (count($users) == 0) {
344 return null;
345 } else {
346 return array('type' => $obj->flag,
347 'operation' => $obj,
348 'title' => $obj->getTitle(count($users)),
349 'users' => $users);
350 }
351 }
352
353 public function events()
354 {
355 $events = array();
356 foreach (self::$classes as $class) {
357 $e = $this->fetchEvents($class);
358 if (!is_null($e)) {
359 $events[] = $e;
360 }
361 }
362 return $events;
363 }
364
365
366 private static function getDate(PlUser &$user, $date)
367 {
368 if (is_null($date)) {
369 $date = $user->watchLast();
370 $limit = time() - (7 * 86400);
371 if ($date < $limit) {
372 $date = $limit;
373 }
374 }
375 return $date;
376 }
377
378 public static function getCount(PlUser &$user, $date = null)
379 {
380 $watch = new Watch($user, $date);
381 return $watch->count();
382 }
383
384 public static function getEvents(PlUser &$user, $date = null)
385 {
386 $watch = new Watch($user, $date);
387 return $watch->events();
388 }
389 }
390
391 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
392 ?>