/application/modules/blog/models/blog_m.php

https://github.com/arimogi/Chimera · PHP · 253 lines · 195 code · 41 blank · 17 comment · 30 complexity · e093d5f89ea120523358721b5c2da6b4 MD5 · raw file

  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Blog_m extends MY_Model {
  3. protected $_table = 'blog';
  4. function get_all()
  5. {
  6. $this->db->select('blog.*, c.title AS category_title, c.slug AS category_slug');
  7. $this->db->join('blog_categories c', 'blog.category_id = c.id', 'left');
  8. $this->db->order_by('created_on', 'DESC');
  9. return $this->db->get('blog')->result();
  10. }
  11. function get($id)
  12. {
  13. $this->db->where(array('id' => $id));
  14. return $this->db->get('blog')->row();
  15. }
  16. function get_many_by($params = array())
  17. {
  18. $this->load->helper('date');
  19. if (!empty($params['category']))
  20. {
  21. if (is_numeric($params['category']))
  22. $this->db->where('c.id', $params['category']);
  23. else
  24. $this->db->where('c.slug', $params['category']);
  25. }
  26. if (!empty($params['month']))
  27. {
  28. $this->db->where('MONTH(FROM_UNIXTIME(created_on))', $params['month']);
  29. }
  30. if (!empty($params['year']))
  31. {
  32. $this->db->where('YEAR(FROM_UNIXTIME(created_on))', $params['year']);
  33. }
  34. // Is a status set?
  35. if (!empty($params['status']))
  36. {
  37. // If it's all, then show whatever the status
  38. if ($params['status'] != 'all')
  39. {
  40. // Otherwise, show only the specific status
  41. $this->db->where('status', $params['status']);
  42. }
  43. }
  44. // Nothing mentioned, show live only (general frontend stuff)
  45. else
  46. {
  47. $this->db->where('status', 'live');
  48. }
  49. // By default, dont show future posts
  50. if (!isset($params['show_future']) || (isset($params['show_future']) && $params['show_future'] == FALSE))
  51. {
  52. $this->db->where('created_on <=', now());
  53. }
  54. // Limit the results based on 1 number or 2 (2nd is offset)
  55. if (isset($params['limit']) && is_array($params['limit']))
  56. $this->db->limit($params['limit'][0], $params['limit'][1]);
  57. elseif (isset($params['limit']))
  58. $this->db->limit($params['limit']);
  59. return $this->get_all();
  60. }
  61. function count_by($params = array())
  62. {
  63. $this->db->join('blog_categories c', 'blog.category_id = c.id', 'left');
  64. if (!empty($params['category']))
  65. {
  66. if (is_numeric($params['category']))
  67. $this->db->where('c.id', $params['category']);
  68. else
  69. $this->db->where('c.slug', $params['category']);
  70. }
  71. if (!empty($params['month']))
  72. {
  73. $this->db->where('MONTH(FROM_UNIXTIME(created_on))', $params['month']);
  74. }
  75. if (!empty($params['year']))
  76. {
  77. $this->db->where('YEAR(FROM_UNIXTIME(created_on))', $params['year']);
  78. }
  79. // Is a status set?
  80. if (!empty($params['status']))
  81. {
  82. // If it's all, then show whatever the status
  83. if ($params['status'] != 'all')
  84. {
  85. // Otherwise, show only the specific status
  86. $this->db->where('status', $params['status']);
  87. }
  88. }
  89. // Nothing mentioned, show live only (general frontend stuff)
  90. else
  91. {
  92. $this->db->where('status', 'live');
  93. }
  94. return $this->db->count_all_results('blog');
  95. }
  96. function update($id, $input)
  97. {
  98. $input['updated_on'] = now();
  99. return parent::update($id, $input);
  100. }
  101. function publish($id = 0)
  102. {
  103. return parent::update($id, array('status' => 'live'));
  104. }
  105. // -- Archive ---------------------------------------------
  106. function get_archive_months()
  107. {
  108. $this->db->select('UNIX_TIMESTAMP(DATE_FORMAT(FROM_UNIXTIME(t1.created_on), "%Y-%m-02")) AS `date`', FALSE);
  109. $this->db->distinct();
  110. $this->db->select('(SELECT count(id) FROM blog t2
  111. WHERE MONTH(FROM_UNIXTIME(t1.created_on)) = MONTH(FROM_UNIXTIME(t2.created_on))
  112. AND YEAR(FROM_UNIXTIME(t1.created_on)) = YEAR(FROM_UNIXTIME(t2.created_on))
  113. AND status = "live"
  114. AND created_on <= ' . now() . '
  115. ) as post_count');
  116. $this->db->where('status', 'live');
  117. $this->db->where('created_on <=', now());
  118. $this->db->having('post_count >', 0);
  119. $this->db->order_by('t1.created_on DESC');
  120. $query = $this->db->get('blog t1');
  121. return $query->result();
  122. }
  123. // DIRTY frontend functions. Move to views
  124. function get_blog_fragment($params = array())
  125. {
  126. $this->load->helper('date');
  127. $this->db->where('status', 'live');
  128. $this->db->where('created_on <=', now());
  129. $string = '';
  130. $this->db->order_by('created_on', 'DESC');
  131. $this->db->limit(5);
  132. $query = $this->db->get('blog');
  133. if ($query->num_rows() > 0)
  134. {
  135. $this->load->helper('text');
  136. foreach ($query->result() as $blog)
  137. {
  138. $string .= '<p>' . anchor('blog/' . date('Y/m') . '/' . $blog->slug, $blog->title) . '<br />' . strip_tags($blog->intro) . '</p>';
  139. }
  140. }
  141. return $string;
  142. }
  143. function check_exists($field, $value = '', $id = 0)
  144. {
  145. if (is_array($field))
  146. {
  147. $params = $field;
  148. $id = $value;
  149. }
  150. else
  151. {
  152. $params[$field] = $value;
  153. }
  154. $params['id !='] = (int) $id;
  155. return parent::count_by($params) == 0;
  156. }
  157. /**
  158. * Searches blog posts based on supplied data array
  159. * @param $data array
  160. * @return array
  161. */
  162. public function search($data = array())
  163. {
  164. if (array_key_exists('category_id', $data))
  165. {
  166. $this->db->where('category_id', $data['category_id']);
  167. }
  168. if (array_key_exists('status', $data))
  169. {
  170. $this->db->where('status', $data['status']);
  171. }
  172. if (array_key_exists('keywords', $data))
  173. {
  174. $matches = array();
  175. if (strstr($data['keywords'], '%'))
  176. {
  177. preg_match_all('/%.*?%/i', $data['keywords'], $matches);
  178. }
  179. if (!empty($matches[0]))
  180. {
  181. foreach ($matches[0] as $match)
  182. {
  183. $phrases[] = str_replace('%', '', $match);
  184. }
  185. }
  186. else
  187. {
  188. $temp_phrases = explode(' ', $data['keywords']);
  189. foreach ($temp_phrases as $phrase)
  190. {
  191. $phrases[] = str_replace('%', '', $phrase);
  192. }
  193. }
  194. $counter = 0;
  195. foreach ($phrases as $phrase)
  196. {
  197. if ($counter == 0)
  198. {
  199. $this->db->like('blog.title', $phrase);
  200. }
  201. else
  202. {
  203. $this->db->or_like('blog.title', $phrase);
  204. }
  205. $this->db->or_like('blog.body', $phrase);
  206. $this->db->or_like('blog.intro', $phrase);
  207. $counter++;
  208. }
  209. }
  210. return $this->get_all();
  211. }
  212. }