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