2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 abstract class WatchOperation
26 public function getTitle($count = 0)
29 return str_replace(array('$x', '$s'), '', $this->title
);
31 return str_replace(array('$x', '$s'), array('x', 's'), $this->title
);
35 public function getCondition(PlUser
&$user, $date)
38 if (!$user->watch($this->flag
)) {
39 return new PFC_False();
41 return $this->buildCondition($user);
45 abstract protected function buildCondition(PlUser
&$user);
46 abstract public function getOrder();
47 abstract public function getDate(PlUser
&$user);
49 public function publicationDate(PlUser
&$user)
51 return $this->getDate($user);
54 public function seen(PlUser
&$user, $last)
56 return strtotime($this->getDate($user)) > $last;
59 public function getData(PlUser
&$user)
65 class WatchProfileUpdate
extends WatchOperation
67 public $flag = 'profile';
68 public $title = 'Mise$s à jour de fiche';
70 public static function register(Profile
&$profile, $field)
72 XDB
::execute('REPLACE INTO watch_profile (uid, ts, field)
73 VALUES ({?}, NOW(), {?})',
74 $profile->id(), $field);
77 protected function buildCondition(PlUser
&$user)
79 return new PFC_And(new UFC_ProfileUpdated('>', $this->date
),
80 new UFC_WatchContact($user));
83 public function getOrder()
85 return new UFO_ProfileUpdate();
88 public function getDate(PlUser
&$user)
90 return $user->profile()->last_change
;
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)
112 $data = XDB
::fetchColumn('SELECT field
114 WHERE uid = {?} AND ts > FROM_UNIXTIME({?}) AND field != \'\'
116 $user->id(), $this->date
);
117 if (count($data) == 0) {
121 foreach ($data as $f) {
122 $text[] = self
::$descriptions[$f];
129 class WatchRegistration
extends WatchOperation
131 public $flag = 'registration';
132 public $title = 'Inscription$s';
134 protected function buildCondition(PlUser
&$user)
136 return new PFC_And(new UFC_Registered(false
, '>', $this->date
),
137 new PFC_Or(new UFC_WatchContact($user),
138 new UFC_WatchPromo($user)));
141 public function getOrder()
143 return new UFO_Registration();
146 public function getDate(PlUser
&$user)
148 return $user->registration_date
;
152 class WatchDeath
extends WatchOperation
154 public $flag = 'death';
155 public $title = 'Décès';
157 protected function buildCondition(PlUser
&$user)
159 return new PFC_And(new UFC_Dead('>', $this->date
, true
),
160 new PFC_Or(new UFC_WatchPromo($user),
161 new UFC_WatchContact($user)));
164 public function getOrder()
166 return new UFO_Death();
169 public function getDate(PlUser
&$user)
171 return $user->profile()->deathdate
;
174 public function publicationDate(PlUser
&$user)
176 return $user->profile()->deathdate_rec
;
179 public function seen(PlUser
&$user, $last)
181 return strtotime($user->profile()->deathdate_rec
) > $last;
185 class WatchBirthday
extends WatchOperation
187 const WATCH_LIMIT
= 604800; // 1 week
189 public $flag = 'birthday';
190 public $title = 'Anniversaire$s';
192 protected function buildCondition(PlUser
&$user)
194 return new PFC_And(new PFC_OR(new UFC_Birthday('=', time()),
195 new PFC_And(new UFC_Birthday('<=', time() + self
::WATCH_LIMIT
),
196 new UFC_Birthday('>', $this->date + self
::WATCH_LIMIT
))),
197 new PFC_Or(new UFC_WatchPromo($user),
198 new UFC_WatchContact($user)));
201 public function getOrder()
203 return new UFO_Birthday();
206 public function getDate(PlUser
&$user)
208 return $user->profile()->next_birthday
;
211 public function publicationDate(PlUser
&$user)
213 return date('Y-m-d', strtotime($user->profile()->next_birthday
) - self
::WATCH_LIMIT
);
216 public function seen(PlUser
&$user, $last)
218 $birthday = strtotime($user->profile()->next_birthday
);
219 return $birthday > $last + self
::WATCH_LIMIT
220 ||
date('Ymd', $birthday) == date('Ymd');
226 private static $classes = array('WatchRegistration',
227 'WatchProfileUpdate',
231 private static function fetchCount(PlUser
&$user, $date, $class)
234 $uf = new UserFilter($obj->getCondition($user, $date));
235 return $uf->getTotalCount();
238 public static function getCount(PlUser
&$user, $date = null
)
241 if (is_null($date)) {
242 $date = $user->watchLast();
244 foreach (self
::$classes as $class) {
245 $count +
= self
::fetchCount($user, $date, $class);
251 private static function fetchEvents(PlUser
&$user, $date, $class)
254 $uf = new UserFilter($obj->getCondition($user, $date),
255 array($obj->getOrder(), new UFO_Name(Profile
::DN_SORT
)));
256 $users = $uf->getUsers();
257 if (count($users) == 0) {
260 return array('operation' => $obj,
261 'title' => $obj->getTitle(count($users)),
266 public static function getEvents(PlUser
&$user, $date = null
)
268 if (is_null($date)) {
269 $date = $user->watchLast();
272 foreach (self
::$classes as $class) {
273 $e = self
::fetchEvents($user, $date, $class);
282 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: