Advertisement
rizki1

home.php

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