code beautiful
authorPierre Habouzit (MadCoder <pierre.habouzit@m4x.org>
Fri, 3 Dec 2004 12:48:55 +0000 (12:48 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 26 Jun 2008 21:26:26 +0000 (23:26 +0200)
git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-30

include/newsletter.inc.php

index a18cfe1..e8b9b2e 100644 (file)
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-define('FEMME', 1);
-define('HOMME', 0);
+// {{{ requires + defines
 
 require_once("xorg.misc.inc.php");
 require_once("diogenes.misc.inc.php");
 
-class NewsLetter {
+define('FEMME', 1);
+define('HOMME', 0);
+
+// }}}
+// {{{ class NewsLetter
+
+class NewsLetter
+{
+    // {{{ properties
+    
     var $_id;
     var $_date;
     var $_title;
     var $_head;
     var $_cats = Array();
     var $_arts = Array();
+
+    // }}}
+    // {{{ constructor
     
-    function NewsLetter($id=null) {
+    function NewsLetter($id=null)
+    {
        global $globals;
 
-       if(isset($id)) {
-           if($id == 'last') {
+       if (isset($id)) {
+           if ($id == 'last') {
                $res = $globals->db->query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
                list($id) = mysql_fetch_row($res);
            }
@@ -46,14 +58,15 @@ class NewsLetter {
            $res = $globals->db->query("SELECT * FROM newsletter WHERE bits='new'");
        }
        $nl = mysql_fetch_assoc($res);
-       $this->_id = $nl['id'];
-       $this->_date = $nl['date'];
-       $this->_title = $nl['titre'];
-       $this->_head = $nl['head'];
        mysql_free_result($res);
 
+       $this->_id    = $nl['id'];
+       $this->_date  = $nl['date'];
+       $this->_title = $nl['titre'];
+       $this->_head  = $nl['head'];
+
        $res = $globals->db->query("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
-       while(list($cid,$title) = mysql_fetch_row($res)) {
+       while (list($cid, $title) = mysql_fetch_row($res)) {
            $this->_cats[$cid] = $title;
        }
        mysql_free_result($res);
@@ -64,39 +77,64 @@ class NewsLetter {
                                LEFT  JOIN  newsletter_cat AS c ON(a.cid=c.cid)
                                     WHERE  a.id={$this->_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);
+       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);
        }
        mysql_free_result($res);
     }
 
-    function setSent() {
+    // }}}
+    // {{{ function setSent()
+
+    function setSent()
+    {
        global $globals;
        $globals->db->query("UPDATE  newsletter SET bits='sent' WHERE id='{$this->_id}'");
     }
 
-    function save() {
+    // }}}
+    // {{{ function save()
+
+    function save()
+    {
        global $globals;
        $globals->db->query("UPDATE  newsletter
                                SET  date='{$this->_date}',titre='{$this->_title}',head='{$this->_head}'
                              WHERE  id='{$this->_id}'");
     }
 
-    function title() {
-       return stripslashes($this->_title);
-    }
-    function head() { return stripslashes($this->_head); }
+    // }}}
+    // {{{ function title()
+
+    function title()
+    { return stripslashes($this->_title); }
 
-    function getArt($aid) {
-       foreach($this->_arts as $key=>$artlist) {
-           if(isset($artlist["a$aid"])) return $artlist["a$aid"];
+    // }}}
+    // {{{ function head()
+    
+    function head()
+    { return stripslashes($this->_head); }
+
+    // }}}
+    // {{{ function getArt()
+    
+    function getArt($aid)
+    {
+       foreach ($this->_arts as $key=>$artlist) {
+           if (isset($artlist["a$aid"])) {
+                return $artlist["a$aid"];
+            }
        }
        return null;
     }
 
-    function saveArticle(&$a) {
+    // }}}
+    // {{{ function saveArticle()
+
+    function saveArticle(&$a)
+    {
        global $globals;
-       if($a->_aid>=0) {
+       if ($a->_aid>=0) {
            $globals->db->query("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}')");
@@ -113,16 +151,24 @@ class NewsLetter {
        }
     }
 
-    function delArticle($aid) {
+    // }}}
+    // {{{ function delArticle()
+    
+    function delArticle($aid)
+    {
        global $globals;
        $globals->db->query("DELETE FROM newsletter_art WHERE id='{$this->_id}' AND aid='$aid'");
-       foreach($this->_arts as $key=>$art) {
+       foreach ($this->_arts as $key=>$art) {
            unset($this->_arts[$key]["a$aid"]);
        }
     }
 
-    function footer($html) {
-       if($html) {
+    // }}}
+    // {{{ function footer
+
+    function footer($html)
+    {
+       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="http://www.polytechnique.org/newsletter/">archives</a>&nbsp;|&nbsp;'
@@ -142,7 +188,11 @@ class NewsLetter {
        }
     }
 
-    function toText($prenom,$nom,$sexe) {
+    // }}}
+    // {{{ function toText()
+
+    function toText($prenom,$nom,$sexe)
+    {
        $res  = "====================================================================\n";
        $res .= ' '.$this->title()."\n";
        $res .= "====================================================================\n\n";
@@ -153,23 +203,25 @@ class NewsLetter {
        $head = str_replace('<nom>',    $nom,    $head);
        $head = enriched_to_text($head,false,true,2,64);
 
-       if($head) $res .= "\n$head\n\n\n";
+       if ($head) {
+            $res .= "\n$head\n\n\n";
+        }
 
        $i = 1;
-       foreach($this->_arts as $cid=>$arts) {
+       foreach ($this->_arts as $cid=>$arts) {
            $res .= "\n$i *{$this->_cats[$cid]}*\n";
-           foreach($arts as $art) {
+           foreach ($arts as $art) {
                $res .= '- '.$art->title()."\n";
            }
            $i ++;
        }
        $res .= "\n\n";
            
-       foreach($this->_arts as $cid=>$arts) {
+       foreach ($this->_arts as $cid=>$arts) {
            $res .= "--------------------------------------------------------------------\n";
            $res .= "*{$this->_cats[$cid]}*\n";
            $res .= "--------------------------------------------------------------------\n\n";
-           foreach($arts as $art) {
+           foreach ($arts as $art) {
                $res .= $art->toText();
                $res .= "\n\n";
            }
@@ -179,8 +231,12 @@ class NewsLetter {
        
        return $res;
     }
+
+    // }}}
+    // {{{ function toHtml()
     
-    function toHtml($prenom,$nom,$sexe,$body=false) {
+    function toHtml($prenom,$nom,$sexe,$body=false)
+    {
        $res  = '<div class="title">'.$this->title().'</div>';
        
        $head = $this->head();
@@ -189,20 +245,21 @@ class NewsLetter {
        $head = str_replace('<nom>',    $nom,    $head);
        $head = enriched_to_text($head,true);
 
-       if($head) $res .= "<div class='intro'>$head</div>";
-
+       if($head) {
+            $res .= "<div class='intro'>$head</div>";
+        }
 
        $i = 1;
-       foreach($this->_arts as $cid=>$arts) {
+       foreach ($this->_arts as $cid=>$arts) {
            $res .= "<div class='lnk'><a href='#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
-           foreach($arts as $art) {
+           foreach ($arts as $art) {
                $res .= "<a href='#art{$art->_aid}'>&nbsp;&nbsp;- ".htmlentities($art->title())."</a>";
            }
            $res .= '</div>';
            $i ++;
        }
 
-       foreach($this->_arts as $cid=>$arts) {
+       foreach ($this->_arts as $cid=>$arts) {
            $res .= "<h1><a id='cat$cid'></a><span>".$this->_cats[$cid].'</span></h1>';
            foreach($arts as $art) {
                $res .= $art->toHtml();
@@ -211,7 +268,7 @@ class NewsLetter {
 
        $res .= $this->footer(true);
 
-       if($body) {
+       if ($body) {
            $res = <<<EOF
 <html>
   <head>
@@ -251,8 +308,12 @@ EOF;
        }
        return $res;
     }
+
+    // }}}
+    // {{{ function sendTo()
     
-    function sendTo($prenom,$nom,$login,$sex,$html) {
+    function sendTo($prenom, $nom, $login, $sex, $html)
+    {
         global $globals;
        require_once('diogenes.hermes.inc.php');
 
@@ -262,104 +323,161 @@ EOF;
        $mailer->addTo("\"$prenom $nom\" <$login@polytechnique.org>");
        $mailer->addHeader('Reply-To',$globals->newsletter->replyto);
        $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
-       if($html) {
+       if ($html) {
            $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true));
        }
        $mailer->send();
     }
+
+    // }}}
 }
 
-class NLArticle {
+// }}}
+// {{{ class NLArticle
+
+class NLArticle
+{
+    // {{{ properties
+    
     var $_aid;
     var $_cid;
     var $_pos;
     var $_title;
     var $_body;
     var $_append;
+
+    // }}}
+    // {{{ constructor
     
-    function NLArticle($title='',$body='',$append='',$aid=-1,$cid=0,$pos=0) {
+    function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
+    {
        $this->_body   = $body;
        $this->_title  = $title;
        $this->_append = $append;
-       $this->_aid = $aid;
-       $this->_cid = $cid;
-       $this->_pos = $pos;
+       $this->_aid    = $aid;
+       $this->_cid    = $cid;
+       $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 title()
+
+    function title()
+    { return stripslashes(trim($this->_title)); }
 
-    function toText() {
+    // }}}
+    // {{{ function body()
+    
+    function body()
+    { return stripslashes(trim($this->_body)); }
+    
+    // }}}
+    // {{{ function append()
+    
+    function append()
+    { return stripslashes(trim($this->_append)); }
+
+    // }}}
+    // {{{ function toText()
+
+    function toText()
+    {
        $title = '*'.$this->title().'*';
        $body  = enriched_to_text($this->_body,false,true);
        $app   = enriched_to_text($this->_append,false,false,4);
        return trim("$title\n\n$body\n\n$app")."\n";
     }
 
-    function toHtml() {
+    // }}}
+    // {{{ function toHtml()
+
+    function toHtml()
+    {
        $title = "<h2><a id='art{$this->_aid}'></a><span>".htmlentities($this->title()).'</span></h2>';
        $body  = enriched_to_text($this->_body,true);
        $app   = enriched_to_text($this->_append,true);
        
-       $art  = "$title\n";
-       $art .= "<div class='art'>\n$body\n";
-       if ($app) $art .= "<div class='app'>$app</div>";
-       $art .= "</div>\n";
+       $art   = "$title\n";
+       $art  .= "<div class='art'>\n$body\n";
+       if ($app) {
+            $art .= "<div class='app'>$app</div>";
+        }
+       $art  .= "</div>\n";
        
        return $art;
     }
 
-    function check() {
+    // }}}
+    // {{{ function check()
+
+    function check()
+    {
        $text = enriched_to_text($this->_body);
-       $arr = explode("\n",wordwrap($text,68));
-       $c = 0;
-       foreach($arr as $line) if(trim($line)) $c++;
+       $arr  = explode("\n",wordwrap($text,68));
+       $c    = 0;
+       foreach ($arr as $line) {
+            if (trim($line)) {
+                $c++;
+            }
+        }
        return $c<9;
     }
 
+    // }}}
 }
 
-/////////////////////////
-// functions ............
-//
-function insert_new_nl() {
+// }}}
+// {{{ Functions
+
+function insert_new_nl()
+{
     global $globals;
     $globals->db->query("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued'");
 }
 
-function get_nl_slist() {
+function get_nl_slist()
+{
     global $globals;
     $res = $globals->db->query("SELECT id,date,titre FROM newsletter ORDER BY date DESC");
     $ans = Array();
-    while($tmp = mysql_fetch_assoc($res)) $ans[] = $tmp;
+    while ($tmp = mysql_fetch_assoc($res)) {
+        $ans[] = $tmp;
+    }
     mysql_free_result($res);
     return $ans;
 }
 
-function get_nl_list() {
+function get_nl_list()
+{
     global $globals;
     $res = $globals->db->query("SELECT id,date,titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
     $ans = Array();
-    while($tmp = mysql_fetch_assoc($res)) $ans[] = $tmp;
+    while ($tmp = mysql_fetch_assoc($res)) {
+        $ans[] = $tmp;
+    }
     mysql_free_result($res);
     return $ans;
 }
 
-function get_nl_state() {
+function get_nl_state()
+{
     global $globals;
     $res = $globals->db->query("SELECT pref FROM newsletter_ins WHERE user_id={$_SESSION['uid']}");
-    if(!(list($st) = mysql_fetch_row($res))) $st = false;
+    if (!(list($st) = mysql_fetch_row($res))) {
+        $st = false;
+    }
     mysql_free_result($res);
     return $st;
 }
  
-function unsubscribe_nl() {
+function unsubscribe_nl()
+{
     global $globals;
     $globals->db->query("DELETE FROM newsletter_ins WHERE user_id={$_SESSION['uid']}");
 }
  
-function subscribe_nl($html=true) {
+function subscribe_nl($html=true)
+{
     global $globals;
     $format = $html ? 'html' : 'text';
     $globals->db->query("REPLACE INTO  newsletter_ins (user_id,last,pref)
@@ -367,53 +485,54 @@ function subscribe_nl($html=true) {
                                 FROM  newsletter WHERE bits!='new'");
 }
  
-function justify($text,$n) {
+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]) : '';
+    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);
+       $nxw_len   = count($nxl_split) ? strlen($nxl_split[0]) : 0;
+       $line      = trim($line);
 
-       if(strlen($line)+1+$nxw_len < $n) {
+       if (strlen($line)+1+$nxw_len < $n) {
            $res .= "$line\n";
            continue;
        }
        
-       if(preg_match('![.:;]$!',$line)) {
+       if (preg_match('![.:;]$!',$line)) {
            $res .= "$line\n";
            continue;
        }
 
-       $tmp = preg_split('! +!',trim($line));
+       $tmp   = preg_split('! +!',trim($line));
        $words = count($tmp);
-       if($words <= 1) {
+       if ($words <= 1) {
            $res .= "$line\n";
            continue;
        }
 
-       $len = array_sum(array_map('strlen',$tmp));
+       $len   = array_sum(array_map('strlen',$tmp));
        $empty = $n - $len;
-       $sw = floatval($empty) / floatval($words-1);
+       $sw    = floatval($empty) / floatval($words-1);
        
        $cur = 0;
-       $l = '';
-       foreach($tmp as $word) {
-           $l .= $word;
+       $l   = '';
+       foreach ($tmp as $word) {
+           $l   .= $word;
            $cur += $sw + strlen($word);
-           $l = str_pad($l,intval($cur+0.5));
+           $l    = str_pad($l,intval($cur+0.5));
        }
        $res .= trim($l)."\n";
     }
     return trim($res);
 }
 
-
-function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68) {
+function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
+{
     $text = stripslashes(trim($input));
-    if($html) {
+    if ($html) {
        $text = htmlspecialchars($text);
        $text = str_replace('[b]','<strong>', $text);
        $text = str_replace('[/b]','</strong>', $text);
@@ -438,4 +557,7 @@ function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68) {
        return $text;
     }
 }
+
+// }}}
+
 ?>