fetchOneCell(); } $res = XDB::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id); } else { $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'"); if (!$res->numRows()) { Newsletter::create(); } $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'"); } $nl = $res->fetchOneAssoc(); $this->_id = $nl['id']; $this->_shortname = $nl['short_name']; $this->_date = $nl['date']; $this->_title = $nl['titre']; $this->_title_mail = $nl['titre_mail']; $this->_head = $nl['head']; $res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos"); while (list($cid, $title) = $res->next()) { $this->_cats[$cid] = $title; } $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) LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid) WHERE a.id={?} ORDER BY c.pos,a.pos", $this->_id); while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) { $this->_arts[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos); } } public function save() { XDB::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}', $this->_date, $this->_title, $this->_title_mail, $this->_head, $this->_shortname,$this->_id); } public function getArt($aid) { foreach ($this->_arts as $key=>$artlist) { if (isset($artlist["a$aid"])) { return $artlist["a$aid"]; } } return null; } public function saveArticle(&$a) { if ($a->_aid>=0) { 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 { XDB::execute('INSERT INTO newsletter_art SELECT {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?} FROM newsletter_art AS a WHERE a.id={?}', $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id); $this->_arts['a'.$a->_aid] = $a; } } public function delArticle($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"]); } } protected function assignData(&$smarty) { $smarty->assign_by_ref('nl', $this); } protected function setSent() { XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id); } static public function subscriptionState($uid = null) { $user = is_null($uid) ? S::v('uid') : $uid; $res = XDB::query("SELECT 1 FROM newsletter_ins WHERE user_id={?}", $user); return $res->fetchOneCell(); } static public function unsubscribe($uid = null) { $user = is_null($uid) ? S::v('uid') : $uid; XDB::execute("DELETE FROM newsletter_ins WHERE user_id={?}", $user); } static public function subscribe($uid = null) { $user = is_null($uid) ? S::v('uid') : $uid; XDB::execute("REPLACE INTO newsletter_ins (user_id,last) VALUES ({?}, 0)", $user); } protected function subscriptionWhere() { return '1'; } static public function create() { XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'"); } static public function listSent() { $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter WHERE bits!='new' ORDER BY date DESC"); return $res->fetchAllAssoc(); } static public function listAll() { $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter ORDER BY date DESC"); return $res->fetchAllAssoc(); } } // }}} // {{{ class NLArticle class NLArticle { // {{{ properties var $_aid; var $_cid; var $_pos; var $_title; var $_body; var $_append; // }}} // {{{ constructor function __construct($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; } // }}} // {{{ function title() public function title() { return trim($this->_title); } // }}} // {{{ function body() public function body() { return trim($this->_body); } // }}} // {{{ function append() public function append() { return trim($this->_append); } // }}} // {{{ function toText() public function toText($hash = null, $login = null) { $title = '*'.$this->title().'*'; $body = MiniWiki::WikiToText($this->_body, true); $app = MiniWiki::WikiToText($this->_append,false,4); $text = trim("$title\n\n$body\n\n$app")."\n"; if (!is_null($hash) && !is_null($login)) { $text = str_replace('%HASH%', "$hash/$login", $text); } else { $text = str_replace('%HASH%', '', $text); } return $text; } // }}} // {{{ function toHtml() public function toHtml($hash = null, $login = null) { $title = "

".pl_entities($this->title()).'

'; $body = MiniWiki::WikiToHTML($this->_body); $app = MiniWiki::WikiToHTML($this->_append); $art = "$title\n"; $art .= "
\n$body\n"; if ($app) { $art .= "
$app
"; } $art .= "
\n"; if (!is_null($hash) && !is_null($login)) { $art = str_replace('%HASH%', "$hash/$login", $art); } else { $art = str_replace('%HASH%', '', $art); } return $art; } // }}} // {{{ function check() public function check() { $text = MiniWiki::WikiToText($this->_body); $arr = explode("\n",wordwrap($text,68)); $c = 0; foreach ($arr as $line) { if (trim($line)) { $c++; } } return $c<9; } // }}} } // }}} // vim:set et sw=4 sts=4 sws=4 enc=utf-8: ?>