Advertisement
timonte

home

May 11th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Home extends CI_Controller {
  5.  
  6. function __construct()
  7. {
  8. parent::__construct();
  9. $this->load->library(array('template', 'cart'));
  10. $this->load->model('app_home');
  11. $this->load->library('form_validation');
  12. }
  13.  
  14. public function index()
  15. {
  16. $data['data'] = $this->app_home->get_where('t_items', ['status' => 1]);
  17. $this->template->alfatihmart('home', $data);
  18. }
  19.  
  20. public function kategori()
  21. {
  22. if (!$this->uri->segment(3)){
  23. redirect('home');
  24. }
  25. $url = strtolower(str_replace([' ','%20','_'], '-', $this->uri->segment(3)));
  26. $table = 't_kategori k JOIN t_rkategori rk ON (k.id_kategori = rk.id_kategori) JOIN t_items i ON (rk.id_item = i.id_item)';
  27.  
  28. $data['data'] = $this->app_home->get_where($table, ['i.status' => 1, 'k.url' -> $url]);
  29. $data['url'] = ucwords(str_replace(['-','%20','_'], ' ', $this->uri->segment(3)));
  30. $this->template->alfatihmart('home', $data);
  31. }
  32.  
  33. public function detail()
  34. {
  35. if (is_numeric($this->uri->segment(3)))
  36. {
  37. $id = $this->uri->segment(3);
  38. $data['data'] = $this->app_home->get_where('t_items', array('id_item' => $id));
  39.  
  40. $this->template->alfatihmart('item_detail', $data);
  41. } else {
  42. redirect('home');
  43. }
  44. }
  45.  
  46. public function registrasi()
  47. {
  48. if($this->input->post('submit', TRUE) == 'Submit')
  49. {
  50. $this->load->library('form_validation');
  51.  
  52. $this->form_validation->set_rules('nama1', 'Nama Depan', "required|min_length[3]|regex_match[/^[a-zA-Z'.]+$/]");
  53. $this->form_validation->set_rules('nama2', 'Nama Belakang', "regex_match[/^[a-zA-Z'.]+$/]");
  54. $this->form_validation->set_rules('user', 'Username', "required|min_length[5]|regex_match[/^[a-zA-Z0-9]+$/]");
  55. $this->form_validation->set_rules('email', 'Email', "required|valid_email");
  56. $this->form_validation->set_rules('pass1', 'Password', "required|min_length[5]");
  57. $this->form_validation->set_rules('pass2', 'Ketik Ulang Password', "required|matches[pass1]");
  58. $this->form_validation->set_rules('jk', 'Jenis Kelamin', "required");
  59. $this->form_validation->set_rules('telp', 'Telp', "required|min_length[5]|numeric");
  60. $this->form_validation->set_rules('alamat', 'Alamat', "required|min_length[5]");
  61.  
  62. if ($this->form_validation->run() == TRUE)
  63. {
  64. $data = array(
  65. 'username' => $this->input->post('user', TRUE),
  66. 'fullname' => $this->input->post('nama1', TRUE).' '.$this->input->post('nama2', TRUE),
  67. 'email' => $this->input->post('email', TRUE),
  68. 'password' => password_hash($this->input->post('pass1', TRUE), PASSWORD_DEFAULT, ['cost' => 10]),
  69. 'jk' => $this->input->post('jk', TRUE),
  70. 'telp' => $this->input->post('telp', TRUE),
  71. 'alamat' => $this->input->post('alamat', TRUE),
  72. 'status' => 1
  73. );
  74.  
  75. if ($this->app_home->insert('t_users', $data))
  76. {
  77. redirect('home');
  78. } else {
  79. echo '<script type="text/javascript">alert("Username / Email tidak tersedia");</script>';
  80. }
  81. }
  82. }
  83.  
  84. if ($this->session->userdata('user_login') == TRUE)
  85. {
  86. redirect('home');
  87. }
  88.  
  89. $data = array(
  90. 'user' => $this->input->post('user', TRUE),
  91. 'nama1' => $this->input->post('nama1', TRUE),
  92. 'nama2' =>$this->input->post('nama2', TRUE),
  93. 'email' => $this->input->post('email', TRUE),
  94. 'jk' => $this->input->post('jk', TRUE),
  95. 'telp' => $this->input->post('telp', TRUE),
  96. 'alamat' => $this->input->post('alamat', TRUE),
  97. );
  98.  
  99. $this->template->alfatihmart('register', $data);
  100. }
  101.  
  102. public function login()
  103. {
  104. if ($this->input->post('submit') == 'Submit')
  105. {
  106. $user = $this->input->post('username', TRUE);
  107. $pass = $this->input->post('password', TRUE);
  108.  
  109. $cek = $this->app_home->get_where('t_users', "username = '$user' || email = '$user'");
  110.  
  111. if ($cek->num_rows() > 0) {
  112. $data = $cek->row();
  113.  
  114. if (password_verify($pass, $data->password))
  115. {
  116. $datauser = array (
  117. 'user_id' => $data->id_user,
  118. 'name' => $data->fullname,
  119. 'user_login' => TRUE
  120. );
  121.  
  122. $this->session->set_userdata($datauser);
  123.  
  124. redirect('home');
  125.  
  126. } else {
  127.  
  128. $this->session->set_flashdata('alert', "Password yang anda masukkan salah..");
  129.  
  130. }
  131.  
  132. } else {
  133. $this->session->set_flashdata('alert', "Username Ditolak");
  134. }
  135.  
  136. }
  137.  
  138. if ($this->session->userdata('user_login') == TRUE)
  139. {
  140. redirect('home');
  141. }
  142.  
  143. $this->load->view('login');
  144. }
  145.  
  146. public function profil()
  147. {
  148. if (!$this->session->userdata('user_login'))
  149. {
  150. redirect('home/login');
  151. }
  152.  
  153. $get = $this->app_home->get_where('t_users', array('id_user' => $this->session->userdata('user_id')))->row();
  154.  
  155. if($this->input->post('submit', TRUE) == 'Submit')
  156. {
  157. $this->load->library('form_validation');
  158.  
  159. $this->form_validation->set_rules('nama1', 'Nama Depan', "required|min_length[3]|regex_match[/^[a-zA-Z'.]+$/]");
  160. $this->form_validation->set_rules('nama2', 'Nama Belakang', "regex_match[/^[a-zA-Z'.]+$/]");
  161. $this->form_validation->set_rules('user', 'Username', "required|min_length[5]|regex_match[/^[a-zA-Z0-9]+$/]");
  162. $this->form_validation->set_rules('pass', 'Masukkan Password Anda', "required|min_length[5]");
  163. $this->form_validation->set_rules('jk', 'Jenis Kelamin', "required");
  164. $this->form_validation->set_rules('telp', 'Telp', "required|min_length[5]|numeric");
  165. $this->form_validation->set_rules('alamat', 'Alamat', "required|min_length[5]");
  166.  
  167. if ($this->form_validation->run() == TRUE)
  168. {
  169. if (password_verify($this->input->post('pass', TRUE), $get->password))
  170. {
  171. $data = array(
  172. 'username' => $this->input->post('user', TRUE),
  173. 'fullname' => $this->input->post('nama1', TRUE).' '.$this->input->post('nama2', TRUE),
  174. 'jk' => $this->input->post('jk', TRUE),
  175. 'telp' => $this->input->post('telp', TRUE),
  176. 'alamat' => $this->input->post('alamat', TRUE)
  177. );
  178.  
  179. if ($this->app_home->update('t_users', $data, array('id_user' => $this->session->userdata('user_id'))))
  180. {
  181. $this->session->set_userdata(array('name' => $this->input->post('nama1', TRUE).' '.$this->input->post('nama2', TRUE)));
  182.  
  183. redirect('home');
  184.  
  185. } else {
  186.  
  187. echo '<script type="text/javascript">alert("Username / Email tidak tersedia");</script>';
  188.  
  189. }
  190. } else {
  191.  
  192. echo '<script type="text/javascript">alert("Password Salah...");window.location.replace("'.base_url().'/home/logout")</script>';
  193.  
  194. }
  195. }
  196. }
  197.  
  198. $name = explode(' ', $get->fullname);
  199. $data['nama1'] = $name[0];
  200. $data['nama2'] = $name[1];
  201. $data['user'] = $get->username;
  202. $data['email'] = $get->email;
  203. $data['jk'] = $get->jk;
  204. $data['telp'] = $get->telp;
  205. $data['alamat'] = $get->alamat;
  206.  
  207. $this->template->alfatihmart('user_profil', $data);
  208. }
  209.  
  210. public function password()
  211. {
  212. if (!$this->session->userdata('user_login'))
  213. {
  214. redirect('home/login');
  215. }
  216.  
  217. if ($this->input->post('submit', TRUE) == 'Submit')
  218. {
  219. $this->load->library('form_validation');
  220. //validasi form
  221.  
  222. $this->form_validation->set_rules('pass1', 'Password Baru', 'required|min_length[5]');
  223. $this->form_validation->set_rules('pass2', 'Ketik Ulang Password Baru', 'required|matches[pass1]');
  224. $this->form_validation->set_rules('pass3', 'Password Lama', 'required');
  225.  
  226. if ($this->form_validation->run() == TRUE)
  227. {
  228. $get_data = $this->app_home->get_where('t_users', array('id_user' => $this->session->userdata('user_id')))->row();
  229.  
  230. if (!password_verify($this->input->post('pass3',TRUE), $get_data->password))
  231. {
  232. echo '<script type="text/javascript">alert("Password lama yang anda masukkan salah");window.location.replace("'.base_url().'home/logout")</script>';
  233.  
  234. } else {
  235.  
  236. $pass = $this->input->post('pass1', TRUE);
  237. $data['password'] = password_hash($pass, PASSWORD_DEFAULT, ['cost' => 10]);
  238. $cond = array('id_user' => $this->session->userdata('user_id'));
  239.  
  240. $this->app_home->update('t_users', $data, $cond);
  241.  
  242. redirect('home/logout');
  243. }
  244. }
  245. }
  246.  
  247. $this->template->alfatihmart('pass');
  248. }
  249.  
  250. public function transaksi()
  251. {
  252. if (!$this->session->userdata('user_id')){
  253. redirect('home');
  254. }
  255.  
  256. $data['get'] = $this->app_home->get_where('t_order', ['id_user' =>$this->session->userdata('user_id')]);
  257.  
  258. $this->template->alfatihmart('transaksi', $data);
  259. }
  260.  
  261. public function hapus_transaksi()
  262. {
  263. if (!is_numeric($this->uri->segment(3))){
  264. redirect('home');
  265. }
  266. $table = array('t_order', 't_detail_order');
  267. $this->app_home->delete($table, ['id_order' =>$this->uri->segment(3)]);
  268.  
  269. redirect('home/transaksi');
  270. }
  271.  
  272. public function detail_transaksi()
  273. {
  274. if (!is_numeric($this->uri->segment(3))){
  275. redirect('home');
  276. }
  277. $table = "t_order o JOIN t_detail_order do ON (o.id_order = do.id_order) JOIN t_items i ON (do.id_item = i.id_item)";
  278. $data['get'] = $this->app_home->get_where($table, ['o.id_order' =>$this->uri->segment(3)]);
  279.  
  280. $this->template->alfatihmart('detail_transaksi', $data);
  281. }
  282. public function logout()
  283. {
  284. $this->session->sess_destroy();
  285. redirect('home');
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement