oversights
[ayans.git] / includes / news-node.php
blob82aaf354737aabb6ac5f6d6d66fb454f6c59025e
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 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) {
43 $this->_view = $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;
50 $this->_count++;
55 public function setPDO (PDO $pdo) {
56 $this->_PDO = $pdo;
59 public function get ($key, $raw=false) {
60 if (is_array($key)) {
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];
73 /**
74 * Add a filter function for the node $node
76 * @param string $function
77 * @param string $node
78 * @param integer $place
79 * @return void
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;
86 } else {
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;
98 } else {
99 throw new News_Node_Exception('The <em>function</em> parameter "'.$function.'" is not callable.');
103 public function render($reload=false,$paginate=false) {
104 try {
105 self::getNews($reload,$paginate);
106 } catch (News_Node_Exception $e) {
107 die ($e->getMessage());
109 $txt = '';
110 if ($reload) {
111 $txt = self::reloadCache();
112 } else {
113 foreach ($this->_content as $node) {
114 $txt.= $node['data'];
117 return $txt;
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);
128 return $content;
131 private function getNews ($reload=false,$paginate=false) {
132 if (!$reload) {
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()));
139 // ---
140 foreach ($cacheData as $file) {
141 if ($file[0] == '.') {
142 continue;
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])) {
148 continue;
150 if ($id-1 > intval($limit[1])) {
151 break;
154 // ---
155 if ($format == self::FORMAT_LIGHT) {
156 $file.= '.minimal';
158 $allnodes[] = array (
159 'id' => $id,
160 'type' => 'cache',
161 'data' => file_get_contents(CACHE_PATH.$file)
164 if (!isset($allnodes)) {
165 $this->_content[]['data'] = '<p>no news in database</p>';
166 } else {
167 $allnodes = array_reverse($allnodes);
168 self::add_nodes($allnodes);
170 } else {
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']);
174 break;
175 case self::SEARCH_VIEW:
176 break;
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']);
180 } else {
181 if ($paginate instanceof paginate) {
182 $PDOStatement = $this->_PDO->query('SELECT * FROM news ORDER BY id DESC '.$paginate->get_sql_limit_statement());
183 } else {
184 throw new News_Node_Exception('$paginate is not a instance of paginate object.');
187 break;
188 default:
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() {
199 $txt='';
200 foreach($this->_content as $key => $data) {
201 $min_tmp = '';
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) {
210 $txt.=$txt_tmp;
211 } else {
212 $txt.=$min_tmp;
215 unset($txt_tmp,$min_tmp);
217 return $txt;
220 ///////
221 // overload
222 ///////
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;