PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/system/pyrocms/modules/news/models/news_m.php

https://github.com/ikhattab/pyrocms
PHP | 195 lines | 143 code | 39 blank | 13 comment | 31 complexity | 42a10dccb9ef1e063eb3a55ca210a5fb MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class News_m extends MY_Model
  3. {
  4. function get_all()
  5. {
  6. $this->db->select('news.*, c.title AS category_title, c.slug AS category_slug');
  7. $this->db->join('news_categories c', 'news.category_id = c.id', 'left');
  8. $this->db->order_by('created_on', 'DESC');
  9. return $this->db->get('news')->result();
  10. }
  11. function get_many_by($params = array())
  12. {
  13. $this->load->helper('date');
  14. if(!empty($params['category']))
  15. {
  16. if(is_numeric($params['category'])) $this->db->where('c.id', $params['category']);
  17. else $this->db->where('c.slug', $params['category']);
  18. }
  19. if(!empty($params['month']))
  20. {
  21. $this->db->where('MONTH(FROM_UNIXTIME(created_on))', $params['month']);
  22. }
  23. if(!empty($params['year']))
  24. {
  25. $this->db->where('YEAR(FROM_UNIXTIME(created_on))', $params['year']);
  26. }
  27. // Is a status set?
  28. if( !empty($params['status']) )
  29. {
  30. // If it's all, then show whatever the status
  31. if($params['status'] != 'all')
  32. {
  33. // Otherwise, show only the specific status
  34. $this->db->where('status', $params['status']);
  35. }
  36. }
  37. // Nothing mentioned, show live only (general frontend stuff)
  38. else
  39. {
  40. $this->db->where('status', 'live');
  41. }
  42. // By default, dont show future articles
  43. if(!isset($params['show_future']) || (isset($params['show_future']) && $params['show_future'] == FALSE))
  44. {
  45. $this->db->where('created_on <=', now());
  46. }
  47. // Limit the results based on 1 number or 2 (2nd is offset)
  48. if(isset($params['limit']) && is_array($params['limit'])) $this->db->limit($params['limit'][0], $params['limit'][1]);
  49. elseif(isset($params['limit'])) $this->db->limit($params['limit']);
  50. return $this->get_all();
  51. }
  52. function count_by($params = array())
  53. {
  54. $this->db->join('news_categories c', 'news.category_id = c.id', 'left');
  55. if(!empty($params['category']))
  56. {
  57. if(is_numeric($params['category'])) $this->db->where('c.id', $params['category']);
  58. else $this->db->where('c.slug', $params['category']);
  59. }
  60. if(!empty($params['month']))
  61. {
  62. $this->db->where('MONTH(FROM_UNIXTIME(created_on))', $params['month']);
  63. }
  64. if(!empty($params['year']))
  65. {
  66. $this->db->where('YEAR(FROM_UNIXTIME(created_on))', $params['year']);
  67. }
  68. // Is a status set?
  69. if( !empty($params['status']) )
  70. {
  71. // If it's all, then show whatever the status
  72. if($params['status'] != 'all')
  73. {
  74. // Otherwise, show only the specific status
  75. $this->db->where('status', $params['status']);
  76. }
  77. }
  78. // Nothing mentioned, show live only (general frontend stuff)
  79. else
  80. {
  81. $this->db->where('status', 'live');
  82. }
  83. return $this->db->count_all_results('news');
  84. }
  85. function insert($input = array())
  86. {
  87. if(isset($input['created_on_day']) && isset($input['created_on_month']) && isset($input['created_on_year']) )
  88. {
  89. $input['created_on'] = mktime(@$input['created_on_hour'], @$input['created_on_minute'], 0, $input['created_on_month'], $input['created_on_day'], $input['created_on_year']);
  90. unset($input['created_on_hour'], $input['created_on_minute'], $input['created_on_month'], $input['created_on_day'], $input['created_on_year']);
  91. }
  92. // Otherwise, use now
  93. else
  94. {
  95. $this->load->helper('date');
  96. $input['created_on'] = now();
  97. }
  98. return parent::insert($input);
  99. }
  100. function update($id, $input)
  101. {
  102. $this->load->helper('date');
  103. $input['updated_on'] = now();
  104. if(isset($input['created_on_day']) && isset($input['created_on_month']) && isset($input['created_on_year']) )
  105. {
  106. $input['created_on'] = mktime($input['created_on_hour'], $input['created_on_minute'], 0, $input['created_on_month'], $input['created_on_day'], $input['created_on_year']);
  107. unset($input['created_on_hour'], $input['created_on_minute'], $input['created_on_month'], $input['created_on_day'], $input['created_on_year']);
  108. }
  109. return parent::update($id, $input);
  110. }
  111. function publish($id = 0)
  112. {
  113. return parent::update($id, array('status' => 'live'));
  114. }
  115. // -- Archive ---------------------------------------------
  116. function get_archive_months()
  117. {
  118. $this->load->helper('date');
  119. $this->db->select('UNIX_TIMESTAMP(DATE_FORMAT(FROM_UNIXTIME(t1.created_on), "%Y-%m-02")) AS `date`', FALSE);
  120. $this->db->distinct();
  121. $this->db->select('(SELECT count(id) FROM news t2
  122. WHERE MONTH(FROM_UNIXTIME(t1.created_on)) = MONTH(FROM_UNIXTIME(t2.created_on))
  123. AND YEAR(FROM_UNIXTIME(t1.created_on)) = YEAR(FROM_UNIXTIME(t2.created_on))
  124. AND status = "live"
  125. AND created_on <= '.now().'
  126. ) as article_count');
  127. $this->db->where('status', 'live');
  128. $this->db->where('created_on <=', now());
  129. $this->db->having('article_count >', 0);
  130. $this->db->order_by('t1.created_on DESC');
  131. $query = $this->db->get('news t1');
  132. return $query->result();
  133. }
  134. // DIRTY frontend functions. Move to views
  135. function get_news_fragment($params = array())
  136. {
  137. $this->load->helper('date');
  138. $this->db->where('status', 'live');
  139. $this->db->where('created_on <=', now());
  140. $string = '';
  141. $this->db->order_by('created_on', 'DESC');
  142. $this->db->limit(5);
  143. $query = $this->db->get('news');
  144. if ($query->num_rows() > 0) {
  145. $this->load->helper('text');
  146. foreach ($query->result() as $blogs) {
  147. $string .= '<p>' . anchor('news/' . date('Y/m') . '/'. $blogs->slug, $blogs->title) . '<br />' . strip_tags($blogs->intro). '</p>';
  148. }
  149. }
  150. return $string ;
  151. }
  152. function check_slug($slug = '')
  153. {
  154. return parent::count_by('slug', $slug) == 0;
  155. }
  156. }