Backport
[platal.git] / include / newsletter.inc.php
index 3fa2a2c..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);
@@ -58,7 +57,11 @@ class NewsLetter
            $res = XDB::query("SELECT * FROM newsletter WHERE id={?}", $id);
        } else {
            $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'];
@@ -88,7 +91,7 @@ class NewsLetter
 
     function setSent()
     {
-       XDB::execute("UPDATE  newsletter SET bits='sent' WHERE id={?}", $this->_id);
+           XDB::execute("UPDATE  newsletter SET bits='sent' WHERE id={?}", $this->_id);
     }
 
     // }}}
@@ -231,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>";
@@ -248,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 ++;
@@ -260,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>";
            }
        }
 
@@ -451,18 +455,18 @@ function get_nl_list()
 
 function get_nl_state()
 {
-    $res = 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()
 {
-    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)
 {
-    $user = ($uid == -1) ? Session::getInt('uid') : $uid;
+    $user = ($uid == -1) ? S::v('uid') : $uid;
     XDB::execute('REPLACE INTO  newsletter_ins (user_id,last)
                         VALUES  ({?}, 0)', $user);
 }