/Quản lý website bán vé máy bay PHP/application/models/mdnews.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien · PHP · 217 lines · 206 code · 11 blank · 0 comment · 8 complexity · 51b38c3daf1b8ecd474d6bda58dadf8c MD5 · raw file

  1. <?php
  2. class MdNews extends CI_Model{
  3. var $obj;
  4. function __Construct(){
  5. parent::__Construct();
  6. $this->obj=&get_instance();
  7. }
  8. function GetNews($id, $type){
  9. $this->db->where(array('news.id'=>$id, 'type'=>$type));
  10. $this->db->from('news');
  11. $this->db->join('news_vi', 'news.id=news_vi.id');
  12. $this->db->join('news_en', 'news.id=news_en.id');
  13. $data=$this->db->get();
  14. if($data->num_rows()!=0){
  15. $data=$data->result_array();
  16. return $data[0];
  17. }else{
  18. return false;
  19. }
  20. }
  21. function CheckSub($sub){
  22. $this->db->where(array('type'=>'news', 'subtype'=>$sub));
  23. $data=$this->db->get('news')->num_rows();
  24. if($data==0){
  25. return false;
  26. }else{
  27. return true;
  28. }
  29. }
  30. function GetTopNewsBySub($sub){
  31. $this->db->where(array('type'=>'news', 'subtype'=>$sub));
  32. $this->db->from('news');
  33. $this->db->join('news_vi', 'news.id=news_vi.id');
  34. $this->db->limit(5);
  35. return $this->db->get()->result_array();
  36. }
  37. function GetAllNewsBySub($sub){
  38. $this->db->where(array('type'=>'news', 'subtype'=>$sub));
  39. $this->db->from('news');
  40. $this->db->join('news_vi', 'news.id=news_vi.id');
  41. return $this->db->get()->result_array();
  42. }
  43. function GetNewsBySub($id, $sub){
  44. $this->db->where(array('news.id'=>$id, 'type'=>'news', 'subtype'=>$sub));
  45. $this->db->from('news');
  46. $this->db->join('news_vi', 'news.id=news_vi.id');
  47. $data=$this->db->get();
  48. if($data->num_rows()!=0){
  49. $data=$data->result_array();
  50. return $data[0];
  51. }else{
  52. return false;
  53. }
  54. }
  55. function GetNewsNotId($id){
  56. $this->db->where('id', $id);
  57. $data=$this->db->get('news')->result_array();
  58. $this->db->where("news.id != '{$data[0]['id']}' and type='{$data[0]['type']}' and subtype='{$data[0]['subtype']}'");
  59. $this->db->order_by('date desc, time desc');
  60. $this->db->from('news');
  61. $this->db->join('news_vi', 'news.id=news_vi.id');
  62. return $this->db->get()->result_array();
  63. }
  64. function Delete_News($id, $type, $url_dir='public/uploads/news/'){
  65. $data=$this->GetNews($id, $type);
  66. unlink($url_dir.$data['image']);
  67. $this->db->where(array('id'=>$id, 'type'=>$type));
  68. $this->db->delete('news');
  69. $this->db->where('id',$id);
  70. $this->db->delete('news_vi');
  71. $this->db->where('id',$id);
  72. $this->db->delete('news_en');
  73. }
  74. function Change_News($type, $sub='', $url_dir='public/uploads/news/'){
  75. $id=$this->uri->segment(4);
  76. $this->load->helper('unicode');
  77. $this->obj->load->model('mdstatistics');
  78. $subtype=strip_tags($this->input->post('sub'));
  79. $log=$this->obj->mdstatistics->get_log();
  80. $title_vi=strip_tags($this->input->post('title_vi'));
  81. $title_en=strip_tags($this->input->post('title_en'));
  82. $date=ngay();
  83. $time=gio();
  84. $summary_vi=nl2br(strip_tags($this->input->post('summary_vi')));
  85. $summary_en=nl2br(strip_tags($this->input->post('summary_en')));
  86. $content_vi=$this->input->post('content_vi');
  87. $content_en=$this->input->post('content_en');
  88. $image=$_FILES['user_file']['name'];
  89. if($image!=''){
  90. $data=$this->GetNews($id, $type);
  91. unlink($url_dir.$data['image']);
  92. $this->load->library('my_img');
  93. $data=$this->my_img->upload($url_dir);
  94. if($data['error']==true){
  95. $image=$data['upload_data']['file_name'];
  96. $this->my_img->doresize(300, 190, $data['upload_data']['full_path'], $data['upload_data']['full_path']);
  97. }else{
  98. $image='default.jpg';
  99. }
  100. $this->db->where('id', $id);
  101. $this->db->update('news', array(
  102. 'date_update' => $date,
  103. 'time_update' => $time,
  104. 'log' => $log,
  105. 'image' => $image,
  106. 'subtype' => $subtype,
  107. ));
  108. $this->db->where('id', $id);
  109. $this->db->update('news_vi', array(
  110. 'summary_vi' => $summary_vi,
  111. 'content_vi' => $content_vi,
  112. 'title_vi' => $title_vi,
  113. ));
  114. $this->db->where('id', $id);
  115. $this->db->update('news_en', array(
  116. 'summary_en' => $summary_en,
  117. 'content_en' => $content_en,
  118. 'title_en' => $title_en,
  119. ));
  120. }else{
  121. $this->db->where('id', $id);
  122. $this->db->update('news', array(
  123. 'date_update' => $date,
  124. 'time_update' => $time,
  125. 'log' => $log,
  126. 'subtype' => $subtype,
  127. ));
  128. $this->db->where('id', $id);
  129. $this->db->update('news_vi', array(
  130. 'summary_vi' => $summary_vi,
  131. 'content_vi' => $content_vi,
  132. 'title_vi' => $title_vi,
  133. ));
  134. $this->db->where('id', $id);
  135. $this->db->update('news_en', array(
  136. 'summary_en' => $summary_en,
  137. 'content_en' => $content_en,
  138. 'title_en' => $title_en,
  139. ));
  140. }
  141. }
  142. function Add_News($type, $sub='', $url_dir='public/uploads/news/'){
  143. $this->load->helper('unicode');
  144. $this->obj->load->model('mdstatistics');
  145. $subtype=strip_tags($this->input->post('sub'));
  146. $log=$this->obj->mdstatistics->get_log();
  147. $title_vi=strip_tags($this->input->post('title_vi'));
  148. $title_en=strip_tags($this->input->post('title_en'));
  149. $id=khongdau($title_vi).rand5str();
  150. $date=ngay();
  151. $time=gio();
  152. $summary_vi=nl2br(strip_tags($this->input->post('summary_vi')));
  153. $summary_en=nl2br(strip_tags($this->input->post('summary_en')));
  154. $content_vi=$this->input->post('content_vi');
  155. $content_en=$this->input->post('content_en');
  156. $image=$_FILES['user_file']['name'];
  157. if($image!=''){
  158. $this->load->library('my_img');
  159. $data=$this->my_img->upload($url_dir);
  160. if($data['error']==true){
  161. $image=$data['upload_data']['file_name'];
  162. $this->my_img->doresize(300, 190, $data['upload_data']['full_path'], $data['upload_data']['full_path']);
  163. }else{
  164. $image='default.jpg';
  165. }
  166. }else{
  167. $image='default.jpg';
  168. }
  169. $this->db->insert('news', array(
  170. 'id' => $id,
  171. 'type' => $type,
  172. 'order' => time(),
  173. 'date' => $date,
  174. 'time' => $time,
  175. 'log' => $log,
  176. 'image' => $image,
  177. 'subtype' => $subtype,
  178. ));
  179. $this->db->insert('news_vi', array(
  180. 'id' => $id,
  181. 'summary_vi'=> $summary_vi,
  182. 'content_vi'=> $content_vi,
  183. 'title_vi' => $title_vi,
  184. ));
  185. $this->db->insert('news_en', array(
  186. 'id' => $id,
  187. 'summary_en'=> $summary_en,
  188. 'content_en'=> $content_en,
  189. 'title_en' => $title_en,
  190. ));
  191. }
  192. function GetCountByType($type){
  193. $this->db->where('type', $type);
  194. return $this->db->get('news')->num_rows();
  195. }
  196. function GetNewsByType($type, $perpage, $page){
  197. $this->db->from('news');
  198. $this->db->where('type', $type);
  199. $this->db->limit($perpage, $page);
  200. $this->db->order_by('date desc, time desc');
  201. $this->db->join('news_vi', 'news.id=news_vi.id');
  202. $this->db->join('news_en', 'news.id=news_en.id');
  203. return $this->db->get()->result_array();
  204. }
  205. }
  206. ?>