simplify a little the code
authorPierre Habouzit (MadCoder <pierre.habouzit@m4x.org>
Fri, 14 Jan 2005 13:19:22 +0000 (13:19 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 26 Jun 2008 21:27:33 +0000 (23:27 +0200)
git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-316

include/notifs.inc.php

index dffb1db..834226f 100644 (file)
@@ -23,12 +23,19 @@ define("WATCH_FICHE", 1);
 define("WATCH_INSCR", 2);
 define("WATCH_DEATH", 3);
 
-function inscription_notifs_base($uid) {
+// {{{ function inscription_notifs_base
+
+function inscription_notifs_base($uid)
+{
     global $globals;
     $globals->xdb->execute('REPLACE INTO  watch_sub (uid,cid) SELECT {?},id FROM watch_cat', $uid);
 }
 
-function register_watch_op($uid,$cid,$date='',$info='') {
+// }}}
+// {{{ function register_watch_op
+
+function register_watch_op($uid, $cid, $date='', $info='')
+{
     global $globals;
     $date = empty($date) ? 'NOW()' : "'$date'";
     $globals->xdb->execute('REPLACE INTO watch_ops (uid,cid,known,date,info) VALUES({?},{?},NOW(),{?},{?})',
@@ -44,89 +51,97 @@ function register_watch_op($uid,$cid,$date='',$info='') {
     }
 }
 
-function select_notifs_base($base_watch, $champ_w, $champ_u, $watcher, $watcher_detail, $is_contact, $not_frequent, $recent, $where_clause) {
-       // base_watch : la base dans laquelle on regarde
-       // is_contact  : test si la personne regardée est un contact
-       // not_frequent : watch seulement les evts nons fréquents
-       // recent : evenement recent
+// }}}
+// {{{ function select_notifs_base
+
+/** function that create the sql query used for notifications
+ * @param   $base_watch string      the table we want to look at
+ */
+function select_notifs_base($base_watch, $full, $recent, $where_clause)
+{
+    // recent : evenement recent
+    $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.frequent=0',
+                '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[$base_watch];
        
-       // les réponses sont-elles des contacts
-       if ($champ_w == 'contact')
-               $contact = '1';
-       elseif ($is_contact)
-               $contact = 'NOT (c.contact IS NULL)';
-       else
-               $contact = '0';
-
-       if ($watcher_detail) $watcher = true;
-       // traduction de la clause not_frequent
-       if ($not_frequent)
-               $not_frequent = " AND wc.frequent=0";
-               
-       // traduction de la clause recent
-       if ($recent)
-               $recent = " AND wo.known > q.watch_last";
-
-       $sql = "
-         ( SELECT u.promo,
-                  u.prenom,
-                  IF(u.epouse='',u.nom,u.epouse) AS nom,
-                  a.alias AS bestalias,
-                  wo.*,
-                  $contact AS contact,
-                  (u.perms IN('admin','user')) AS inscrit";
-       if ($watcher_detail) $sql.="
-                  w.uid AS aid,
-                  v.prenom AS aprenom,
-                  IF(v.epouse='',v.nom,v.prenom) AS anom,
-                  b.alias AS abestalias,
-                  (v.flags='femme') AS sexe"; 
-       $sql .= "
-             FROM ".$base_watch." AS w
-       INNER JOIN auth_user_md5   AS u  ON(u.$champ_u = w.$champ_w)";
-       if ($watcher) $sql .="
-       INNER JOIN auth_user_quick AS q  ON(q.user_id = w.uid)";
-       if ($watcher_detail) $sql .="
-       INNER JOIN auth_user_md5   AS v  ON(v.user_id = q.user_id)
-       INNER JOIN aliases         AS b  ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
-       if ($is_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".$recent.")
-       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".$not_frequent.")
-        LEFT JOIN aliases         AS a  ON(a.id = u.user_id AND FIND_IN_SET('bestalias', a.flags))
-            WHERE $where_clause )";
-        return $sql;
+        // traduction de la clause recent
+    if ($recent) { $recent = " AND wo.known > q.watch_last"; }
+
+    $sql = "
+        ( SELECT  u.promo, u.prenom, IF(u.epouse='',u.nom,u.epouse) AS nom,
+                  a.alias AS bestalias,
+                  wo.*,
+                  {$our['contact_sql']} AS contact,
+                  (u.perms IN('admin','user')) AS inscrit";
+    if ($full) {
+        $sql.=",
+                  w.uid AS aid, v.prenom AS aprenom, IF(v.epouse='',v.nom,v.prenom) AS anom,
+                  b.alias AS abestalias, (v.flags='femme') AS sexe"; 
+    }
+
+    $sql .= "
+            FROM  $base_watch     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)";
+    if ($full) {
+        $sql .="
+      INNER JOIN  auth_user_md5   AS v  ON(v.user_id = q.user_id)
+      INNER JOIN  aliases         AS b  ON(b.id = q.user_id AND FIND_IN_SET('bestalias', b.flags))";
+    }
+    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".$recent.")
+      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_clause )";
+    return $sql;
 }
 
 // récupère les evenements de $wuid, avec ou sans details, depuis $last, sous surveillance ou pas, ordonnés ou pas
 function select_notifs($wuid, $details, $last, $wflag, $order) {
-       $recent = ($last == 'watch_last');
-
-       // équivalent de FIND_IN_SET('contacts', q.watch_flags) mais en plus rapide
-       $contactflag = "(q.watch_flags=1 OR q.watch_flags=3)";
-
-       $where_clause = "";
-       if ($wuid != 'all')
-               $where_clause .= "w.uid = $wuid AND ";
-       if (!$recent)
-               $where_clause .= "wo.known > $last AND ";
-       if ($wflag)
-               $where_clause .= $contactflag." AND ";
-       $where_clause = substr($where_clause, 0, -5);
-
-       $sql = 
-       select_notifs_base('contacts','contact','user_id','q',$details,false,false,$recent, $where_clause.($wflag?'':" AND $contactflag"))."
-    UNION DISTINCT ".
-       select_notifs_base('watch_promo','promo','promo',false,$details,'is contact','not frequent',$recent, $where_clause)."
-    UNION DISTINCT ".
-       select_notifs_base("watch_nonins",'ni_id', 'user_id',false,$details,false,false,$recent, $where_clause);
-       
-       if ($order) $sql.="
-         ORDER BY cid, promo, nom";
-
-       return $sql;
+    $recent = ($last == 'watch_last');
+
+    // équivalent de FIND_IN_SET('contacts', q.watch_flags) mais en plus rapide
+    $contactflag = "(q.watch_flags=1 OR q.watch_flags=3)";
+
+    $where_clause = "";
+    if ($wuid != 'all')
+        $where_clause .= "w.uid = $wuid AND ";
+    if (!$recent)
+        $where_clause .= "wo.known > $last AND ";
+    if ($wflag)
+        $where_clause .= $contactflag." AND ";
+    $where_clause = substr($where_clause, 0, -5);
+
+    $sql = 
+        select_notifs_base('contacts',     $details, $recent, $where_clause.($wflag?'':" AND $contactflag"))."
+        UNION DISTINCT ".
+        select_notifs_base('watch_promo',  $details, $recent, $where_clause)."
+        UNION DISTINCT ".
+        select_notifs_base('watch_nonins', $details, $recent, $where_clause);
+
+    if ($order) $sql.="
+        ORDER BY cid, promo, nom";
+
+    return $sql;
 }
 
 function getNbNotifs() {
@@ -160,7 +175,7 @@ class AllNotifs {
         }
 
        // 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 = $globals->xdb->iterator(select_notifs('all', true, 'watch_last', 'watch_flag', 'order'));
+       $res = $globals->xdb->iterator(select_notifs('all', true, 'watch_last', 'watch_flag', true));
 
        while($tmp = $res->next()) {
            $aid = $tmp['aid'];
@@ -189,7 +204,7 @@ class Notifs {
        $lastweek = date('YmdHis',mktime() - 7*24*60*60);
 
        // recupere les notifs du watcher $uid, sans detail sur le watcher, depuis la semaine dernière, meme ceux sans surveillance, ordonnés
-       $res = $globals->xdb->iterator(select_notifs('{?}', false, $lastweek, false, 'order'), $uid, $uid, $uid);
+       $res = $globals->xdb->iterator(select_notifs('{?}', false, $lastweek, false, true), $uid, $uid, $uid);
        while($tmp = $res->next()) {
            $this->_data[$tmp['cid']][$tmp['promo']][] = $tmp;
        }