Use a template to format the newsletter and use the power of PlMailer
authorx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Mon, 18 Dec 2006 21:04:13 +0000 (21:04 +0000)
committerx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Mon, 18 Dec 2006 21:04:13 +0000 (21:04 +0000)
NL CSS is not duplicated anymore (one for web and one for the mail)

git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1289 839d8a87-29fc-0310-9880-83ba4fa771e5

classes/plmailer.php
configs/mails.conf
include/newsletter.inc.php
modules/newsletter.php
templates/newsletter/nl.tpl [new file with mode: 0644]
templates/newsletter/show.tpl

index f418b3c..1bfe781 100644 (file)
@@ -50,6 +50,7 @@ class PlMail extends Smarty
         $this->register_function('cc',      Array($this, 'addCc'));
         $this->register_function('bcc',     Array($this, 'addBcc'));
         $this->register_function('subject', Array($this, 'setSubject'));
+        $this->register_function('add_header', Array($this, 'addHeader'));
     }
 
     // }}}
@@ -144,6 +145,17 @@ class PlMail extends Smarty
     }
 
     // }}}
+    // {{{ function addHeader()
+
+    /** template function : add_header.
+     * {add_header name=... value=...}
+     */
+    function addHeader($params, &$smarty)
+    {
+        $smarty->mailer->addHeader($params['name'], $params['value']);
+    }
+
+    // }}}
 }
 // }}}
 
@@ -261,15 +273,47 @@ class PlMailer extends Mail_Mime {
     }
     
     // }}}
+    // {{{ function assign_by_ref()
+    
+    function assign_by_ref($var, &$value)
+    {
+        if (!is_null($this->page)) {
+            $this->page->assign_by_ref($var, $value);
+        }
+    }
+
+    // }}}
+    // {{{ function register_modifier()
+
+    function register_modifier($var, $callback)
+    {
+        if (!is_null($this->page)) {
+            $this->page->register_modifier($var, $callback);
+        }
+    }
+    
+    // }}}
+    // {{{ function register_function()
+
+    function register_function($var, $callback)
+    {
+        if (!is_null($this->page)) {
+            $this->page->register_function($var, $callback);
+        }
+    }
+    
+    // }}}
     // {{{ function processPage()
 
-    private function processPage()
+    private function processPage($with_html = true)
     {
         if (!is_null($this->page)) {
             $this->setTxtBody($this->page->run(false));
-            $html = trim($this->page->run(true));
-            if (!empty($html)) {
-                $this->setHtmlBody($html);
+            if ($with_html) {
+                $html = trim($this->page->run(true));
+                if (!empty($html)) {
+                    $this->setHtmlBody($html);
+                }
             }
         }
     }
@@ -277,9 +321,9 @@ class PlMailer extends Mail_Mime {
     // }}}
     // {{{ function send()
 
-    function send()
+    function send($with_html = true)
     {
-        $this->processPage();
+        $this->processPage($with_html);
         if (S::v('forlife')) {
             $this->addHeader('X-Org-Mail', S::v('forlife') . '@polytechnique.org');
         }
index 2555c0b..12dcbc0 100644 (file)
@@ -18,3 +18,8 @@ to=support@polytechnique.org
 
 [inscription]
 from=support@polytechnique.org
+
+[newsletter]
+from="Lettre Mensuelle Polytechnique.org" <info_newsletter@polytechnique.org>
+replyto=info+nlp@polytechnique.org
+
index 426bc27..6c31a41 100644 (file)
@@ -119,29 +119,52 @@ class NewsLetter
     // {{{ function title()
 
     function title($mail = false) {
-       if ($mail) {
-               return $this->_title_mail;
-       }
-               return $this->_title;
-       }
+        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;
     }
 
     // }}}
@@ -149,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;
+        }
     }
 
     // }}}
@@ -171,176 +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="foot1">Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.</div>'
-        .  '<div class="foot2">'
-        .  "[<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>".$this->_cats[$cid].'</h1>';
-        foreach($arts as $art) {
-            $res .= $art->toHtml();
-            $res .= "<div class='top_lnk'><a href='$u#top_lnk'>Revenir au sommaire</a></div>";
-        }
-    }
-
-    $res .= $this->footer(true);
-
-    if ($body) {
-        $res = <<<EOF
-<?xml version="1.0" encoding="iso-8859-15"?>
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
-  <head>
-    <title></title>
-    <style type="text/css">
-    <!--
-      body      { background-color: #ddd; color: #000; }
-      div.nl    { margin: auto; width: 72ex;
-          font-family: "Georgia", "Times New Roman", serif; font-size: 11pt;
-          text-align: justify;
-          background-color: #fff; color: #000; }
-      a[href]       { text-decoration: none;
-                      background-color: #fff; color: #36c; }
-      a[href]:hover { background-color: #fff; color: #6c0; }
-
-      div.title { margin: 0ex 0ex 3ex 0ex; padding: 5ex 1ex 1ex 15ex;
-          font-size: 130%; font-weight: bold; text-align: right; 
-          background-color: #369; color: #fff;
-          background-image: url(http://dev.m4x.org/images/logo_xorg_nl.png);
-          background-repeat: no-repeat; background-position: 0.5ex 0.5ex; }
-      div.intro { margin: 4ex 3ex; }
-      div.lnk   { margin: 2ex 6ex;}
-      div.lnk a { display: block; font-size: 95%; }
-      div.top_lnk   { margin: 2ex; padding: 0ex; font-size: 85%; text-align: right; }
-      h1.xorg_nl    { margin: 3ex 0ex 2ex 0ex; padding: 1.5ex 2ex 0.5ex 1ex;
-          font-size: 120%; font-weight: bold; text-align: right; 
-          background-color: #369; color: #fff; }
-      h2.xorg_nl    { margin: 2ex 0ex 0ex 0ex; padding: 0.4ex 2ex;
-          font-size: 100%; font-weight: bold; font-style: italic;
-          background-color: #fff; color: #369;
-          border-width: thin 0; border-style: solid; border-color: #369; }
-      div.art   { margin: 2ex 3ex; }
-      div.app   { margin: 2ex 6ex 0ex 3ex; font-size: 95%; text-align: left; }
-      div.foot1 { margin: 8ex 0ex 0ex 0ex; padding: 0.5ex 2ex;
-          font-size: 90%; background-color: #fff; color: #999;
-          border-width: thin 0; border-style: solid; border-color: #ddd;
-          text-align: center; }
-      div.foot2 { padding: 1ex 0ex;
-          font-size: 90%; background-color: #fff; color: #999;
-          text-align: center; }
-    -->
-    </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);
     }
 
     // }}}
@@ -350,21 +233,15 @@ EOF;
     {
         global $globals;
 
-        $mailer = new PlMailer();
-        $mailer->setFrom($globals->newsletter->from);
-        $mailer->setSubject($this->title(true));
+        $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}>");
-        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->send($html);
     }
 
     // }}}
index 5cee6b7..4f981fb 100644 (file)
@@ -63,8 +63,11 @@ class NewsletterModule extends PLModule
         require_once 'newsletter.inc.php';
 
         $nl  = new NewsLetter($nid);
-        $page->assign_by_ref('nl', $nl);
-
+        if (Get::has('text')) {
+            $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme'));
+        } else {
+            $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
+        }
         if (Post::has('send')) {
             $nl->sendTo(S::v('prenom'), S::v('nom'),
                         S::v('bestalias'), S::v('femme'),
diff --git a/templates/newsletter/nl.tpl b/templates/newsletter/nl.tpl
new file mode 100644 (file)
index 0000000..79caa0d
--- /dev/null
@@ -0,0 +1,125 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2006 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+{if !$html_version}
+{if $is_mail}
+{config_load file="mails.conf" section="newsletter"}
+{from full=#from#}
+{subject text=$nl->title(true)}
+{if #replyto#}{add_header name='Reply-To' value=#replyto#}{/if}
+{if #retpath#}{add_header name='Return-Path' value=#retpath#}{/if}
+{else}
+<pre style="width : 72ex; margin: auto">
+{/if}
+====================================================================
+{$nl->title()}
+====================================================================
+
+{$nl->head($prenom, $nom, $sexe, 'text')}
+
+
+{foreach from=$nl->_arts key=cid item=arts name=cats}
+{$smarty.foreach.cats.iteration} *{$nl->_cats[$cid]}*
+{foreach from=$arts item=art}
+- {$art->title()}
+{/foreach}
+
+{/foreach}
+
+{foreach from=$nl->_arts key=cid item=arts}
+--------------------------------------------------------------------
+*{$nl->_cats[$cid]}*
+--------------------------------------------------------------------
+
+{foreach from=$arts item=art}
+{$art->toText()}
+
+{/foreach}
+{/foreach}
+
+--------------------------------------------------------------------
+Cette lettre est envoyée à tous les Polytechniciens sur Internet par
+l'intermédiaire de Polytechnique.org.
+
+archives         : [https://www.polytechnique.org/nl]
+écrire           : [https://www.polytechnique.org/nl/submit]
+ne plus recevoir : [https://www.polytechnique.org/nl/out]
+
+{if $is_mail}
+</pre>
+{/if}
+{else}
+{if $is_mail}
+<?xml version="1.0" encoding="iso-8859-15"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"> 
+  <head>  
+    <title></title>
+    <style type="text/css">
+    <!--
+      {literal}
+      body      { background-color: #ddd; color: #000; }
+      {/literal}
+      {$nl->getCss()}
+    -->
+    </style>
+  </head>
+  <body>
+{/if}
+    <div class='nl'>
+      <div class="title">{$nl->title()}</div>
+      <div class="intro">{$nl->head($prenom, $nom, $sexe, 'html')|smarty:nodefaults}</div>
+      <a id="top_lnk"></a>
+      {foreach from=$nl->_arts key=cid item=arts name=cats}
+      <div class="lnk">
+        <a href="{$prefix}#cat{$cid}"><strong>{$smarty.foreach.cats.iteration}. {$nl->_cats[$cid]}</strong></a>
+        {foreach from=$arts item=art}
+        <a href="{$prefix}#art{$art->_aid}">&nbsp;&nbsp;- {$art->title()|htmlentities}</a>
+        {/foreach}
+      </div>
+      {/foreach}
+
+      {foreach from=$nl->_arts key=cid item=arts name=cats}
+      <h1 class="xorg_nl"><a id="cat{$cid}"></a>
+        {$nl->_cats[$cid]}
+      </h1>
+      {foreach from=$arts item=art}
+        {$art->toHtml()|smarty:nodefaults}
+        <div class="top_lnk"><a href="{$prefix}#top_lnk">Revenir au sommaire</a></div>
+      {/foreach}
+      {/foreach}
+      <div class="foot1">
+        Cette lettre est envoyée à tous les Polytechniciens sur Internet par l'intermédiaire de lytechnique.org.
+      </div>
+      <div class="foot2">
+        [<a href="https://www.polytechnique.org/nl">archives</a>&nbsp;|
+         <a href="https://www.polytechnique.org/nl/submit">écrire dans la NL</a>&nbsp;|
+         <a href="https://www.polytechnique.org/nl/out">ne plus recevoir</a>]
+      </div>
+    </div>
+{if $is_mail}
+  </body>
+</html>
+{/if}
+{/if}
+
+{* vim:set et sw=2 sts=2 sws=2: *}
index a681ec9..159c68a 100644 (file)
 
 <fieldset>
 <legend>{$nl->title(true)}</legend>
-  {if $smarty.get.text}
-  <pre style="width : 72ex; margin: auto">{$nl->toText($smarty.session.prenom, $smarty.session.nom, $smarty.session.femme)}
-  </pre>
-  {else}
-  <div class='nl'>
-    {$nl->toHtml($smarty.session.prenom, $smarty.session.nom, $smarty.session.femme,
-                     false, true)|smarty:nodefaults}
-  </div>
-  {/if}
+  {include file="newsletter/nl.tpl"}
 </fieldset>
 
 {* vim:set et sw=2 sts=2 sws=2: *}