Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controllers;
- use App\Controllers\BaseController;
- use App\Models\product;
- use App\Models\login;
- use CodeIgniter\Database\Query;
- class users extends BaseController
- {
- public function index()
- {
- if (!isset($_SESSION['login'])) {
- return redirect()->to('/users/login');
- }
- $data = [
- 'title' => 'Index'
- ];
- return view('/ram/index', $data);
- }
- protected $product;
- protected $login;
- public function __construct()
- {
- $this->product = new product;
- $this->login = new login;
- }
- public function product()
- {
- $product = $this->product->findAll();
- $data = [
- 'title' => 'product',
- 'product' => $product
- ];
- return view('/ram/product', $data);
- }
- public function create()
- {
- $data = [
- 'title' => 'create',
- 'pesan' => \Config\Services::validation()
- ];
- return view('/ram/create', $data);
- }
- public function save()
- {
- // validasi
- 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'
- ]
- ]
- ])) {
- return redirect()->to('/users/create')->withInput();
- }
- // ambil gambar
- $filegambar = $this->request->getFile('gambar');
- // cek apakah tidak ada gambar yang di upload
- if ($filegambar->getError() == 4) {
- $namagambar = "default.jpg";
- } else {
- // generate sampul random
- $namagambar = $filegambar->getRandomName();
- // pindahkan folder ke img bosku
- $filegambar->move('img', $namagambar);
- }
- $this->product->save([
- 'nama' => $this->request->getVar('nama'),
- 'brand' => $this->request->getVar('brand'),
- 'harga' => $this->request->getVar('harga'),
- 'gambar' => $namagambar
- ]);
- return redirect()->to('/users/product');
- }
- public function delete($id)
- {
- dd($this->request->getVar('gambar'));
- if ($this->request->getVar('gambar') != ('default.jpg')) {
- unlink("img/" . $this->request->getVar('gambar'));
- }
- $this->product->delete($id);
- return redirect()->to('/users/product');
- }
- public function edit($id)
- {
- $data = [
- 'title' => 'edit',
- 'product' => $this->product->find($id),
- 'pesan' => \Config\Services::validation()
- ];
- return view('/ram/edit', $data);
- }
- public function update($id)
- {
- 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'
- ]
- ]
- ])) {
- return redirect()->to('/users/save/' . $id)->withInput();
- }
- $filegambar = $this->request->getFile('gambar');
- // cek gambar apakah tetap gambar lama
- if ($this->request->getVar('gambar') == $this->request->getVar('gambarlama')) {
- $namagambar = $this->request->getVar('gambarlama');
- } else {
- // generate nama file random
- $namagambar = $filegambar->getRandomName();
- // pindahkan gambar ke img
- $filegambar->move('img', $namagambar);
- // hapus file lama
- unlink("img/" . $this->request->getVar('gambarlama'));
- }
- $this->product->save([
- 'id' => $id,
- 'nama' => $this->request->getVar('nama'),
- 'brand' => $this->request->getVar('brand'),
- 'harga' => $this->request->getVar('harga'),
- 'gambar' => $namagambar
- ]);
- return redirect()->to('/users/product');
- }
- public function login()
- {
- if (isset($_SESSION['login'])) {
- return redirect()->to('/users');
- }
- $data = [
- 'title' => 'LOGIN',
- 'validasi' => \Config\Services::validation()
- ];
- return view('/ram/login', $data);
- // jika sudah login users tidak bisa lagi ke login
- }
- public function registrasi()
- {
- if (!session()->get('log') > 0) {
- $data = [
- 'title' => 'Registrasi',
- 'validasi' => \Config\Services::validation()
- ];
- return view('/ram/registrasi', $data);
- } else {
- echo "<script>alert('anda sudah login')
- location.href='/users';
- </script>";
- }
- }
- public function tambah()
- {
- 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>'
- ]
- ]
- ])) {
- return redirect()->to('/users/registrasi')->withInput();
- }
- $username = $this->request->getVar('username');
- $password = $this->request->getVar('password');
- $password2 = $this->request->getVar('password2');
- // mengecek apakah username sudah ada didatabase
- // mengecek password 1 sama tidak dengan password 2
- if ($password != $password2) {
- echo "<script>alert('password tidak sama')
- location.href='/users/registrasi';
- </script>";
- } else {
- $passwordenkripsi = password_hash($this->request->getVar('password'), PASSWORD_DEFAULT);
- $this->login->save([
- 'username' => $username,
- 'password' => $passwordenkripsi
- ]);
- return redirect()->to('/users/login');
- }
- }
- public function log()
- {
- if (!$this->validate([
- 'username' => [
- 'rules' => 'required',
- 'errors' => [
- 'required' => 'masukan username'
- ]
- ],
- 'password' => [
- 'rules' => 'required',
- 'errors' => [
- 'required' => 'masukan password'
- ]
- ]
- ])) {
- return redirect()->to('/users/login')->withInput();
- }
- $username = $this->request->getPost('username');
- $password = $this->request->getPost('password');
- $array = ['username' => $username];
- $user = $this->login->where($array)->first();
- session()->set('login', $user);
- if (!$user) {
- echo "<script>alert('username atau password tidak ada')
- location.href='/users/login';
- </script>";
- }
- $pass = password_verify($password, $user['password']);
- if ($user != $pass) {
- echo "<script>alert('password salah')
- location.href='/users/login';
- </script>";
- }
- if ($user) {
- if ($pass) {
- session()->set('log', $username);
- $_SESSION['login'] = true;
- session()->set('user', $username);
- session()->set('role', $user['role']);
- return redirect()->to('/users');
- }
- }
- }
- public function query()
- {
- $db = \Config\Database::connect();
- $sql = "SELECT * FROM orang";
- $result = $db->query($sql);
- $row = $result->getResult('array');
- foreach ($row as $key) {
- echo "<br>";
- echo "Nama : " . $key['nama'];
- echo " - ";
- echo "<br>";
- echo "<br>";
- echo "Alamat : " . $key['alamat'];
- }
- // var_dump($row);
- }
- public function logout()
- {
- session_destroy();
- return redirect()->to('/users/login');
- }
- }
- // $username = $this->request->getVar('username');
- // $password = $this->request->getVar('password');
- // $hash = $this->login->where('password')->first();
- // if ($username == $this->login->where('username')->first()) {
- // if (password_verify($password, $hash)) {
- // return redirect()->to('/users');
- // }
- // echo "<script>alert('username atau password tidak ada:)')
- // </script>";
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement