+ const WATCH_LIMIT = 604800; // 1 week
+
+ private static $order = null;
+
+ public $flag = 'birthday';
+ public $title = 'Anniversaire$s';
+
+ protected function buildCondition(Watch $watch)
+ {
+ $select_date = new PFC_OR(new UFC_Birthday('=', time()),
+ new PFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
+ new UFC_Birthday('>', $watch->date() + self::WATCH_LIMIT)));
+ $profile = $watch->profile();
+ $cond = $watch->contactCondition();
+ if ($profile) {
+ $cond = new PFC_Or($cond,
+ new PFC_And($watch->promoCondition(),
+ new UFC_Promo('>=', $profile->mainGrade(), $profile->yearpromo() - 1),
+ new UFC_Promo('<=', $profile->mainGrade(), $profile->yearpromo() + 1)));
+ }
+ return new PFC_And($select_date, $cond);
+ }
+
+ public function getOrder()
+ {
+ if (!self::$order) {
+ self::$order = new UFO_Birthday();
+ }
+ return self::$order;
+ }
+
+ public function getDate(PlUser &$user)
+ {
+ return $user->profile()->next_birthday;
+ }
+
+ public function publicationDate(PlUser &$user)
+ {
+ return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
+ }
+
+ public function seen(PlUser &$user, $last)
+ {
+ $birthday = strtotime($user->profile()->next_birthday);
+ return $birthday > $last + self::WATCH_LIMIT
+ || date('Ymd', $birthday) == date('Ymd');
+ }
+}
+
+class Watch
+{
+ private static $classes = array('WatchRegistration',
+ 'WatchProfileUpdate',
+ 'WatchDeath',
+ 'WatchBirthday');
+ private static $events = array();
+
+ private $user = null;
+ private $date = null;
+ private $contactCond = null;
+ private $promoCond = null;
+
+ private $filters = array();
+
+ public function __construct(PlUser $user, $date = null)
+ {
+ $this->user = $user;
+ $this->date = self::getDate($user, $date);
+ }
+
+ public function user()
+ {
+ return $this->user;
+ }
+
+ public function profile()
+ {
+ return $this->user->profile();
+ }
+
+ public function date()
+ {
+ return $this->date;
+ }
+
+ public function contactCondition()
+ {
+ if (!$this->contactCond) {
+ $this->contactCond = new UFC_WatchContact($this->user);
+ }
+ return $this->contactCond;
+ }
+
+ public function promoCondition()
+ {
+ if (!$this->promoCond) {
+ $this->promoCond = new UFC_WatchPromo($this->user);
+ }
+ return $this->promoCond;
+ }
+
+ private function fetchEventWatch($class)
+ {
+ if (!isset(self::$events[$class])) {
+ self::$events[$class] = new $class();
+ }
+ return self::$events[$class];
+ }
+
+ private function fetchFilter($class)
+ {
+
+ if (!isset($this->filters[$class])) {
+ $event = $this->fetchEventWatch($class);
+ $this->filters[$class] = new UserFilter($event->getCondition($this),
+ array($event->getOrder(), new UFO_Name(Profile::DN_SORT)));
+ }
+ return $this->filters[$class];
+ }
+
+ public function count()
+ {
+ $count = 0;
+ foreach (self::$classes as $class) {
+ $uf = $this->fetchFilter($class);
+ $count += $uf->getTotalCount();
+ }
+ return $count;
+ }
+
+
+ private function fetchEvents($class)
+ {
+ $obj = $this->fetchEventWatch($class);
+ $uf = $this->fetchFilter($class);
+ $users = $uf->getUsers();
+ if (count($users) == 0) {
+ return null;
+ } else {
+ return array('type' => $obj->flag,
+ 'operation' => $obj,
+ 'title' => $obj->getTitle(count($users)),
+ 'users' => $users);
+ }
+ }
+
+ public function events()
+ {
+ $events = array();
+ foreach (self::$classes as $class) {
+ $e = $this->fetchEvents($class);
+ if (!is_null($e)) {
+ $events[] = $e;
+ }
+ }
+ return $events;
+ }
+
+
+ private static function getDate(PlUser &$user, $date)
+ {
+ if (is_null($date)) {
+ $date = $user->watchLast();
+ $limit = time() - (7 * 86400);
+ if ($date < $limit) {
+ $date = $limit;
+ }
+ }
+ return $date;
+ }
+
+ public static function getCount(PlUser &$user, $date = null)
+ {
+ $watch = new Watch($user, $date);
+ return $watch->count();
+ }