file upload issue in book
This commit is contained in:
parent
94ebbb5793
commit
bd30a706f8
@ -60,21 +60,24 @@ class Books extends BaseController
|
|||||||
public function insert_books()
|
public function insert_books()
|
||||||
{
|
{
|
||||||
$requested_details = $this->request->getPost();
|
$requested_details = $this->request->getPost();
|
||||||
try{
|
|
||||||
|
try {
|
||||||
$session_uid = get_logged_user_id();
|
$session_uid = get_logged_user_id();
|
||||||
$session_bid = get_business_id();
|
$session_bid = get_business_id();
|
||||||
$BooksModel = new BooksModel();
|
$BooksModel = new BooksModel();
|
||||||
$book_id = $requested_details['book_id'];
|
$book_id = $requested_details['book_id'];
|
||||||
$requested_details['publication_date'] = (isset($requested_details['publication_date']) && $requested_details['publication_date'] != "") ? $requested_details['publication_date'] : NULL ;
|
$requested_details['publication_date'] = (isset($requested_details['publication_date']) && $requested_details['publication_date'] != "") ? $requested_details['publication_date'] : NULL;
|
||||||
if (empty($book_id )) {
|
|
||||||
|
// Step 1: Insert or update book details
|
||||||
|
if (empty($book_id)) {
|
||||||
$requested_details['created_by'] = (int)$session_uid;
|
$requested_details['created_by'] = (int)$session_uid;
|
||||||
if ($BooksModel->insert($requested_details)) {
|
if ($BooksModel->insert($requested_details)) {
|
||||||
$book_id = $BooksModel->insertID();
|
$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);
|
$this->logger->info("Books: has been added successfully. Inserted ID = " . $book_id);
|
||||||
} else {
|
} else {
|
||||||
session()->setFlashdata('error', 'product could not be created. Please try again..');
|
session()->setFlashdata('error', 'Product could not be created. Please try again.');
|
||||||
$this->logger->error("Books: Err could not be added. Please try again.");
|
$this->logger->error("Books: Error could not be added. Please try again.");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$isactive = $this->request->getPost('isactive');
|
$isactive = $this->request->getPost('isactive');
|
||||||
@ -82,36 +85,29 @@ class Books extends BaseController
|
|||||||
$requested_details['updated_by'] = (int)$session_uid;
|
$requested_details['updated_by'] = (int)$session_uid;
|
||||||
if ($BooksModel->update($book_id, $requested_details)) {
|
if ($BooksModel->update($book_id, $requested_details)) {
|
||||||
$book_affectedRows = $BooksModel->db->affectedRows();
|
$book_affectedRows = $BooksModel->db->affectedRows();
|
||||||
session()->setFlashdata('success', 'product successfully updated.');
|
session()->setFlashdata('success', 'Product successfully updated.');
|
||||||
$this->logger->info("Books: has been updated successfully. Updated Book ID = " . $book_id . " Aff ".$book_affectedRows);
|
$this->logger->info("Books: has been updated successfully. Updated Book ID = " . $book_id . " Aff " . $book_affectedRows);
|
||||||
} else {
|
} else {
|
||||||
session()->setFlashdata('error', 'product updation failed. Please try again.');
|
session()->setFlashdata('error', 'Product updation failed. Please try again.');
|
||||||
$this->logger->error("Books: Err Failed to update ID =" . $book_id);
|
$this->logger->error("Books: Error failed to update ID =" . $book_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
## book category section.
|
// Step 11: Get existing image details before the update
|
||||||
$category_id = $requested_details['category_id'];
|
$existing_image_record = $BooksModel->db->table('book_images')
|
||||||
if ($category_id != "") {
|
->where(['book_id' => $book_id, 'isactive' => 1, 'type' => 1])
|
||||||
|
|
||||||
$existingRecord = $BooksModel->db->table('book_categories')
|
|
||||||
->where(['book_id'=> $book_id,'category_id'=> $category_id])
|
|
||||||
->get()
|
->get()
|
||||||
->getRow();
|
->getRow();
|
||||||
|
|
||||||
if (!$existingRecord) {
|
if ($existing_image_record) {
|
||||||
$BooksModel->db->table('book_categories')->insert(['book_id' => $book_id, 'category_id' => $category_id]);
|
$existing_image_id = $existing_image_record->book_img_id;
|
||||||
$book_categories_id = $BooksModel->db->insertID();
|
$existing_image_name = $existing_image_record->img_name;
|
||||||
$this->logger->info("Books Category : has been added successfully. Inserted ID = " . $book_categories_id);
|
} else {
|
||||||
}else{
|
$existing_image_id = null;
|
||||||
$existingRecordId = $existingRecord->id;
|
$existing_image_name = null;
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
## CI validation rule for img_name
|
// CI validation rule for img_name
|
||||||
$validationRule = [
|
$validationRule = [
|
||||||
'img_name' => [
|
'img_name' => [
|
||||||
'label' => 'Image File',
|
'label' => 'Image File',
|
||||||
@ -119,95 +115,82 @@ class Books extends BaseController
|
|||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
$img_details = $this->request->getFile('img_name'); // Here I Have Image details ;
|
$img_details = $this->request->getFile('img_name'); // Image details
|
||||||
$final_img_name = NULL;//Just flag
|
$final_img_name = NULL;
|
||||||
$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
|
// Step 1: Image details available
|
||||||
if($img_details){
|
if ($img_details) {
|
||||||
$this->logger->info("Books: image details avaiable");
|
|
||||||
##Step 2 : i have image details .<br/>
|
|
||||||
if ($img_details->isValid()) {
|
if ($img_details->isValid()) {
|
||||||
##Step 3 : image Name.$new_img_name."<br/>";
|
$new_img_name = $img_details->getName(); // Before movement name
|
||||||
$new_img_name = $img_details->getName(); // before movement name
|
|
||||||
$this->logger->info("Books: Image Name B4 Upload".$new_img_name);
|
// Validation Rule Apply
|
||||||
##Step 4 : Validation Rule Apply here.if not throw the error"<br/>";
|
|
||||||
if (!$this->validate($validationRule)) {
|
if (!$this->validate($validationRule)) {
|
||||||
if($new_img_name !== ''){
|
if ($new_img_name !== '') {
|
||||||
|
|
||||||
$error = $this->validator->getErrors();
|
$error = $this->validator->getErrors();
|
||||||
// echo "Error : ".(string)$error."<br/>";
|
$this->logger->error("Books: Error on image upload" . $error['img_name']);
|
||||||
$this->logger->error("Books: Err on image upload".$error['img_name']);
|
throw new \Exception($error['img_name']);
|
||||||
throw new \Exception((string)$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);
|
// Check if existing and new image name are different
|
||||||
## Step 6 : Different Image Name means removed on target folder using Unlink;
|
if ($new_img_name !== $existing_image_name) {
|
||||||
if ($existing_image_id && $existing_img_name && is_file($img_path.$existing_img_name)) {
|
// Step 6: Different Image Name means removed from the database
|
||||||
$BooksModel->db->table('book_images')->where(['book_img_id'=>$existing_image_id,"book_id"=>$book_id])->update(['isactive' => 0,"updated_by"=>(int)$session_uid]);
|
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();
|
$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: 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);
|
// Optionally, delete the physical file from the server
|
||||||
}else{
|
if (is_file('public/uploads/'.$existing_image_name)) {
|
||||||
if(!$existing_image_id) $this->logger->info("Books: already Image Available but existing image id not availble in our DB So Unable to deactive");
|
$this->logger->info("Books: Already Image Available on target Folder Path: " . 'public/uploads/' . $existing_image_name . " So, Deleted.");
|
||||||
if(!$existing_img_name) $this->logger->info("Books: already Image Available but existing image name not founded So Unable to deactive");
|
unlink('public/uploads/'.$existing_image_name);
|
||||||
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;
|
} else {
|
||||||
$img_details->move($img_path, $new_img_name);
|
$this->logger->info("Books: No existing image record found. Nothing to update/delete.");
|
||||||
$final_img_name = $img_details->getName(); // after movement name
|
}
|
||||||
|
|
||||||
|
// 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 = [
|
$image_data = [
|
||||||
'book_id' => $book_id,
|
'book_id' => $book_id,
|
||||||
'img_name' => $new_img_name,
|
'img_name' => $new_img_name,
|
||||||
'is_cover' => 1, // cover images means 1
|
'is_cover' => 1, // Cover image means 1
|
||||||
'type' => 1, // manual uploaded means 1
|
'type' => 1, // Manual uploaded means 1
|
||||||
];
|
];
|
||||||
|
|
||||||
// Update image data in the 'book_images' table
|
|
||||||
$BooksModel->insertImage($image_data);
|
$BooksModel->insertImage($image_data);
|
||||||
|
|
||||||
$this->logger->info("Books: Image Name After Upload".$final_img_name);
|
$this->logger->info("Books: Image Name After Upload" . $final_img_name);
|
||||||
}else{
|
} else {
|
||||||
$final_img_name = $existing_img_name;
|
$final_img_name = $existing_image_name;
|
||||||
$this->logger->info("Books: Image Name are same".$new_img_name." & ".$existing_img_name." Can't Upload");
|
$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{
|
} else {
|
||||||
## Step 8 : Not a Vaild Image File so replace Existing file name;
|
// Testing Purpose: File details not available, so can't move it
|
||||||
$final_img_name = $existing_img_name;
|
$this->logger->info("Books: Testing Purpose Image Details Not Available. Can't move it.");
|
||||||
$this->logger->info("Books: Not a Vaild Image File Nothing To Update/Upload");
|
$final_img_name = $existing_image_name;
|
||||||
// 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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} 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());
|
session()->setFlashdata('error', 'Message: ' . $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return redirect()->route('book_list');
|
return redirect()->route('book_list');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## For delete the book details (Which means inactive the details)
|
## For delete the book details (Which means inactive the details)
|
||||||
public function delete_books($id)
|
public function delete_books($id)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -79,17 +79,19 @@ public function getCategories()
|
|||||||
return $categories;
|
return $categories;
|
||||||
}
|
}
|
||||||
public function getBooksAndImagesByBookId($id)
|
public function getBooksAndImagesByBookId($id)
|
||||||
{
|
{
|
||||||
$builder = $this->db->table('books');
|
$builder = $this->db->table('books');
|
||||||
$builder->select('books.*, book_images.img_name, book_images.is_cover, book_images.type,book_categories.category_id');
|
$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_images', 'book_images.book_id = books.book_id', 'left');
|
||||||
$builder->join('book_categories', 'book_categories.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->where('books.book_id', $id);
|
||||||
|
$builder->orderBy('book_images.created_on', 'DESC');
|
||||||
|
|
||||||
$query = $builder->get();
|
$query = $builder->get();
|
||||||
|
|
||||||
return $query->getResultArray();
|
return $query->getResultArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function insertImage($imageData)
|
public function insertImage($imageData)
|
||||||
{
|
{
|
||||||
return $this->db->table('book_images')->insert($imageData);
|
return $this->db->table('book_images')->insert($imageData);
|
||||||
|
|||||||
@ -115,7 +115,7 @@
|
|||||||
<div class="media-body">
|
<div class="media-body">
|
||||||
<h5 class="mt-0">Existing Image</h5>
|
<h5 class="mt-0">Existing Image</h5>
|
||||||
<p class="mb-1"><?= $details['img_name']; ?></p>
|
<p class="mb-1"><?= $details['img_name']; ?></p>
|
||||||
<input type="hidden" id="existing_img_name_name" name="existing_img_name_name" placeholder="hidden Field for Image Cover name" readonly value="<?= $details['img_name']; ?>" />
|
<input type="hidden" id="existing_img_name" name="existing_img_name" placeholder="hidden Field for Image Cover name" readonly value="<?= $details['img_name']; ?>" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user