typos
[platal.git] / include / notifs.inc.php
index 77dd994..b77bd34 100644 (file)
@@ -53,45 +53,6 @@ function register_watch_op($uid, $cid, $date='', $info='')
 }
 
 // }}}
-// {{{ function _select_notifs_count
-
-function _select_notifs_count($table)
-{
-    $cases = Array(
-        'contacts'     => Array('wfield' => 'contact',
-                                'ufield' => 'user_id',
-                                'freq_sql' => '',
-                                'where' => 'AND (q.watch_flags=1 OR q.watch_flags=3)'
-        ),
-        'watch_promo'  => Array('wfield' => 'promo',
-                                'ufield' => 'promo',
-                                'freq_sql' => ' AND ( wc.type = "basic" OR wc.type="near" 
-                                                     AND (u.promo <= v.promo_sortie-2 AND u.promo_sortie >= v.promo+2) )',
-                                'where' => '',
-        ),
-        'watch_nonins' => Array('wfield' => 'ni_id',
-                                'ufield' => 'user_id',
-                                'freq_sql' => '',
-                                'where' => '',
-        )
-    );
-    $our = $cases[$table];
-    $sql = "
-        (
-            SELECT  COUNT(1)        AS count
-              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)
-        INNER JOIN  watch_ops       AS wo ON(wo.uid = u.user_id AND wo.known > q.watch_last)
-        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']})
-             WHERE  w.uid = {?} {$our['where']}
-    )";
-    return $sql;
-}
-
-// }}}
 // {{{ function _select_notifs_base
 
 function _select_notifs_base($table, $mail, $where)
@@ -103,11 +64,11 @@ function _select_notifs_base($table, $mail, $where)
         ),
         '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)'
+            'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
         ),
-        'watch_nonins' => Array('wfield' => 'ni_id',   'ufield' => 'user_id', 'need_contact' => false,
+        'watch_nonins' => Array('wfield' => 'ni_id',   'ufield' => 'user_id', 'need_contact' => true,
             'freq_sql' => '',
-            'contact_sql' => '0'
+            'contact_sql' => 'IF(c.contact IS NULL, 0, 1)'
         )
     );
 
@@ -176,14 +137,14 @@ function getNbNotifs()
     if (!S::has('uid')) {
         return 0;
     }
-    $uid       = S::i('uid');
+    $uid       = S::v('uid', -1);
+    $watchlast = S::v('watch_last');
 
     // selectionne les notifs de uid, sans detail sur le watcher, depuis
     // $watchlast, meme ceux sans surveillance, non ordonnĂ©s
-    $res = XDB::query(_select_notifs_count('contacts') . ' UNION DISTINCT '
-                    . _select_notifs_count('watch_promo') . ' UNION DISTINCT '
-                    . _select_notifs_count('watch_nonins'), $uid, $uid, $uid);
-    $n = array_sum($res->fetchColumn());
+    $res = select_notifs(false, $uid, $watchlast, false);
+    $n   = $res->numRows();
+    $res->free();
     if ($n == 0) {
         return;
     }
@@ -276,8 +237,8 @@ class Watch
         $this->_nonins = new NoninsNotifs($uid);
         $this->_subs = new WatchSub($uid);
         $res = XDB::query("SELECT  FIND_IN_SET('contacts',watch_flags),FIND_IN_SET('mail',watch_flags)
-            FROM  auth_user_quick
-            WHERE  user_id={?}", $uid);
+                             FROM  auth_user_quick
+                            WHERE  user_id={?}", $uid);
         list($this->watch_contacts,$this->watch_mail) = $res->fetchOneRow();
 
         $res = XDB::iterator("SELECT * FROM watch_cat");
@@ -450,12 +411,12 @@ class NoninsNotifs
     public function del($p)
     {
         unset($this->_data["$p"]);
-        XDB::execute('DELETE FROM watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
+        XDB::execute('DELETE FROM  watch_nonins WHERE uid={?} AND ni_id={?}', $this->_uid, $p);
     }
 
     public function add($p)
     {
-        XDB::execute('INSERT INTO watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
+        XDB::execute('INSERT INTO  watch_nonins (uid,ni_id) VALUES({?},{?})', $this->_uid, $p);
         $res = XDB::query('SELECT  prenom,IF(nom_usage="",nom,nom_usage) AS nom,promo,user_id
                              FROM  auth_user_md5
                             WHERE  user_id={?}', $p);