PageRenderTime 36ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/Blog/Blog.php

https://github.com/allanfreitas/CandyCMS
PHP | 552 lines | 374 code | 168 blank | 10 comment | 51 complexity | 140162f59b86ea4eec2826b769542d39 MD5 | raw file
  1<?php
  2
  3/**
  4 * @plugin Blog
  5 * @description A simple blog for CandyCMS. Use {{blog}}
  6 * @author Cocoon Design
  7 * @authorURI http://www.wearecocoon.co.uk/
  8 * @copyright 2012 (C) Cocoon Design  
  9 * @version 1.0
 10 * @since 0.1
 11 */
 12 
 13 class Blog {
 14 
 15 	public static function install() {
 16 		
 17 		CandyDB::q("CREATE TABLE IF NOT EXISTS ". DB_PREFIX ."posts (post_id INT(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY(post_id), post_title VARCHAR(64) NOT NULL, UNIQUE KEY (`post_title`), post_body TEXT NOT NULL, permalink TEXT NOT NULL, cat_id VARCHAR(256) NOT NULL, post_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, status TEXT NOT NULL)");
 18 		CandyDB::q("CREATE TABLE IF NOT EXISTS ". DB_PREFIX ."categories (cat_id INT(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (cat_id), cat_name VARCHAR(256), UNIQUE KEY (`cat_name`))");
 19 
 20 		CandyDB::q("INSERT INTO ".DB_PREFIX."options (option_key, option_value) VALUES (:key, :value)", array('key' => 'disqus', 'value' => ''));
 21 		CandyDB::q("INSERT INTO ".DB_PREFIX."options (option_key, option_value) VALUES (:key, :value)", array('key' => 'perpage', 'value' => 5));
 22
 23 	}
 24
 25 	public static function listCategories($cat_id){
 26 		
 27 		$cats = array();		
 28 		$cat_id = json_decode(stripslashes($cat_id));
 29 		
 30 		foreach ($cat_id as $value) {
 31 			$cats[] = CandyDB::col("SELECT cat_name FROM ".DB_PREFIX."categories WHERE cat_id = :id", array('id' => $value));
 32 		}
 33 		
 34 		$html = '';
 35 		
 36 		if (!empty($cats)) {
 37 			$html .= '<ul class="category-list">';
 38 			foreach ($cats as $value) {
 39 				$catlink = str_replace(' ', '-', strtolower($value));
 40 				$html .= '<li class="cat-'.strtolower($value).'"><a href="'.URL_PATH.self::getBlogPage().'/'.$catlink.'" title="'.$value.'">'.$value.'</a></li>';
 41 			}	
 42 			$html .= '</ul>';	
 43 		}
 44 		
 45 		echo $html;
 46 	
 47 	}
 48 
 49 	public static function adminNav(){
 50 		return array('blog' => 'Blog');
 51 	}
 52 
 53 	public static function postsTable(){
 54
 55 		$posts = CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts ORDER BY post_id DESC');
 56 		
 57 		$html = '<table>';
 58 		$html .= '<thead><tr><th>Post Title</th><th>Posted</th><th>Status</th><th></th><th></th></tr></thead>';
 59 		
 60 		foreach ($posts as $post) {
 61 			$html .= '<tr>';
 62 			$html .= '<td>'.$post->post_title.'</td>';
 63 			$html .= '<td>'.date('d/m/Y H:i:s', strtotime($post->post_date)).'</td>';
 64 			$html .= '<td>'.ucwords($post->status).'</td>';
 65 			$html .= '<td><a href="dashboard.php?page=blog&edit='.$post->post_id.'" title="Edit Page">Edit</a></td>';
 66 			$html .= '<td><a href="dashboard.php?page=blog&delete='.$post->post_id.'" title="'.$post->post_title.'" class="delete">[x]</a></td>';
 67 			$html .= '</tr>';	
 68 		}
 69 		
 70 		$html .= '</table>';
 71 		
 72 		echo $html;
 73 		
 74 	}
 75 	
 76 	public static function catsTable(){
 77 		
 78 		$cats = CandyDB::results('SELECT * FROM '.DB_PREFIX.'categories ORDER BY cat_id DESC');
 79 		
 80 		$html = '<table id="catstable">';
 81 		$html .= '<thead><tr><th>ID</th><th>Category Name</th><th></th><th></th></tr></thead>';
 82 		
 83 		foreach ($cats as $cat) {
 84 			$html .= '<tr>';
 85 			$html .= '<td>'.$cat->cat_id.'</td>';
 86 			$html .= '<td>'.$cat->cat_name.'</td>';
 87 			$html .= '<td><!--Edit--></td>';
 88 			$html .= '<td><a href="#'.$cat->cat_id.'" title="'.$cat->cat_name.'" class="delcat">[x]</a></td>';
 89 			$html .= '</tr>';
 90 		}
 91 		
 92 		$html .= '</table>';
 93 		
 94 		echo $html;
 95 		
 96 	}
 97 	
 98 	public static function addPost($post_title, $post_body, $categories, $permalink, $status){
 99 		
100 		$categories = json_encode($categories);
101 		$cats = addslashes($categories);
102 		$title = $post_title;
103 		$body = $post_body;
104
105		CandyDB::q("INSERT INTO ". DB_PREFIX ."posts (post_title, post_body, cat_id, permalink, status) VALUES (:title, :body, :cats, :permalink, :status)",
106			array(
107				'title' => $title,
108				'body' => $body,
109				'cats' => $cats,
110				'permalink' => $permalink,
111				'status' => $status
112			)
113		);
114		 	
115 	}
116 
117 	public static function editPost($post_title, $post_body, $categories, $permalink, $pid, $status=null){
118 
119 		$cats = addslashes(json_encode($categories));
120 		$title = $post_title;
121 		$body = $post_body;
122 		
123 		if (is_null($status)) {
124 			CandyDB::q("UPDATE ".DB_PREFIX."posts SET post_title = :title, post_body = :body, cat_id = :cats, permalink = :permalink WHERE post_id = :id",
125 				array(
126 					'title' => $title,
127					'body' => $body,
128					'cats' => $cats,
129					'permalink' => $permalink,
130					'id' => $pid
131 				)
132 			);
133 		} else {
134 			CandyDB::q("UPDATE ".DB_PREFIX."posts SET post_title = :title, post_body = :body, cat_id = :cats, permalink = :permalink, status = :status WHERE post_id = :id",
135 				array(
136 					'title' => $title,
137					'body' => $body,
138					'cats' => $cats,
139					'permalink' => $permalink,
140					'status' => $status,
141					'id' => $pid
142 				)
143 			);
144 		}
145 		
146 	}
147 	
148 	public static function deletePost($id){
149 		CandyDB::q('DELETE FROM '. DB_PREFIX .'posts WHERE post_id = :id', compact('id')); 		
150 	}
151 	
152 	public static function postDate($id, $format = "d/m/Y H:i:s"){
153 		$dbdate = CandyDB::col("SELECT post_date FROM ". DB_PREFIX ."posts WHERE post_id = :id", compact('id'));
154 		echo date($format, strtotime($dbdate));
155 	}
156 	
157 	public static function addShorttag(){
158 		
159 		$theme = Candy::Options('theme');
160 		
161		ob_start();
162		include 'frontend.php';
163		$include = ob_get_clean();
164		
165		ob_start();
166		$sidebar = THEME_PATH.$theme.'/blog/sidebar.php';
167		if (file_exists($sidebar)) {
168			include($sidebar);
169		} else {
170			include('templates/sidebar.php');
171		}
172		$sidebar = ob_get_clean();
173	  
174 		return array('{{blog}}' => $include, '{{sidebar}}' => $sidebar);	
175 		
176 	}
177 	
178 	public static function getPostUri($id){
179
180 		$permalink = CandyDB::col("SELECT permalink FROM ". DB_PREFIX ."posts WHERE post_id = :id", compact('id'));
181 		
182 		$cats = CandyDB::col("SELECT cat_id FROM ". DB_PREFIX ."posts WHERE post_id = :id", compact('id'));
183 		$cats = json_decode(stripslashes($cats));
184 		if (empty($cats)) {
185 			$cat = false;
186 		} else {
187 			$cat = CandyDB::col("SELECT cat_name FROM ". DB_PREFIX ."categories WHERE cat_id = :cat", array('cat' => $cats[0]));
188 		}
189 		$catname = ($cat == false) ? 'uncategorised' : str_replace(' ', '-', strtolower($cat));
190 		
191 		$uri = self::getBlogPage();
192
193 		return URL_PATH.$uri.'/'.$catname.'/'.$permalink;
194 		
195 	}
196 	
197 	public static function postUri($id){
198 	 	echo self::getPostUri($id);
199 	}
200 	
201 	public static function postExcerpt($id, $length = 200){
202 		
203 		$body = CandyDB::col("SELECT post_body FROM ". DB_PREFIX ."posts WHERE post_id = :id", compact('id'));
204 		
205 		if (strlen($body) >= 200) {
206 			echo substr($body, 0, $length).'&hellip;';
207 		} else {
208 			echo $body;
209 		}
210 		
211 	}
212 	
213 	public static function getCats(){
214 		return CandyDB::results("SELECT * FROM ". DB_PREFIX ."categories");
215 	}
216 	
217 	public static function adminCats($selected = false){
218 		
219 		$cats = self::getCats();
220 		
221 		$html = '<div id="blog-cats">';
222 		$html .= '<h3>Categories</h3>';
223 		$html .= '<ul>';
224 		
225 		if (!empty($cats)) {
226 		
227 			foreach ($cats as $cat) {
228 				
229 				if ($selected == false) {
230 			
231 					$html .= "<li>{$cat->cat_name}<input type='checkbox' value='{$cat->cat_id}' name='categories[]' /></li>";
232 					
233 				} else {
234 					
235 					$cats = json_decode(stripslashes($selected));	
236 					
237 					if (is_array($cats) && in_array($cat->cat_id, $cats)) {
238 						$html .= "<li>{$cat->cat_name}<input type='checkbox' value='{$cat->cat_id}' name='categories[]' checked='checked' /></li>";	
239 					} else {
240 						$html .= "<li>{$cat->cat_name}<input type='checkbox' value='{$cat->cat_id}' name='categories[]' /></li>";
241 					}
242 					
243 				}
244 				
245 			}	
246 			
247 		} else {
248 			$html .= '<li>Add a category to begin</li>';
249 		}
250 		
251 		$html .= '</ul>';
252 		
253 		$html .= '<div><input type="text" name="addcat" placeholder="Category" id="newcat" /><a href="javascript:void(0);" id="addcat" class="button">Add +</a></div>';
254 		
255 		echo $html;
256 			
257 	}
258 	
259 	public static function adminHead(){
260 	 	return '<script type="text/javascript" src="'.PLUGIN_URL.'Blog/js/admin.jquery.js"></script>';
261 	}
262 	
263 	public static function ajax(){
264 		
265 		if (isset($_POST['catname'])) {
266 			
267 			CandyDB::q("INSERT INTO ".DB_PREFIX."categories (`cat_name`) VALUES (:name)", array('name' => $_POST['catname']));
268 			echo CandyDB::col("SELECT cat_id FROM ".DB_PREFIX."categories WHERE cat_name = :name", array('name' => $_POST['catname']));
269 		
270 		} elseif (isset($_POST['search'])) {
271 		
272 			$posts = searchBlog($_POST['q']['term']);
273 			echo json_encode($posts);
274 			
275 		} else {
276 			
277 			$id = $_POST['id'];
278 			CandyDB::q("DELETE FROM ".DB_PREFIX."categories WHERE cat_id = :id", compact('id'));
279				
280 		}
281 		
282 	}
283 	
284 	public static function disqusAccount(){
285 		return CandyDB::col("SELECT option_value FROM ". DB_PREFIX ."options WHERE option_key = :key", array('key' => 'disqus'));
286 	}
287 	
288 	public static function commentForm(){
289 		
290 		$post = getBlogPost($_GET['post']);
291 	
292 		$url = Candy::Options('site_url');
293 		
294 		$html = '<div id="disqus_thread"></div>'."\n";
295 		$html .= '<script type="text/javascript">'."\n";
296 		      
297 		$html .= "var disqus_shortname = '".self::disqusAccount()."';\n";
298 		
299 		if (!empty($post)) $html .= "var disqus_identifier = '".$post[0]->post_id."';\n";
300 		
301 		$html .= "var disqus_url = '$url".$_GET['page']."/".$_GET['category']."/".$_GET['post']."';\n";
302 		   
303 		$html .= "(function() {\n";
304 		$html .= "var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;\n";
305 		$html .= "dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';\n";
306 		$html .= "(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);\n";
307 		$html .= "})();\n";
308 		$html .= "</script>";
309 
310 		echo $html;
311 		
312 	}
313 	
314 	public static function adminSettings(){
315
316 		$limit = CandyDB::col("SELECT `option_value` FROM `".DB_PREFIX."options` WHERE `option_key` = :key", array('key' => 'perpage'));
317 		
318 		$disqus = self::disqusAccount();
319 		
320 		$html = "<h3>Blog Settings</h3>";
321 		
322 		$html .= "<ul>";
323 		$html .= "<li>";
324 		$html .= "<label>Disqus Account</label>";
325 		$html .= "<input type='text' name='disqus' value='$disqus'/>";
326 		$html .= "</li>";
327 		
328 		$html .= "<label>Posts Per Page</label>";
329 		$html .= "<input type='text' name='perpage' value='$limit'/>";
330 		$html .= "</li>";
331 		
332 		$html .= "</ul>";
333 		
334 		return $html;
335 	}
336 	
337 	public static function saveSettings(){
338 		$account = $_POST['disqus'];
339 		$limit = $_POST['perpage'];
340 
341 		CandyDB::q('UPDATE '. DB_PREFIX .'options SET option_value = :value WHERE option_key = :key', array('value' => $account, 'key' => 'disqus'));
342 		CandyDB::q('UPDATE '. DB_PREFIX .'options SET option_value = :value WHERE option_key = :key', array('value' => $limit, 'key' => 'perpage'));
343 		
344 	}
345 	
346 	public static function nextLink($text = 'Next', $class = false){
347 	
348 		$site_url = Candy::Options('site_url');
349
350 		$count = CandyDB::col("SELECT COUNT(*) FROM `".DB_PREFIX."posts`");
351
352 		$limit = CandyDB::col("SELECT option_value FROM ".DB_PREFIX."options WHERE option_key = :key", array('key' => 'perpage'));
353
354 		if (isset($_GET['page'])) {
355 			$uri = $_GET['page'];
356 		} else {
357 			$uri = Candy::Options('homepage');
358 		}
359 
360 		if (isset($_GET['category']) && is_numeric($_GET['category'])) {
361 			$offset = $_GET['category']+1;
362 			$offset = $offset*$limit;
363 			$offset = $offset-$limit;
364 		
365 			$posts = CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts ORDER BY post_id DESC LIMIT '.$limit.' OFFSET '.$offset);
366 			
367 			$page = $_GET['category']+1;
368 			
369 			if ($posts != false) {
370 				if ($class !=false) {
371 					echo "<a href='".$site_url."$uri/$page' class='$class'>$text</a>";	
372 				} else {
373 					echo "<a href='".$site_url."$uri/$page'>$text</a>";
374 				}
375 			}
376 			
377 		} elseif ($count > $limit) {
378 			if ($class !=false) {
379 				echo "<a href='".$site_url."$uri/2' class='$class'>$text</a>";
380 			} else {
381 				echo "<a href='".$site_url."$uri/2'>$text</a>";
382 			}
383 		}
384
385 	}
386	
387	
388	public static function prevLink($text = 'Prev', $class = false){
389		
390		$site_url = Candy::Options('site_url');
391		
392		if (isset($_GET['category']) && is_numeric($_GET['category'])) {
393
394			$limit = CandyDB::col("SELECT option_value FROM ".DB_PREFIX."options WHERE option_key = :key", array('key' => 'perpage'));
395			
396			if (isset($_GET['page'])) {
397	 			$uri = explode('/', $_SERVER['REQUEST_URI']);
398	 			$uri = $uri[1];
399	 		} else {
400	 			$uri = Candy::Options('homepage');
401	 		}
402			
403			if ($_GET['category'] == 2) {
404				if ($class !=false) {
405					echo "<a href='".$site_url.Blog::getBlogPage()."' class='$class'>$text</a>";
406				} else {
407					echo "<a href='".$site_url."'>$text</a>";
408				}
409			} else {
410				
411				$page = $_GET['category']-1;
412				
413				if ($class !=false) {
414					echo "<a href='".$site_url."$uri/$page' class='$class'>$text</a>";
415				} else {
416					echo "<a href='".$site_url."$uri/$page'>$text</a>";
417				}
418			}
419			
420		}
421	
422	}
423	
424	public static function theCategories(){
425		
426		$cats = CandyDB::results('SELECT cat_name FROM '.DB_PREFIX.'categories ORDER BY cat_id DESC');
427		
428		$html = '';
429		
430		if (!empty($cats)) {
431			
432			$path = URL_PATH;
433			$html .= '<ul>';
434		
435			foreach ($cats as $cat) {
436				$html .= "<li><a href='".$path.self::getBlogPage()."/".str_replace(' ', '-', strtolower($cat->cat_name))."'>".$cat->cat_name."</a></li>";
437			}	
438			
439			$html .= '</ul>';
440		}
441		
442		echo $html;
443		
444	}
445	
446	public static function getBlogPage() {
447		return CandyDB::col('SELECT rewrite FROM '.DB_PREFIX.'pages WHERE page_body LIKE "%{{blog}}%"');
448	}
449
450	public static function getPostTitle($permalink){
451		return CandyDB::col('SELECT post_title FROM '.DB_PREFIX.'posts WHERE permalink = :permalink', compact('permalink'));
452	}
453	
454 }
455 
456 function listBlogPosts(){
457 
458 	$limit = CandyDB::col("SELECT `option_value` FROM `".DB_PREFIX."options` WHERE `option_key` = :key", array('key' => 'perpage'));
459 	
460 	if (isset($_GET['category']) && is_numeric($_GET['category'])) {
461 		
462 		$page = $_GET['category'];
463 		
464		$offset = $page*$limit;
465		$offset = $offset-$limit;
466 		
467 		$results = CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts WHERE `status` = :status ORDER BY post_id DESC LIMIT '.$limit.' OFFSET '.$offset,
468 			array(
469 				'status' => 'published'
470 			)
471 		);
472 		
473 	} else {
474	 	$results = CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts WHERE `status` = :status ORDER BY post_id DESC LIMIT '.$limit,
475	 		array(
476	 			'status' => 'published'
477	 		)
478	 	);
479 	}
480
481 	return $results;
482
483 }
484 
485 function listCategoryPosts(){
486  	
487  	$category = str_replace('-', ' ', $_GET['category']);
488
489	$catid = CandyDB::col('SELECT cat_id FROM '.DB_PREFIX.'categories WHERE cat_name = :cat', array('cat' => $category));
490  	$posts = CandyDB::results('SELECT post_id, cat_id FROM '.DB_PREFIX.'posts WHERE status = :status', array('status' => 'published'));
491  	
492  	$return = array();
493  	
494  	foreach ($posts as $post) {
495  		$ids = json_decode(stripslashes($post->cat_id));
496  		
497  		if (is_array($ids) && in_array($catid, $ids)) {
498  			$return[] = $post->post_id;
499  		}
500  	}
501  	
502  	$ids = join(',', $return);
503
504  	return CandyDB::results("SELECT * FROM ". DB_PREFIX ."posts WHERE `post_id` IN (".$ids.") ORDER BY post_title ASC");
505 
506  }
507  
508  function searchBlog($query){
509  	$search = addslashes($query);
510  	return CandyDB::results("SELECT * FROM ".DB_PREFIX."posts WHERE (`post_title` LIKE :title) OR (`post_body` LIKE :body)", array('title' => "%$search%", 'body' => "%$search%"));
511  }
512
513 function getBlogPost($uri){
514 	return CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts WHERE `permalink` = :permalink', array('permalink' => $uri));
515 }
516 
517 function getBlogPostById($id){
518 	return CandyDB::results('SELECT * FROM '. DB_PREFIX .'posts WHERE `post_id` = :id', compact('id'));
519 }
520 
521$uri = $_SERVER['REQUEST_URI'];
522
523if (!stristr($uri, 'cms-admin') && !stristr($uri, 'ajax.php')) {
524
525	//The following will generate and rss feed in the root of the CandyCMS install
526
527	$xml = '<?xml version="1.0" encoding="UTF-8"?>';
528	$xml .= '<rss version="2.0">';
529	$xml .=	'<channel>';
530	$xml .= '<title>'.Candy::Options('site_title').'</title>';
531	$xml .=	'<link>'.Candy::Options('site_url').'</link>';
532	$xml .= '<description>'.Candy::Options('site_title').' Blog</description>';
533	$xml .= '<pubDate>'.date('Y-m-d H:i:s').'</pubDate>';
534
535	$posts = listBlogPosts();
536
537	foreach ($posts as $post) {
538		$xml .= '<item>';
539		$xml .= '<title>'.$post->post_title.'</title>';
540		$xml .= '<link>'.Blog::getPostUri($post->post_id).'</link>';
541		$xml .= '<pubDate>'.$post->post_date.'</pubDate>';
542		$xml .= '<description><![CDATA['.$post->post_body.']]></description>';
543		$xml .= '</item>';
544	}
545
546	$xml .= '</channel>';
547	$xml .= '</rss>';
548
549	$fp = fopen(CMS_PATH.'rss.xml', 'w');
550	fwrite($fp, $xml);
551	fclose($fp);
552}