Advertisement
Guest User

Untitled

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