5 * @copyright Copyright (c) 2008, Bellière Ludovic
6 * @license http://opensource.org/licenses/mit-license.php MIT license
9 class News_Node_Exception
extends Exception
{}
12 * @author Bellière Ludovic
15 * @copyright Copyright (c) 2008, Bellière Ludovic
16 * @license http://opensource.org/licenses/mit-license.php MIT license
18 class news_node
implements Countable
{
20 protected $_content = array();
21 protected $_count = 0;
22 private $_filters = array();
23 private $_requiered_keynode;
27 const DEFAULT_VIEW
= 0xEFFFF69C;
28 const SEARCH_VIEW
= 0xE925030B;
29 const ARCHIVE_VIEW
= 0x9A2B1B01;
31 const FORMAT_LIGHT
= 'formatLight';
32 const FORMAT_FULL
= 'formatFull';
34 public $_default_view_options = array(
35 'sql_limit' => '0, 10',
38 function __construct() {
39 $this->_view
= self
::DEFAULT_VIEW
;
42 public function setView($view) {
46 public function add_nodes(array $content) {
47 foreach ($content as $node) {
48 if (!isset($this->_content
['n-'.$node['id']])) {
49 $this->_content
['n-'.$node['id']] = $node;
55 public function setPDO (PDO
$pdo) {
59 public function get ($key, $raw=false) {
61 foreach ($key as $parent_key => $child_key) {
62 if (array_key_exists($child_key,$this->_content
[$parent_key])) {
63 return (!is_array($this->_content
[$parent_key]) && $raw == true) ? self
::escape($this->_content
[$parent_key]) : $this->_content
[$parent_key];
68 if (array_key_exists($key,$this->_content
)) {
69 return (!is_array($this->_content
[$parent_key]) && $raw == true) ? self
::escape($this->_content
[$parent_key]) : $this->_content
[$parent_key];
74 * Add a filter function for the node $node
76 * @param string $function
78 * @param integer $place
80 * @throws News_Node_Exception for invalid type / not a function
82 public function add_filter($function,$node='all',$place=null) {
83 if (is_callable($function,true)) {
84 if (is_null($place)) {
85 $this->_filters
[$node][] = $function;
87 if (!is_int($place)) {
88 throw new News_Node_Exception('The param `place\' is not an integer');
91 if (isset($this->_filters
[$place])) {
92 $for = (is_array($function)) ?
$function[0].'->'.$function[1] : $function ;
93 trigger_error('Replacement of filter number '.$place.' for content <em>'.$for.'</em>',E_USER_NOTICE
);
96 $this->_filters
[$node][$place] = $function;
99 throw new News_Node_Exception('The <em>function</em> parameter "'.$function.'" is not callable.');
103 public function render($reload=false,$paginate=false) {
105 self
::getNews($reload,$paginate);
106 } catch (News_Node_Exception
$e) {
107 die ($e->getMessage());
111 $txt = self
::reloadCache();
113 foreach ($this->_content
as $node) {
114 $txt.= $node['data'];
120 protected function escape ($content,$tag) {
121 foreach($this->_filters
as $node => $filters) {
122 if ($node == 'all' ||
$tag == $node) {
123 foreach ($filters as $filter) {
124 $content = call_user_func($filter,$content);
131 private function getNews ($reload=false,$paginate=false) {
133 $cacheData = scandir(CACHE_PATH
);
134 // custom action for ARCHIVE_VIEW
135 if ($this->_view
== self
::ARCHIVE_VIEW
) {
136 $elements = $paginate->elements
;
137 $limit = explode(', ',str_replace('LIMIT ','',$paginate->get_sql_limit_statement()));
140 foreach ($cacheData as $file) {
141 if ($file[0] == '.') {
144 $id = substr($file,2);
145 // custom action for ARCHIVE_VIEW
146 if ($this->_view
== self
::ARCHIVE_VIEW
) {
147 if ($id-1 < intval($limit[0])) {
150 if ($id-1 > intval($limit[1])) {
155 if ($format == self
::FORMAT_LIGHT
) {
158 $allnodes[] = array (
161 'data' => file_get_contents(CACHE_PATH
.$file)
164 if (!isset($allnodes)) {
165 $this->_content
[]['data'] = '<p>no news in database</p>';
167 $allnodes = array_reverse($allnodes);
168 self
::add_nodes($allnodes);
171 switch ($this->_view
) {
172 case self
::DEFAULT_VIEW
:
173 $PDOStatement = $this->_PDO
->query('SELECT * FROM news ORDER BY id DESC LIMIT '.$this->_default_view_options
['sql_limit']);
175 case self
::SEARCH_VIEW
:
177 case self
::ARCHIVE_VIEW
:
178 if ($format == self
::FORMAT_FULL
) {
179 $PDOStatement = $this->_PDO
->query('SELECT * FROM news ORDER BY id DESC LIMIT '.$this->_default_view_options
['sql_limit']);
181 if ($paginate instanceof paginate
) {
182 $PDOStatement = $this->_PDO
->query('SELECT * FROM news ORDER BY id DESC '.$paginate->get_sql_limit_statement());
184 throw new News_Node_Exception('$paginate is not a instance of paginate object.');
189 throw new News_Node_Exception('View \''.$this->_view
.'\' not registered.');
191 self
::add_nodes($PDOStatement->fetchAll());
196 * Make all files in cache directory
198 private function reloadCache() {
200 foreach($this->_content
as $key => $data) {
202 $txt_tmp = '<h2 id="'.$key.'">'.self
::escape($data['title'],'title').'</h2>'."\n";
203 $txt_tmp.= '<p><sup><a href="#'.$key.'">#'.$key.'</a> On '.date('r',$data['postedon']).' by '.self
::escape($data['author'],'author')."</sup></p>\n";
204 $txt_tmp.= ''.self
::escape($data['text'],'text')."\n";
206 file_put_contents(CACHE_PATH
.$key.'.minimal',$min_tmp);
207 file_put_contents(CACHE_PATH
.$key,$txt_tmp);
209 if ($format == self
::FORMAT_FULL
) {
215 unset($txt_tmp,$min_tmp);
224 public function __get($key) {
225 return $this->_content
[$key];
228 protected function __isset($name) {
229 return isset($this->_content
[$name]);
232 public function count() {
233 return $this->_count
;