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 public $_default_view_options = array(
32 'sql_limit' => '0, 10',
35 function __construct() {
36 $this->_view
= self
::DEFAULT_VIEW
;
39 public function setView($view) {
43 public function add_nodes(array $content) {
44 foreach ($content as $node) {
45 if (!isset($this->_content
['n-'.$node['id']])) {
46 $this->_content
['n-'.$node['id']] = $node;
52 public function setPDO (PDO
$pdo) {
56 public function get ($key, $raw=false) {
58 foreach ($key as $parent_key => $child_key) {
59 if (array_key_exists($child_key,$this->_content
[$parent_key])) {
60 return (!is_array($this->_content
[$parent_key]) && $raw == true) ? self
::escape($this->_content
[$parent_key]) : $this->_content
[$parent_key];
65 if (array_key_exists($key,$this->_content
)) {
66 return (!is_array($this->_content
[$parent_key]) && $raw == true) ? self
::escape($this->_content
[$parent_key]) : $this->_content
[$parent_key];
71 * Add a filter function for the node $node
73 * @param string $function
75 * @param integer $place
77 * @throws News_Node_Exception for invalid type / not a function
79 public function add_filter($function,$node='all',$place=null) {
80 if (is_callable($function,true)) {
81 if (is_null($place)) {
82 $this->_filters
[$node][] = $function;
84 if (!is_int($place)) {
85 throw new News_Node_Exception('The param `place\' is not an integer');
88 if (isset($this->_filters
[$place])) {
89 $for = (is_array($function)) ?
$function[0].'->'.$function[1] : $function ;
90 trigger_error('Replacement of filter number '.$place.' for content <em>'.$for.'</em>',E_USER_NOTICE
);
93 $this->_filters
[$node][$place] = $function;
96 throw new News_Node_Exception('The <em>function</em> parameter "'.$function.'" is not callable.');
100 public function render($reload=false,$paginate=false) {
102 self
::getNews($reload,$paginate);
103 } catch (News_Node_Exception
$e) {
104 die ($e->getMessage());
108 $txt = self
::reloadCache();
110 foreach ($this->_content
as $node) {
111 $txt.= $node['data'];
117 protected function escape ($content,$tag) {
118 foreach($this->_filters
as $node => $filters) {
119 if ($node == 'all' ||
$tag == $node) {
120 foreach ($filters as $filter) {
121 $content = call_user_func($filter,$content);
128 private function getNews ($reload=false,$paginate=false) {
130 $cacheData = scandir(CACHE_PATH
);
131 // custom action for ARCHIVE_VIEW
132 if ($this->_view
== self
::ARCHIVE_VIEW
) {
133 $elements = $paginate->elements
;
134 $limit = explode(', ',str_replace('LIMIT ','',$paginate->get_sql_limit_statement()));
137 foreach ($cacheData as $file) {
138 if ($file[0] == '.') {
141 $id = substr($file,2);
142 // custom action for ARCHIVE_VIEW
143 if ($this->_view
== self
::ARCHIVE_VIEW
) {
144 if ($id-1 < intval($limit[0])) {
147 if ($id-1 > intval($limit[1])) {
152 $allnodes[] = array (
155 'data' => file_get_contents(CACHE_PATH
.$file)
158 if (!isset($allnodes)) {
159 $this->_content
[]['data'] = '<p>no news in database</p>';
161 $allnodes = array_reverse($allnodes);
162 self
::add_nodes($allnodes);
165 switch ($this->_view
) {
166 case self
::DEFAULT_VIEW
:
167 $PDOStatement = $this->_PDO
->query('SELECT * FROM news ORDER BY id DESC LIMIT '.$this->_default_view_options
['sql_limit']);
169 case self
::SEARCH_VIEW
:
171 case self
::ARCHIVE_VIEW
:
172 if ($paginate instanceof paginate
) {
173 $PDOStatement = $this->_PDO
->query('SELECT * FROM news ORDER BY id DESC '.$paginate->get_sql_limit_statement());
175 throw new News_Node_Exception('$paginate is not a instance of paginate object.');
179 throw new News_Node_Exception('View \''.$this->_view
.'\' not registered.');
181 self
::add_nodes($PDOStatement->fetchAll());
186 * Make all files in cache directory
188 private function reloadCache() {
190 foreach($this->_content
as $key => $data) {
191 $txt_tmp = '<h2 id="'.$key.'">'.self
::escape($data['title'],'title').'</h2>'."\n";
192 $txt_tmp.= '<p><sup><a href="#'.$key.'">#'.$key.'</a> On '.date('r',$data['postedon']).' by '.self
::escape($data['author'],'author')."</sup></p>\n";
193 $txt_tmp.= ''.self
::escape($data['text'],'text')."\n";
194 file_put_contents(CACHE_PATH
.$key,$txt_tmp);
205 public function __get($key) {
206 return $this->_content
[$key];
209 protected function __isset($name) {
210 return isset($this->_content
[$name]);
213 public function count() {
214 return $this->_count
;