Merge commit 'origin/fusionax' into account
[platal.git] / include / notifs.inc.php
index 45e8531..6601c1e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-define('WATCH_FICHE', 1);
-define('WATCH_INSCR', 2);
-define('WATCH_DEATH', 3);
-define('WATCH_BIRTH', 4);
-
-// {{{ function inscription_notifs_base
-
-function inscription_notifs_base($uid)
+abstract class WatchOperation
 {
-    global $globals;
-    $globals->xdb->execute('REPLACE INTO  watch_sub (uid,cid) SELECT {?},id FROM watch_cat', $uid);
-}
-
-// }}}
-// {{{ function register_watch_op
+    protected $date;
+
+    public function getTitle($count = 0)
+    {
+        if ($count == 1) {
+            return str_replace(array('$x', '$s'), '', $this->title);
+        } else {
+            return str_replace(array('$x', '$s'), array('x', 's'), $this->title);
+        }
+    }
 
-function register_watch_op($uid, $cid, $date='', $info='')
-{
-    global $globals;
-    if (empty($date)) {
-        $date = date('Y-m-d');
-    };
-    $globals->xdb->execute('REPLACE INTO watch_ops (uid,cid,known,date,info) VALUES({?},{?},NOW(),{?},{?})',
-            $uid, $cid, $date, $info);
-    if($cid == WATCH_FICHE) {
-       $globals->xdb->execute('UPDATE auth_user_md5 SET DATE=NOW() WHERE user_id={?}', $uid);
-    } elseif($cid == WATCH_INSCR) {
-       $globals->xdb->execute('REPLACE INTO  contacts (uid,contact)
-                                     SELECT  uid,ni_id
-                                       FROM  watch_nonins
-                                      WHERE  ni_id={?}', $uid);
-       $globals->xdb->execute('DELETE FROM watch_nonins WHERE ni_id={?}', $uid);
+    public function getCondition(PlUser &$user, $date)
+    {
+        $this->date = $date;
+        if (!$user->watch($this->flag)) {
+            return new UFC_False();
+        } else {
+            return $this->buildCondition($user);
+        }
     }
-}
 
-// }}}
-// {{{ function _select_notifs_base
+    abstract protected function buildCondition(PlUser &$user);
+    abstract public function getOrder();
+    abstract public function getDate(PlUser &$user);
 
-function _select_notifs_base($table, $mail, $where)
-{
-    $cases = Array(
-            'contacts'     => Array('wfield' => 'contact', 'ufield' => 'user_id', 'need_contact' => false,
-                'freq_sql' => '',
-                'contact_sql' => '1'
-            ),
-            'watch_promo'  => Array('wfield' => 'promo',   'ufield' => 'promo',   'need_contact' => true,
-                'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )',
-                'contact_sql' => 'NOT (c.contact IS NULL)'
-            ),
-            'watch_nonins' => Array('wfield' => 'ni_id',   'ufield' => 'user_id', 'need_contact' => false,
-                'freq_sql' => '',
-                'contact_sql' => '0'
-            )
-    );
-
-    $our   = $cases[$table];
-    $sql = "
-        (
-          SELECT  u.promo, u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom,
-                  u.deces != 0 AS dcd,
-                  a.alias AS bestalias,
-                  wo.*,
-                  {$our['contact_sql']} AS contact,
-                  (u.perms IN('admin','user')) AS inscrit";
-    if ($mail) {
-        $sql.=",
-                  w.uid AS aid, v.prenom AS aprenom, IF(v.nom_usage='',v.nom,v.nom_usage) AS anom,
-                  b.alias AS abestalias, (v.flags='femme') AS sexe, q.core_mail_fmt AS mail_fmt"; 
+    public function publicationDate(PlUser &$user)
+    {
+        return $this->getDate($user);
     }
 
-    $sql .= "
-            FROM  $table          AS w
-      INNER JOIN  auth_user_md5   AS u  ON(u.{$our['ufield']} = w.{$our['wfield']})
-      INNER JOIN  auth_user_quick AS q  ON(q.user_id = w.uid)
-      INNER JOIN  auth_user_md5   AS v  ON(v.user_id = q.user_id)";
-    if ($mail) {
-        $sql .="
-      INNER JOIN  aliases         AS b  ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
+    public function seen(PlUser &$user, $last)
+    {
+        return strtotime($this->getDate($user)) > $last;
     }
-    if ($our['need_contact']) {
-        $sql .="
-       LEFT JOIN  contacts        AS c  ON(c.uid = w.uid AND c.contact = u.user_id)";
-    }
-
-    $sql .="
-      INNER JOIN  watch_ops       AS wo ON(wo.uid = u.user_id AND ".($mail ? 'wo.known > q.watch_last' : '( wo.known > {?} OR wo.date=NOW() )').")
-      INNER JOIN  watch_sub       AS ws ON(ws.cid = wo.cid AND ws.uid = w.uid)
-      INNER JOIN  watch_cat       AS wc ON(wc.id = wo.cid{$our['freq_sql']})
-       LEFT JOIN  aliases         AS a  ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
-           WHERE  $where
-        )";
 
-    return $sql;
+    public function getData(PlUser &$user)
+    {
+        return null;
+    }
 }
 
-// }}}
-// {{{ function select_notifs
-
-function select_notifs($mail, $uid=null, $last=null, $iterator=true)
+class WatchProfileUpdate extends WatchOperation
 {
-    global $globals;
-    $where = $mail ? 'q.watch_flags=3' : 'w.uid = {?}';
-    $sql   = _select_notifs_base('contacts',     $mail, $where.($mail?'':' AND (q.watch_flags=1 OR q.watch_flags=3)')) . " UNION DISTINCT ";
-    $sql  .= _select_notifs_base('watch_promo',  $mail, $where) .  " UNION DISTINCT ";
-    $sql  .= _select_notifs_base('watch_nonins', $mail, $where);
-
-    if ($iterator) {
-        return $globals->xdb->iterator($sql . ' ORDER BY cid, promo, date DESC, nom', $last, $uid, $last, $uid, $last, $uid);
-    } else {
-        return $globals->xdb->query($sql, $last, $uid, $last, $uid, $last, $uid);
+    public $flag  = 'profile';
+    public $title = 'Mise$s à jour de fiche';
+
+    public static function register(Profile &$profile, $field)
+    {
+        XDB::execute('REPLACE INTO  watch_profile (uid, ts, field)
+                            VALUES  ({?}, NOW(), {?})',
+                     $profile->id(), $field);
     }
-}
 
-// }}}
-// {{{ function getNbNotifs
-
-function getNbNotifs() {
-    global $globals;
-    if (!Session::has('uid')) {
-        return 0;
+    protected function buildCondition(PlUser &$user)
+    {
+        return new UFC_And(new UFC_ProfileUpdated('>', $this->date),
+                           new UFC_WatchContact($user));
     }
-    $uid       = Session::getInt('uid', -1);
-    $watchlast = Session::get('watch_last');
-
-    // selectionne les notifs de uid, sans detail sur le watcher, depuis $watchlast, meme ceux sans surveillance, non ordonnés
-    $res = select_notifs(false, $uid, $watchlast, false);
-    $n   = $res->numRows();
-    $res->free();
-    $url = $globals->baseurl."/carnet/panel";
-    if($n==0) { return; }
-    if($n==1) { return "<a href='$url'>1 évènement !</a>"; }
-    return "<a href='$url'>$n évènements !</a>";
-}
 
-// }}}
-// {{{ class AllNotifs
-
-class AllNotifs {
-    var $_cats = Array();
-    var $_data = Array();
-
-    function AllNotifs() {
-       global $globals;
-       
-       $res = $globals->xdb->iterator("SELECT * FROM watch_cat");
-       while($tmp = $res->next()) {
-            $this->_cats[$tmp['id']] = $tmp;
-        }
+    public function getOrder()
+    {
+        return new UFO_ProfileUpdate();
+    }
 
-       // recupère tous les watchers, avec détails des watchers, a partir du watch_last de chacun, seulement ceux qui sont surveillés, ordonnés
-       $res = select_notifs(true);
+    public function getDate(PlUser &$user)
+    {
+        return $user->profile()->last_change;
+    }
 
-       while($tmp = $res->next()) {
-           $aid = $tmp['aid'];
-            if (empty($this->_data[$aid])) {
-                $this->_data[$aid] = Array("prenom" => $tmp['aprenom'], 'nom' => $tmp['anom'],
-                        'bestalias'=>$tmp['abestalias'], 'sexe' => $tmp['sexe'], 'mail_fmt' => $tmp['mail_fmt'],
-                        'dcd'=>$tmp['dcd']);
+    static private $descriptions = array('search_names' => 'L\'un de ses noms',
+                                         'freetext'     => 'Le texte libre',
+                                         'mobile'       => 'Son numéro de téléphone portable',
+                                         'nationalite'  => 'Sa nationalité',
+                                         'nationalite2' => 'Sa seconde nationalité',
+                                         'nationalite3' => 'Sa troisième nationalité',
+                                         'nick'         => 'Son surnom',
+                                         'networking'   => 'La liste de ses adresses de networking',
+                                         'edus'         => 'Ses formations',
+                                         'addresses'    => 'Ses adresses',
+                                         'section'      => 'Sa section sportive',
+                                         'binets'       => 'La liste de ses binets',
+                                         'medals'       => 'Ses décorations',
+                                         'cv'           => 'Son Curriculum Vitae',
+                                         'corps'        => 'Son Corps d\'État',
+                                         'jobs'         => 'Ses informations professionnelles',
+                                         'photo'        => 'Sa photographie');
+    public function getData(PlUser &$user)
+    {
+        $data = XDB::fetchColumn('SELECT  field
+                                    FROM  watch_profile
+                                   WHERE  uid = {?} AND ts > FROM_UNIXTIME({?}) AND field != \'\'
+                                ORDER BY  ts',
+                                 $user->id(), $this->date);
+        if (count($data) == 0) {
+            return null;
+        } else {
+            $text = array();
+            foreach ($data as $f) {
+                $text[] = self::$descriptions[$f];
             }
-           unset($tmp['aprenom'], $tmp['anom'], $tmp['abestalias'], $tmp['aid'], $tmp['sexe'], $tmp['mail_fmt'], $tmp['dcd']);
-           $this->_data[$aid]['data'][$tmp['cid']][] = $tmp;
-       }
+            return $text;
+        }
     }
 }
 
-// }}}
-// {{{ class Notifs
-
-class Notifs {
-    var $_uid;
-    var $_cats = Array();
-    var $_data = Array();
-    
-    function Notifs($uid, $up=false) {
-       global $globals;
-       $this->_uid = $uid;
-       
-       $res = $globals->xdb->iterator("SELECT * FROM watch_cat");
-       while($tmp = $res->next()) {
-            $this->_cats[$tmp['id']] = $tmp;
-        }
-
-       $lastweek = date('YmdHis',mktime() - 7*24*60*60);
+class WatchRegistration extends WatchOperation
+{
+    public $flag  = 'registration';
+    public $title = 'Inscription$s';
+
+    protected function buildCondition(PlUser &$user)
+    {
+        return new UFC_And(new UFC_Registered(false, '>', $this->date),
+                           new UFC_Or(new UFC_WatchContact($user),
+                                      new UFC_WatchPromo($user)));
+    }
 
-       // recupere les notifs du watcher $uid, sans detail sur le watcher, depuis la semaine dernière, meme ceux sans surveillance, ordonnés
-        $res = select_notifs(false, $uid, $lastweek);
-       while($tmp = $res->next()) {
-           $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
-       }
+    public function getOrder()
+    {
+        return new UFO_Registration();
+    }
 
-       if($up) {
-           $globals->xdb->execute('UPDATE auth_user_quick SET watch_last=NOW() WHERE user_id={?}', $uid);
-       }
+    public function getDate(PlUser &$user)
+    {
+        return $user->registration_date;
     }
 }
 
-// }}}
-// {{{ class Watch
-
-class Watch {
-    var $_uid;
-    var $_promos;
-    var $_nonins;
-    var $_cats = Array();
-    var $_subs;
-    var $watch_contacts;
-    var $watch_mail;
-    
-    function Watch($uid) {
-       global $globals;
-       $this->_uid = $uid;
-       $this->_promos = new PromoNotifs($uid);
-       $this->_nonins = new NoninsNotifs($uid);
-       $this->_subs = new WatchSub($uid);
-       $res = $globals->xdb->query("SELECT  FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
-                                      FROM  auth_user_quick
-                                     WHERE  user_id={?}", $uid);
-       list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
-       
-       $res = $globals->xdb->iterator("SELECT * FROM watch_cat");
-       while($tmp = $res->next()) {
-            $this->_cats[$tmp['id']] = $tmp;
-        }
+class WatchDeath extends WatchOperation
+{
+    public $flag  = 'death';
+    public $title = 'Décès';
+
+    protected function buildCondition(PlUser &$user)
+    {
+        return new UFC_And(new UFC_Dead('>', $this->date, true),
+                           new UFC_Or(new UFC_WatchPromo($user),
+                                      new UFC_WatchContact($user)));
     }
 
-    function saveFlags() {
-       global $globals;
-       $flags = "";
-       if($this->watch_contacts) $flags = "contacts";
-       if($this->watch_mail) $flags .= ($flags ? ',' : '')."mail";
-       $globals->xdb->execute('UPDATE auth_user_quick SET watch_flags={?} WHERE user_id={?}', $flags, $this->_uid);
-       
+    public function getOrder()
+    {
+        return new UFO_Death();
     }
 
-    function cats() {
-       return $this->_cats;
+    public function getDate(PlUser &$user)
+    {
+        return $user->profile()->deathdate;
     }
 
-    function subs($i) {
-       return $this->_subs->_data[$i];
+    public function publicationDate(PlUser &$user)
+    {
+        return $user->profile()->deathdate_rec;
     }
-    
-    function promos() {
-       return $this->_promos->toRanges();
-    }
-    
-    function nonins() {
-       return $this->_nonins->_data;
+
+    public function seen(PlUser &$user, $last)
+    {
+        return strtotime($user->profile()->deathdate_rec) > $last;
     }
 }
 
-// }}}
-// {{{ class WatchSub
-
-class WatchSub {
-    var $_uid;
-    var $_data = Array();
-
-    function WatchSub($uid) {
-       $this->_uid = $uid;
-       global $globals;
-       $res = $globals->xdb->iterRow('SELECT cid FROM watch_sub WHERE uid={?}', $uid);
-       while(list($c) = $res->next()) {
-            $this->_data[$c] = $c;
-        }
+class WatchBirthday extends WatchOperation
+{
+    const WATCH_LIMIT = 604800; // 1 week
+
+    public $flag  = 'birthday';
+    public $title = 'Anniversaire$s';
+
+    protected function buildCondition(PlUser &$user)
+    {
+        return new UFC_And(new UFC_OR(new UFC_Birthday('=', time()),
+                                      new UFC_And(new UFC_Birthday('<=', time() + self::WATCH_LIMIT),
+                                                  new UFC_Birthday('>', $this->date + self::WATCH_LIMIT))),
+                           new UFC_Or(new UFC_WatchPromo($user),
+                                      new UFC_WatchContact($user)));
     }
 
-    function update($ind) {
-       global $globals;
-       $this->_data = Array();
-       $globals->xdb->execute('DELETE FROM watch_sub WHERE uid={?}', $this->_uid);
-       foreach(Env::getMixed($ind) as $key=>$val) {
-           $globals->xdb->query('INSERT INTO watch_sub SELECT {?},id FROM watch_cat WHERE id={?}', $this->_uid, $key);
-           if(mysql_affected_rows()) {
-                $this->_data[$key] = $key;
-            }
-       }
+    public function getOrder()
+    {
+        return new UFO_Birthday();
     }
-}
-
-// }}}
-// {{{ class PromoNotifs
 
-class PromoNotifs {
-    var $_uid;
-    var $_data = Array();
-
-    function PromoNotifs($uid) {
-       $this->_uid = $uid;
-       global $globals;
-       $res = $globals->xdb->iterRow('SELECT promo FROM watch_promo WHERE uid={?} ORDER BY promo', $uid);
-       while (list($p) = $res->next()) {
-            $this->_data[intval($p)] = intval($p);
-        }
+    public function getDate(PlUser &$user)
+    {
+        return $user->profile()->next_birthday;
     }
 
-    function add($p) {
-       global $globals;
-       $promo = intval($p);
-       $globals->xdb->execute('REPLACE INTO watch_promo (uid,promo) VALUES({?},{?})', $this->_uid, $promo);
-       $this->_data[$promo] = $promo;
-       asort($this->_data);
-    }
-    
-    function del($p) {
-       global $globals;
-       $promo = intval($p);
-       $globals->xdb->execute('DELETE FROM watch_promo WHERE uid={?} AND promo={?}', $this->_uid, $promo);
-       unset($this->_data[$promo]);
-    }
-    
-    function addRange($_p1,$_p2) {
-       global $globals;
-       $p1 = intval($_p1);
-       $p2 = intval($_p2);
-       $values = Array();
-       for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
-           $values[] = "('{$this->_uid}',$i)";
-           $this->_data[$i] = $i;
-       }
-       $globals->xdb->execute('REPLACE INTO watch_promo (uid,promo) VALUES '.join(',',$values));
-       asort($this->_data);
+    public function publicationDate(PlUser &$user)
+    {
+        return date('Y-m-d', strtotime($user->profile()->next_birthday) - self::WATCH_LIMIT);
     }
 
-    function delRange($_p1,$_p2) {
-       global $globals;
-       $p1 = intval($_p1);
-       $p2 = intval($_p2);
-       $where = Array();
-       for($i = min($p1,$p2); $i<=max($p1,$p2); $i++) {
-           $where[] = "promo=$i";
-           unset($this->_data[$i]);
-       }
-       $globals->xdb->execute('DELETE FROM watch_promo WHERE uid={?} AND ('.join(' OR ',$where).')', $this->_uid);
+    public function seen(PlUser &$user, $last)
+    {
+        $birthday = strtotime($user->profile()->next_birthday);
+        return $birthday >  $last + self::WATCH_LIMIT
+            || date('Ymd', $birthday) == date('Ymd');
     }
+}
 
-    function toRanges() {
-       $ranges = Array();
-       $I = Array();
-       foreach($this->_data as $promo) {
-           if(!isset($I[0])) {
-               $I = Array($promo,$promo);
-           }
-           elseif($I[1]+1 == $promo) {
-               $I[1] ++;
-           }
-           else {
-               $ranges[] = $I;
-               $I = Array($promo,$promo);
-           }
-       }
-       if(isset($I[0])) $ranges[] = $I;
-       return $ranges;
+class Watch
+{
+    private static  $classes = array('WatchRegistration',
+                                     'WatchProfileUpdate',
+                                     'WatchDeath',
+                                     'WatchBirthday');
+
+    private static function fetchCount(PlUser &$user, $date, $class)
+    {
+        $obj = new $class();
+        $uf = new UserFilter($obj->getCondition($user, $date));
+        return $uf->getTotalCount();
     }
-}
 
-// }}}
-// {{{ class NoninsNotifs
-
-class NoninsNotifs {
-    var $_uid;
-    var $_data = Array();
-
-    function NoninsNotifs($uid) {
-       global $globals;
-       $this->_uid = $uid;
-       $res = $globals->xdb->iterator("SELECT  u.prenom,IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, u.promo, u.user_id
-                                          FROM  watch_nonins  AS w
-                                    INNER JOIN  auth_user_md5 AS u ON (u.user_id = w.ni_id)
-                                         WHERE  w.uid = {?}
-                                     ORDER BY  promo,nom", $uid);
-       while($tmp = $res->next()) {
-            $this->_data[$tmp['user_id']] = $tmp;
+    public static function getCount(PlUser &$user, $date = null)
+    {
+        $count = 0;
+        if (is_null($date)) {
+            $date = $user->watchLast();
         }
+        foreach (self::$classes as $class) {
+            $count += self::fetchCount($user, $date, $class);
+        }
+        return $count;
     }
 
-    function del($p) {
-       global $globals;
-       unset($this->_data["$p"]);
-       $globals->xdb->execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
+
+    private static function fetchEvents(PlUser &$user, $date, $class)
+    {
+        $obj = new $class();
+        $uf = new UserFilter($obj->getCondition($user, $date),
+                             array($obj->getOrder(), new UFO_Name(UserFilter::DN_SORT)));
+        $users = $uf->getUsers();
+        if (count($users) == 0) {
+            return null;
+        } else {
+            return array('operation' => $obj,
+                         'title'     => $obj->getTitle(count($users)),
+                         'users'     => $users);
+        }
     }
 
-    function add($p) {
-       global $globals;
-       $globals->xdb->execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
-       $res = $globals->xdb->query('SELECT  prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
-                                       FROM  auth_user_md5
-                                      WHERE  user_id={?}', $p);
-       $this->_data["$p"] = $res->fetchOneAssoc();
+    public static function getEvents(PlUser &$user, $date = null)
+    {
+        if (is_null($date)) {
+            $date = $user->watchLast();
+        }
+        $events = array();
+        foreach (self::$classes as $class) {
+            $e = self::fetchEvents($user, $date, $class);
+            if (!is_null($e)) {
+                $events[] = $e;
+            }
+        }
+        return $events;
     }
 }
-// }}}
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>