From 1d97c2bcda628c54245d3c4b61b51fb6e700a6d3 Mon Sep 17 00:00:00 2001 From: x2000habouzit Date: Sat, 16 Oct 2004 22:06:32 +0000 Subject: [PATCH] various bugfixes --- htdocs/admin/newsletter_edit.php | 3 +- include/newsletter.inc.php | 214 +++++++++++++++++++----------------- templates/admin/newsletter_edit.tpl | 12 +- 3 files changed, 123 insertions(+), 106 deletions(-) diff --git a/htdocs/admin/newsletter_edit.php b/htdocs/admin/newsletter_edit.php index 43ab984..8e39166 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.2 2004-10-16 21:14:15 x2000habouzit Exp $ + $Id: newsletter_edit.php,v 1.3 2004-10-16 22:06:32 x2000habouzit Exp $ ***************************************************************************/ require("auto.prepend.inc.php"); @@ -34,6 +34,7 @@ if(isset($_GET['del_aid'])) { if(isset($_POST['update'])) { $nl->_title = $_POST['title']; + $nl->_date = $_POST['date']; $nl->save(); } diff --git a/include/newsletter.inc.php b/include/newsletter.inc.php index b3b50b1..559d485 100644 --- a/include/newsletter.inc.php +++ b/include/newsletter.inc.php @@ -18,74 +18,13 @@ * Foundation, Inc., * * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *************************************************************************** - $Id: newsletter.inc.php,v 1.9 2004-10-16 21:14:15 x2000habouzit Exp $ + $Id: newsletter.inc.php,v 1.10 2004-10-16 22:06:33 x2000habouzit Exp $ ***************************************************************************/ define('FEMME', 1); define('HOMME', 0); -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); - $text = str_replace('[b]','', $text); - $text = str_replace('[/b]','', $text); - $text = str_replace('[i]','', $text); - $text = str_replace('[/i]','', $text); - $text = str_replace('[u]','', $text); - $text = str_replace('[/u]','', $text); - $text = preg_replace('!((https?|ftp)://[^\r\n\t ]*)!','\1', $text); - $text = preg_replace('!([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)!','\1', $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)://[^\r\n\t ]*)!','[\1]', $text); - $text = preg_replace('!([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)!','[mailto:\1]', $text); - return $just ? justify($text,68) : wordwrap($text,68); - } -} - class NewsLetter { var $_id; @@ -122,6 +61,7 @@ class NewsLetter { FROM newsletter_art AS a INNER JOIN newsletter AS n USING(id) LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid) + WHERE a.id=$id ORDER BY c.pos,a.pos"); while(list($title,$body,$append,$aid,$cid,$pos) = mysql_fetch_row($res)) { $this->_arts[$cid]["a$aid"] = new NLArticle($title,$body,$append,$aid,$cid,$pos); @@ -156,10 +96,10 @@ class NewsLetter { $globals->db->query( "INSERT INTO newsletter_art SELECT {$this->_id},MAX(aid)+1,{$a->_cid}, - ". ($a->_pos ? $a->_pos : "IF(MAX(pos)<100,100,MAX(pos)+1),").", + ".($a->_pos ? $a->_pos : "MAX(pos)+1").", '{$a->_title}','{$a->_body}','{$a->_append}' FROM newsletter_art AS a - WHERE a.id={$this->_id}"); + WHERE a.id={$this->_id} AND cid='{$a->_cid}'"); $this->_arts['a'.$a->_aid] = $a; } } @@ -173,7 +113,23 @@ class NewsLetter { } function toText() { - $res = ""; + $res = "====================================================================\n"; + $res .= ' '.$this->title()."\n"; + $res .= "====================================================================\n\n"; + + $res .= "INTRO\n\n"; + + $res .= "--------------------------------------------------------------------\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"; + foreach($this->_arts as $cid=>$arts) { $res .= "--------------------------------------------------------------------\n"; $res .= "*{$this->_cats[$cid]}*\n"; @@ -187,9 +143,22 @@ class NewsLetter { } function toHtml() { - $res = ""; + $res = '
'; + $res .= $this->title(); + $res .= '
'; + + $i = 1; foreach($this->_arts as $cid=>$arts) { - $res .= '
'; + $res .= "$i. {$this->_cats[$cid]}
"; + foreach($arts as $art) { + $res .= '- '.$art->title()."
\n"; + } + $res .= '
'; + $i ++; + } + + foreach($this->_arts as $cid=>$arts) { + $res .= '
'; $res .= $this->_cats[$cid].'
'; foreach($arts as $art) { $res .= $art->toHtml(); @@ -216,23 +185,24 @@ class NLArticle { $this->_pos = $pos; } + function title() { return stripslashes(trim($this->_title)); } function body() { return stripslashes(trim($this->_body)); } function append() { return stripslashes(trim($this->_append)); } function toText() { - $title = '*'.stripslashes($this->_title).'*'; + $title = '*'.$this->title().'*'; $body = enriched_to_text($this->_body,false,true); $app = enriched_to_text($this->_append); return trim("$title\n\n$body\n\n$app")."\n"; } function toHtml() { - $title = '' - .stripslashes($this->_title).''; + $title = '
' + .$this->title().'
'; $body = enriched_to_text($this->_body,true); $app = enriched_to_text($this->_append,true); - $art = "$title

$body
"; + $art = "$title\n$body
"; if ($app) $art .= "
$app
"; return $art; @@ -247,38 +217,9 @@ class NLArticle { } } -class NLConstraint { - var $_func; - var $_arg; - - function NLConstraint($func, $arg) { - $this->_func = $func; - $this->_arg = $arg; - } - - function check($user) { return false; } -} - -class NLPromoConstraint { - function check($user) { - $promo = $user['promo']; - switch($this->_func) { - case 'eq': return ( $promo == $this->_arg ); - case 'neq': return ( $promo != $this->_arg ); - case 'geq': return ( $promo >= $this->_arg ); - case 'leq': return ( $promo <= $this->_arg ); - case 'odd': return ( $promo % 2 == 1 ); - case 'even': return ( $promo % 2 == 0 ); - default: return false; - } - } -} - -class NLSexeConstraint { - function check($user) { return $user['sexe'] == $_arg; } -} - - +///////////////////////// +// functions ............ +// function get_nl_slist() { global $globals; $res = $globals->db->query("SELECT id,date,titre FROM newsletter ORDER BY date DESC"); @@ -317,4 +258,71 @@ function subscribe_nl() { FROM newsletter WHERE bits!='new'"); } +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; + $line = trim($line); + + if(strlen($line)+1+$nxw_len < 68) { + $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) { + $text = stripslashes(trim($input)); + if($html) { + $text = htmlspecialchars($text); + $text = str_replace('[b]','', $text); + $text = str_replace('[/b]','', $text); + $text = str_replace('[i]','', $text); + $text = str_replace('[/i]','', $text); + $text = str_replace('[u]','', $text); + $text = str_replace('[/u]','', $text); + $text = preg_replace('!((https?|ftp)://[^\r\n\t ]*)!','\1', $text); + $text = preg_replace('!([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)!','\1', $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)://[^\r\n\t ]*)!','[\1]', $text); + $text = preg_replace('!([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)!','[mailto:\1]', $text); + return $just ? justify($text,68) : wordwrap($text,68); + } +} + ?> diff --git a/templates/admin/newsletter_edit.tpl b/templates/admin/newsletter_edit.tpl index 98ae3f4..1125dc8 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.2 2004-10-16 21:14:15 x2000habouzit Exp $ + $Id: newsletter_edit.tpl,v 1.3 2004-10-16 22:06:33 x2000habouzit Exp $ ***************************************************************************} {dynamic} @@ -44,6 +44,14 @@ + + + Date d'envoi + + + + + @@ -120,7 +128,7 @@ Sujet - + -- 2.1.4