Advertisement
atysaa

user.php

Aug 29th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class User extends CI_Controller {
  5.  
  6. public function index()
  7. {
  8. $this->load->model('user_model');
  9.  
  10. $getData=$this->user_model->getPost();
  11.  
  12. $data = array(
  13. 'getdata' => $getData,
  14. );
  15.  
  16. $this->slice->view('user.view_user', $data);
  17. }
  18.  
  19. public function edit_user()
  20. {
  21. $this->load->model('user_model');
  22.  
  23.  
  24. $getData=$this->user_model->getPost();
  25. $data = array(
  26. 'getdata' => $getData,
  27. );
  28. print_r($getData);
  29. $this->slice->view('user.edit_user', $data);
  30. }
  31.  
  32.  
  33. // function show_user_id() {
  34. // $id = $this->uri->segment(3);
  35. // $data['tblUser'] = $this->user_model->show_user();
  36. // $data['tblUser'] = $this->user_model->show_user_id($id);
  37. // $this->load->view('user.edit_user', $data);
  38.  
  39. // }
  40. public function tambah()
  41. {
  42. $file_name = date('Ymdhis').".jpg";
  43. $config['upload_path']='./uploads/';
  44. $config['allowed_types']='jpg';
  45. $config['overwrite']=TRUE;
  46. $config['max_size']='1000000';
  47. $config['file_name']=$file_name;
  48. $this->load->library('upload', $config);
  49.  
  50. if(!$this->upload->do_upload('fotouser')){
  51.  
  52. $error = $this->upload->display_errors('','');
  53. }
  54.  
  55. $this->load->library('form_validation');
  56. $this->form_validation->set_rules(
  57. 'namauser', 'nama',
  58. 'required|min_length[5]|max_length[15]|is_unique[tblUser.nama]',
  59. array(
  60. 'required' => 'You have not provided %s.',
  61. 'is_unique' => 'This %s already exists.'
  62. )
  63. );
  64. $this->form_validation->set_rules('emailuser', 'Email', 'required|valid_email|is_unique[tbluser.email]');
  65. $this->form_validation->set_rules('fnamauser','Fullname','required');
  66. $this->form_validation->set_rules('passworduser', 'Password', 'trim|required|min_length[6]',
  67. array('required' => 'You must provide a %s.')
  68. );
  69. $this->form_validation->set_rules('passworduser2', 'Password Confirmation', 'trim|required|matches[passworduser]');
  70. $this->form_validation->set_rules('tipeuser','tipe','required');
  71. $this->form_validation->set_rules('datepicker1','tgl_daftar','required');
  72. $this->form_validation->set_error_delimiters('', '');
  73.  
  74. if($this->form_validation->run() != false){
  75. $data = array(
  76. 'fullname' => $this->input->post('fnamauser'),
  77. 'nama' => $this->input->post('namauser'),
  78. 'email' => $this->input->post('emailuser'),
  79. 'password' => $this->input->post('passworduser'),
  80. 'tgl_daftar'=> date('Y-m-d',strtotime($this->input->post('datepicker1'))),
  81. 'tipe' => $this->input->post('tipeuser'),
  82. 'foto' => $file_name,
  83. );
  84.  
  85. $insert = $this->db->insert('tbluser', $data);
  86.  
  87. $this->createJSON($insert, ["User baru berhasil ditambahkan.".$this->input->post('tgl_daftar'), "User baru gagal ditambahkan."]);
  88. }
  89. else
  90. {
  91. $this->createJSON(false, ["User baru berhasil.", $data['errors'] = validation_errors()]);
  92.  
  93. }
  94.  
  95. // return 0;
  96.  
  97. }
  98.  
  99. public function edit()
  100. {
  101. $file_name = $this->input->post('file_name');
  102. $config['upload_path']='./uploads/';
  103. $config['allowed_types']='jpg';
  104. $config['overwrite']=TRUE;
  105. $config['max_size']='1000000';
  106. $config['file_name']= $file_name;
  107. $this->load->library('upload', $config);
  108.  
  109. if(!$this->upload->do_upload('fotouser')){
  110.  
  111. $error = $this->upload->display_errors('','');
  112. }
  113.  
  114. $this->load->library('form_validation');
  115. $this->form_validation->set_rules('fnamauser','Fullname');
  116. $this->form_validation->set_rules(
  117. 'namauser', 'nama',
  118. 'min_length[5]|max_length[15]|is_unique[tblUser.nama]');
  119. $this->form_validation->set_rules('emailuser', 'Email', 'valid_email|is_unique[tbluser.email]');
  120. $this->form_validation->set_rules('passworduser', 'Password', 'trim|min_length[6]');
  121. $this->form_validation->set_rules('passworduser2', 'Password Confirmation', 'trim|matches[passworduser');
  122. $this->form_validation->set_rules('tipeuser','tipe');
  123. $this->form_validation->set_rules('datepicker1','tgl_daftar');
  124. $this->form_validation->set_error_delimiters('', '');
  125.  
  126. if($this->form_validation->run() != false){
  127. // $this->createJSON(true, ["User berhasil dihapus.", "User gagal dihapus"]);
  128. $data = array(
  129. 'id_user' => $this->input->post('id'),
  130. 'fullname' => $this->input->post('fnamauser'),
  131. 'nama' => $this->input->post('namauser'),
  132. 'email' => $this->input->post('emailuser'),
  133. 'password' => $this->input->post('passworduser'),
  134. 'tgl_daftar'=> date('Y-m-d',strtotime($this->input->post('datepicker1'))),
  135. 'tipe' => $this->input->post('tipeuser'),
  136. 'foto' => $file_name,
  137. );
  138.  
  139.  
  140. $this->db->where('id_user', $this->input->post('id_user'));
  141. $update = $this->db->update('tblUser', $data, array('id_user' => $id));
  142.  
  143. $this->createJSON(true, ["User baru berhasil Diedit.".$this->input->post('fnamauser'), "Kategori baru gagal ditambahkan."]);
  144. }
  145. else
  146. {
  147. $this->createJSON(false, ["User baru berhasil.", $data['errors'] = validation_errors()]);
  148.  
  149. }
  150.  
  151. }
  152.  
  153. public function delete()
  154. {
  155. $this->db->delete('tblUser',['id_user'=>$this->input->post('id')]);
  156.  
  157. $this->createJSON(true, ["User berhasil dihapus.", "User gagal dihapus"]);
  158. }
  159.  
  160. /* BEGIN HELPER FUNCTION */
  161. private function createJSON($condition, $text, $push = [])
  162. {
  163. if ($condition) :
  164. $json = array(
  165. 'status' => 'success',
  166. 'title' => 'God Job!',
  167. 'text' => $text[0]
  168. );
  169.  
  170. if ($push) :
  171. foreach ($push as $key => $val) :
  172. $json[$key] = $val;
  173. endforeach;
  174. endif;
  175. else :
  176. $json = array(
  177. 'status' => 'error',
  178. 'title' => 'Oops!',
  179. 'text' => $text[1]
  180. );
  181. endif;
  182.  
  183. header('Content-Type: application/json');
  184. echo json_encode($json, JSON_PRETTY_PRINT);
  185. }
  186. /* END HELPER FUNCTION */
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement