Fix deco javascript: current grade was not properly selected
[platal.git] / modules / axletter / axletter.inc.php
index 178a0d3..386eb26 100644 (file)
@@ -33,7 +33,7 @@ class AXLetter extends MassMailer
 
     function __construct($id)
     {
-        parent::__construct('axletter/letter.tpl', 'ax.css', 'ax/show');
+        parent::__construct('axletter/letter.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins');
         $this->_head = '<cher> <prenom>,';
 
         if (!is_array($id)) {
@@ -56,6 +56,9 @@ class AXLetter extends MassMailer
         list($this->_id, $this->_shortname, $this->_title_mail, $this->_title,
              $this->_body, $this->_signature, $this->_promo_min, $this->_promo_max,
              $this->_echeance, $this->_date, $this->_bits) = $id;
+        if ($this->_date == '0000-00-00') {
+            $this->_date = 0;
+        }
     }
 
     protected function assignData(&$smarty)
@@ -73,38 +76,6 @@ class AXLetter extends MassMailer
         return format_text($this->_signature, $format, 10);
     }
 
-    static public function create($subject, $title, $body, $signature, $promo_min, $promo_max, $date, $shortname = null)
-    {
-        $id = AXLetter::awaiting();
-        if ($id) {
-            return new AXLetter($id);
-        }
-        XDB::execute("INSERT INTO  axletter (shortname, echeance,  promo_min, promo_max,
-                                     subject, title, body, signature,
-                                     subject_ini, title_ini, body_ini, signature_ini)
-                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
-                     $shortname, $date, $promo_min, $promo_max,
-                     $subject, $title, $body, $signature, $subject, $title, $body, $signature);
-        return new AXLetter(XDB::insertId());
-    }
-
-    public function update($subject, $title, $body, $signature, $promo_min, $promo_max, $date, $shortname = null)
-    {
-        $this->_shortname  = $shortname;
-        $this->_title      = $title;
-        $this->_title_mail = $subject;
-        $this->_body       = $body;
-        $this->_signature  = $signature;
-        $this->_promo_min  = $promo_min;
-        $this->_promo_max  = $promo_max;
-        $this->_date       = $date;
-        return XDB::execute("UPDATE  axletter (shortname, subject, title, body, signature, promo_min, promo_max, echeance)
-                                SET  shorname={?}, subject={?}, title={?}, body={?}, signature={?},
-                                     promo_min={?}, promo_max={?}, echeance={?}
-                              WHERE  id = {?}",
-                            $shortname, $subject, $title, $body, $signature, $promo_min, $promo_max, $date, $this->_id);
-    }
-
     public function valid()
     {
         return XDB::execute("UPDATE  axletter
@@ -126,6 +97,24 @@ class AXLetter extends MassMailer
                        WHERE  id={?}", $this->_id);
     }
 
+    protected function getAllRecipients()
+    {
+        return "SELECT  ni.user_id, IF(ni.user_id = 0, ni.email, a.alias) AS alias,
+                        IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom,
+                        IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom,
+                        FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe,
+                        IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref,
+                        IF(ni.user_id = 0, ni.hash, 0) AS hash
+                  FROM  axletter_ins  AS ni
+             LEFT JOIN  auth_user_md5   AS u  USING(user_id)
+             LEFT JOIN  auth_user_quick AS q  ON(q.user_id = u.user_id)
+             LEFT JOIN  aliases         AS a  ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
+             LEFT JOIN  emails          AS e  ON(e.uid=u.user_id AND e.flags='active')
+                 WHERE  ni.last < {?} AND {$this->subscriptionWhere()}
+                        AND (e.email IS NOT NULL OR ni.user_id = 0)
+              GROUP BY  u.user_id";
+    }
+
     static public function subscriptionState($uid = null)
     {
         $user = is_null($uid) ? S::v('uid') : $uid;
@@ -133,13 +122,24 @@ class AXLetter extends MassMailer
                              FROM  axletter_ins
                             WHERE  user_id={?}", $user);
         return $res->fetchOneCell();
-    }   
-    
-    static public function unsubscribe($uid = null)
+    }
+
+    static public function unsubscribe($uid = null, $hash = false)
     {
         $user = is_null($uid) ? S::v('uid') : $uid;
+        $field = !$hash ? 'user_id' : 'hash';
+        if (is_null($uid) && $hash) {
+            return false;
+        }
+        $res = XDB::query("SELECT *
+                             FROM axletter_ins
+                            WHERE $field={?}", $user);
+        if (!$res->numRows()) {
+            return false;
+        }
         XDB::execute("DELETE FROM  axletter_ins
-                            WHERE  user_id={?} OR hash = {?}", $user, $user);
+                            WHERE  $field = {?}", $user);
+        return true;
     }
 
     static public function subscribe($uid = null)
@@ -160,35 +160,78 @@ class AXLetter extends MassMailer
         return $res->fetchOneCell();
     }
 
-    protected function subscriptionTable()
+    static public function grantPerms($uid)
+    {
+        if (!is_numeric($uid)) {
+            $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
+            $uid = $res->fetchOneCell();
+        }
+        if (!$uid) {
+            return false;
+        }
+        return XDB::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid);
+    }
+
+    static public function revokePerms($uid)
     {
-        return 'axletter_ins';
+        if (!is_numeric($uid)) {
+            $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
+            $uid = $res->fetchOneCell();
+        }
+        if (!$uid) {
+            return false;
+        }
+        return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid);
     }
 
     protected function subscriptionWhere()
     {
-        return 'ni.last';
+        if (!$this->_promo_min && !$this->_promo_max) {
+            return '1';
+        }
+        $where = array();
+        if ($this->_promo_min) {
+            $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))";
+        }
+        if ($this->_promo_max) {
+            $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))";
+        }
+        return implode(' AND ', $where);
     }
 
     static public function awaiting()
     {
-        $res = XDB::query("SELECT  id
+        $res = XDB::query("SELECT  *
                              FROM  axletter
                             WHERE  FIND_IN_SET('new', bits)");
-        return $res->fetchOneCell();
+        if ($res->numRows()) {
+            return new AXLetter($res->fetchOneRow());
+        }
+        return null;
+    }
+
+    static public function toSend()
+    {
+        $res = XDB::query("SELECT  *
+                             FROM  axletter
+                            WHERE  FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
+        if ($res->numRows()) {
+            return new AXLetter($res->fetchOneRow());
+        }
+        return null;
     }
 
     static public function listSent()
-    {   
+    {
         $res = XDB::query("SELECT  IF(shortname IS NULL, id, shortname) as id, date, subject AS titre
                              FROM  axletter
                             WHERE  NOT (FIND_IN_SET('new', bits))
                          ORDER BY  date DESC");
         return $res->fetchAllAssoc();
     }
-    
+
     static public function listAll()
-    {   
+    {
         $res = XDB::query("SELECT  IF(shortname IS NULL, id, shortname) as id, date, subject AS titre
                              FROM  axletter
                          ORDER BY  date DESC");
@@ -196,5 +239,5 @@ class AXLetter extends MassMailer
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4:
+// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
 ?>