5e8d8aeda5d97c657ac425a033213f3d1be8a618
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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
24 public function getTitle($count = 0)
27 return str_replace(array('$x', '$s'), '', $this->title
);
29 return str_replace(array('$x', '$s'), array('x', 's'), $this->title
);
33 public function getCondition(PlUser
&$user, $date)
35 if (!$user->watch($this->flag
)) {
36 return new UFC_False();
38 return $this->buildCondition($user, $date);
42 abstract protected function buildCondition(PlUser
&$user, $date);
43 abstract public function getOrder();
44 abstract public function getDate(PlUser
&$user);
46 public function publicationDate(PlUser
&$user)
48 return $this->getDate($user);
51 public function seen(PlUser
&$user, $last)
53 return strtotime($this->getDate($user)) > $last;
56 public function getData(PlUser
&$user)
62 class WatchProfileUpdate
extends WatchOperation
64 public $flag = 'profile';
65 public $title = 'Mise$s à jour de fiche';
67 public static function register(Profile
&$profile, $field)
69 XDB
::execute('REPLACE INTO watch_profile (uid, ts, field)
70 VALUES ({?}, NOW(), {?})',
71 $profile->id(), $field);
74 protected function buildCondition(PlUser
&$user, $date)
76 return new UFC_And(new UFC_ProfileUpdated('>', $date),
77 new UFC_WatchContact($user));
80 public function getOrder()
82 return new UFO_ProfileUpdate();
85 public function getDate(PlUser
&$user)
87 return $user->profile()->last_change
;
91 class WatchRegistration
extends WatchOperation
93 public $flag = 'registration';
94 public $title = 'Inscription$s';
96 protected function buildCondition(PlUser
&$user, $date)
98 return new UFC_And(new UFC_Registered(false
, '>', $date),
99 new UFC_Or(new UFC_WatchContact($user),
100 new UFC_WatchPromo($user)));
103 public function getOrder()
105 return new UFO_Registration();
108 public function getDate(PlUser
&$user)
110 return $user->registration_date
;
114 class WatchDeath
extends WatchOperation
116 public $flag = 'death';
117 public $title = 'Décès';
119 protected function buildCondition(PlUser
&$user, $date)
121 return new UFC_And(new UFC_Dead('>', $date, true
),
122 new UFC_Or(new UFC_WatchPromo($user),
123 new UFC_WatchContact($user)));
126 public function getOrder()
128 return new UFO_Death();
131 public function getDate(PlUser
&$user)
133 return $user->profile()->deathdate
;
136 public function publicationDate(PlUser
&$user)
138 return $user->profile()->deathdate_rec
;
141 public function seen(PlUser
&$user, $last)
143 return strtotime($user->profile()->deathdate_rec
) > $last;
147 class WatchBirthday
extends WatchOperation
149 const WATCH_LIMIT
= 604800; // 1 week
151 public $flag = 'birthday';
152 public $title = 'Anniversaire$s';
154 protected function buildCondition(PlUser
&$user, $date)
156 return new UFC_And(new UFC_OR(new UFC_Birthday('=', time()),
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)));
163 public function getOrder()
165 return new UFO_Birthday();
168 public function getDate(PlUser
&$user)
170 return $user->profile()->next_birthday
;
173 public function publicationDate(PlUser
&$user)
175 return date('Y-m-d', strtotime($user->profile()->next_birthday
) - self
::WATCH_LIMIT
);
178 public function seen(PlUser
&$user, $last)
180 $birthday = strtotime($user->profile()->next_birthday
);
181 return $birthday > $last + self
::WATCH_LIMIT
182 ||
date('Ymd', $birthday) == date('Ymd');
188 private static $classes = array('WatchRegistration',
189 'WatchProfileUpdate',
193 private static function fetchCount(PlUser
&$user, $date, $class)
196 $uf = new UserFilter($obj->getCondition($user, $date));
197 return $uf->getTotalCount();
200 public static function getCount(PlUser
&$user, $date = null
)
203 if (is_null($date)) {
204 $date = $user->watchLast();
206 foreach (self
::$classes as $class) {
207 $count +
= self
::fetchCount($user, $date, $class);
213 private static function fetchEvents(PlUser
&$user, $date, $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) {
222 return array('operation' => $obj,
223 'title' => $obj->getTitle(count($users)),
228 public static function getEvents(PlUser
&$user, $date = null
)
230 if (is_null($date)) {
231 $date = $user->watchLast();
234 foreach (self
::$classes as $class) {
235 $e = self
::fetchEvents($user, $date, $class);
244 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: