From 11f44323b9f6d4ee4d21eb8cfb3084e5d3964253 Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Mon, 18 Dec 2006 21:04:13 +0000 Subject: [PATCH] Use a template to format the newsletter and use the power of PlMailer 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 | 56 ++++++++- configs/mails.conf | 5 + include/newsletter.inc.php | 273 ++++++++++++------------------------------ modules/newsletter.php | 7 +- templates/newsletter/nl.tpl | 125 +++++++++++++++++++ templates/newsletter/show.tpl | 10 +- 6 files changed, 261 insertions(+), 215 deletions(-) create mode 100644 templates/newsletter/nl.tpl diff --git a/classes/plmailer.php b/classes/plmailer.php index f418b3c..1bfe781 100644 --- a/classes/plmailer.php +++ b/classes/plmailer.php @@ -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'); } diff --git a/configs/mails.conf b/configs/mails.conf index 2555c0b..12dcbc0 100644 --- a/configs/mails.conf +++ b/configs/mails.conf @@ -18,3 +18,8 @@ to=support@polytechnique.org [inscription] from=support@polytechnique.org + +[newsletter] +from="Lettre Mensuelle Polytechnique.org" +replyto=info+nlp@polytechnique.org + diff --git a/include/newsletter.inc.php b/include/newsletter.inc.php index 426bc27..6c31a41 100644 --- a/include/newsletter.inc.php +++ b/include/newsletter.inc.php @@ -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('', $sexe ? 'Chère' : 'Cher', $head); + $head = str_replace('', $prenom, $head); + $head = str_replace('', $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 '
Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.
' - . '
' - . "[archives | " - . "écrire dans la NL | " - . "ne plus recevoir]" - . '
'; - } 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('', $sexe ? 'Chère' : 'Cher', $head); - $head = str_replace('', $prenom, $head); - $head = str_replace('', $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 = '
'.$this->title().'
'; - - $head = $this->head(); - $head = str_replace('', $sexe ? 'Chère' : 'Cher', $head); - $head = str_replace('', $prenom, $head); - $head = str_replace('', $nom, $head); - $head = enriched_to_text($head, true); - - if($head) { - $res .= "
$head
"; - } - - $i = 1; - $res .= ""; - foreach ($this->_arts as $cid=>$arts) { - $res .= "
$i. {$this->_cats[$cid]}"; - foreach ($arts as $art) { - $res .= "  - ".htmlentities($art->title()).""; - } - $res .= '
'; - $i ++; - } - - foreach ($this->_arts as $cid=>$arts) { - $res .= "

".$this->_cats[$cid].'

'; - foreach($arts as $art) { - $res .= $art->toHtml(); - $res .= ""; - } - } - - $res .= $this->footer(true); - - if ($body) { - $res = << - - - - - - - -
- $res -
- - -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); } // }}} diff --git a/modules/newsletter.php b/modules/newsletter.php index 5cee6b7..4f981fb 100644 --- a/modules/newsletter.php +++ b/modules/newsletter.php @@ -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 index 0000000..79caa0d --- /dev/null +++ b/templates/newsletter/nl.tpl @@ -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} +
+{/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}
+
+{/if} +{else} +{if $is_mail} + + + + + + + + +{/if} +
+
{$nl->title()}
+
{$nl->head($prenom, $nom, $sexe, 'html')|smarty:nodefaults}
+ + {foreach from=$nl->_arts key=cid item=arts name=cats} + + {/foreach} + + {foreach from=$nl->_arts key=cid item=arts name=cats} +

+ {$nl->_cats[$cid]} +

+ {foreach from=$arts item=art} + {$art->toHtml()|smarty:nodefaults} + + {/foreach} + {/foreach} +
+ Cette lettre est envoyée à tous les Polytechniciens sur Internet par l'intermédiaire de lytechnique.org. +
+ +
+{if $is_mail} + + +{/if} +{/if} + +{* vim:set et sw=2 sts=2 sws=2: *} diff --git a/templates/newsletter/show.tpl b/templates/newsletter/show.tpl index a681ec9..159c68a 100644 --- a/templates/newsletter/show.tpl +++ b/templates/newsletter/show.tpl @@ -44,15 +44,7 @@
{$nl->title(true)} - {if $smarty.get.text} -
{$nl->toText($smarty.session.prenom, $smarty.session.nom, $smarty.session.femme)}
-  
- {else} -
- {$nl->toHtml($smarty.session.prenom, $smarty.session.nom, $smarty.session.femme, - false, true)|smarty:nodefaults} -
- {/if} + {include file="newsletter/nl.tpl"}
{* vim:set et sw=2 sts=2 sws=2: *} -- 2.1.4