/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. class Blog_Model extends Model {
  18. function Blog_Model()
  19. {
  20. parent::Model();
  21. // get siteID, if available
  22. if (defined('SITEID'))
  23. {
  24. $this->siteID = SITEID;
  25. }
  26. }
  27. function get_all_posts()
  28. {
  29. $this->db->where('published', 1);
  30. $this->db->where('deleted', 0);
  31. $this->db->where('siteID', $this->siteID);
  32. $query = $this->db->get('blog_posts');
  33. if ($query->num_rows() > 0)
  34. {
  35. $result = $query->result_array();
  36. return $result;
  37. }
  38. else
  39. {
  40. return FALSE;
  41. }
  42. }
  43. function get_posts($num = 10)
  44. {
  45. // start cache
  46. $this->db->start_cache();
  47. // default where
  48. $this->db->where(array(
  49. 'published' => 1,
  50. 'deleted' => 0,
  51. 'siteID' => $this->siteID
  52. ));
  53. // order
  54. $this->db->order_by('dateCreated', 'desc');
  55. // stop cache
  56. $this->db->stop_cache();
  57. // get total rows
  58. $query = $this->db->get('blog_posts');
  59. $totalRows = $query->num_rows();
  60. // get comment count and post data
  61. $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);
  62. // init paging
  63. $this->core->set_paging($totalRows, $num);
  64. $query = $this->db->get('blog_posts', $num, $this->pagination->offset);
  65. // flush cache
  66. $this->db->flush_cache();
  67. if ($query->num_rows() > 0)
  68. {
  69. return $query->result_array();
  70. }
  71. else
  72. {
  73. return FALSE;
  74. }
  75. }
  76. function get_posts_by_tag($tag, $limit = 20)
  77. {
  78. // get rows based on this tag
  79. $tags = $this->tags->fetch_rows(array(
  80. 'table' => 'blog_posts',
  81. 'tags' => array(1, $tag),
  82. 'limit' => $limit,
  83. 'siteID' => $this->siteID
  84. ));
  85. if (!$tags)
  86. {
  87. return FALSE;
  88. }
  89. // build tags array
  90. foreach ($tags as $tag)
  91. {
  92. $tagsArray[] = $tag['row_id'];
  93. }
  94. // default where
  95. $this->db->start_cache();
  96. $this->db->where(array(
  97. 'published' => 1,
  98. 'deleted' => 0,
  99. 'siteID' => $this->siteID
  100. ));
  101. // where tags
  102. $this->db->where_in('postID', $tagsArray);
  103. $this->db->order_by('dateCreated', 'desc');
  104. // get comment count and post data
  105. $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);
  106. $this->db->stop_cache();
  107. $query = $this->db->get('blog_posts');
  108. $totalRows = $query->num_rows();
  109. // init paging
  110. $this->core->set_paging($totalRows, $limit);
  111. $query = $this->db->get('blog_posts', $limit, $this->pagination->offset);
  112. $this->db->flush_cache();
  113. if ($query->num_rows() > 0)
  114. {
  115. return $query->result_array();
  116. }
  117. else
  118. {
  119. return FALSE;
  120. }
  121. }
  122. function get_posts_by_category($cat, $limit = 10)
  123. {
  124. // get cat IDs
  125. if (!$postsArray = $this->get_catmap_post_ids($cat))
  126. {
  127. return FALSE;
  128. }
  129. // stop cache
  130. $this->db->start_cache();
  131. // default where
  132. $this->db->where(array(
  133. 'published' => 1,
  134. 'deleted' => 0,
  135. 'siteID' => $this->siteID
  136. ));
  137. // where category
  138. $this->db->where_in('postID', $postsArray);
  139. // order
  140. $this->db->order_by('dateCreated', 'desc');
  141. // get comment count and post data
  142. $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);
  143. // stop cache
  144. $this->db->stop_cache();
  145. // get total rows
  146. $query = $this->db->get('blog_posts');
  147. $totalRows = $query->num_rows();
  148. // init paging
  149. $this->core->set_paging($totalRows, $limit);
  150. $query = $this->db->get('blog_posts', $limit, $this->pagination->offset);
  151. // flush cache
  152. $this->db->flush_cache();
  153. if ($query->num_rows() > 0)
  154. {
  155. return $query->result_array();
  156. }
  157. else
  158. {
  159. return FALSE;
  160. }
  161. }
  162. function get_catmap_post_ids($cat)
  163. {
  164. // get rows based on this category
  165. $this->db->join('blog_cats', 'blog_cats.catID = blog_catmap.catID');
  166. $this->db->where('blog_cats.catSafe', $cat);
  167. // get result
  168. $result = $this->db->get('blog_catmap');
  169. if ($result->num_rows())
  170. {
  171. $cats = $result->result_array();
  172. foreach ($cats as $cat)
  173. {
  174. $postsArray[] = $cat['postID'];
  175. }
  176. return $postsArray;
  177. }
  178. else
  179. {
  180. return FALSE;
  181. }
  182. }
  183. function get_posts_by_date($year, $month = '', $limit = 10)
  184. {
  185. if ($month)
  186. {
  187. $next_month = $month + 1;
  188. $from = date("Y-m-d H:i:s", mktime(0, 0, 0, $month, 0, $year));
  189. $to = date("Y-m-d H:i:s", mktime(23, 59, 59, $next_month, 0, $year));
  190. }
  191. else
  192. {
  193. $from = date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 0, $year));
  194. $to = date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 0, ($year+1)));
  195. }
  196. $this->db->start_cache();
  197. $this->db->where('dateCreated >', $from);
  198. $this->db->where('dateCreated <', $to);
  199. $this->db->where('published', 1);
  200. $this->db->where('deleted', 0);
  201. $this->db->where('siteID', $this->siteID);
  202. // get comment count and post data
  203. $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);
  204. $this->db->stop_cache();
  205. $query = $this->db->get('blog_posts');
  206. $totalRows = $query->num_rows();
  207. // init paging
  208. $this->core->set_paging($totalRows, $limit);
  209. $query = $this->db->get('blog_posts', $limit, $this->pagination->offset);
  210. $this->db->flush_cache();
  211. if ($query->num_rows() > 0)
  212. {
  213. return $query->result_array();
  214. }
  215. else
  216. {
  217. return FALSE;
  218. }
  219. }
  220. function get_post_by_title($title = '', $limit = 20)
  221. {
  222. $this->db->start_cache();
  223. $this->db->where('postTitle', $title);
  224. $this->db->where('published', 1);
  225. $this->db->where('deleted', 0);
  226. $this->db->where('siteID', $this->siteID);
  227. // get comment count and post data
  228. $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);
  229. $this->db->stop_cache();
  230. $query = $this->db->get('blog_posts');
  231. // init paging
  232. $this->core->set_paging($totalRows, $limit);
  233. $query = $this->db->get('blog_posts', $limit, $this->pagination->offset);
  234. $this->db->flush_cache();
  235. if ($query->num_rows() > 0)
  236. {
  237. return $query->row_array();
  238. }
  239. else
  240. {
  241. return FALSE;
  242. }
  243. }
  244. function get_post($year, $month, $uri)
  245. {
  246. $next_month = $month + 1;
  247. $from = date("Y-m-d H:i:s", mktime(0, 0, 0, $month, 0, $year));
  248. $to = date("Y-m-d H:i:s", mktime(23, 59, 59, $next_month, 0, $year));
  249. $this->db->where('dateCreated >', $from);
  250. $this->db->where('dateCreated <', $to);
  251. $this->db->where('uri', $uri);
  252. if (!$this->session->userdata('session_admin'))
  253. {
  254. $this->db->where('published', 1);
  255. }
  256. $this->db->where('deleted', 0);
  257. $this->db->where('siteID', $this->siteID);
  258. // get comment count and post data
  259. $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);
  260. $query = $this->db->get('blog_posts', 1);
  261. if ( $query->num_rows() == 1 )
  262. {
  263. $post = $query->row_array();
  264. return $post;
  265. }
  266. else
  267. {
  268. return FALSE;
  269. }
  270. }
  271. function get_post_by_id($postID)
  272. {
  273. $this->db->where('postID', $postID);
  274. $query = $this->db->get('blog_post', 1);
  275. if ($query->num_rows())
  276. {
  277. $post = $query->row_array();
  278. return $post;
  279. }
  280. else
  281. {
  282. return FALSE;
  283. }
  284. }
  285. function get_tags()
  286. {
  287. $this->db->join('tags_ref', 'tags_ref.tag_id = tags.id');
  288. $this->db->where('tags_ref.siteID', $this->siteID);
  289. $query = $this->db->get('tags');
  290. if ($query->num_rows())
  291. {
  292. return $query->result_array();
  293. }
  294. else
  295. {
  296. return FALSE;
  297. }
  298. }
  299. function update_cats($postID, $catsArray = '')
  300. {
  301. $this->db->delete('blog_catmap', array('postID' => $postID, 'siteID' => $this->siteID));
  302. if ($catsArray)
  303. {
  304. foreach($catsArray as $cat)
  305. {
  306. if ($cat)
  307. {
  308. $cat = trim(htmlentities($cat));
  309. $query = $this->db->get_where('blog_cats', array('catName' => $cat, 'siteID' => $this->siteID));
  310. if (!$query->num_rows())
  311. {
  312. $this->db->insert('blog_cats', array('catName' => $cat, 'catSafe' => url_title(strtolower(trim($cat))), 'siteID' => $this->siteID));
  313. $catID = $this->db->insert_id();
  314. }
  315. else
  316. {
  317. $row = $query->row_array();
  318. $catID = $row['catID'];
  319. }
  320. $query = $this->db->get_where('blog_catmap', array('postID' => $postID, 'catID' => $catID, 'siteID' => $this->siteID));
  321. if (!$query->num_rows())
  322. {
  323. $this->db->insert('blog_catmap',array('postID' => $postID, 'catID' => $catID, 'siteID' => $this->siteID));
  324. }
  325. }
  326. }
  327. }
  328. return TRUE;
  329. }
  330. function get_categories($catID = '')
  331. {
  332. // default where
  333. $this->db->where(array('siteID' => $this->siteID, 'deleted' => 0));
  334. $this->db->order_by('catOrder');
  335. // get based on category ID
  336. if ($catID)
  337. {
  338. $query = $this->db->get_where('blog_cats', array('catID' => $catID), 1);
  339. if ($query->num_rows())
  340. {
  341. return $query->row_array();
  342. }
  343. else
  344. {
  345. return FALSE;
  346. }
  347. }
  348. // or just get all of em
  349. else
  350. {
  351. // template type
  352. $query = $this->db->get('blog_cats');
  353. if ($query->num_rows())
  354. {
  355. return $query->result_array();
  356. }
  357. else
  358. {
  359. return FALSE;
  360. }
  361. }
  362. }
  363. function get_cats()
  364. {
  365. $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');
  366. $this->db->join('blog_catmap', 'blog_cats.catID = blog_catmap.catID', 'left');
  367. $this->db->where('blog_cats.deleted', 0);
  368. $this->db->group_by('catSafe');
  369. $this->db->order_by('catName');
  370. $this->db->where('blog_cats.siteID', $this->siteID);
  371. $query = $this->db->get('blog_cats');
  372. if ($query->num_rows())
  373. {
  374. return $query->result_array();
  375. }
  376. else
  377. {
  378. return FALSE;
  379. }
  380. }
  381. function get_cats_for_post($postID)
  382. {
  383. // get cats for this post
  384. $this->db->join('blog_cats', 'blog_catmap.catID = blog_cats.catID', 'left');
  385. $this->db->order_by('catOrder');
  386. $query = $this->db->get_where('blog_catmap', array('postID' => $postID));
  387. $catsArray = $query->result_array();
  388. $cats = array();
  389. foreach($catsArray as $cat)
  390. {
  391. $cats[$cat['catID']] = $cat['catName'];
  392. }
  393. return $cats;
  394. }
  395. function parse_post($body, $condense = FALSE, $uri = '')
  396. {
  397. if ($condense)
  398. {
  399. if ($endchr = strpos($body, '{more}'))
  400. {
  401. $body = substr($body, 0, ($endchr + 6));
  402. $body = str_replace('{more}', '<p><strong><a href="'.$uri.'" class="button">Read more</a></strong></p>', $body);
  403. }
  404. }
  405. else
  406. {
  407. $body = str_replace('{more}', '', $body);
  408. }
  409. $body = $this->parse_images($body);
  410. $body = mkdn($body);
  411. return $body;
  412. }
  413. function get_archive($limit = 20)
  414. {
  415. $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);
  416. $this->db->where('published', 1);
  417. $this->db->where('deleted', 0);
  418. $this->db->where('siteID', $this->siteID);
  419. $this->db->order_by('dateCreated', 'desc');
  420. $this->db->group_by('dateStr');
  421. $query = $this->db->get('blog_posts');
  422. if ($query->num_rows() > 0)
  423. {
  424. return $query->result_array();
  425. }
  426. else
  427. {
  428. return FALSE;
  429. }
  430. }
  431. function get_headlines($num = 10)
  432. {
  433. // default where
  434. $this->db->where(array(
  435. 'published' => 1,
  436. 'deleted' => 0,
  437. 'siteID' => $this->siteID
  438. ));
  439. $this->db->order_by('dateCreated', 'desc');
  440. // get comment count and post data
  441. $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);
  442. $query = $this->db->get('blog_posts', $num);
  443. if ($query->num_rows() > 0)
  444. {
  445. return $query->result_array();
  446. }
  447. else
  448. {
  449. return FALSE;
  450. }
  451. }
  452. function get_latest_comments($postID = '')
  453. {
  454. // get comments based on a post
  455. if ($postID)
  456. {
  457. $this->db->where('t1.postID', $postID, FALSE);
  458. $this->db->where('t1.active', 1, FALSE);
  459. }
  460. $this->db->select('t1.*, t2.postTitle, t2.dateCreated as uriDate, t2.uri');
  461. $this->db->where('t1.deleted', 0, FALSE);
  462. $this->db->where('t1.siteID', $this->siteID, FALSE);
  463. $this->db->join('blog_posts t2', 't2.postID = t1 . postID');
  464. $this->db->order_by('t1 . dateCreated', 'desc');
  465. $query = $this->db->get('blog_comments t1', 30);
  466. $comments = array();
  467. if ( $query->num_rows() > 0)
  468. {
  469. $comments = $query->result_array();
  470. }
  471. return $comments;
  472. }
  473. function get_comments($postID = '')
  474. {
  475. // get comments based on a post
  476. if ($postID)
  477. {
  478. $this->db->where('t1.postID', $postID, FALSE);
  479. $this->db->where('t1.active', 1, FALSE);
  480. }
  481. $this->db->select('t1.*, t2.postTitle, t2.dateCreated as uriDate, t2.uri');
  482. $this->db->where('t1.deleted', 0, FALSE);
  483. $this->db->where('t1.siteID', $this->siteID, FALSE);
  484. $this->db->join('blog_posts t2', 't2.postID = t1 . postID');
  485. $this->db->order_by('t1 . dateCreated', 'asc');
  486. $query = $this->db->get('blog_comments t1');
  487. $comments = array();
  488. if ( $query->num_rows() > 0)
  489. {
  490. $comments = $query->result_array();
  491. }
  492. return $comments;
  493. }
  494. function approve_comment($commentID)
  495. {
  496. $this->db->set('active', 1);
  497. $this->db->where('siteID', $this->siteID);
  498. $this->db->where('commentID', $commentID);
  499. $this->db->update('blog_comments');
  500. return TRUE;
  501. }
  502. function get_user($userID)
  503. {
  504. $query = $this->db->get_where('users', array('userID' => $userID), 1);
  505. if ($query->num_rows())
  506. {
  507. return $query->row_array();
  508. }
  509. else
  510. {
  511. return FALSE;
  512. }
  513. }
  514. function lookup_user($userID, $display = FALSE)
  515. {
  516. // default wheres
  517. $this->db->where('userID', $userID);
  518. // grab
  519. $query = $this->db->get('users', 1);
  520. if ($query->num_rows())
  521. {
  522. $row = $query->row_array();
  523. if ($display !== FALSE)
  524. {
  525. return ($row['displayName']) ? $row['displayName'] : $row['firstName'].' '.$row['lastName'];
  526. }
  527. else
  528. {
  529. return $row;
  530. }
  531. }
  532. else
  533. {
  534. return FALSE;
  535. }
  536. }
  537. function search_posts($query = '', $ids = '')
  538. {
  539. if (!$query && !$ids)
  540. {
  541. return FALSE;
  542. }
  543. // default wheres
  544. $this->db->where('deleted', 0);
  545. $this->db->where('published', 1);
  546. $this->db->where('siteID', $this->siteID);
  547. // search
  548. if ($query)
  549. {
  550. // tidy query
  551. $q = $this->db->escape_like_str($query);
  552. $sql = '(postTitle LIKE "%'.$q.'%" OR body LIKE "%'.$q.'%")';
  553. }
  554. if ($ids)
  555. {
  556. $sql .= ' OR postID IN ('.implode(',', $ids).')';
  557. }
  558. $this->db->where($sql);
  559. $this->db->orderby('dateCreated', 'desc');
  560. // get comment count and post data
  561. $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.*');
  562. $query = $this->db->get('blog_posts');
  563. if ($query->num_rows() > 0)
  564. {
  565. return $query->result_array();
  566. }
  567. else
  568. {
  569. return FALSE;
  570. }
  571. }
  572. function add_view($pageID)
  573. {
  574. $this->db->set('views', 'views+1', FALSE);
  575. $this->db->where('postID', $pageID);
  576. $this->db->where('siteID', $this->siteID);
  577. $this->db->update('blog_posts');
  578. }
  579. }