From 95df4b79f3cf5c4726c225cb089430519f0ca658 Mon Sep 17 00:00:00 2001 From: x2000habouzit Date: Sat, 16 Oct 2004 21:14:15 +0000 Subject: [PATCH] auto justifiate, and I cannot remember what ... --- htdocs/admin/newsletter_edit.php | 19 +++++---- include/newsletter.inc.php | 79 ++++++++++++++++++++++++++++++++++--- templates/admin/newsletter_edit.tpl | 41 +++++++++++++------ templates/newsletter/show.tpl | 23 +++++++++-- 4 files changed, 135 insertions(+), 27 deletions(-) diff --git a/htdocs/admin/newsletter_edit.php b/htdocs/admin/newsletter_edit.php index 3173581..43ab984 100644 --- a/htdocs/admin/newsletter_edit.php +++ b/htdocs/admin/newsletter_edit.php @@ -18,7 +18,7 @@ * Foundation, Inc., * * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *************************************************************************** - $Id: newsletter_edit.php,v 1.1 2004-10-16 19:54:34 x2000habouzit Exp $ + $Id: newsletter_edit.php,v 1.2 2004-10-16 21:14:15 x2000habouzit Exp $ ***************************************************************************/ require("auto.prepend.inc.php"); @@ -32,6 +32,11 @@ if(isset($_GET['del_aid'])) { header("Location: ?nid=$nid"); } +if(isset($_POST['update'])) { + $nl->_title = $_POST['title']; + $nl->save(); +} + if(isset($_POST['save'])) { $eaid = $_GET['edit_aid']; $art = new NLArticle($_POST['title'], $_POST['body'], $_POST['append'], $eaid, $_POST['cid'], $_POST['pos']); @@ -41,13 +46,11 @@ if(isset($_POST['save'])) { if(isset($_GET['edit_aid'])) { $eaid = $_GET['edit_aid']; - if($eaid<0) { - if(!empty($_POST)) { - $art = new NLArticle($_POST['title'], $_POST['body'], $_POST['append'], - $eaid, $_POST['cid'], $_POST['pos']); - } else { - $art = new NLArticle(); - } + if(isset($_POST['aid'])) { + $art = new NLArticle($_POST['title'], $_POST['body'], $_POST['append'], + $eaid, $_POST['cid'], $_POST['pos']); + } elseif($eaid<0) { + $art = new NLArticle(); } else { $art = $nl->getArt($_GET['edit_aid']); } diff --git a/include/newsletter.inc.php b/include/newsletter.inc.php index 8a252bc..b3b50b1 100644 --- a/include/newsletter.inc.php +++ b/include/newsletter.inc.php @@ -18,14 +18,52 @@ * Foundation, Inc., * * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *************************************************************************** - $Id: newsletter.inc.php,v 1.8 2004-10-16 19:54:35 x2000habouzit Exp $ + $Id: newsletter.inc.php,v 1.9 2004-10-16 21:14:15 x2000habouzit Exp $ ***************************************************************************/ define('FEMME', 1); define('HOMME', 0); -function enriched_to_text($input,$html=false) { +function justify($text,$n) { + $arr = split("\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; + + if(strlen($line)+1+$nxw_len <= 68) { + $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) { $text = stripslashes(trim($input)); if($html) { $text = htmlspecialchars($text); @@ -44,7 +82,7 @@ function enriched_to_text($input,$html=false) { $text = preg_replace('!\[\/?i\]!','/',$text); $text = preg_replace('!((https?|ftp)://[^\r\n\t ]*)!','[\1]', $text); $text = preg_replace('!([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)!','[mailto:\1]', $text); - return wordwrap($text, 68); + return $just ? justify($text,68) : wordwrap($text,68); } } @@ -91,6 +129,15 @@ class NewsLetter { mysql_free_result($res); } + function save() { + global $globals; + $globals->db->query("UPDATE newsletter + SET date='{$this->_date}',titre='{$this->_title}' + WHERE id='{$this->_id}'"); + } + + function title() { return stripslashes($this->_title); } + function getArt($aid) { foreach($this->_arts as $key=>$artlist) { if(isset($artlist["a$aid"])) return $artlist["a$aid"]; @@ -125,8 +172,30 @@ class NewsLetter { } } + function toText() { + $res = ""; + 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"; + } + } + return $res; + } + function toHtml() { - return "foo"; + $res = ""; + foreach($this->_arts as $cid=>$arts) { + $res .= '
'; + $res .= $this->_cats[$cid].'
'; + foreach($arts as $art) { + $res .= $art->toHtml(); + } + } + return $res; } } @@ -152,7 +221,7 @@ class NLArticle { function toText() { $title = '*'.stripslashes($this->_title).'*'; - $body = enriched_to_text($this->_body); + $body = enriched_to_text($this->_body,false,true); $app = enriched_to_text($this->_append); return trim("$title\n\n$body\n\n$app")."\n"; } diff --git a/templates/admin/newsletter_edit.tpl b/templates/admin/newsletter_edit.tpl index 9f36fff..98ae3f4 100644 --- a/templates/admin/newsletter_edit.tpl +++ b/templates/admin/newsletter_edit.tpl @@ -17,7 +17,7 @@ * Foundation, Inc., * * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *************************************************************************** - $Id: newsletter_edit.tpl,v 1.1 2004-10-16 19:54:35 x2000habouzit Exp $ + $Id: newsletter_edit.tpl,v 1.2 2004-10-16 21:14:15 x2000habouzit Exp $ ***************************************************************************} {dynamic} @@ -27,14 +27,35 @@ {if !$art} +

[_id}">visualiser]

+ +
+ + + + + + + + + + + +
+ Propriétés de la newsletter +
+ Titre + + +
+ +
+
+ +
+ - - - - @@ -52,9 +73,7 @@ {foreach from=$arts item=art}
- {$nl->_title|default:"[no title]"} -
Créer un nouvel article ...
-
- {$art->toHtml()|smarty:nodefaults} -
+
{$art->toText()|smarty:nodefaults}
Pos: {$art->_pos}
@@ -90,7 +109,7 @@
-
+
diff --git a/templates/newsletter/show.tpl b/templates/newsletter/show.tpl index f0e6c7a..dba318a 100644 --- a/templates/newsletter/show.tpl +++ b/templates/newsletter/show.tpl @@ -17,7 +17,7 @@ * Foundation, Inc., * * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *************************************************************************** - $Id: show.tpl,v 1.1 2004-10-16 18:17:51 x2000habouzit Exp $ + $Id: show.tpl,v 1.2 2004-10-16 21:14:16 x2000habouzit Exp $ ***************************************************************************} {dynamic} @@ -25,7 +25,17 @@ Lettre de Polytechnique.org de {$nl->_date|date_format:"%B %Y"} -

[liste des lettres]

+

+[liste des lettres] +{if $smarty.get.text} +[version HTML] +{else} +[version Texte] +{/if} +{perms level='admin'} +[Editer] +{/perms} +

@@ -35,10 +45,17 @@ + + +
{$nl->title()}
+ {if $smarty.get.text} +
{$nl->toText()}
+ {else}
- {$nl->toHtml()} + {$nl->toHtml()|smarty:nodefaults}
+ {/if}
-- 2.1.4