Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3.  
  4.  
  5. class Category_model extends CI_Model
  6.  
  7. {
  8.  
  9. public $v_fields=array('name', 'category.name');
  10.  
  11.  
  12.  
  13. function __construct()
  14.  
  15. {
  16.  
  17. parent::__construct();
  18.  
  19. }
  20.  
  21.  
  22.  
  23. function getList($table, $pagination=array()) {
  24.  
  25.  
  26.  
  27. // PAGINATION START
  28.  
  29. if((isset($pagination['cur_page'])) and !empty($pagination['per_page']))
  30.  
  31. {
  32.  
  33. $this->db->limit($pagination['per_page'],$pagination['cur_page']);
  34.  
  35. }
  36.  
  37. // PAGINATION END
  38.  
  39.  
  40.  
  41. // sort
  42.  
  43. $order_by = isset($_GET['sortBy']) && in_array($_GET['sortBy'], $this->v_fields)?$_GET['sortBy']:'';
  44.  
  45. $order = isset($_GET['order']) && $_GET['order']=='asc'?'asc':'desc';
  46.  
  47. if($order_by!=''){
  48.  
  49. $this->db->order_by($order_by, $order);
  50.  
  51. }
  52.  
  53.  
  54.  
  55. // end sort
  56.  
  57.  
  58.  
  59. // SEARCH START
  60.  
  61. if (!empty($_GET['searchValue']) && $_GET['searchValue']!="" && !empty($_GET['searchBy']) && $_GET['searchBy']!="" && in_array($_GET['searchBy'],$this->v_fields) ) {
  62.  
  63. $this->db->like($_GET['searchBy'],$_GET['searchValue']);
  64.  
  65. }
  66.  
  67. // SEARCH END
  68.  
  69.  
  70.  
  71. $this->db->select("$table.* , category.name as parent_id ");
  72.  
  73. $this->db->from($table);
  74.  
  75. $this->db->join("category", "category.parent_id = category.id", "left");
  76.  
  77. $this->db->order_by("id","desc");
  78.  
  79. $query = $this->db->get();
  80.  
  81. return $result = $query->result();
  82.  
  83. }
  84.  
  85.  
  86.  
  87. function getListTable($table) {
  88.  
  89. $this->db->select("*");
  90.  
  91. $this->db->from($table);
  92.  
  93. $query = $this->db->get();
  94.  
  95. return $result = $query->result();
  96.  
  97. }
  98.  
  99.  
  100.  
  101. function getRow($table, $id) {
  102.  
  103. $this->db->select('*');
  104.  
  105. $query = $this->db->get_where($table, array('id' => $id));
  106.  
  107. $data = $query->result();
  108.  
  109. return $data[0];
  110.  
  111. }
  112.  
  113.  
  114.  
  115. function getSelectedData($table, $field, $idArr) {
  116.  
  117. $this->db->select('*');
  118.  
  119. $this->db->from($table);
  120.  
  121. $this->db->where_in('id', $idArr);
  122.  
  123. $query = $this->db->get();
  124.  
  125. $data = $query->result();
  126.  
  127. foreach ($data as $key => $value) {
  128.  
  129. $arr[] = $value->$field;
  130.  
  131. }
  132.  
  133. return $arr;
  134.  
  135. }
  136.  
  137.  
  138.  
  139. function getCount($table, $key='', $value='') {
  140.  
  141. $this->db->select("$table.*");
  142.  
  143. if(isset($key) && isset($value) && !empty($key) && !empty($value))
  144.  
  145. {
  146.  
  147. $this->db->where($key,$value);
  148.  
  149. }
  150.  
  151. $this->db->from($table);
  152.  
  153. $this->db->join("category", "category.parent_id = category.id", "left");
  154.  
  155. $query = $this->db->get();
  156.  
  157. return $query->num_rows();
  158.  
  159. }
  160.  
  161.  
  162.  
  163. function insert($table, $data){
  164.  
  165. $this->db->insert($table, $data);
  166.  
  167. return $this->db->insert_id();
  168.  
  169. }
  170.  
  171.  
  172.  
  173. function multiSelectInsert($r_table, $field1, $value1, $field2, $value2=array())
  174.  
  175. {
  176.  
  177. $this->db->query("delete from $r_table where $field1='$value1'");
  178.  
  179. if ($r_table!="" && $field1!="" && $value1!="" && $field2!="" && count($value2)>0) {
  180.  
  181. for ($i=0; $i < count($value2); $i++) {
  182.  
  183. $data[] = array(
  184.  
  185. $field1 => $value1,
  186.  
  187. $field2 => $value2[$i],
  188.  
  189. );
  190.  
  191. }
  192.  
  193. $this->db->insert_batch($r_table, $data);
  194.  
  195. }
  196.  
  197. }
  198.  
  199.  
  200.  
  201. function getSelectedIds($table, $id, $select_field, $where_field)
  202.  
  203. {
  204.  
  205. $arr=array();
  206.  
  207. $this->db->select("$select_field");
  208.  
  209. $this->db->from($table);
  210.  
  211. $this->db->where("$where_field",$id);
  212.  
  213. $query = $this->db->get();
  214.  
  215. $data = $query->result();
  216.  
  217. foreach ($data as $key => $value) {
  218.  
  219. $arr[] = $value->$select_field;
  220.  
  221. }
  222.  
  223. return $arr;
  224.  
  225. }
  226.  
  227.  
  228.  
  229. function updateData($table, $data, $id)
  230.  
  231. {
  232.  
  233. $this->db->where("id",$id);
  234.  
  235. $this->db->update($table,$data);
  236.  
  237. }
  238.  
  239.  
  240.  
  241. function delete($table, $key='', $value='')
  242.  
  243. {
  244.  
  245. if(isset($key) && isset($value) && !empty($key) && !empty($value))
  246.  
  247. {
  248.  
  249. $this->db->where($key,$value);
  250.  
  251. }
  252.  
  253. $this->db->delete($table);
  254.  
  255. }
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263. public function uploadData(&$data, $file_name, $file_path, $postfix='', $allowedTypes)
  264.  
  265. {
  266.  
  267. $config = NULL;
  268.  
  269. $config['upload_path'] = $this->config->item($file_path);
  270.  
  271. $config['allowed_types'] = $allowedTypes;
  272.  
  273. if (isset($_FILES[$file_name]['name']) && !empty($_FILES[$file_name]['name']))
  274.  
  275. {
  276.  
  277. $this->load->library('upload', $config);
  278.  
  279. $this->upload->initialize($config);
  280.  
  281. $exts = explode(".",$_FILES[$file_name]['name']);
  282.  
  283. $_FILES[$file_name]['name'] = $exts[0].$postfix.".".end($exts);
  284.  
  285. if ( ! $this->upload->do_upload($file_name))
  286.  
  287. {
  288.  
  289. $data[$file_name.'_err'] = array('error' => $this->upload->display_errors());
  290.  
  291. }
  292.  
  293. else
  294.  
  295. {
  296.  
  297. $uploadImg = $this->upload->data();
  298.  
  299. if($uploadImg['file_name'] != '')
  300.  
  301. {
  302.  
  303. if (isset($_POST['old_'.$file_name]) && !empty($_POST['old_'.$file_name]))
  304.  
  305. {
  306.  
  307. @unlink($this->config->item($file_path).$_POST['old_'.$file_name]);
  308.  
  309. }
  310.  
  311. $data[$file_name] = $uploadImg['file_name'];
  312.  
  313. }
  314.  
  315. }
  316.  
  317. }
  318.  
  319. elseif (isset($_POST['old_'.$file_name]) && !empty($_POST['old_'.$file_name]))
  320.  
  321. {
  322.  
  323. $data[$file_name] = $_POST['old_'.$file_name];
  324.  
  325. }
  326.  
  327. }
  328.  
  329.  
  330.  
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement