AmalModel = new ModelAmal(); } public function index() { if (session()->has('nip') && session()->has('username')) { $nip = session('nip'); $data = array( 'body' => 'Amal/list', 'amal' => $this->AmalModel->getAmal($nip), 'username' => session('username'), 'nama' => session('nama'), 'hak_akses' => session('hak_akses'), ); return view('index', $data); } else { // Jika pengguna belum login, arahkan ke halaman login return redirect()->to(base_url() . 'login'); } } public function index2() { if (session()->has('nip') && session()->has('username')) { $data = array( 'body' => 'Amal/listAll', 'amal' => $this->AmalModel->getAmalAll(), 'username' => session('username'), 'nama' => session('nama'), 'hak_akses' => session('hak_akses'), ); return view('index', $data); } else { // Jika pengguna belum login, arahkan ke halaman login return redirect()->to(base_url() . 'login'); } } public function input() { if (session()->has('nip') && session()->has('username')) { $data = array( 'body' => 'Amal/input', 'username' => session('username'), 'nama' => session('nama'), 'hak_akses' => session('hak_akses'), ); return view('index', $data); } else { // Jika pengguna belum login, arahkan ke halaman login return redirect()->to(base_url() . 'login'); } } public function input2() { if (session()->has('nip') && session()->has('username')) { $data = array( 'body' => 'Amal/input2', 'username' => session('username'), 'nama' => session('nama'), 'hak_akses' => session('hak_akses'), ); return view('index', $data); } else { // Jika pengguna belum login, arahkan ke halaman login return redirect()->to(base_url() . 'login'); } } public function insert() { $lampiran = $this->request->getFile('lampiran'); $newName = ''; if ($lampiran->isValid() && !$lampiran->hasMoved()) { $newName = $lampiran->getName(); $lampiran->move(ROOTPATH . 'public/assets/dokumen/amal/'); // Move the image to the desired directory } $data = array( 'bulan' => $this->request->getPost('bulan'), 'tahun' => $this->request->getPost('tahun'), 'sholat_jamaah' => $this->request->getPost('sholat_jamaah'), 'tilawah' => $this->request->getPost('tilawah'), 'sholat_tahajud' => $this->request->getPost('sholat_tahajud'), 'sholat_dhuha' => $this->request->getPost('sholat_dhuha'), 'bantu_ortu' => $this->request->getPost('bantu_ortu'), 'literasi' => $this->request->getPost('literasi'), 'infaq' => $this->request->getPost('infaq'), 'olahraga' => $this->request->getPost('olahraga'), 'hafalan' => $this->request->getPost('hafalan'), 'nip' => session('nip'), 'lampiran' => $newName, ); if ($this->AmalModel->insert_amal($data)) { session()->setFlashdata('success', 'Berhasil Menambahkan Amal Yaumi!'); return redirect()->to(base_url() . 'amal/list'); } else { session()->setFlashdata('error', 'Gagal Menambahkan Amal Yaumi!'); return redirect()->to(base_url() . 'amal/input'); } } public function edit($id) { if (session()->has('nip') && session()->has('username')) { $data_amal = new ModelAmal(); $data = array( 'body' => 'Amal/edit', 'data' => $data_amal->getById($id)->getRow(), 'username' => session('username'), 'nama' => session('nama'), 'hak_akses' => session('hak_akses'), ); return view('index', $data); } else { // Jika pengguna belum login, arahkan ke halaman login return redirect()->to(base_url() . 'login'); } } public function update($id) { $lampiran = $this->request->getFile('lampiran'); // Get the uploaded image file // Get the current data from database $currentData = $this->AmalModel->getById($id)->getRow(); // Initialize newName with the current file name $newName = $currentData->lampiran; // Check if there is a new file uploaded if ($lampiran->isValid() && !$lampiran->hasMoved()) { // Get the new file name $newName = $lampiran->getName(); $lampiran->move(ROOTPATH . 'public/assets/dokumen/amal/'); // Move the image to the desired directory // Delete old file if there was a previous file if (!empty($currentData->lampiran)) { $oldFilePath = ROOTPATH . 'public/assets/dokumen/amal/' . $currentData->lampiran; if (file_exists($oldFilePath)) { unlink($oldFilePath); // Delete the old file } } } // Update the database record $data = $this->AmalModel->update($id, [ 'bulan' => $this->request->getVar('bulan'), 'tahun' => $this->request->getVar('tahun'), 'sholat_jamaah' => $this->request->getVar('sholat_jamaah'), 'tilawah' => $this->request->getVar('tilawah'), 'sholat_tahajud' => $this->request->getVar('sholat_tahajud'), 'sholat_dhuha' => $this->request->getVar('sholat_dhuha'), 'bantu_ortu' => $this->request->getVar('bantu_ortu'), 'literasi' => $this->request->getVar('literasi'), 'infaq' => $this->request->getVar('infaq'), 'olahraga' => $this->request->getVar('olahraga'), 'hafalan' => $this->request->getVar('hafalan'), 'lampiran' => $newName ]); // Check if update was successful and redirect accordingly if ($data) { return redirect()->to(base_url() . 'amal/list')->with('success', 'Data Berhasil Diubah!'); } else { return redirect()->to(base_url() . 'amal/edit/' . $id)->with('error', 'Data Gagal Diubah!'); } } public function delete($id) { $currentData = $this->AmalModel->getById($id)->getRow(); if (!empty($currentData->lampiran)) { $oldFilePath = ROOTPATH . 'public/assets/dokumen/amal/' . $currentData->lampiran; if (file_exists($oldFilePath)) { unlink($oldFilePath); // Delete the old file } } $this->AmalModel->delete($id); return redirect()->to(base_url() . 'amal/list')->with('success', 'Data Berhasil Dihapus!'); } }