/application/models/setting_model.php

https://gitlab.com/exabyte-lab/tiffinbox · PHP · 196 lines · 160 code · 24 blank · 12 comment · 16 complexity · ed17f4142722e8b291e8f92c7efa3f0f MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Setting_Model extends CI_Model {
  3. public $table = 'setting';
  4. public function __construct()
  5. {
  6. $this->load->library('session');
  7. }
  8. function insert($data){
  9. $insert_data = array(
  10. 'institute_name' =>$data['institute_name'],
  11. 'address' =>$data['address'],
  12. 'contact_no' =>$data['contact_no'],
  13. 'email' =>$data['email'],
  14. );
  15. if(@$_FILES['institute_logo']['name']){
  16. var_dump($_FILES['institute_logo']);
  17. $insert_data['institute_logo'] = $this->uploadinputfile($data);
  18. }
  19. $this->db->where('Id', $data['id']);
  20. if($this->db->update('setting',$insert_data)):
  21. return TRUE;
  22. else:
  23. return FALSE;
  24. endif;
  25. }
  26. function add_slider($data){
  27. $query = $this->db->get($this->table, 1);
  28. if($query->num_rows() > 0){
  29. $row = $query->result()[0];
  30. if($row->sliders){
  31. $sliders = unserialize($row->sliders);
  32. array_push($sliders, [
  33. 'image'=> site_url().'/media/slider/'.$this->upload_image($data, 'image', 'title', 'slider', './media/slider'),
  34. 'title'=>$data['title'],
  35. 'content'=>$data['content']
  36. ]);
  37. }else{
  38. $sliders = [[
  39. 'image'=> site_url().'/media/slider/'.$this->upload_image($data, 'image', 'title', 'slider', './media/slider'),
  40. 'title'=>$data['title'],
  41. 'content'=>$data['content']
  42. ]];
  43. }
  44. $insert_data = [
  45. 'sliders'=>serialize($sliders)
  46. ];
  47. $this->db->where('id', $row->Id);
  48. if($this->db->update('setting',$insert_data)){
  49. return true;
  50. }else{
  51. return false;
  52. }
  53. }else{
  54. $sliders = [[
  55. 'image'=> site_url().'media/slider/'.$this->upload_image($data, 'image', 'slider', './media/slider'),
  56. 'title'=>$data['title'],
  57. 'content'=>$data['content']
  58. ]];
  59. $insert_data = [
  60. 'sliders'=>serialize($sliders)
  61. ];
  62. if($this->db->insert('setting',$insert_data)){
  63. return true;
  64. }else{
  65. return false;
  66. }
  67. }
  68. }
  69. public function update($insert_data){
  70. $query = $this->db->get($this->table, 1);
  71. if($query->num_rows() > 0){
  72. $row = $query->result()[0];
  73. $this->db->where('id', $row->Id);
  74. if($this->db->update('setting',$insert_data)){
  75. return true;
  76. }else{
  77. return false;
  78. }
  79. }else{
  80. if($this->db->insert('setting',$insert_data)){
  81. return true;
  82. }else{
  83. return false;
  84. }
  85. }
  86. }
  87. public function remove_slide($id){
  88. $query = $this->db->get($this->table, 1);
  89. if($query->num_rows() > 0){
  90. $row = $query->result()[0];
  91. if($row->sliders){
  92. $sliders = unserialize($row->sliders);
  93. $slide = $sliders[$id];
  94. $image = str_replace(site_url(), '', $slide['image']) ;
  95. file_exists('.'.$image) && unlink('.'.$image);
  96. array_splice($sliders, $id, 1);
  97. $insert_data = [
  98. 'sliders'=>serialize($sliders)
  99. ];
  100. $this->db->where('id', $row->Id);
  101. if($this->db->update('setting',$insert_data)){
  102. return true;
  103. }else{
  104. return false;
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. #file updload
  111. public function uploadinputfile($data){
  112. $new_file_name=NULL;
  113. //start file upload and resize process
  114. $config['upload_path'] = './media/schools';
  115. if(!is_dir($config['upload_path'])):
  116. mkdir($config['upload_path']);
  117. endif;
  118. $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
  119. //load ci upload library:
  120. $this->load->library('upload', $config);
  121. //check for upload:
  122. $this->upload->do_upload('institute_logo');
  123. // Resize the Image 2:
  124. $file_data = $this->upload->data(); // get the file upload data
  125. $new_file_name = 'school_'.$data['institute_name'].mb_strtolower($file_data['file_ext']);
  126. $photo_name = 'school_'.$data['institute_name'].mb_strtolower($file_data['file_ext']);
  127. $filenew = rename($config['upload_path']
  128. .'/'.$file_data['file_name'], $config['upload_path'].'/'.$new_file_name);
  129. $resize_me['image_library'] = 'gd2';
  130. $resize_me['source_image'] = $config['upload_path'].'/'.$new_file_name;
  131. $resize_me['create_thumb'] = TRUE;
  132. $resize_me['maintain_ratio'] = TRUE;
  133. $resize_me['master_dim'] = 'auto';
  134. $resize_me['width'] = 100;
  135. $resize_me['height'] = 100;
  136. //load the library:
  137. $this->load->library('image_lib', $resize_me);
  138. $this->image_lib->resize();
  139. return $photo_name;
  140. }
  141. #file updload
  142. public function upload_image($data, $attr, $rmAttr, $prefix, $path='./media/schools', $width=1024, $height=700){
  143. $new_file_name=NULL;
  144. //start file upload and resize process
  145. $config['upload_path'] = $path;
  146. if(!is_dir($config['upload_path'])):
  147. mkdir($config['upload_path']);
  148. endif;
  149. $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
  150. //load ci upload library:
  151. $this->load->library('upload', $config);
  152. //check for upload:
  153. $this->upload->do_upload($attr);
  154. // Resize the Image 2:
  155. $file_data = $this->upload->data(); // get the file upload data
  156. $new_file_name = $prefix.'_'.$data[$rmAttr].mb_strtolower($file_data['file_ext']);
  157. $photo_name = $prefix.'_'.$data[$rmAttr].mb_strtolower($file_data['file_ext']);
  158. $filenew = rename($config['upload_path']
  159. .'/'.$file_data['file_name'], $config['upload_path'].'/'.$new_file_name);
  160. $resize_me['image_library'] = 'gd2';
  161. $resize_me['source_image'] = $config['upload_path'].'/'.$new_file_name;
  162. $resize_me['create_thumb'] = false;
  163. $resize_me['maintain_ratio'] = TRUE;
  164. $resize_me['master_dim'] = 'auto';
  165. $resize_me['width'] = $width;
  166. $resize_me['height'] = $height;
  167. //load the library:
  168. $this->load->library('image_lib', $resize_me);
  169. $this->image_lib->resize();
  170. return $photo_name;
  171. }
  172. }