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

/system/codecms/models/posts_model.php

https://bitbucket.org/jo_racingdesign/codecms
PHP | 255 lines | 144 code | 86 blank | 25 comment | 12 complexity | 0738c6cd48f3eea5cdb4c74ed0a86a64 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * CodeCMS an alternative responsive open source cms made from Philippines.
  4. *
  5. * @package CodeCMS
  6. * @author @jsd
  7. * @copyright Copyright (c) 2013
  8. * @license http://creativecommons.org/licenses/by-sa/3.0/deed.en_US
  9. * @link https://bitbucket.org/jsdecena/codecms
  10. * @since Version 0.1
  11. *
  12. */
  13. class Posts_model extends CI_Model {
  14. public $database = 'codecms';
  15. public $posts_table = 'posts';
  16. public $settings_table = 'settings';
  17. public $users_table = 'users';
  18. function __construct() {
  19. // Call the Model constructor
  20. parent::__construct();
  21. }
  22. /* =============================================================== BACK END =============================================================== */
  23. //LIST ALL THE POSTS
  24. function get_all_posts($post_type = 'post', $order_by ='post_id', $arrange_by ='desc', $limit = 10, $offset = 0){
  25. $this->db->select('*')->from('posts')->where('post_type', $post_type)->order_by($order_by , $arrange_by)->limit($limit, $offset);
  26. $query = $this->db->get();
  27. if ( $query->num_rows() > 0 ) :
  28. return $query->result_array();
  29. endif;
  30. }
  31. //WHEN POST ID AS ALREADY CREATED AND CAN BE RETRIEVE IN THE URI SEGMENT
  32. function get_post($post_id){
  33. $query = $this->db->get_where($this->posts_table, array( 'post_id' => $post_id ));
  34. if($query->num_rows() == 1):
  35. return $query->row();
  36. endif;
  37. }
  38. function get_page(){
  39. $post_title = $this->uri->segment(3);
  40. $query = $this->db->get_where($this->posts_table, array( 'slug' => $post_title ));
  41. if($query->num_rows() == 1):
  42. return $query->row();
  43. endif;
  44. }
  45. //WHEN THE ID IS NOT YET AVAILABLE FOR QUERY. GET THE MATCH ON THE TITLE IN THE DB TO GET ITS ID
  46. function get_post_id() {
  47. $query = $this->db->get_where($this->posts_table, array( 'title' => $this->input->post('title') ));
  48. return $query->row('post_id');
  49. }
  50. function insert_post(){
  51. $query = $this->db->get_where('users', array( 'email' => $this->session->userdata('email') ));
  52. $author = $query->row();
  53. $data = array(
  54. 'post_type' => $this->input->post('post_type'),
  55. 'users_id' => $author->users_id,
  56. 'title' => $this->input->post('title'),
  57. 'content' => $this->input->post('content'),
  58. 'slug' => strtolower(url_title($this->input->post('title'))),
  59. 'author' => $author->first_name ." ". $author->last_name,
  60. 'status' => $this->input->post('status'),
  61. 'date_add' => date("Y-m-d H:i:s")
  62. );
  63. $this->db->insert($this->posts_table, $data);
  64. return TRUE;
  65. }
  66. function update_post(){
  67. $data = array(
  68. 'title' => $this->input->post('title'),
  69. 'status' => $this->input->post('status'),
  70. 'content' => $this->input->post('content'),
  71. 'slug' => strtolower(url_title($this->input->post('title'))),
  72. );
  73. $this->db->where('post_id', $this->input->post('post_id'));
  74. $this->db->update($this->posts_table, $data);
  75. }
  76. //QUICK UPDATE A POST TYPE ( PAGE OR POST )
  77. function quick_update(){
  78. $data = array(
  79. 'status' => $this->input->post('status'),
  80. 'post_type' => $this->input->post('post_type')
  81. );
  82. $this->db->where('post_id', $this->input->post('post_id'));
  83. $this->db->update($this->posts_table, $data);
  84. return true;
  85. }
  86. //SINGLE POST DELETE
  87. function delete_post() {
  88. if ( $this->input->post('delete_post') ) :
  89. //DELETE A POST
  90. $this->db->delete('posts', array('post_id' => $this->input->post('delete_post')));
  91. else:
  92. //DELETE A PAGE
  93. $this->db->delete('posts', array('post_id' => $this->input->post('delete_page')));
  94. endif;
  95. return true;
  96. }
  97. //MULTIPLE DELETE
  98. function delete_post_selection($selectedIds) {
  99. $this->db->where_in('post_id', $selectedIds)->delete('posts');
  100. return true;
  101. }
  102. //COUNT ALL POSTS
  103. function count_all_posts(){
  104. $this->db->count_all_results('posts');
  105. $this->db->from('posts');
  106. $this->db->where('post_type', 'post');
  107. $query = $this->db->get();
  108. if ( $query->num_rows() > 0) :
  109. return $query->num_rows();
  110. endif;
  111. }
  112. //COUNT ALL PAGES
  113. function count_all_pages(){
  114. $this->db->count_all_results('posts');
  115. $this->db->from('posts');
  116. $this->db->where('post_type', 'page');
  117. $query = $this->db->get();
  118. if ( $query->num_rows() > 0) :
  119. return $query->num_rows();
  120. endif;
  121. }
  122. function update_post_settings() {
  123. $data = array(
  124. array(
  125. 'settings_id' => 1,
  126. 'settings_name' => 'post_page_chosen',
  127. 'settings_value' => $this->input->post('post_page_chosen')
  128. ),
  129. array(
  130. 'settings_id' => 2,
  131. 'settings_name' => 'post_per_page',
  132. 'settings_value' => $this->input->post('post_per_page')
  133. ),
  134. array(
  135. 'settings_id' => 3,
  136. 'settings_name' => 'arrange_post_by',
  137. 'settings_value' => $this->input->post('arrange_post_by')
  138. ),
  139. array(
  140. 'settings_id' => 4,
  141. 'settings_name' => 'order_post_by',
  142. 'settings_value' => $this->input->post('order_post_by')
  143. )
  144. );
  145. $this->db->update_batch('settings', $data, 'settings_id');
  146. }
  147. function view_post_settings() {
  148. $query = $this->db->get('settings');
  149. if ( $query->num_rows() > 0 ) :
  150. return $query->result_array();
  151. endif;
  152. }
  153. //CHECK FOR THE PAGE THAT WILL SHOW ALL THE POSTS. THIS IS SET IN THE DATABASE BY THE SETTINGS.
  154. function check_post_page(){
  155. $query = $this->db->get('cc_settings');
  156. if ( $query->num_rows() > 0 ) :
  157. foreach ($query->result_array() as $value) :
  158. return $value;
  159. endforeach;
  160. endif;
  161. }
  162. /* =============================================================== FRONT END =============================================================== */
  163. function view_post(){
  164. $query = $this->db->get_where('posts', array('slug' => $this->uri->segment(3,0)), 1);
  165. if($query->num_rows() == 1):
  166. return $query->row();
  167. endif;
  168. }
  169. }