$session_bid, 'books.isactive' => 1]; } else { $where = ['books.isactive !=' => NULL]; } $model = new BooksModel(); $model->setTable('books'); $book_details =$model->getnonMembershiplist($session_bid); $data['page_name'] = 'Book Details'; $data['details'] = $book_details; $this->render_page('book_list', $data); } else { // Session is not active or user is not logged in return redirect()->to('login'); } } ## To Load Book Form (Add/Update) public function book_page($id) { helper('session'); $data['session_bid'] = get_business_id(); // Fetch categories from the model $model = new BooksModel(); $categories = $model->getCategories(); if ($id === '0') { $data['page_name'] = 'Add Book'; $data['details'] = []; } elseif ($id !== '0') { $data['page_name'] = 'Edit Book'; $imageData = $model->getBooksAndImagesByBookId($id); $data['details'] = $imageData[0]; } $data['categories'] = $categories; // Pass the categories to the view $this->render_page('book_form', $data); } ## For inserting/updating details of book public function insert_books() { $requested_details = $this->request->getPost(); try { $session_uid = get_logged_user_id(); $session_bid = get_business_id(); $BooksModel = new BooksModel(); $book_id = $requested_details['book_id']; $requested_details['publication_date'] = (isset($requested_details['publication_date']) && $requested_details['publication_date'] != "") ? $requested_details['publication_date'] : NULL; // Step 1: Insert or update book details if (empty($book_id)) { $requested_details['created_by'] = (int)$session_uid; if ($BooksModel->insert($requested_details)) { $book_id = $BooksModel->insertID(); session()->setFlashdata('success', 'Product successfully created.'); $this->logger->info("Books: has been added successfully. Inserted ID = " . $book_id); } else { session()->setFlashdata('error', 'Product could not be created. Please try again.'); $this->logger->error("Books: Error could not be added. Please try again."); } } else { $isactive = $this->request->getPost('isactive'); $requested_details['isactive'] = ($isactive == 'on') ? 1 : 0; $requested_details['updated_by'] = (int)$session_uid; if ($BooksModel->update($book_id, $requested_details)) { $book_affectedRows = $BooksModel->db->affectedRows(); session()->setFlashdata('success', 'Product successfully updated.'); $this->logger->info("Books: has been updated successfully. Updated Book ID = " . $book_id . " Aff " . $book_affectedRows); } else { session()->setFlashdata('error', 'Product updation failed. Please try again.'); $this->logger->error("Books: Error failed to update ID =" . $book_id); } } ## book category section. $category_id = $requested_details['category_id']; if ($category_id != "") { $existingRecord = $BooksModel->db->table('book_categories') ->where(['book_id'=> $book_id,'isactive' => 1]) ->get() ->getRow(); if($existingRecord){ $existingRecordId = $existingRecord->id; $BooksModel->db->table('book_categories')->where('id', $existingRecordId)->update(['isactive' => 0,'updated_by'=>(int)$session_uid]); $book_categories_affectedRows = $BooksModel->db->affectedRows(); $this->logger->info("Books Category : has been updated successfully. Updated Book Category ID = " . $existingRecordId." Aff ".$book_categories_affectedRows); } if((int)$category_id !== 0){ $BooksModel->db->table('book_categories')->insert(['book_id' => $book_id, 'category_id' => $category_id]); $book_categories_id = $BooksModel->db->insertID(); $this->logger->info("Books Category : has been added successfully. Inserted ID = " . $book_categories_id); } }else{ $this->logger->info("Books Category : categories id not founded in this book id =". $book_id ); } // Step 11: Get existing image details before the update $existing_image_record = $BooksModel->db->table('book_images') ->where(['book_id' => $book_id, 'isactive' => 1, 'type' => 1]) ->get() ->getRow(); if ($existing_image_record) { $existing_image_id = $existing_image_record->book_img_id; $existing_image_name = $existing_image_record->img_name; } else { $existing_image_id = null; $existing_image_name = null; } // CI validation rule for img_name $validationRule = [ 'img_name' => [ 'label' => 'Image File', 'rules' => 'uploaded[img_name]|is_image[img_name]|mime_in[img_name,image/jpg,image/jpeg,image/gif,image/png,image/webp]' ] ]; $img_details = $this->request->getFile('img_name'); // Image details $final_img_name = NULL; // Step 1: Image details available if ($img_details) { if ($img_details->isValid()) { $new_img_name = $img_details->getName(); // Before movement name // Validation Rule Apply if (!$this->validate($validationRule)) { if ($new_img_name !== '') { $error = $this->validator->getErrors(); $this->logger->error("Books: Error on image upload" . $error['img_name']); throw new \Exception($error['img_name']); } } // Check if existing and new image name are different if ($new_img_name !== $existing_image_name) { // Step 6: Different Image Name means removed from the database if ($existing_image_id) { $BooksModel->db->table('book_images') ->where(['book_img_id' => $existing_image_id, 'book_id' => $book_id]) ->delete(); $book_images_affectedRows = $BooksModel->db->affectedRows(); $this->logger->info("Books: Image has been updated successfully. Updated Book Image ID = " . $existing_image_id . " Aff " . $book_images_affectedRows); // Optionally, delete the physical file from the server if (is_file('public/uploads/'.$existing_image_name)) { $this->logger->info("Books: Already Image Available on target Folder Path: " . 'public/uploads/' . $existing_image_name . " So, Deleted."); unlink('public/uploads/'.$existing_image_name); } } else { $this->logger->info("Books: No existing image record found. Nothing to update/delete."); } // Step 7: Move the new image to the target folder $img_details->move('public/uploads/', $new_img_name); $final_img_name = $img_details->getName(); // After movement name // Step 8: Insert image data into the 'book_images' table $image_data = [ 'book_id' => $book_id, 'img_name' => $new_img_name, 'is_cover' => 1, // Cover image means 1 'type' => 1, // Manual uploaded means 1 ]; $BooksModel->insertImage($image_data); $this->logger->info("Books: Image Name After Upload" . $final_img_name); } else { $final_img_name = $existing_image_name; $this->logger->info("Books: Image Name are the same" . $new_img_name . " & " . $existing_image_name . ". Can't Upload"); } } else { // Not a valid image file, so replace existing file name $final_img_name = $existing_image_name; $this->logger->info("Books: Not a valid Image File. Nothing to Update/Upload"); } } else { // Testing Purpose: File details not available, so can't move it $this->logger->info("Books: Testing Purpose Image Details Not Available. Can't move it."); $final_img_name = $existing_image_name; } } catch (\Exception $e) { $this->logger->error("Book: Error Occurred = " . $e->getMessage() . " File = " . $e->getFile() . " Line = " . $e->getLine()); session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } return redirect()->route('book_list'); } ## For delete the book details (Which means inactive the details) public function delete_books($id) { try { helper('session'); $session_uid = get_logged_user_id(); $BooksModel = new BooksModel(); $where = ['book_id' => (int)$id, 'isactive =' => 1]; // $existingBook = $BooksModel->find($id); $existingBook = $BooksModel->where($where)->find($id); if ($existingBook) { $this->logger->Info("BOOK: Going to Inactive ID = ".$id); $data = ['isactive' => 0 , 'updated_by' => $session_uid]; if ($BooksModel->update($id, $data)) { $activeBookImages = $BooksModel->getData('book_images', $where); if(!empty($activeBookImages)){ for ($y = 0; $y < count($activeBookImages); $y++) { $bookImgIds[$y] = $activeBookImages[$y]->book_img_id; } if(!empty($bookImgIds)){ $this->logger->Info("BOOK: Images Going to Inactived = ".implode(" ",$bookImgIds)); $inactive_img_whereIn[0] = 'book_img_id'; $inactive_img_whereIn[1] = $bookImgIds; $BooksModel->inactiveMissingDetails('book_images',$data,$where, $inactive_img_whereIn); } } $activeBookCategory = $BooksModel->getData('book_categories', $where); if(!empty($activeBookCategory)){ for ($z = 0; $z < count($activeBookCategory); $z++) { $bookCategoryIds[$z] = $activeBookCategory[$z]->id; } if(!empty($bookCategoryIds)){ $this->logger->Info("BOOK: Categories Going to Inactived = ".implode(" ",$bookCategoryIds)); $inactive_category_whereIn[0] = 'id'; $inactive_category_whereIn[1] = $bookCategoryIds; $BooksModel->inactiveMissingDetails('book_categories',$data,$where, $inactive_category_whereIn); } } session()->setFlashdata('success', 'product successfully deleted.'); $this->logger->info("BOOK: has been Inactived successfully. Inactived ID = " . $id); } else { $this->logger->error("BOOK: Not able to Inactive ID =" . $id); throw new \Exception("Data Not able to Deleted"); } } } catch (\Exception $e) { $this->logger->error("Book: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine()); session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } return redirect()->route('book_list'); } }