Bye xorg.misc.inc.php
[platal.git] / include / newsletter.inc.php
index 112686d..9e1a59b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 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                *
  ***************************************************************************/
 
-// {{{ requires + defines
+require_once("massmailer.inc.php");
 
-require_once("xorg.misc.inc.php");
-
-if (isset($page)) {
-    $page->addCssLink('nl.css');
-}
-
-define('FEMME', 1);
-define('HOMME', 0);
-
-// }}}
 // {{{ class NewsLetter
 
-class NewsLetter
+class NewsLetter extends MassMailer
 {
-    // {{{ properties
-    
-    var $_id;
-    var $_shortname;
-    var $_date;
-    var $_title;
-    var $_title_mail;
-    var $_head;
-    var $_cats = Array();
-    var $_arts = Array();
+    public $_date;
+    public $_cats = Array();
+    public $_arts = Array();
 
-    // }}}
-    // {{{ constructor
-    
-    function NewsLetter($id=null)
+    function __construct($id = null)
     {
+        parent::__construct('newsletter/nl.mail.tpl', 'nl.css', 'nl/show', 'newsletter', 'newsletter_ins');
         if (isset($id)) {
             if ($id == 'last') {
                 $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
@@ -60,7 +41,7 @@ class NewsLetter
         } else {
             $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
             if (!$res->numRows()) {
-                insert_new_nl();
+                Newsletter::create();
             }
             $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
         }
@@ -77,7 +58,7 @@ class NewsLetter
         while (list($cid, $title) = $res->next()) {
             $this->_cats[$cid] = $title;
         }
-    
+
         $res = XDB::iterRow(
                 "SELECT  a.title,a.body,a.append,a.aid,a.cid,a.pos
                    FROM  newsletter_art AS a
@@ -90,75 +71,13 @@ class NewsLetter
         }
     }
 
-    // }}}
-    // {{{ function setSent()
-
-    function setSent()
-    {
-        XDB::execute("UPDATE  newsletter SET bits='sent' WHERE id={?}", $this->_id);
-    }
-
-    // }}}
-    // {{{ function save()
-
-    function save()
+    public function save()
     {
         XDB::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
                      $this->_date, $this->_title, $this->_title_mail, $this->_head, $this->_shortname,$this->_id);
     }
 
-    // }}}
-    // {{{ function id()
-
-    function id()
-    {
-        return is_null($this->_shortname) ? $this->_id : $this->_shortname;
-    }
-
-    // }}}
-    // {{{ function title()
-
-    function title($mail = false) {
-        if ($mail) {
-            return $this->_title_mail;
-        }
-        return $this->_title;
-    }
-
-    // }}}
-    // {{{ function head()
-    
-    function head($prenom = null, $nom = null, $sexe = null, $type = 'text')
-    {
-        if (is_null($prenom)) {
-            return $this->_head;
-        } else {
-            $head = $this->_head;
-            $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
-            $head = str_replace('<prenom>', $prenom, $head);
-            $head = str_replace('<nom>',    $nom,    $head);
-            if ($type == 'text') {
-                $head = enriched_to_text($head,false,true,2,64);
-            } else {
-                $head = enriched_to_text($head, true);
-            }
-            return $head;
-        }
-    }
-
-    // }}}
-    // {{{ funciton getCss()
-
-    function getCss()
-    {
-        $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/nl.css');
-        return preg_replace('@/\*.*?\*/@s', '', $css);
-    }
-
-    // }}}
-    // {{{ function getArt()
-    
-    function getArt($aid)
+    public function getArt($aid)
     {
         foreach ($this->_arts as $key=>$artlist) {
             if (isset($artlist["a$aid"])) {
@@ -168,10 +87,7 @@ class NewsLetter
         return null;
     }
 
-    // }}}
-    // {{{ function saveArticle()
-
-    function saveArticle(&$a)
+    public function saveArticle(&$a)
     {
         if ($a->_aid>=0) {
             XDB::execute('REPLACE INTO  newsletter_art (id,aid,cid,pos,title,body,append)
@@ -189,10 +105,7 @@ class NewsLetter
         }
     }
 
-    // }}}
-    // {{{ function delArticle()
-    
-    function delArticle($aid)
+    public function delArticle($aid)
     {
         XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
         foreach ($this->_arts as $key=>$art) {
@@ -200,52 +113,66 @@ class NewsLetter
         }
     }
 
-    // }}}
-    // {{{ function toText()
+    protected function assignData(&$smarty)
+    {
+        $smarty->assign_by_ref('nl', $this);
+    }
 
-    function toText(&$page, $prenom,$nom,$sexe)
+    protected function setSent()
     {
-        $page->assign('is_mail', false);
-        $page->assign('html_version', false);
-        $page->assign('prenom', $prenom);
-        $page->assign('nom', $nom);
-        $page->assign('sexe', $sexe);
-        $page->assign_by_ref('nl', $this);
+        XDB::execute("UPDATE newsletter  SET bits='sent' WHERE id={?}", $this->_id);
     }
 
-    // }}}
-    // {{{ function toHtml()
-    
-    function toHtml(&$page, $prenom, $nom, $sexe)
+    static public function subscriptionState($uid = null)
     {
-        $page->assign('prefix', 'nl/show/' . $this->id());
-        $page->assign('is_mail', false);
-        $page->assign('html_version', true);
-        $page->assign('prenom', $prenom);
-        $page->assign('nom', $nom);
-        $page->assign('sexe', $sexe);
-        $page->assign_by_ref('nl', $this);
+        $user = is_null($uid) ? S::v('uid') : $uid;
+        $res = XDB::query("SELECT  1
+                             FROM  newsletter_ins
+                            WHERE  user_id={?}", $user);
+        return $res->fetchOneCell();
     }
 
-    // }}}
-    // {{{ function sendTo()
-    
-    function sendTo($prenom, $nom, $login, $sex, $html)
+    static public function unsubscribe($uid = null)
     {
-        global $globals;
-
-        $mailer = new PlMailer('newsletter/nl.tpl');
-        $mailer->assign('is_mail', true);
-        $mailer->assign('prenom', $prenom);
-        $mailer->assign('nom', $nom);
-        $mailer->assign('sexe', $sex);
-        $mailer->assign_by_ref('nl', $this);
-        $mailer->assign('prefix', null);
-        $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
-        $mailer->send($html);
+        $user = is_null($uid) ? S::v('uid') : $uid;
+        XDB::execute("DELETE FROM  newsletter_ins
+                            WHERE  user_id={?}", $user);
     }
 
-    // }}}
+    static public function subscribe($uid = null)
+    {
+        $user = is_null($uid) ? S::v('uid') : $uid;
+        XDB::execute("REPLACE INTO  newsletter_ins (user_id,last)
+                            VALUES  ({?}, 0)", $user);
+    }
+
+    protected function subscriptionWhere()
+    {
+        return '1';
+    }
+
+    static public function create()
+    {
+        XDB::execute("INSERT INTO newsletter
+                              SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
+    }
+
+    static public function listSent()
+    {
+        $res = XDB::query("SELECT  IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
+                             FROM  newsletter
+                            WHERE  bits!='new'
+                            ORDER  BY date DESC");
+        return $res->fetchAllAssoc();
+    }
+
+    static public function listAll()
+    {
+        $res = XDB::query("SELECT  IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
+                             FROM  newsletter
+                         ORDER BY  date DESC");
+        return $res->fetchAllAssoc();
+    }
 }
 
 // }}}
@@ -254,7 +181,7 @@ class NewsLetter
 class NLArticle
 {
     // {{{ properties
-    
+
     var $_aid;
     var $_cid;
     var $_pos;
@@ -264,196 +191,96 @@ class NLArticle
 
     // }}}
     // {{{ constructor
-    
-    function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
+
+    function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
     {
-    $this->_body   = $body;
-    $this->_title  = $title;
-    $this->_append = $append;
-    $this->_aid    = $aid;
-    $this->_cid    = $cid;
-    $this->_pos    = $pos;
+        $this->_body   = $body;
+        $this->_title  = $title;
+        $this->_append = $append;
+        $this->_aid    = $aid;
+        $this->_cid    = $cid;
+        $this->_pos    = $pos;
     }
 
     // }}}
     // {{{ function title()
 
-    function title()
+    public function title()
     { return trim($this->_title); }
 
     // }}}
     // {{{ function body()
-    
-    function body()
+
+    public function body()
     { return trim($this->_body); }
-    
+
     // }}}
     // {{{ function append()
-    
-    function append()
+
+    public function append()
     { return trim($this->_append); }
 
     // }}}
     // {{{ function toText()
 
-    function toText()
+    public function toText($hash = null, $login = null)
     {
-    $title = '*'.$this->title().'*';
-    $body  = enriched_to_text($this->_body,false,true);
-    $app   = enriched_to_text($this->_append,false,false,4);
-    return trim("$title\n\n$body\n\n$app")."\n";
+        $title = '*'.$this->title().'*';
+        $body  = MiniWiki::WikiToText($this->_body, true);
+        $app   = MiniWiki::WikiToText($this->_append,false,4);
+        $text = trim("$title\n\n$body\n\n$app")."\n";
+        if (!is_null($hash) && !is_null($login)) {
+            $text = str_replace('%HASH%', "$hash/$login", $text);
+        } else {
+            $text = str_replace('%HASH%', '', $text);
+        }
+        return $text;
     }
 
     // }}}
     // {{{ function toHtml()
 
-    function toHtml()
+    public function toHtml($hash = null, $login = null)
     {
-    $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
-    $body  = enriched_to_text($this->_body,true);
-    $app   = enriched_to_text($this->_append,true);
-    
-    $art   = "$title\n";
-    $art  .= "<div class='art'>\n$body\n";
-    if ($app) {
+        $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".pl_entities($this->title()).'</h2>';
+        $body  = MiniWiki::WikiToHTML($this->_body);
+        $app   = MiniWiki::WikiToHTML($this->_append);
+
+        $art   = "$title\n";
+        $art  .= "<div class='art'>\n$body\n";
+        if ($app) {
             $art .= "<div class='app'>$app</div>";
         }
-    $art  .= "</div>\n";
-    
-    return $art;
+        $art  .= "</div>\n";
+        if (!is_null($hash) && !is_null($login)) {
+            $art = str_replace('%HASH%', "$hash/$login", $art);
+        } else {
+            $art = str_replace('%HASH%', '', $art);
+        }
+
+        return $art;
     }
 
     // }}}
     // {{{ function check()
 
-    function check()
+    public function check()
     {
-    $text = enriched_to_text($this->_body);
-    $arr  = explode("\n",wordwrap($text,68));
-    $c    = 0;
-    foreach ($arr as $line) {
+        $text = MiniWiki::WikiToText($this->_body);
+        $arr  = explode("\n",wordwrap($text,68));
+        $c    = 0;
+        foreach ($arr as $line) {
             if (trim($line)) {
                 $c++;
             }
         }
-    return $c<9;
+        return $c<9;
     }
 
     // }}}
 }
 
 // }}}
-// {{{ Functions
-
-function insert_new_nl()
-{
-    XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
-}
-
-function get_nl_slist()
-{
-    $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter ORDER BY date DESC");
-    return $res->fetchAllAssoc();
-}
-
-function get_nl_list()
-{
-    $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
-    return $res->fetchAllAssoc();
-}
-
-function get_nl_state()
-{
-    $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
-    return $res->fetchOneCell();
-}
-function unsubscribe_nl()
-{
-    XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
-}
-function subscribe_nl($uid=-1)
-{
-    $user = ($uid == -1) ? S::v('uid') : $uid;
-    XDB::execute('REPLACE INTO  newsletter_ins (user_id,last)
-                        VALUES  ({?}, 0)', $user);
-}
-function justify($text,$n)
-{
-    $arr = explode("\n",wordwrap($text,$n));
-    $arr = array_map('trim',$arr);
-    $res = '';
-    foreach ($arr as $key => $line) {
-    $nxl       = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
-    $nxl_split = preg_split('! +!',$nxl);
-    $nxw_len   = count($nxl_split) ? strlen($nxl_split[0]) : 0;
-    $line      = trim($line);
-
-    if (strlen($line)+1+$nxw_len < $n) {
-        $res .= "$line\n";
-        continue;
-    }
-    
-    if (preg_match('![.:;]$!',$line)) {
-        $res .= "$line\n";
-        continue;
-    }
-
-    $tmp   = preg_split('! +!',trim($line));
-    $words = count($tmp);
-    if ($words <= 1) {
-        $res .= "$line\n";
-        continue;
-    }
-
-    $len   = array_sum(array_map('strlen',$tmp));
-    $empty = $n - $len;
-    $sw    = floatval($empty) / floatval($words-1);
-    
-    $cur = 0;
-    $l   = '';
-    foreach ($tmp as $word) {
-        $l   .= $word;
-        $cur += $sw + strlen($word);
-        $l    = str_pad($l,intval($cur+0.5));
-    }
-    $res .= trim($l)."\n";
-    }
-    return trim($res);
-}
-
-function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
-{
-    $text = trim($input);
-    if ($html) {
-        $text = htmlspecialchars($text);
-        $text = str_replace('[b]','<strong>', $text);
-        $text = str_replace('[/b]','</strong>', $text);
-        $text = str_replace('[i]','<em>', $text);
-        $text = str_replace('[/i]','</em>', $text);
-        $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
-        $text = str_replace('[/u]','</span>', $text);
-        require_once('url_catcher.inc.php');
-        $text = url_catcher($text);
-        return nl2br($text);
-    } else {
-        $text = preg_replace('!\[\/?b\]!','*',$text);
-        $text = preg_replace('!\[\/?u\]!','_',$text);
-        $text = preg_replace('!\[\/?i\]!','/',$text);
-        $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
-        $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
-        $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent);
-        if($indent) {
-            $ind = str_pad('',$indent);
-            $text = $ind.str_replace("\n","\n$ind",$text);
-        }
-        return $text;
-    }
-}
-
-// }}}
 
-// vim:set et sw=4 sts=4 sws=4:
+// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
 ?>