Add an icon for "Adding attachment in emails"
[platal.git] / include / newsletter.inc.php
index 236026c..6c31a41 100644 (file)
@@ -41,6 +41,7 @@ class NewsLetter
     var $_shortname;
     var $_date;
     var $_title;
+    var $_title_mail;
     var $_head;
     var $_cats = Array();
     var $_arts = Array();
@@ -69,6 +70,7 @@ class NewsLetter
         $this->_shortname = $nl['short_name'];
         $this->_date      = $nl['date'];
         $this->_title     = $nl['titre'];
+        $this->_title_mail = $nl['titre_mail'];
         $this->_head      = $nl['head'];
 
         $res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
@@ -101,8 +103,8 @@ class NewsLetter
 
     function save()
     {
-        XDB::execute('UPDATE newsletter SET date={?},titre={?},head={?},short_name={?} WHERE id={?}',
-                     $this->_date, $this->_title, $this->_head, $this->_shortname,$this->_id);
+        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);
     }
 
     // }}}
@@ -116,26 +118,53 @@ class NewsLetter
     // }}}
     // {{{ function title()
 
-    function title()
-    { return $this->_title; }
+    function title($mail = false) {
+        if ($mail) {
+            return $this->_title_mail;
+        }
+        return $this->_title;
+    }
 
     // }}}
     // {{{ function head()
     
-    function head()
-    { return $this->_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()
+    {
+        return file_get_contents(dirname(__FILE__) . '/../htdocs/css/nl.css');
+    }
 
     // }}}
     // {{{ function getArt()
     
     function getArt($aid)
     {
-    foreach ($this->_arts as $key=>$artlist) {
-        if (isset($artlist["a$aid"])) {
+        foreach ($this->_arts as $key=>$artlist) {
+            if (isset($artlist["a$aid"])) {
                 return $artlist["a$aid"];
             }
-    }
-    return null;
+        }
+        return null;
     }
 
     // }}}
@@ -143,21 +172,20 @@ class NewsLetter
 
     function saveArticle(&$a)
     {
-    if ($a->_aid>=0) {
-        XDB::execute('REPLACE INTO  newsletter_art (id,aid,cid,pos,title,body,append)
-                                          VALUES  ({?},{?},{?},{?},{?},{?},{?})',
-                                          $this->_id, $a->_aid, $a->_cid, $a->_pos,
-                                          $a->_title, $a->_body, $a->_append);
-        $this->_arts['a'.$a->_aid] = $a;
-    } else {
-        XDB::execute(
-        'INSERT INTO  newsletter_art
-              SELECT  {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
-            FROM  newsletter_art AS a
-               WHERE  a.id={?}',
-                       $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
-        $this->_arts['a'.$a->_aid] = $a;
-    }
+        if ($a->_aid>=0) {
+            XDB::execute('REPLACE INTO  newsletter_art (id,aid,cid,pos,title,body,append)
+                                VALUES  ({?},{?},{?},{?},{?},{?},{?})',
+                          $this->_id, $a->_aid, $a->_cid, $a->_pos,
+                          $a->_title, $a->_body, $a->_append);
+                          $this->_arts['a'.$a->_aid] = $a;
+        } else {
+            XDB::execute('INSERT INTO  newsletter_art
+                               SELECT  {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
+                                 FROM  newsletter_art AS a
+                                WHERE  a.id={?}',
+                         $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
+                         $this->_arts['a'.$a->_aid] = $a;
+        }
     }
 
     // }}}
@@ -165,160 +193,37 @@ class NewsLetter
     
     function delArticle($aid)
     {
-    XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
-    foreach ($this->_arts as $key=>$art) {
-        unset($this->_arts[$key]["a$aid"]);
-    }
-    }
-
-    // }}}
-    // {{{ function footer
-
-    function footer($html)
-    {
-        global $globals;
-        $url = 'https://www.polytechnique.org';
-
-    if ($html) {
-        return '<div class="foot">Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.</div>'
-        .  '<div class="foot">'
-        .  "[<a href=\"$url/nl\">archives</a>&nbsp;|&nbsp;"
-        .  "<a href=\"$url/nl/submit\">écrire dans la NL</a>&nbsp;|&nbsp;"
-        .  "<a href=\"$url/nl/out\">ne plus recevoir</a>]"
-        .  '</div>';
-    } else {
-        return "\n\n--------------------------------------------------------------------\n"
-             . "Cette lettre est envoyée à tous les Polytechniciens sur Internet par\n"
-             . "l'intermédiaire de Polytechnique.org.\n"
-         . "\n"
-         . "archives : [$url/nl]\n"
-         . "écrire   : [$url/nl/submit]\n"
-         . "ne plus recevoir: [$url/nl/out]\n";
-    }
+        XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
+        foreach ($this->_arts as $key=>$art) {
+            unset($this->_arts[$key]["a$aid"]);
+        }
     }
 
     // }}}
     // {{{ function toText()
 
-    function toText($prenom,$nom,$sexe)
+    function toText(&$page, $prenom,$nom,$sexe)
     {
-    $res  = "====================================================================\n";
-    $res .= ' '.$this->title()."\n";
-    $res .= "====================================================================\n\n";
-
-    $head = $this->head();
-    $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
-    $head = str_replace('<prenom>', $prenom, $head);
-    $head = str_replace('<nom>',    $nom,    $head);
-    $head = enriched_to_text($head,false,true,2,64);
-
-    if ($head) {
-            $res .= "\n$head\n\n\n";
-        }
-
-    $i = 1;
-    foreach ($this->_arts as $cid=>$arts) {
-        $res .= "\n$i *{$this->_cats[$cid]}*\n";
-        foreach ($arts as $art) {
-        $res .= '- '.$art->title()."\n";
-        }
-        $i ++;
-    }
-    $res .= "\n\n";
-        
-    foreach ($this->_arts as $cid=>$arts) {
-        $res .= "--------------------------------------------------------------------\n";
-        $res .= "*{$this->_cats[$cid]}*\n";
-        $res .= "--------------------------------------------------------------------\n\n";
-        foreach ($arts as $art) {
-        $res .= $art->toText();
-        $res .= "\n\n";
-        }
-    }
-    
-    $res .= $this->footer(false);
-    
-    return $res;
+        $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);
     }
 
     // }}}
     // {{{ function toHtml()
     
-    function toHtml($prenom, $nom, $sexe, $body=false, $urlprefix = false)
+    function toHtml(&$page, $prenom, $nom, $sexe)
     {
-        $u    = $urlprefix ? 'nl/show/'.$this->id() : '';
-    $res  = '<div class="title">'.$this->title().'</div>';
-    
-    $head = $this->head();
-    $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
-    $head = str_replace('<prenom>', $prenom, $head);
-    $head = str_replace('<nom>',    $nom,    $head);
-    $head = enriched_to_text($head, true);
-
-    if($head) {
-            $res .= "<div class='intro'>$head</div>";
-        }
-
-    $i = 1;
-    $res .= "<a id='top_lnk'></a>";
-    foreach ($this->_arts as $cid=>$arts) {
-        $res .= "<div class='lnk'><a href='$u#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
-        foreach ($arts as $art) {
-        $res .= "<a href='$u#art{$art->_aid}'>&nbsp;&nbsp;- ".htmlentities($art->title())."</a>";
-        }
-        $res .= '</div>';
-        $i ++;
-    }
-
-    foreach ($this->_arts as $cid=>$arts) {
-        $res .= "<h1 class='xorg_nl'><a id='cat$cid'></a><span>".$this->_cats[$cid].'</span></h1>';
-        foreach($arts as $art) {
-            $res .= $art->toHtml();
-            $res .= "<p><a href='$u#top_lnk'>Revenir au sommaire</a></p>";
-        }
-    }
-
-    $res .= $this->footer(true);
-
-    if ($body) {
-        $res = <<<EOF
-<html>
-  <head>
-    <style type="text/css">
-    <!--
-      div.nl    { margin: auto; font-family: "Georgia","times new roman",serif; width: 60ex; text-align: justify; font-size: 10pt; }
-      div.title { margin: 2ex 0ex 2ex 0ex; padding: 1ex; width: 100%; font-size: 140%; text-align: center;
-          font-weight: bold; border-bottom: 3px red solid; border-top: 3px red solid; }
-      
-      a[href]       { text-decoration: none; }
-      a[href]:hover { text-decoration: underline; }
-      
-      div.lnk   { margin: 2ex 0ex 2ex 0ex; padding: 0ex 2ex 0ex 2ex; }
-      div.lnk a { display: block; }
-      
-      h1.xorg_nl { margin: 6ex 0ex 4ex 0ex; padding: 2px 4ex 2px 0ex; width: 60ex; font-size: 100%;
-        border-bottom: 3px red solid; border-top: 3px red solid; }
-      h2.xorg_nl { width: 100%; margin: 0ex 1ex 0ex 1ex; padding: 2px 0px 2px 0px; font-weight: bold; font-style: italic; font-size: 95%; }
-      h1.xorg_nl span { font-size: 140%; padding: 2px 1ex 2px 1ex; border-bottom: 3px red solid; }
-      h2.xorg_nl span { padding: 2px 4px 2px 4px; border-bottom: 2px yellow solid; }
-      
-      div.art   { padding: 2ex; margin: 0ex 1ex 2ex 1ex; width: 58ex; border-top: 2px yellow solid; }
-      div.app   { padding: 2ex 3ex 0ex 3ex; width: 100%; margin: 0ex; text-align: left; font-size: 95%; }
-      div.intro { padding: 2ex; }
-      div.foot  { border-top: 1px #808080 dashed; font-size: 95%; padding: 1ex; color: #808080; background: inherit;
-          text-align: center; width: 100% }
-    -->
-    </style>
-  </head>
-  <body>
-    <div class='nl'>
-    $res
-    </div>
-  </body>
-</html>
-EOF;
-    }
-    return $res;
+        $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);
     }
 
     // }}}
@@ -327,23 +232,16 @@ EOF;
     function sendTo($prenom, $nom, $login, $sex, $html)
     {
         global $globals;
-    require_once('diogenes/diogenes.hermes.inc.php');
-
-    $mailer = new HermesMailer();
-    $mailer->setFrom($globals->newsletter->from);
-    $mailer->setSubject($this->title());
-    $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
-        if (!empty($globals->newsletter->replyto)) {
-            $mailer->addHeader('Reply-To',$globals->newsletter->replyto);
-        }
-        if (!empty($globals->newsletter->retpath)) {
-            $mailer->addHeader('Return-Path',$globals->newsletter->retpath);
-        }
-    $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
-    if ($html) {
-        $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true));
-    }
-    $mailer->send();
+
+        $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);
     }
 
     // }}}
@@ -410,7 +308,7 @@ class NLArticle
 
     function toHtml()
     {
-    $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a><span>".htmlentities($this->title()).'</span></h2>';
+    $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);
     
@@ -448,18 +346,18 @@ class NLArticle
 
 function insert_new_nl()
 {
-    XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued'");
+    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 FROM newsletter ORDER BY date DESC");
+    $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 FROM newsletter WHERE bits!='new' ORDER BY date DESC");
+    $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();
 }