Advertisement
kura2yamato

fix 110

Aug 27th, 2020
4,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace App\Controllers;
  3.  
  4. use App\Controllers\BaseController;
  5. use App\Models\product;
  6. use App\Models\login;
  7. use CodeIgniter\Database\Query;
  8.  
  9. class users extends BaseController
  10.   {
  11.     public function index()
  12.       {
  13.  
  14.         if (!isset($_SESSION['login']))
  15.           {
  16.             return redirect()->to('/users/login');
  17.           }
  18.         $data = ['title' => 'Index'];
  19.         return view('/ram/index', $data);
  20.       }
  21.  
  22.     protected $product;
  23.     protected $login;
  24.  
  25.     public function __construct()
  26.       {
  27.         $this->product = new product;
  28.         $this->login = new login;
  29.       }
  30.  
  31.     public function product()
  32.       {
  33.         $product = $this
  34.             ->product
  35.             ->findAll();
  36.         $data = ['title' => 'product', 'product' => $product];
  37.         return view('/ram/product', $data);
  38.       }
  39.  
  40.     public function create()
  41.       {
  42.  
  43.         $data = ['title' => 'create', 'pesan' => \Config\Services::validation() ];
  44.         return view('/ram/create', $data);
  45.       }
  46.  
  47.     public function save()
  48.       {
  49.  
  50.         // validasi
  51.         if (!$this->validate(['nama' => ['rules' => 'required', 'errors' => ['required' => 'nama tidak boleh kosong']], 'brand' => ['rules' => 'required', 'errors' => ['required' => 'nama brand tidak boleh kosong']], 'harga' => ['rules' => 'required', 'errors' => ['required' => 'nama harga tidak boleh kosong']], 'gambar' => ['rules' => 'max_size[gambar,1024]|is_image[gambar]|mime_in[gambar,image/jpg,image/jpeg,image/png]', 'errors' => ['max_size' => 'Ukuran gambar terlalu besar', 'is_image' => 'yang ada pilih bukan gambar', 'mime_in' => 'yang ada pilih bukan gambar']]]))
  52.           {
  53.             return redirect()
  54.                 ->to('/users/create')
  55.                 ->withInput();
  56.           }
  57.         // ambil gambar
  58.         $filegambar = $this
  59.             ->request
  60.             ->getFile('gambar');
  61.         // cek apakah tidak ada gambar yang di upload
  62.         if ($filegambar->getError() == 4)
  63.           {
  64.             $namagambar = "default.jpg";
  65.           }
  66.         else
  67.           {
  68.             // generate sampul random
  69.             $namagambar = $filegambar->getRandomName();
  70.             // pindahkan folder ke img bosku
  71.             $filegambar->move('img', $namagambar);
  72.           }
  73.  
  74.         $this
  75.             ->product
  76.             ->save(['nama' => $this
  77.             ->request
  78.             ->getVar('nama') , 'brand' => $this
  79.             ->request
  80.             ->getVar('brand') , 'harga' => $this
  81.             ->request
  82.             ->getVar('harga') , 'gambar' => $namagambar]);
  83.         return redirect()->to('/users/product');
  84.       }
  85.     public function delete($id)
  86.       {
  87.         dd($this
  88.             ->request
  89.             ->getVar('gambar'));
  90.         if ($this
  91.             ->request
  92.             ->getVar('gambar') != ('default.jpg'))
  93.           {
  94.             unlink("img/" . $this
  95.                 ->request
  96.                 ->getVar('gambar'));
  97.           }
  98.  
  99.         $this
  100.             ->product
  101.             ->delete($id);
  102.         return redirect()->to('/users/product');
  103.       }
  104.     public function edit($id)
  105.       {
  106.         $data = ['title' => 'edit', 'product' => $this
  107.             ->product
  108.             ->find($id) , 'pesan' => \Config\Services::validation()
  109.  
  110.         ];
  111.  
  112.         return view('/ram/edit', $data);
  113.       }
  114.     public function update($id)
  115.       {
  116.         if (!$this->validate(['nama' => ['rules' => 'required', 'errors' => ['required' => 'nama tidak boleh kosong']], 'brand' => ['rules' => 'required', 'errors' => ['required' => 'nama brand tidak boleh kosong']], 'harga' => ['rules' => 'required', 'errors' => ['required' => 'nama harga tidak boleh kosong']],
  117.  
  118.         ]))
  119.           {
  120.             return redirect()
  121.                 ->to('/users/save/' . $id)->withInput();
  122.           }
  123.         /*
  124.         'gambar' => ['rules' => 'max_size[gambar,1024]|is_image[gambar]|mime_in[gambar,image/jpg,image/jpeg,image/png]', 'errors' => ['max_size' => 'Ukuran gambar terlalu besar', 'is_image' => 'yang ada pilih bukan gambar', 'mime_in' => 'yang ada pilih bukan gambar']]
  125.         */
  126.         $filegambar = $this
  127.             ->request
  128.             ->getFile('gambar');
  129.         if ($filegambar)
  130.           {
  131.             $product = $this
  132.                 ->product
  133.                 ->find($id);
  134.             $namagambar = $filegambar->getRandomName();
  135.             // pindahkan gambar ke img
  136.             $filegambar->move('img', $namagambar);
  137.             // hapus file lama
  138.             unlink("img/" . $product['namagambar']);
  139.             //update
  140.             $this
  141.                 ->product
  142.                 ->save(['id' => $id, 'gambar' => $namagambar]);
  143.           }
  144.         /*
  145.         // cek gambar apakah tetap gambar lama
  146.         if ($this
  147.             ->request
  148.             ->getVar('gambar') == $this
  149.             ->request
  150.             ->getVar('gambarlama'))
  151.         {
  152.             $namagambar = $this
  153.                 ->request
  154.                 ->getVar('gambarlama');
  155.         }
  156.         else
  157.         {
  158.             // generate nama file random
  159.             $namagambar = $filegambar->getRandomName();
  160.             // pindahkan gambar ke img
  161.             $filegambar->move('img', $namagambar);
  162.             // hapus file lama
  163.             unlink("img/" . $this
  164.                 ->request
  165.                 ->getVar('gambarlama'));
  166.         }
  167.         */
  168.         $this
  169.             ->product
  170.             ->save(['id' => $id, 'nama' => $this
  171.             ->request
  172.             ->getVar('nama') , 'brand' => $this
  173.             ->request
  174.             ->getVar('brand') , 'harga' => $this
  175.             ->request
  176.             ->getVar('harga') ]);
  177.         //'gambar' => $namagambar
  178.         return redirect()
  179.             ->to('/users/product');
  180.       }
  181.     public function login()
  182.       {
  183.  
  184.         if (isset($_SESSION['login']))
  185.           {
  186.             return redirect()->to('/users');
  187.           }
  188.         $data = ['title' => 'LOGIN', 'validasi' => \Config\Services::validation() ];
  189.         return view('/ram/login', $data);
  190.  
  191.         // jika sudah login users tidak bisa lagi ke login
  192.        
  193.       }
  194.  
  195.     public function registrasi()
  196.       {
  197.         if (!session()->get('log') > 0)
  198.           {
  199.  
  200.             $data = ['title' => 'Registrasi', 'validasi' => \Config\Services::validation() ];
  201.  
  202.             return view('/ram/registrasi', $data);
  203.           }
  204.         else
  205.           {
  206.             echo "<script>alert('anda sudah login')
  207.            location.href='/users';
  208.            </script>";
  209.           }
  210.       }
  211.     public function tambah()
  212.       {
  213.         if (!$this->validate(['username' => ['rules' => 'required|is_unique[login.username]', 'errors' => ['required' => '<script>alert("masukan username")</script>', 'is_unique' => '<script>alert("Username sudah digunakaan")</script>']], 'password' => ['rules' => 'required', 'errors' => ['required' => '<script>alert("masukan password")</script>']], 'password2' => ['rules' => 'required', 'errors' => ['required' => '<script>alert("masukan konfirmasi password")</script>']]]))
  214.           {
  215.             return redirect()
  216.                 ->to('/users/registrasi')
  217.                 ->withInput();
  218.           }
  219.  
  220.         $username = $this
  221.             ->request
  222.             ->getVar('username');
  223.         $password = $this
  224.             ->request
  225.             ->getVar('password');
  226.         $password2 = $this
  227.             ->request
  228.             ->getVar('password2');
  229.         // mengecek apakah username sudah ada didatabase
  230.        
  231.  
  232.         // mengecek password 1 sama tidak dengan password 2
  233.         if ($password != $password2)
  234.           {
  235.             echo "<script>alert('password tidak sama')
  236.            location.href='/users/registrasi';
  237.            </script>";
  238.           }
  239.         else
  240.           {
  241.             $passwordenkripsi = password_hash($this
  242.                 ->request
  243.                 ->getVar('password') , PASSWORD_DEFAULT);
  244.             $this
  245.                 ->login
  246.                 ->save(['username' => $username, 'password' => $passwordenkripsi]);
  247.             return redirect()->to('/users/login');
  248.           }
  249.       }
  250.     public function log()
  251.       {
  252.  
  253.         if (!$this->validate(['username' => ['rules' => 'required', 'errors' => ['required' => 'masukan username']], 'password' => ['rules' => 'required', 'errors' => ['required' => 'masukan password']]]))
  254.           {
  255.             return redirect()
  256.                 ->to('/users/login')
  257.                 ->withInput();
  258.           }
  259.         $username = $this
  260.             ->request
  261.             ->getPost('username');
  262.         $password = $this
  263.             ->request
  264.             ->getPost('password');
  265.  
  266.         $array = ['username' => $username];
  267.         $user = $this
  268.             ->login
  269.             ->where($array)->first();
  270.         session()
  271.             ->set('login', $user);
  272.         if (!$user)
  273.           {
  274.             echo "<script>alert('username atau password tidak ada')
  275.            location.href='/users/login';
  276.            </script>";
  277.           }
  278.         $pass = password_verify($password, $user['password']);
  279.         if ($user != $pass)
  280.           {
  281.             echo "<script>alert('password salah')
  282.            location.href='/users/login';
  283.            </script>";
  284.           }
  285.  
  286.         if ($user)
  287.           {
  288.  
  289.             if ($pass)
  290.               {
  291.                 session()->set('log', $username);
  292.                 $_SESSION['login'] = true;
  293.                 session()->set('user', $username);
  294.                 session()->set('role', $user['role']);
  295.                 return redirect()->to('/users');
  296.               }
  297.           }
  298.       }
  299.  
  300.     public function query()
  301.       {
  302.         $db = \Config\Database::connect();
  303.         $sql = "SELECT * FROM orang";
  304.         $result = $db->query($sql);
  305.         $row = $result->getResult('array');
  306.         foreach ($row as $key)
  307.           {
  308.             echo "<br>";
  309.             echo "Nama : " . $key['nama'];
  310.             echo "                       -                            ";
  311.             echo "<br>";
  312.             echo "<br>";
  313.             echo "Alamat : " . $key['alamat'];
  314.           }
  315.         // var_dump($row);
  316.        
  317.       }
  318.     public function logout()
  319.       {
  320.         session_destroy();
  321.         return redirect()->to('/users/login');
  322.       }
  323.   }
  324. // $username = $this->request->getVar('username');
  325. // $password = $this->request->getVar('password');
  326. // $hash = $this->login->where('password')->first();
  327. // if ($username == $this->login->where('username')->first()) {
  328. //     if (password_verify($password, $hash)) {
  329. //         return redirect()->to('/users');
  330. //     }
  331. //     echo "<script>alert('username atau password tidak ada:)')
  332. //     </script>";
  333. // }
  334.  
  335.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement