Small lifting for the email configuration portal.
[platal.git] / include / notifs.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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{
ee682285
FB
24 protected $date;
25
009b8ab7
FB
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(PlUser &$user, $date)
36 {
ee682285 37 $this->date = $date;
a87530ea 38 if (!$user->watchType($this->flag)) {
06598c13 39 return new PFC_False();
009b8ab7 40 } else {
ee682285 41 return $this->buildCondition($user);
009b8ab7
FB
42 }
43 }
44
ee682285 45 abstract protected function buildCondition(PlUser &$user);
009b8ab7
FB
46 abstract public function getOrder();
47 abstract public function getDate(PlUser &$user);
48
c350577b
FB
49 public function publicationDate(PlUser &$user)
50 {
51 return $this->getDate($user);
52 }
53
009b8ab7
FB
54 public function seen(PlUser &$user, $last)
55 {
56 return strtotime($this->getDate($user)) > $last;
57 }
c350577b
FB
58
59 public function getData(PlUser &$user)
60 {
61 return null;
62 }
009b8ab7
FB
63}
64
65class WatchProfileUpdate extends WatchOperation
66{
67 public $flag = 'profile';
68 public $title = 'Mise$s à jour de fiche';
a3a049fc 69
4e7bf1e0 70 public static function register(Profile &$profile, $field)
612a2d8a 71 {
bf0e2a48 72 XDB::execute('REPLACE INTO watch_profile (pid, ts, field)
4e7bf1e0
FB
73 VALUES ({?}, NOW(), {?})',
74 $profile->id(), $field);
0337d704 75 }
76
ee682285 77 protected function buildCondition(PlUser &$user)
612a2d8a 78 {
9b80d073 79 return new PFC_And(new UFC_ProfileUpdated('>', $this->date),
009b8ab7
FB
80 new UFC_WatchContact($user));
81 }
82
83 public function getOrder()
84 {
85 return new UFO_ProfileUpdate();
86 }
87
88 public function getDate(PlUser &$user)
89 {
90 return $user->profile()->last_change;
0337d704 91 }
ee682285
FB
92
93 static private $descriptions = array('search_names' => 'L\'un de ses noms',
94 'freetext' => 'Le texte libre',
95 'mobile' => 'Son numéro de téléphone portable',
96 'nationalite' => 'Sa nationalité',
97 'nationalite2' => 'Sa seconde nationalité',
98 'nationalite3' => 'Sa troisième nationalité',
99 'nick' => 'Son surnom',
100 'networking' => 'La liste de ses adresses de networking',
101 'edus' => 'Ses formations',
102 'addresses' => 'Ses adresses',
103 'section' => 'Sa section sportive',
104 'binets' => 'La liste de ses binets',
105 'medals' => 'Ses décorations',
106 'cv' => 'Son Curriculum Vitae',
107 'corps' => 'Son Corps d\'État',
108 'jobs' => 'Ses informations professionnelles',
109 'photo' => 'Sa photographie');
110 public function getData(PlUser &$user)
111 {
bf0e2a48 112 $data = XDB::fetchColumn("SELECT field
ee682285 113 FROM watch_profile
bf0e2a48
SJ
114 WHERE pid = {?} AND ts > FROM_UNIXTIME({?}) AND field != ''
115 ORDER BY ts",
116 $user->profile()->id(), $this->date);
ee682285
FB
117 if (count($data) == 0) {
118 return null;
119 } else {
120 $text = array();
121 foreach ($data as $f) {
122 $text[] = self::$descriptions[$f];
123 }
124 return $text;
125 }
126 }
0337d704 127}
128
009b8ab7 129class WatchRegistration extends WatchOperation
612a2d8a 130{
009b8ab7
FB
131 public $flag = 'registration';
132 public $title = 'Inscription$s';
133
ee682285 134 protected function buildCondition(PlUser &$user)
009b8ab7 135 {
06598c13
RB
136 return new PFC_And(new UFC_Registered(false, '>', $this->date),
137 new PFC_Or(new UFC_WatchContact($user),
009b8ab7
FB
138 new UFC_WatchPromo($user)));
139 }
140
141 public function getOrder()
142 {
143 return new UFO_Registration();
144 }
0337d704 145
009b8ab7 146 public function getDate(PlUser &$user)
612a2d8a 147 {
009b8ab7 148 return $user->registration_date;
0337d704 149 }
150}
151
009b8ab7 152class WatchDeath extends WatchOperation
612a2d8a 153{
009b8ab7
FB
154 public $flag = 'death';
155 public $title = 'Décès';
156
ee682285 157 protected function buildCondition(PlUser &$user)
009b8ab7 158 {
06598c13
RB
159 return new PFC_And(new UFC_Dead('>', $this->date, true),
160 new PFC_Or(new UFC_WatchPromo($user),
009b8ab7
FB
161 new UFC_WatchContact($user)));
162 }
0337d704 163
009b8ab7 164 public function getOrder()
612a2d8a 165 {
009b8ab7
FB
166 return new UFO_Death();
167 }
168
169 public function getDate(PlUser &$user)
170 {
171 return $user->profile()->deathdate;
172 }
173
c350577b
FB
174 public function publicationDate(PlUser &$user)
175 {
176 return $user->profile()->deathdate_rec;
177 }
178
009b8ab7
FB
179 public function seen(PlUser &$user, $last)
180 {
181 return strtotime($user->profile()->deathdate_rec) > $last;
0337d704 182 }
183}
184
009b8ab7 185class WatchBirthday extends WatchOperation
612a2d8a 186{
009b8ab7 187 const WATCH_LIMIT = 604800; // 1 week
0337d704 188
009b8ab7
FB
189 public $flag = 'birthday';
190 public $title = 'Anniversaire$s';
191
ee682285 192 protected function buildCondition(PlUser &$user)
612a2d8a 193 {
06598c13
RB
194 return new PFC_And(new PFC_OR(new UFC_Birthday('=', time()),
195 new PFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
ee682285 196 new UFC_Birthday('>', $this->date + self::WATCH_LIMIT))),
06598c13 197 new PFC_Or(new UFC_WatchPromo($user),
009b8ab7
FB
198 new UFC_WatchContact($user)));
199 }
200
201 public function getOrder()
202 {
203 return new UFO_Birthday();
204 }
205
206 public function getDate(PlUser &$user)
207 {
208 return $user->profile()->next_birthday;
209 }
210
c350577b
FB
211 public function publicationDate(PlUser &$user)
212 {
213 return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
214 }
215
009b8ab7
FB
216 public function seen(PlUser &$user, $last)
217 {
218 $birthday = strtotime($user->profile()->next_birthday);
219 return $birthday > $last + self::WATCH_LIMIT
220 || date('Ymd', $birthday) == date('Ymd');
0337d704 221 }
222}
a3a049fc 223
7e735012
FB
224class Watch
225{
226 private static $classes = array('WatchRegistration',
227 'WatchProfileUpdate',
228 'WatchDeath',
229 'WatchBirthday');
230
009b8ab7 231 private static function fetchCount(PlUser &$user, $date, $class)
7e735012
FB
232 {
233 $obj = new $class();
009b8ab7 234 $uf = new UserFilter($obj->getCondition($user, $date));
7e735012
FB
235 return $uf->getTotalCount();
236 }
237
009b8ab7 238 public static function getCount(PlUser &$user, $date = null)
7e735012
FB
239 {
240 $count = 0;
009b8ab7
FB
241 if (is_null($date)) {
242 $date = $user->watchLast();
243 }
7e735012 244 foreach (self::$classes as $class) {
009b8ab7 245 $count += self::fetchCount($user, $date, $class);
7e735012
FB
246 }
247 return $count;
248 }
009b8ab7
FB
249
250
251 private static function fetchEvents(PlUser &$user, $date, $class)
252 {
253 $obj = new $class();
254 $uf = new UserFilter($obj->getCondition($user, $date),
5b3680d5 255 array($obj->getOrder(), new UFO_Name(Profile::DN_SORT)));
009b8ab7
FB
256 $users = $uf->getUsers();
257 if (count($users) == 0) {
258 return null;
259 } else {
260 return array('operation' => $obj,
261 'title' => $obj->getTitle(count($users)),
262 'users' => $users);
263 }
264 }
265
266 public static function getEvents(PlUser &$user, $date = null)
267 {
268 if (is_null($date)) {
269 $date = $user->watchLast();
270 }
271 $events = array();
272 foreach (self::$classes as $class) {
273 $e = self::fetchEvents($user, $date, $class);
274 if (!is_null($e)) {
275 $events[] = $e;
276 }
277 }
278 return $events;
279 }
7e735012
FB
280}
281
282// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 283?>