Backport
[platal.git] / include / newsletter.inc.php
index 9fb1ec4..b7bba9c 100644 (file)
 // {{{ requires + defines
 
 require_once("xorg.misc.inc.php");
-require_once("diogenes/diogenes.misc.inc.php");
 
 if (isset($page)) {
-    $page->addCssLink('css/nl.css');
+    $page->addCssLink('nl.css');
 }
 
 define('FEMME', 1);
@@ -50,17 +49,19 @@ class NewsLetter
     
     function NewsLetter($id=null)
     {
-       global $globals;
-
        if (isset($id)) {
            if ($id == 'last') {
-               $res = $globals->xdb->query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
+               $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
                 $id  = $res->fetchOneCell();
            }
-           $res = $globals->xdb->query("SELECT * FROM newsletter WHERE id={?}", $id);
+           $res = XDB::query("SELECT * FROM newsletter WHERE id={?}", $id);
        } else {
-           $res = $globals->xdb->query("SELECT * FROM newsletter WHERE bits='new'");
-       }
+           $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
+        if (!$res->numRows()) {
+            insert_new_nl();
+        }
+        $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
+    }
        $nl = $res->fetchOneAssoc();
 
        $this->_id    = $nl['id'];
@@ -68,12 +69,12 @@ class NewsLetter
        $this->_title = $nl['titre'];
        $this->_head  = $nl['head'];
 
-       $res = $globals->xdb->iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
+       $res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
        while (list($cid, $title) = $res->next()) {
            $this->_cats[$cid] = $title;
        }
        
-       $res = $globals->xdb->iterRow(
+       $res = XDB::iterRow(
                 "SELECT  a.title,a.body,a.append,a.aid,a.cid,a.pos
                    FROM  newsletter_art AS a
              INNER JOIN  newsletter     AS n USING(id)
@@ -90,8 +91,7 @@ class NewsLetter
 
     function setSent()
     {
-       global $globals;
-       $globals->xdb->execute("UPDATE  newsletter SET bits='sent' WHERE id={?}", $this->_id);
+           XDB::execute("UPDATE  newsletter SET bits='sent' WHERE id={?}", $this->_id);
     }
 
     // }}}
@@ -99,9 +99,8 @@ class NewsLetter
 
     function save()
     {
-       global $globals;
-       $globals->xdb->execute('UPDATE newsletter SET date={?},titre={?},head={?} WHERE id={?}',
-                $this->_date, $this->_title, $this->_head, $this->_id);
+       XDB::execute('UPDATE newsletter SET date={?},titre={?},head={?} WHERE id={?}',
+                     $this->_date, $this->_title, $this->_head, $this->_id);
     }
 
     // }}}
@@ -134,15 +133,14 @@ class NewsLetter
 
     function saveArticle(&$a)
     {
-       global $globals;
        if ($a->_aid>=0) {
-           $globals->xdb->execute('REPLACE INTO  newsletter_art (id,aid,cid,pos,title,body,append)
+           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 {
-           $globals->xdb->execute(
+           XDB::execute(
                'INSERT INTO  newsletter_art
                      SELECT  {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
                        FROM  newsletter_art AS a
@@ -157,8 +155,7 @@ class NewsLetter
     
     function delArticle($aid)
     {
-       global $globals;
-       $globals->xdb->execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $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"]);
        }
@@ -237,15 +234,16 @@ class NewsLetter
     // }}}
     // {{{ function toHtml()
     
-    function toHtml($prenom,$nom,$sexe,$body=false)
+    function toHtml($prenom, $nom, $sexe, $body=false, $urlprefix = false)
     {
+        $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);
+       $head = enriched_to_text($head, true);
 
        if($head) {
             $res .= "<div class='intro'>$head</div>";
@@ -254,9 +252,9 @@ class NewsLetter
        $i = 1;
        $res .= "<a id='top_lnk'></a>";
        foreach ($this->_arts as $cid=>$arts) {
-           $res .= "<div class='lnk'><a href='#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
+           $res .= "<div class='lnk'><a href='$u#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
            foreach ($arts as $art) {
-               $res .= "<a href='#art{$art->_aid}'>&nbsp;&nbsp;- ".htmlentities($art->title())."</a>";
+               $res .= "<a href='$u#art{$art->_aid}'>&nbsp;&nbsp;- ".htmlentities($art->title())."</a>";
            }
            $res .= '</div>';
            $i ++;
@@ -266,7 +264,7 @@ class NewsLetter
            $res .= "<h1><a id='cat$cid'></a><span>".$this->_cats[$cid].'</span></h1>';
            foreach($arts as $art) {
                $res .= $art->toHtml();
-               $res .= "<p><a href='#top_lnk'>Revenir au sommaire</a></p>";
+               $res .= "<p><a href='$u#top_lnk'>Revenir au sommaire</a></p>";
            }
        }
 
@@ -440,43 +438,37 @@ class NLArticle
 
 function insert_new_nl()
 {
-    global $globals;
-    $globals->xdb->execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued'");
+    XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued'");
 }
 
 function get_nl_slist()
 {
-    global $globals;
-    $res = $globals->xdb->query("SELECT id,date,titre FROM newsletter ORDER BY date DESC");
+    $res = XDB::query("SELECT id,date,titre FROM newsletter ORDER BY date DESC");
     return $res->fetchAllAssoc();
 }
 
 function get_nl_list()
 {
-    global $globals;
-    $res = $globals->xdb->query("SELECT id,date,titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
+    $res = XDB::query("SELECT id,date,titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
     return $res->fetchAllAssoc();
 }
 
 function get_nl_state()
 {
-    global $globals;
-    $res = $globals->xdb->query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', Session::getInt('uid'));
+    $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
     return $res->fetchOneCell();
 }
  
 function unsubscribe_nl()
 {
-    global $globals;
-    $globals->xdb->execute('DELETE FROM newsletter_ins WHERE user_id={?}', Session::getInt('uid'));
+    XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
 }
  
 function subscribe_nl($uid=-1)
 {
-    global $globals;
-    $user = ($uid == -1) ? Session::getInt('uid') : $uid;
-    $globals->xdb->execute('REPLACE INTO  newsletter_ins (user_id,last)
-                                 VALUES  ({?}, 0)', $user);
+    $user = ($uid == -1) ? S::v('uid') : $uid;
+    XDB::execute('REPLACE INTO  newsletter_ins (user_id,last)
+                        VALUES  ({?}, 0)', $user);
 }
  
 function justify($text,$n)