diff --git a/app/Controllers/Books.php b/app/Controllers/Books.php
index ebd43a51..02eb3c3b 100755
--- a/app/Controllers/Books.php
+++ b/app/Controllers/Books.php
@@ -60,21 +60,24 @@ class Books extends BaseController
public function insert_books()
{
$requested_details = $this->request->getPost();
- try{
+
+ 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 ;
- if (empty($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.');
+ 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: Err could not be added. Please try again.");
+ 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');
@@ -82,131 +85,111 @@ class Books extends BaseController
$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);
+ 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: Err Failed to update ID =" . $book_id);
+ 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,'category_id'=> $category_id])
- ->get()
- ->getRow();
-
- if (!$existingRecord) {
- $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{
- $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);
- }
+
+ // 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'); // Here I Have Image details ;
- $final_img_name = NULL;//Just flag
- $existing_img_name = $this->request->getPost('existing_img_name'); // HiddenField for if have any pic name means;
- $img_path = 'public/uploads/';
-
- ##Step 1 : image details available
- if($img_details){
- $this->logger->info("Books: image details avaiable");
- ##Step 2 : i have image details .
- if ($img_details->isValid()) {
- ##Step 3 : image Name.$new_img_name."
";
- $new_img_name = $img_details->getName(); // before movement name
- $this->logger->info("Books: Image Name B4 Upload".$new_img_name);
- ##Step 4 : Validation Rule Apply here.if not throw the error"
";
- if (!$this->validate($validationRule)) {
- if($new_img_name !== ''){
-
- $error = $this->validator->getErrors();
- // echo "Error : ".(string)$error."
";
- $this->logger->error("Books: Err on image upload".$error['img_name']);
- throw new \Exception((string)$error['img_name']);
- }
+
+ // 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']);
}
- ##Step 5 : Check Existing and New Image Name Same Or not same no use to move on target folder
- if($new_img_name !== $existing_img_name){
- $existing_images_record = $BooksModel->db->table('book_images')
- ->where(['book_id'=>$book_id,'isactive'=>1,'type'=>1])
- ->get()->getRow();
-
- if ($existing_images_record) {
- $existing_image_id = $existing_images_record->book_img_id;
- } else {
- // Handle the case when the record does not exist
- $existing_image_id = null; // Or set it to an appropriate default value
- }
-
- $this->logger->info("Books: Image Name are Differ".$new_img_name." & ".$existing_img_name);
- ## Step 6 : Different Image Name means removed on target folder using Unlink;
- if ($existing_image_id && $existing_img_name && is_file($img_path.$existing_img_name)) {
- $BooksModel->db->table('book_images')->where(['book_img_id'=>$existing_image_id,"book_id"=>$book_id])->update(['isactive' => 0,"updated_by"=>(int)$session_uid]);
- $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);
- $this->logger->info("Books: already Image Available on target Folder Path : ".$img_path.$existing_img_name." So, Deleted.");
- unlink($img_path.$existing_img_name);
- }else{
- if(!$existing_image_id) $this->logger->info("Books: already Image Available but existing image id not availble in our DB So Unable to deactive");
- if(!$existing_img_name) $this->logger->info("Books: already Image Available but existing image name not founded So Unable to deactive");
- if(!is_file($img_path.$existing_img_name)) $this->logger->info("Books: Image on target Folder Path : ".$img_path.$existing_img_name." So Unable to deactive");
- }
- ## Step 7 : Moved to target;
- $img_details->move($img_path, $new_img_name);
- $final_img_name = $img_details->getName(); // after movement name
- $image_data = [
- 'book_id' => $book_id,
- 'img_name' => $new_img_name,
- 'is_cover' => 1, // cover images means 1
- 'type' => 1, // manual uploaded means 1
- ];
-
- // Update image data in the 'book_images' table
- $BooksModel->insertImage($image_data);
-
- $this->logger->info("Books: Image Name After Upload".$final_img_name);
- }else{
- $final_img_name = $existing_img_name;
- $this->logger->info("Books: Image Name are same".$new_img_name." & ".$existing_img_name." Can't Upload");
- }
- }
- else{
- ## Step 8 : Not a Vaild Image File so replace Existing file name;
- $final_img_name = $existing_img_name;
- $this->logger->info("Books: Not a Vaild Image File Nothing To Update/Upload");
- // throw new \Exception("Not a Vaild Image File");
}
- }
- else{
- ## Step 9 : Testing Purpose File Details Not Available. so i can't move it.;
- $this->logger->info("Books: Testing Purpose Image Details Not Available. so i can't move it.");
- $final_img_name = $existing_img_name;
- //throw new \Exception("Testing Purpose File Details Not Available so i can't move it if you have existing filee means save it or your choice");
- }
+
+ // 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])
+ ->update(['isactive' => 0, 'updated_by' => (int)$session_uid]);
+ $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: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine());
+ $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)
diff --git a/app/Models/BooksModel.php b/app/Models/BooksModel.php
index 2d762339..525dd3cd 100644
--- a/app/Models/BooksModel.php
+++ b/app/Models/BooksModel.php
@@ -79,17 +79,19 @@ public function getCategories()
return $categories;
}
public function getBooksAndImagesByBookId($id)
-{
- $builder = $this->db->table('books');
- $builder->select('books.*, book_images.img_name, book_images.is_cover, book_images.type,book_categories.category_id');
- $builder->join('book_images', 'book_images.book_id = books.book_id', 'left');
- $builder->join('book_categories', 'book_categories.book_id = books.book_id', 'left');
- $builder->where('books.book_id', $id);
-
- $query = $builder->get();
-
- return $query->getResultArray();
-}
+ {
+ $builder = $this->db->table('books');
+ $builder->select('books.*, book_images.img_name, book_images.is_cover, book_images.type, book_categories.category_id');
+ $builder->join('book_images', 'book_images.book_id = books.book_id', 'left');
+ $builder->join('book_categories', 'book_categories.book_id = books.book_id', 'left');
+ $builder->where('books.book_id', $id);
+ $builder->orderBy('book_images.created_on', 'DESC');
+
+ $query = $builder->get();
+
+ return $query->getResultArray();
+ }
+
public function insertImage($imageData)
{
return $this->db->table('book_images')->insert($imageData);
diff --git a/app/Views/book_form.php b/app/Views/book_form.php
index 7bfb9bc5..39f8f9a5 100644
--- a/app/Views/book_form.php
+++ b/app/Views/book_form.php
@@ -115,7 +115,7 @@
= $details['img_name']; ?>
- +