Advertisement
darmariduan

controllers/Home.php

Aug 7th, 2019
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.05 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');
  11. }
  12.  
  13. public function index($offset=0)
  14. {
  15. $this->load->library('pagination');
  16. //configure
  17. $config['base_url'] = base_url().'home/index';
  18. $config['total_rows'] = $this->app->get_all('t_items')->num_rows();
  19. $config['per_page'] = 6;
  20. $config['uri_segment'] = 3;
  21.  
  22. $this->pagination->initialize($config);
  23.  
  24. $data['link'] = $this->pagination->create_links();
  25.  
  26. if ($this->session->userdata('user_login') == TRUE)
  27. {
  28. $data['fav'] = $this->app->get_where('t_favorite', ['id_user' => $this->session->userdata('user_id')]);
  29. }
  30.  
  31. $data['data'] = $this->app->select_where_limit('t_items', ['aktif' => 1], $config['per_page'], $offset);
  32. $this->template->olshop('home', $data);
  33.  
  34. }
  35.  
  36. public function search()
  37. {
  38. if ($this->input->post('search', TRUE))
  39. {
  40.  
  41. $this->session->set_userdata(['s' => $this->input->post('search', TRUE)]);
  42. $search = $this->session->userdata('s');
  43.  
  44. } else {
  45.  
  46. $search = $this->uri->segment(3);
  47.  
  48. }
  49.  
  50. if (!$this->uri->segment(4))
  51. {
  52.  
  53. $offset = 0;
  54.  
  55. } else {
  56.  
  57. $offset = $this->uri->segment(4);
  58.  
  59. }
  60.  
  61. $this->load->library('pagination');
  62. //configure
  63. $config['base_url'] = base_url().'home/search/'.$search;
  64. $config['total_rows'] = $this->app->get_like('t_items', ['aktif' => 1], ['nama_item' => $search])->num_rows();
  65. $config['per_page'] = 6;
  66. $config['uri_segment'] = 4;
  67.  
  68. $this->pagination->initialize($config);
  69.  
  70. $data['link'] = $this->pagination->create_links();
  71. $data['data'] = $this->app->select_like('t_items', ['aktif' => 1], ['nama_item' => $search], $config['per_page'], $offset);
  72. $data['search'] = $search;
  73. $this->template->olshop('home', $data);
  74.  
  75. }
  76.  
  77. public function price()
  78. {
  79.  
  80. if ($this->input->post('submit', TRUE) == 'Filter')
  81. {
  82.  
  83. $this->session->set_userdata([
  84. 'min' => $this->input->post('min', TRUE),
  85. 'max' => $this->input->post('max', TRUE)
  86. ]);
  87.  
  88. $min = str_replace('.','',$this->session->userdata('min'));
  89. $max = str_replace('.','',$this->session->userdata('max'));
  90.  
  91. } else {
  92.  
  93. $min = $this->uri->segment(3);
  94. $max = $this->uri->segment(4);
  95.  
  96. }
  97.  
  98. if (!is_numeric($min) || !is_numeric($max))
  99. {
  100.  
  101. redirect('home');
  102.  
  103. }
  104.  
  105. if (!$this->uri->segment(5))
  106. {
  107.  
  108. $offset = 0;
  109.  
  110. } else {
  111.  
  112. $offset = $this->uri->segment(5);
  113.  
  114. }
  115.  
  116. $where = ['harga >=' => $min, 'harga <=' => $max, 'aktif' => 1];
  117.  
  118. $this->load->library('pagination');
  119. //configure
  120. $config['base_url'] = base_url().'home/price/'.$min.'/'.$max;
  121. $config['total_rows'] = $this->app->get_where('t_items', $where)->num_rows();
  122. $config['per_page'] = 6;
  123. $config['uri_segment'] = 5;
  124.  
  125. $this->pagination->initialize($config);
  126.  
  127. $data['link'] = $this->pagination->create_links();
  128. $data['data'] = $this->app->select_where_limit('t_items', $where, $config['per_page'], $offset);
  129. $this->template->olshop('home', $data);
  130.  
  131. }
  132.  
  133. public function kategori()
  134. {
  135.  
  136. if (!$this->uri->segment(3))
  137. {
  138. redirect('home');
  139. }
  140.  
  141. $offset = (!$this->uri->segment(4)) ? 0 : $this->uri->segment(4);
  142.  
  143. $url = strtolower(str_replace([' ','%20','_'], '-', $this->uri->segment(3)));
  144.  
  145. $table = 't_kategori k
  146. JOIN t_rkategori rk ON (k.id_kategori = rk.id_kategori)
  147. JOIN t_items i ON (rk.id_item = i.id_item)';
  148. //load library pagination
  149. $this->load->library('pagination');
  150. //configure
  151. $config['base_url'] = base_url().'home/kategori/'.$this->uri->segment(3);
  152. $config['total_rows'] = $this->app->get_where($table, ['i.aktif' => 1, 'k.url' => $url])->num_rows();
  153. $config['per_page'] = 6;
  154. $config['uri_segment'] = 4;
  155.  
  156. $this->pagination->initialize($config);
  157.  
  158. $data['link'] = $this->pagination->create_links();
  159. $data['data'] = $this->app->select_where_limit($table, ['i.aktif' => 1, 'k.url' => $url], $config['per_page'], $offset);
  160. $data['url'] = ucwords(str_replace(['-','%20','_'], ' ', $this->uri->segment(3)));
  161.  
  162. $this->template->olshop('home', $data);
  163.  
  164. }
  165.  
  166. public function detail()
  167. {
  168.  
  169. if (is_numeric($this->uri->segment(3)))
  170. {
  171.  
  172. $id = $this->uri->segment(3);
  173.  
  174. $items = $this->app->get_where('t_items', array('link' => $id));
  175. $get = $items->row();
  176.  
  177. $table = "t_rkategori rk
  178. JOIN t_kategori k ON (k.id_kategori = rk.id_kategori)";
  179.  
  180. $data['kat'] = $this->app->get_where($table, array('rk.id_item' => $get->id_item));
  181. $data['data'] = $items;
  182. $data['img'] = $this->app->get_where('t_img', ['id_item' => $get->id_item]);
  183.  
  184. $this->template->olshop('item_detail', $data);
  185.  
  186. } else {
  187.  
  188. redirect('home');
  189.  
  190. }
  191.  
  192. }
  193.  
  194. public function favorite()
  195. {
  196. //paksa login
  197. if ($this->session->userdata('user_login') != TRUE)
  198. {
  199. redirect('home/login');
  200. }
  201. //validasi link
  202. if (!is_numeric($this->uri->segment(3)))
  203. {
  204. redirect('home');
  205. }
  206. //ambil data
  207. $get = $this->app->get_where('t_items', ['link' => $this->uri->segment(3)])->row();
  208.  
  209. //cek data
  210. $where = [
  211. 'id_user' => $this->session->userdata('user_id'),
  212. 'id_item' => $get->id_item
  213. ];
  214.  
  215. $cek = $this->app->get_where('t_favorite', $where)->num_rows();
  216.  
  217. if ($cek > 0)
  218. {
  219. $this->session->set_flashdata('alert', 'Item dihapus dari daftar favorite');
  220. //hapus data
  221. $this->app->delete('t_favorite', $where);
  222. } else {
  223. //masukkan data ke variabel
  224. $data = array(
  225. 'id_user' => $this->session->userdata('user_id'),
  226. 'id_item' => $get->id_item
  227. );
  228.  
  229. $this->session->set_flashdata('success', 'Item ditambahkan ke daftar favorite');
  230. //insert DataBase
  231. $this->app->insert('t_favorite', $data);
  232. }
  233.  
  234. echo '<script type="text/javascript">window.history.go(-1)</script>';
  235. }
  236.  
  237. public function list_fav()
  238. {
  239. if (!$this->session->userdata('user_login'))
  240. {
  241. redirect('home/login');
  242. }
  243.  
  244. //ambil data
  245. $table = 't_favorite f
  246. JOIN t_items i ON (f.id_item = i.id_item)';
  247.  
  248. $data['data'] = $this->app->get_where($table, ['aktif' => 1, 'f.id_user' => $this->session->userdata('user_id')]);
  249.  
  250. $data['fav'] = $this->app->get_where('t_favorite', ['id_user' => $this->session->userdata('user_id')]);
  251.  
  252. $this->template->olshop('fav', $data);
  253.  
  254. }
  255.  
  256. public function registrasi()
  257. {
  258.  
  259. if($this->input->post('submit', TRUE) == 'Submit')
  260. {
  261.  
  262. $this->load->library('form_validation');
  263.  
  264. $this->form_validation->set_rules('nama1', 'Nama Depan', "required|min_length[3]|regex_match[/^[a-zA-Z'.]+$/]");
  265. $this->form_validation->set_rules('nama2', 'Nama Belakang', "regex_match[/^[a-zA-Z'.]+$/]");
  266. $this->form_validation->set_rules('user', 'Username', "required|min_length[5]|regex_match[/^[a-zA-Z0-9]+$/]");
  267. $this->form_validation->set_rules('email', 'Email', "required|valid_email");
  268. $this->form_validation->set_rules('pass1', 'Password', "required|min_length[5]");
  269. $this->form_validation->set_rules('pass2', 'Ketik Ulang Password', "required|matches[pass1]");
  270. $this->form_validation->set_rules('jk', 'Jenis Kelamin', "required");
  271. $this->form_validation->set_rules('telp', 'Telp', "required|min_length[8]|numeric");
  272. $this->form_validation->set_rules('alamat', 'Alamat', "required|min_length[10]");
  273.  
  274. if ($this->form_validation->run() == TRUE)
  275. {
  276.  
  277. $data = array(
  278. 'username' => $this->input->post('user', TRUE),
  279. 'fullname' => $this->input->post('nama1', TRUE).' '.$this->input->post('nama2', TRUE),
  280. 'email' => $this->input->post('email', TRUE),
  281. 'password' => password_hash($this->input->post('pass1', TRUE), PASSWORD_DEFAULT, ['cost' => 10]),
  282. 'jk' => $this->input->post('jk', TRUE),
  283. 'telp' => $this->input->post('telp', TRUE),
  284. 'alamat' => $this->input->post('alamat', TRUE),
  285. 'status' => 1
  286. );
  287.  
  288. if ($this->app->insert('t_users', $data))
  289. {
  290.  
  291. $halaman = 'reg_success';
  292.  
  293. } else {
  294.  
  295. echo '<script type="text/javascript">alert("Username / Email tidak tersedia");</script>';
  296.  
  297. $halaman = 'register';
  298.  
  299. }
  300.  
  301. } else {
  302.  
  303. $halaman = 'register';
  304.  
  305. }
  306.  
  307. } else {
  308.  
  309. $halaman = 'register';
  310.  
  311. }
  312.  
  313. if ($this->session->userdata('user_login') == TRUE)
  314. {
  315. redirect('home');
  316. }
  317.  
  318. $data = array(
  319. 'user' => $this->input->post('user', TRUE),
  320. 'nama1' => $this->input->post('nama1', TRUE),
  321. 'nama2' => $this->input->post('nama2', TRUE),
  322. 'email' => $this->input->post('email', TRUE),
  323. 'jk' => $this->input->post('jk', TRUE),
  324. 'telp' => $this->input->post('telp', TRUE),
  325. 'alamat' => $this->input->post('alamat', TRUE),
  326. );
  327.  
  328. $this->template->olshop($halaman, $data);
  329.  
  330. }
  331.  
  332. public function login()
  333. {
  334.  
  335. if ($this->input->post('submit') == 'Submit')
  336. {
  337.  
  338. $user = $this->input->post('username', TRUE);
  339. $pass = $this->input->post('password', TRUE);
  340. $where = "username = '".$user."' && status = 1 || email = '".$user."' && status = 1";
  341.  
  342. $cek = $this->app->get_where('t_users', $where);
  343.  
  344. if ($cek->num_rows() > 0)
  345. {
  346.  
  347. $data = $cek->row();
  348.  
  349. if (password_verify($pass, $data->password))
  350. {
  351. $datauser = array (
  352. 'user_id' => $data->id_user,
  353. 'name' => $data->fullname,
  354. 'user_login' => TRUE
  355. );
  356.  
  357. $this->session->set_userdata($datauser);
  358.  
  359. redirect('home');
  360.  
  361. } else {
  362.  
  363. echo '<script type="text/javascript">alert("Password ditolak");</script>';
  364.  
  365. }
  366.  
  367. } else {
  368.  
  369. echo '<script type="text/javascript">alert("Username tidak dikenali");</script>';
  370.  
  371. }
  372.  
  373. }
  374.  
  375. if ($this->session->userdata('user_login') == TRUE)
  376. {
  377. redirect('home');
  378. }
  379.  
  380. $profil['data'] = $this->app->get_all('t_profil');
  381.  
  382. $this->load->view('login', $profil);
  383.  
  384. }
  385.  
  386. public function profil()
  387. {
  388.  
  389. if (!$this->session->userdata('user_login'))
  390. {
  391. redirect('home/login');
  392. }
  393.  
  394. $get = $this->app->get_where('t_users', array('id_user' => $this->session->userdata('user_id')))->row();
  395.  
  396. if($this->input->post('submit', TRUE) == 'Submit')
  397. {
  398.  
  399. $this->load->library('form_validation');
  400.  
  401. $this->form_validation->set_rules('nama1', 'Nama Depan', "required|min_length[3]|regex_match[/^[a-zA-Z'.]+$/]");
  402. $this->form_validation->set_rules('nama2', 'Nama Belakang', "regex_match[/^[a-zA-Z'.]+$/]");
  403. $this->form_validation->set_rules('pass', 'Masukkan Password Anda', "required|min_length[5]");
  404. $this->form_validation->set_rules('jk', 'Jenis Kelamin', "required");
  405. $this->form_validation->set_rules('telp', 'Telp', "required|min_length[8]|numeric");
  406. $this->form_validation->set_rules('alamat', 'Alamat', "required|min_length[10]");
  407.  
  408. if ($this->form_validation->run() == TRUE)
  409. {
  410.  
  411. if (password_verify($this->input->post('pass', TRUE), $get->password))
  412. {
  413.  
  414. $data = array(
  415. 'fullname' => $this->input->post('nama1', TRUE).' '.$this->input->post('nama2', TRUE),
  416. 'jk' => $this->input->post('jk', TRUE),
  417. 'telp' => $this->input->post('telp', TRUE),
  418. 'alamat' => $this->input->post('alamat', TRUE)
  419. );
  420. $where = ['id_user' => $this->session->userdata('user_id')];
  421.  
  422. if ($this->app->update('t_users', $data, $where))
  423. {
  424.  
  425. $this->session->set_userdata(array('name' => $this->input->post('nama1', TRUE).' '.$this->input->post('nama2', TRUE)));
  426.  
  427. redirect('home');
  428.  
  429. } else {
  430.  
  431. echo '<script type="text/javascript">alert("Username / Email tidak tersedia");</script>';
  432.  
  433. }
  434.  
  435. } else {
  436.  
  437. echo '<script type="text/javascript">alert("Password Salah...");window.location.replace("'.base_url().'/home/logout")</script>';
  438.  
  439. }
  440.  
  441. }
  442.  
  443. }
  444.  
  445. $name = explode(' ', $get->fullname);
  446. $data['nama1'] = $name[0];
  447. $data['nama2'] = $name[1];
  448. $data['user'] = $get->username;
  449. $data['email'] = $get->email;
  450. $data['jk'] = $get->jk;
  451. $data['telp'] = $get->telp;
  452. $data['alamat']= $get->alamat;
  453.  
  454. $this->template->olshop('user_profil', $data);
  455.  
  456. }
  457.  
  458. public function password()
  459. {
  460.  
  461. if (!$this->session->userdata('user_login'))
  462. {
  463. redirect('home/login');
  464. }
  465.  
  466. if ($this->input->post('submit', TRUE) == 'Submit')
  467. {
  468.  
  469. $this->load->library('form_validation');
  470. //validasi form
  471. $this->form_validation->set_rules('pass1', 'Password Baru', 'required|min_length[5]');
  472. $this->form_validation->set_rules('pass2', 'Ketik Ulang Password Baru', 'required|matches[pass1]');
  473. $this->form_validation->set_rules('pass3', 'Password Lama', 'required');
  474.  
  475. if ($this->form_validation->run() == TRUE)
  476. {
  477.  
  478. $get_data = $this->app->get_where('t_users', array('id_user' => $this->session->userdata('user_id')))->row();
  479.  
  480. if (!password_verify($this->input->post('pass3',TRUE), $get_data->password))
  481. {
  482.  
  483. echo '<script type="text/javascript">alert("Password lama yang anda masukkan salah");window.location.replace("'.base_url().'home/logout")</script>';
  484.  
  485. } else {
  486.  
  487. $pass = $this->input->post('pass1', TRUE);
  488. $data['password'] = password_hash($pass, PASSWORD_DEFAULT, ['cost' => 10]);
  489. $cond = array('id_user' => $this->session->userdata('user_id'));
  490.  
  491. $this->app->update('t_users', $data, $cond);
  492.  
  493. redirect('home/logout');
  494.  
  495. }
  496.  
  497. }
  498.  
  499. }
  500.  
  501. $this->template->olshop('pass');
  502.  
  503. }
  504.  
  505. public function transaksi()
  506. {
  507.  
  508. if (!$this->session->userdata('user_id'))
  509. {
  510. redirect('home');
  511. }
  512.  
  513. $table = "t_order o JOIN t_users u ON (o.email = u.email)";
  514. $data['get'] = $this->app->get_where($table, ['id_user' => $this->session->userdata('user_id')]);
  515.  
  516. $this->template->olshop('transaksi', $data);
  517.  
  518. }
  519.  
  520. public function detail_transaksi()
  521. {
  522.  
  523. if (!is_numeric($this->uri->segment(3)))
  524. {
  525. redirect('home');
  526. }
  527.  
  528. $table = "t_order o
  529. JOIN t_detail_order do ON (o.id_order = do.id_order)
  530. JOIN t_items i ON (do.id_item = i.id_item)";
  531.  
  532. $data['get'] = $this->app->get_where($table, ['o.id_order' => $this->uri->segment(3)]);
  533.  
  534. $this->template->olshop('detail_transaksi', $data);
  535.  
  536. }
  537.  
  538. public function hapus_transaksi()
  539. {
  540.  
  541. if (!is_numeric($this->uri->segment(3)))
  542. {
  543. redirect('home');
  544. }
  545. //kembalikan stok
  546. $table = 't_detail_order do
  547. JOIN t_items i ON (do.id_item = i.id_item)';
  548. $get = $this->app->get_where($table, ['id_order' => $this->uri->segment(3)]);
  549.  
  550. foreach ($get->result() as $key) {
  551. //jumlahkan stok
  552. $stok = ($key->qty + $key->stok);
  553. //update stok
  554. $this->app->update('t_items', ['stok' => $stok], ['id_item' => $key->id_item]);
  555. }
  556.  
  557. $tables = array('t_order', 't_detail_order');
  558. $this->app->delete($tables, ['id_order' => $this->uri->segment(3)]);
  559.  
  560. redirect('home/transaksi');
  561.  
  562. }
  563.  
  564. public function transaksi_selesai()
  565. {
  566.  
  567. if (!is_numeric($this->uri->segment(3)))
  568. {
  569. redirect('home');
  570. }
  571.  
  572. $this->app->update('t_order', ['status_proses' => 'selesai'], ['id_order' => $this->uri->segment(3)]);
  573.  
  574. redirect('home/transaksi');
  575.  
  576. }
  577.  
  578. public function upload_bukti()
  579. {
  580. if ($this->input->post('submit', TRUE) == 'Submit')
  581. {
  582. $this->load->library('form_validation');
  583.  
  584. $this->form_validation->set_rules('id_invoice', 'No. Invoice / Id Pemesanan', 'required|min_length[10]');
  585.  
  586. if ($this->form_validation->run() == TRUE)
  587. {
  588. //cek data
  589. $get = $this->app->get_where('t_order', ['id_order' => $this->input->post('id_invoice', TRUE)]);
  590. $hitung = $get->num_rows();
  591.  
  592. if ($hitung > 0)
  593. {
  594. //fetch data
  595. $detail = $get->row();
  596.  
  597. $config['upload_path'] = './assets/bukti/';
  598. $config['allowed_types'] = 'jpg|png|jpeg';
  599. $config['max_size'] = '2048';
  600. $config['file_name'] = 'bukti'.$detail->id_order;
  601.  
  602. $this->load->library('upload', $config);
  603.  
  604. if ($this->upload->do_upload('bukti'))
  605. {
  606. $gbr = $this->upload->data();
  607. //proses insert data item
  608. $bukti = array ('bukti' => $gbr['file_name']);
  609. $where = array ('id_order' => $detail->id_order);
  610. //update data
  611. $update = $this->app->update('t_order', $bukti, $where);
  612.  
  613. if ($update)
  614. {
  615. $admin = $this->app->get_where('t_admin', ['id_admin' => 1])->row();
  616. $profil = $this->app->get_where('t_profil', ['id_profil' => 1])->row();
  617.  
  618. //proses
  619. $this->load->library('email');
  620.  
  621. $config['smtp_user'] = $profil->email_toko; //isi dengan email gmail
  622. $config['smtp_pass'] = $profil->pass_toko; //isi dengan password
  623.  
  624. $this->email->initialize($config);
  625.  
  626. $tanggal = date('d - m - Y');
  627.  
  628. $this->email->from($profil->email_toko, $profil->title);
  629. $this->email->to($admin->email);
  630. $this->email->subject('Status Pembayaran');
  631. $this->email->message(
  632. 'Pesanan dengan ID. '.$detail->id_order.' Telah dibayar pada tanggal '.$tanggal.', silahkan cek menu transaksi untuk melihat bukti pembayaran
  633. '
  634. );
  635.  
  636. if ($this->email->send())
  637. {
  638. echo '<script type="text/javascript">alert("Bukti Pembayaran Berhasil Diunggah...");window.location.replace("'.base_url().'")</script>';
  639. }
  640.  
  641. } else {
  642.  
  643. echo '<script type="text/javascript">alert("Maaf Telah Terjadi Kesalahan... silahkan ulangi lagi")</script>';
  644.  
  645. }
  646.  
  647. } else {
  648.  
  649. echo '<script type="text/javascript">alert("Bukti Gagal Diunggah...")</script>';
  650.  
  651. }
  652.  
  653. } else {
  654.  
  655. echo '<script type="text/javascript">alert("Id Pemesanan Tidak dikenali..")</script>';
  656.  
  657. }
  658. }
  659. }
  660.  
  661. $data['id_invoice'] = $this->input->post('id_invoice', TRUE);
  662. $this->template->olshop('up_bukti', $data);
  663. }
  664.  
  665.  
  666. public function pengiriman()
  667. {
  668. $data['data'] = $this->app->get_all('t_resis');
  669. $this->template->olshop('pengiriman', $data);
  670.  
  671.  
  672. }
  673.  
  674.  
  675. public function logout()
  676. {
  677.  
  678. $this->session->sess_destroy();
  679. redirect('home');
  680.  
  681. }
  682. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement