last changes
[ayans.git] / includes / news-node.php
blobfc76ad0b804fc0e6e6df55e2e21fa65a921643e4
1 <?php
2 /**
3 * @category News
4 * @package News_Node
5 * @copyright Copyright (c) 2008, Bellière Ludovic
6 * @license http://opensource.org/licenses/mit-license.php MIT license
7 */
9 class News_Node_Exception extends Exception {}
11 /**
12 * @author Bellière Ludovic
13 * @category News
14 * @package News_Node
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;
24 private $_PDO;
25 private $_view;
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) {
40 $this->_view = $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;
47 $this->_count++;
52 public function setPDO (PDO $pdo) {
53 $this->_PDO = $pdo;
56 public function get ($key, $raw=false) {
57 if (is_array($key)) {
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];
70 /**
71 * Add a filter function for the node $node
73 * @param string $function
74 * @param string $node
75 * @param integer $place
76 * @return void
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;
83 } else {
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;
95 } else {
96 throw new News_Node_Exception('The <em>function</em> parameter "'.$function.'" is not callable.');
100 public function render($reload=false,$paginate=false) {
101 try {
102 self::getNews($reload,$paginate);
103 } catch (News_Node_Exception $e) {
104 die ($e->getMessage());
106 $txt = '';
107 if ($reload) {
108 $txt = self::reloadCache();
109 } else {
110 foreach ($this->_content as $node) {
111 $txt.= $node['data'];
114 return $txt;
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);
125 return $content;
128 private function getNews ($reload=false,$paginate=false) {
129 if (!$reload) {
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()));
136 // ---
137 foreach ($cacheData as $file) {
138 if ($file[0] == '.') {
139 continue;
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])) {
145 continue;
147 if ($id-1 > intval($limit[1])) {
148 break;
151 // ---
152 $allnodes[] = array (
153 'id' => $id,
154 'type' => 'cache',
155 'data' => file_get_contents(CACHE_PATH.$file)
158 if (!isset($allnodes)) {
159 $this->_content[]['data'] = '<p>no news in database</p>';
160 } else {
161 $allnodes = array_reverse($allnodes);
162 self::add_nodes($allnodes);
164 } else {
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']);
168 break;
169 case self::SEARCH_VIEW:
170 break;
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());
174 } else {
175 throw new News_Node_Exception('$paginate is not a instance of paginate object.');
177 break;
178 default:
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() {
189 $txt='';
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);
195 $txt.=$txt_tmp;
196 unset($txt_tmp);
198 return $txt;
201 ///////
202 // overload
203 ///////
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;