PageRenderTime 630ms CodeModel.GetById 286ms app.highlight 121ms RepoModel.GetById 137ms app.codeStats 1ms

/halogy/application/modules/blog/models/blog_model.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 713 lines | 517 code | 134 blank | 62 comment | 40 complexity | 04a66e5c8ae0860fcab8f173468a8bca MD5 | raw file
  1<?php
  2/**
  3 * Halogy
  4 *
  5 * A user friendly, modular content management system for PHP 5.0
  6 * Built on CodeIgniter - http://codeigniter.com
  7 *
  8 * @package		Halogy
  9 * @author		Haloweb Ltd.
 10 * @copyright	Copyright (c) 2008-2011, Haloweb Ltd.
 11 * @license		http://halogy.com/license
 12 * @link		http://halogy.com/
 13 * @since		Version 1.0
 14 * @filesource
 15 */
 16
 17// ------------------------------------------------------------------------
 18
 19class Blog_Model extends Model {
 20	
 21	function Blog_Model()
 22	{
 23		parent::Model();
 24
 25		// get siteID, if available
 26		if (defined('SITEID'))
 27		{
 28			$this->siteID = SITEID;
 29		}	
 30	}
 31
 32	function get_all_posts()
 33	{
 34		$this->db->where('published', 1);
 35		$this->db->where('deleted', 0);
 36		$this->db->where('siteID', $this->siteID);
 37		
 38		$query = $this->db->get('blog_posts');
 39
 40		if ($query->num_rows() > 0)
 41		{
 42			$result = $query->result_array();
 43			return $result;
 44		}
 45		else
 46		{
 47			return FALSE;
 48		}
 49	}
 50	
 51	function get_posts($num = 10)
 52	{
 53		// start cache
 54		$this->db->start_cache();
 55		
 56		// default where
 57		$this->db->where(array(
 58			'published' => 1,
 59			'deleted' => 0,
 60			'siteID' => $this->siteID
 61		));
 62		
 63		// order
 64		$this->db->order_by('dateCreated', 'desc');
 65		
 66		// stop cache
 67		$this->db->stop_cache();
 68
 69		// get total rows
 70		$query = $this->db->get('blog_posts');
 71		$totalRows = $query->num_rows();
 72		
 73		// get comment count and post data
 74		$this->db->select('(SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments, blog_posts.*', FALSE);
 75
 76		// init paging
 77		$this->core->set_paging($totalRows, $num);
 78		$query = $this->db->get('blog_posts', $num, $this->pagination->offset);
 79		
 80		// flush cache
 81		$this->db->flush_cache();
 82
 83		if ($query->num_rows() > 0)
 84		{
 85			return $query->result_array();
 86		}
 87		else
 88		{
 89			return FALSE;
 90		}
 91	}	
 92
 93	function get_posts_by_tag($tag, $limit = 20)
 94	{
 95		// get rows based on this tag
 96		$tags = $this->tags->fetch_rows(array(
 97			'table' => 'blog_posts',
 98			'tags' => array(1, $tag),
 99			'limit' => $limit,
100			'siteID' => $this->siteID
101		));
102		if (!$tags)
103		{
104			return FALSE;
105		}
106		
107		// build tags array
108		foreach ($tags as $tag)
109		{
110			$tagsArray[] = $tag['row_id'];
111		}
112
113		// default where
114		$this->db->start_cache();		
115		$this->db->where(array(
116			'published' => 1,
117			'deleted' => 0,
118			'siteID' => $this->siteID
119		));
120
121		// where tags
122		$this->db->where_in('postID', $tagsArray);
123		$this->db->order_by('dateCreated', 'desc');
124
125		// get comment count and post data
126		$this->db->select('(SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments, blog_posts.*', FALSE);		
127		$this->db->stop_cache();		
128
129		$query = $this->db->get('blog_posts');
130		$totalRows = $query->num_rows();
131
132		// init paging
133		$this->core->set_paging($totalRows, $limit);
134		$query = $this->db->get('blog_posts', $limit, $this->pagination->offset);
135		$this->db->flush_cache();
136		
137		if ($query->num_rows() > 0)
138		{
139			return $query->result_array();
140		}
141		else
142		{
143			return FALSE;
144		}
145	}
146
147	function get_posts_by_category($cat, $limit = 10)
148	{
149		// get cat IDs
150		if (!$postsArray = $this->get_catmap_post_ids($cat))
151		{
152			return FALSE;
153		}
154
155		// stop cache
156		$this->db->start_cache();
157		
158		// default where
159		$this->db->where(array(
160			'published' => 1,
161			'deleted' => 0,
162			'siteID' => $this->siteID
163		));
164
165		// where category
166		$this->db->where_in('postID', $postsArray);
167		
168		// order
169		$this->db->order_by('dateCreated', 'desc');
170
171		// get comment count and post data
172		$this->db->select('(SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments, blog_posts.*', FALSE);
173		
174		// stop cache
175		$this->db->stop_cache();		
176
177		// get total rows
178		$query = $this->db->get('blog_posts');
179		$totalRows = $query->num_rows();	
180
181		// init paging
182		$this->core->set_paging($totalRows, $limit);
183		$query = $this->db->get('blog_posts', $limit, $this->pagination->offset);
184		
185		// flush cache
186		$this->db->flush_cache();
187		
188		if ($query->num_rows() > 0)
189		{
190			return $query->result_array();
191		}
192		else
193		{
194			return FALSE;
195		}
196	}
197	
198	function get_catmap_post_ids($cat)
199	{
200		// get rows based on this category
201		$this->db->join('blog_cats', 'blog_cats.catID = blog_catmap.catID');
202		$this->db->where('blog_cats.catSafe', $cat);
203		
204		// get result
205		$result = $this->db->get('blog_catmap');
206		
207		if ($result->num_rows())
208		{
209			$cats = $result->result_array();
210			
211			foreach ($cats as $cat)
212			{
213				$postsArray[] = $cat['postID'];
214			}
215			
216			return $postsArray;
217		}
218		else
219		{
220			return FALSE;
221		}
222	}
223
224	function get_posts_by_date($year, $month = '', $limit = 10)
225	{
226		if ($month)
227		{
228			$next_month = $month + 1;
229			$from =  date("Y-m-d H:i:s", mktime(0, 0, 0, $month, 0, $year));
230			$to =  date("Y-m-d H:i:s", mktime(23, 59, 59, $next_month, 0, $year));
231		}
232		else
233		{
234			$from =  date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 0, $year));
235			$to =  date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 0, ($year+1)));
236		}
237
238		$this->db->start_cache();
239		$this->db->where('dateCreated >', $from);
240		$this->db->where('dateCreated <', $to);
241		$this->db->where('published', 1);
242		$this->db->where('deleted', 0);	
243		$this->db->where('siteID', $this->siteID);
244
245		// get comment count and post data
246		$this->db->select('(SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments, blog_posts.*', FALSE);			
247		$this->db->stop_cache();		
248				
249		$query = $this->db->get('blog_posts');
250		$totalRows = $query->num_rows();
251
252		// init paging
253		$this->core->set_paging($totalRows, $limit);
254		$query = $this->db->get('blog_posts', $limit, $this->pagination->offset);
255		$this->db->flush_cache();
256
257		if ($query->num_rows() > 0)
258		{
259			return $query->result_array();
260		}
261		else
262		{
263			return FALSE;
264		}
265	}
266
267	function get_post_by_title($title = '', $limit = 20)
268	{
269		$this->db->start_cache();
270		$this->db->where('postTitle', $title);
271		$this->db->where('published', 1);
272		$this->db->where('deleted', 0);	
273		$this->db->where('siteID', $this->siteID);
274
275		// get comment count and post data
276		$this->db->select('(SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments, blog_posts.*', FALSE);
277		$this->db->stop_cache();
278				
279		$query = $this->db->get('blog_posts');
280
281		// init paging
282		$this->core->set_paging($totalRows, $limit);
283		$query = $this->db->get('blog_posts', $limit, $this->pagination->offset);
284		$this->db->flush_cache();
285
286		if ($query->num_rows() > 0)
287		{
288			return $query->row_array();
289		}
290		else
291		{
292			return FALSE;
293		}
294	}
295	
296	function get_post($year, $month, $uri)
297	{
298		$next_month = $month + 1;
299		
300		$from =  date("Y-m-d H:i:s", mktime(0, 0, 0, $month, 0, $year));
301		$to =  date("Y-m-d H:i:s", mktime(23, 59, 59, $next_month, 0, $year));
302		
303		$this->db->where('dateCreated >', $from);
304		$this->db->where('dateCreated <', $to);
305		$this->db->where('uri', $uri);
306
307		if (!$this->session->userdata('session_admin'))
308		{
309			$this->db->where('published', 1);
310		}
311		
312		$this->db->where('deleted', 0);
313		$this->db->where('siteID', $this->siteID);
314		
315		// get comment count and post data
316		$this->db->select('(SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments, blog_posts.*', FALSE);
317		
318		$query = $this->db->get('blog_posts', 1);
319		
320		if ( $query->num_rows() == 1 )
321		{
322			$post = $query->row_array();
323						
324			return $post;
325		}
326		else
327		{
328			return FALSE;
329		}
330	}		
331
332	function get_post_by_id($postID)
333	{
334		$this->db->where('postID', $postID);
335		
336		$query = $this->db->get('blog_post', 1);
337		
338		if ($query->num_rows())
339		{
340			$post = $query->row_array();
341			
342			return $post;
343		}
344		else
345		{
346			return FALSE;
347		}
348	}
349
350	function get_tags()
351	{
352		$this->db->join('tags_ref', 'tags_ref.tag_id = tags.id');	
353		$this->db->where('tags_ref.siteID', $this->siteID);
354		
355		$query = $this->db->get('tags');
356
357		if ($query->num_rows())
358		{
359			return $query->result_array();
360		}
361		else
362		{
363			return FALSE;
364		}
365	}	
366	
367	function update_cats($postID, $catsArray = '')
368	{
369		$this->db->delete('blog_catmap', array('postID' => $postID, 'siteID' => $this->siteID));
370
371		if ($catsArray)
372		{		
373			foreach($catsArray as $cat)
374			{
375				if ($cat)
376				{
377					$cat = trim(htmlentities($cat));
378					
379					$query = $this->db->get_where('blog_cats', array('catName' => $cat, 'siteID' => $this->siteID));
380					
381					if (!$query->num_rows())
382					{
383						$this->db->insert('blog_cats', array('catName' => $cat, 'catSafe' => url_title(strtolower(trim($cat))), 'siteID' => $this->siteID));
384						$catID = $this->db->insert_id();
385					}
386					else
387					{
388						$row = $query->row_array();
389						$catID = $row['catID'];
390					}
391					
392					$query = $this->db->get_where('blog_catmap', array('postID' => $postID, 'catID' => $catID, 'siteID' => $this->siteID));
393					
394					if (!$query->num_rows())
395					{
396						$this->db->insert('blog_catmap',array('postID' => $postID, 'catID' => $catID, 'siteID' => $this->siteID));
397					}
398				}
399			}
400		}
401
402		return TRUE;
403	}
404
405	function get_categories($catID = '')
406	{
407		// default where
408		$this->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
409		$this->db->order_by('catOrder');
410		
411		// get based on category ID
412		if ($catID)
413		{
414			$query = $this->db->get_where('blog_cats', array('catID' => $catID), 1);
415			
416			if ($query->num_rows())
417			{
418				return $query->row_array();
419			}
420			else
421			{
422				return FALSE;
423			}	
424		}
425		// or just get all of em
426		else
427		{
428			// template type
429			$query = $this->db->get('blog_cats');
430			
431			if ($query->num_rows())
432			{
433				return $query->result_array();
434			}
435			else
436			{
437				return FALSE;
438			}
439		}
440	}	
441
442	function get_cats()
443	{
444		$this->db->select('(SELECT COUNT(*) FROM '.$this->db->dbprefix.'blog_posts JOIN '.$this->db->dbprefix.'blog_catmap USING(postID) WHERE '.$this->db->dbprefix.'blog_catmap.catID = '.$this->db->dbprefix.'blog_cats.catID AND '.$this->db->dbprefix.'blog_posts.deleted = 0 AND published = 1) AS numPosts, catName, catSafe');
445		$this->db->join('blog_catmap', 'blog_cats.catID = blog_catmap.catID', 'left');
446		$this->db->where('blog_cats.deleted', 0);		
447		$this->db->group_by('catSafe');
448		$this->db->order_by('catName');
449		$this->db->where('blog_cats.siteID', $this->siteID);
450		
451		$query = $this->db->get('blog_cats');
452
453		if ($query->num_rows())
454		{
455			return $query->result_array();
456		}
457		else
458		{
459			return FALSE;
460		}
461	}
462	
463	function get_cats_for_post($postID)
464	{
465		// get cats for this post
466		$this->db->join('blog_cats', 'blog_catmap.catID = blog_cats.catID', 'left');
467		$this->db->order_by('catOrder');
468		$query = $this->db->get_where('blog_catmap', array('postID' => $postID));		
469	
470		$catsArray = $query->result_array();
471		$cats = array();
472	
473		foreach($catsArray as $cat)
474		{
475			$cats[$cat['catID']] = $cat['catName'];
476		}
477	
478		return $cats;
479	}
480
481	function parse_post($body, $condense = FALSE, $uri = '')
482	{
483		if ($condense)
484		{
485			if ($endchr = strpos($body, '{more}'))
486			{
487				$body = substr($body, 0, ($endchr + 6));
488				$body = str_replace('{more}', '<p><strong><a href="'.$uri.'" class="button">Read more</a></strong></p>', $body);				
489			}
490		}
491		else
492		{
493			$body = str_replace('{more}', '', $body);
494		}
495		
496		$body = $this->parse_images($body);
497
498		$body = mkdn($body);
499
500		return $body;
501	}
502
503	function get_archive($limit = 20)
504	{
505		$this->db->select('COUNT(postID) as numPosts, DATE_FORMAT(dateCreated, "%M %Y") as dateStr, DATE_FORMAT(dateCreated, "%m") as month, DATE_FORMAT(dateCreated, "%Y") as year, (SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments', FALSE);
506		$this->db->where('published', 1);
507		$this->db->where('deleted', 0);
508		$this->db->where('siteID', $this->siteID);	
509
510		$this->db->order_by('dateCreated', 'desc');
511		$this->db->group_by('dateStr');
512		
513		$query = $this->db->get('blog_posts');
514
515		if ($query->num_rows() > 0)
516		{
517			return $query->result_array();
518		}
519		else
520		{
521			return FALSE;
522		}
523	}
524
525	function get_headlines($num = 10)
526	{	
527		// default where
528		$this->db->where(array(
529			'published' => 1,
530			'deleted' => 0,
531			'siteID' => $this->siteID
532		));
533		$this->db->order_by('dateCreated', 'desc');
534
535		// get comment count and post data
536		$this->db->select('(SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments, blog_posts.*', FALSE);
537		
538		$query = $this->db->get('blog_posts', $num);
539
540		if ($query->num_rows() > 0)
541		{
542			return $query->result_array();
543		}
544		else
545		{
546			return FALSE;
547		}
548	}
549
550	function get_latest_comments($postID = '')
551	{
552		// get comments based on a post
553		if ($postID)
554		{
555			$this->db->where('t1.postID', $postID, FALSE);
556			$this->db->where('t1.active', 1, FALSE);
557		}
558
559		$this->db->select('t1.*, t2.postTitle, t2.dateCreated as uriDate, t2.uri');
560		
561		$this->db->where('t1.deleted', 0, FALSE);
562		$this->db->where('t1.siteID', $this->siteID, FALSE);
563
564		$this->db->join('blog_posts t2', 't2.postID = t1 . postID');
565		
566		$this->db->order_by('t1 . dateCreated', 'desc');
567		
568		$query = $this->db->get('blog_comments t1', 30);
569		
570		$comments = array();
571		
572		if ( $query->num_rows() > 0)
573		{
574			$comments = $query->result_array();
575		}
576		
577		return $comments;
578	}
579
580	function get_comments($postID = '')
581	{
582		// get comments based on a post
583		if ($postID)
584		{
585			$this->db->where('t1.postID', $postID, FALSE);
586			$this->db->where('t1.active', 1, FALSE);
587		}
588
589		$this->db->select('t1.*, t2.postTitle, t2.dateCreated as uriDate, t2.uri');
590		
591		$this->db->where('t1.deleted', 0, FALSE);
592		$this->db->where('t1.siteID', $this->siteID, FALSE);
593
594		$this->db->join('blog_posts t2', 't2.postID = t1 . postID');
595		
596		$this->db->order_by('t1 . dateCreated', 'asc');
597		
598		$query = $this->db->get('blog_comments t1');
599		
600		$comments = array();
601		
602		if ( $query->num_rows() > 0)
603		{
604			$comments = $query->result_array();
605		}
606		
607		return $comments;
608	}
609
610	function approve_comment($commentID)
611	{
612		$this->db->set('active', 1);
613		$this->db->where('siteID', $this->siteID);		
614		$this->db->where('commentID', $commentID);
615
616		$this->db->update('blog_comments');
617
618		return TRUE;	
619	}
620
621	function get_user($userID)
622	{
623		$query = $this->db->get_where('users', array('userID' => $userID), 1);
624		
625		if ($query->num_rows())
626		{				
627			return $query->row_array();
628		}
629		else
630		{
631			return FALSE;
632		}
633	}
634
635	function lookup_user($userID, $display = FALSE)
636	{
637		// default wheres
638		$this->db->where('userID', $userID);
639
640		// grab
641		$query = $this->db->get('users', 1);
642
643		if ($query->num_rows())
644		{
645			$row = $query->row_array();
646			
647			if ($display !== FALSE)
648			{
649				return ($row['displayName']) ? $row['displayName'] : $row['firstName'].' '.$row['lastName'];
650			}
651			else
652			{
653				return $row;
654			}
655		}
656		else
657		{
658			return FALSE;
659		}		
660	}
661
662	function search_posts($query = '', $ids = '')
663	{
664		if (!$query && !$ids)
665		{
666			return FALSE;
667		}
668
669		// default wheres
670		$this->db->where('deleted', 0);
671		$this->db->where('published', 1);
672		$this->db->where('siteID', $this->siteID);
673
674		// search
675		if ($query)
676		{
677			// tidy query
678			$q = $this->db->escape_like_str($query);
679
680			$sql = '(postTitle LIKE "%'.$q.'%" OR body LIKE "%'.$q.'%")';
681		}
682		if ($ids)
683		{
684			$sql .= ' OR postID IN ('.implode(',', $ids).')';
685		}
686		$this->db->where($sql);
687	
688		$this->db->orderby('dateCreated', 'desc');
689
690		// get comment count and post data
691		$this->db->select('(SELECT COUNT(*) from '.$this->db->dbprefix.'blog_comments where '.$this->db->dbprefix.'blog_comments.postID = '.$this->db->dbprefix.'blog_posts.postID and deleted = 0 and active = 1) AS numComments, blog_posts.*');				
692	
693		$query = $this->db->get('blog_posts');
694		
695		if ($query->num_rows() > 0)
696		{
697			return $query->result_array();
698		}
699		else
700		{
701			return FALSE;
702		}
703	}
704
705	function add_view($pageID)
706	{
707		$this->db->set('views', 'views+1', FALSE);
708		$this->db->where('postID', $pageID);
709		$this->db->where('siteID', $this->siteID);		
710		$this->db->update('blog_posts');
711	}
712			
713}