diff --git a/app/Controllers/Books.php b/app/Controllers/Books.php index 8d51d4b0..5a165bfb 100755 --- a/app/Controllers/Books.php +++ b/app/Controllers/Books.php @@ -54,6 +54,7 @@ class Books extends BaseController ## For inserting/updating details of book public function insert_books() { + try{ helper('session'); $session_uid = get_logged_user_id(); $session_bid = get_business_id(); @@ -121,13 +122,29 @@ class Books extends BaseController $data['isactive'] = 1; $data['category'] = 1; $data['created_by'] = $session_uid; - $BooksModel->insert($data); + if ($BooksModel->insert($data)) { + session()->setFlashdata('success', 'Book has been added successfully.'); + $this->logger->info("Books: has been added successfully. Inserted ID = " . $BooksModel->insertID()); + } else { + session()->setFlashdata('error', 'Book could not be added. Please try again..'); + $this->logger->error("Books: Err could not be added. Please try again."); + } } else { // It's an update operation $isactive = $this->request->getPost('isactive'); $data['isactive'] = ($isactive == 'on') ? 1 : 0; $data['updated_by'] = $session_uid; - $BooksModel->update($book_id, $data); + if ($BooksModel->update($book_id, $data)) { + session()->setFlashdata('success', 'Book has been updated successfully.'); + $this->logger->info("Books: has been updated successfully. Updated Book ID = " . $book_id); + } else { + session()->setFlashdata('error', 'Book update failed. Please try again.'); + $this->logger->error("Books: Err Failed to update ID =" . $book_id); + } + } + } 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'); } @@ -135,21 +152,55 @@ class Books extends BaseController ## For delete the book details (Which means inactive the details) public function delete_books($id) { - - helper('session'); - $session_uid = get_logged_user_id(); - - $BooksModel = new BooksModel(); - - $existingBook = $BooksModel->find($id); - if (!$existingBook) { - return redirect()->route('book_list'); + 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',$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',$where, $inactive_category_whereIn); + } + } + + session()->setFlashdata('success', 'Deleted successfully.'); + $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()); } - - $data['isactive'] = 0; - $data['updated_by'] = $session_uid; - $BooksModel->update($id, $data); return redirect()->route('book_list'); } + } diff --git a/app/Models/BooksModel.php b/app/Models/BooksModel.php index 80dfc8d6..4a7d9ca2 100644 --- a/app/Models/BooksModel.php +++ b/app/Models/BooksModel.php @@ -58,4 +58,22 @@ class BooksModel extends Model return $books; } +public function getData($table, $where = null,$whereIn = null) +{ + $query = $this->db->table($table); + if ($where) { + $query->where($where); + } + if($whereIn){ + // $query->whereIn($column_name,$missingValues) + $query->whereIn($whereIn[0],$whereIn[1]); + } + return $query->get()->getResult(); +} + +public function inactiveMissingDetails($table_name,$where,$whereIn){ + $dataToUpdate = ['isactive' => 0]; + $this->db->table($table_name)->where($where)->whereIn($whereIn[0],$whereIn[1])->update($dataToUpdate); +} + } diff --git a/app/Models/CustomerModel.php b/app/Models/CustomerModel.php index 79a7a894..7afe40a5 100644 --- a/app/Models/CustomerModel.php +++ b/app/Models/CustomerModel.php @@ -9,11 +9,8 @@ class CustomerModel extends Model { protected $table = 'customers'; protected $primaryKey = 'customer_id '; -<<<<<<< HEAD - protected $allowedFields = ['customer_id ','first_name','city','last_name','email','mobile_no','country','state','postal_code' ,'address','profile_picture','date_of_birth','gender','mode','isactive','business_id']; -======= + // protected $allowedFields = ['customer_id ','first_name','city','last_name','email','mobile_no','country','state','postal_code' ,'address','profile_picture','date_of_birth','gender','mode','isactive','business_id']; protected $allowedFields = ['customer_id ','first_name','city','last_name','email','mobile_no','country','state','postal_code' ,'address','profile_picture','date_of_birth','gender','mode','type','isactive','business_id','created_by','updated_by']; ->>>>>>> caaaaa0ffd2a11af5f49570cb1fadb521e89b52f public function insertCustomer($data) { diff --git a/app/Views/book_form.php b/app/Views/book_form.php index 6bef9284..20499904 100644 --- a/app/Views/book_form.php +++ b/app/Views/book_form.php @@ -53,7 +53,7 @@
- +
Please provide.
@@ -83,7 +83,7 @@
- +
diff --git a/app/Views/book_list.php b/app/Views/book_list.php index 28e8b58e..deb249e5 100644 --- a/app/Views/book_list.php +++ b/app/Views/book_list.php @@ -7,6 +7,24 @@

+ getFlashdata('success') || session()->getFlashdata('error')) : ?> + getFlashdata('success')) : ?> + + + getFlashdata('error')) : ?> + + +