From 911cd18af2f075bdf1e422836e7df986883c991c Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Wed, 20 Dec 2023 11:20:35 +0530 Subject: [PATCH] Product Image and category : ps --- app/Controllers/ApiIntegration.php | 95 ++++++++++++++++-------------- 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/app/Controllers/ApiIntegration.php b/app/Controllers/ApiIntegration.php index 1fa5d1b3..d370b0bb 100644 --- a/app/Controllers/ApiIntegration.php +++ b/app/Controllers/ApiIntegration.php @@ -90,7 +90,7 @@ class ApiIntegration extends ResourceController $response = $this->save_book_details($records, $wordpress_book_id); $this->logger->info("Api CallMethods : ".str_replace("_", " ",ucwords($api_request_text))." response = ".$response['status']); } else { - throw new \Exception('The "id" field is not set or empty.'); + throw new \Exception('The "id" (wp_api_product_id) field is not set or empty.'); } } catch (\Exception $e) { // $this->logger->error("Api CallMethods : ".str_replace("_", " ",ucwords($api_request_text))." {exception} ", ['exception' => $e]); @@ -108,7 +108,7 @@ class ApiIntegration extends ResourceController $response = $this->save_customer_details($records, $wordpress_customer_id); $this->logger->info("Api CallMethods : response = ".$response['status']); } else { - throw new \Exception('The "id" field is not set or empty.'); + throw new \Exception('The "id" (which means wordpress_api_customer_id) field is not set or empty.'); } } catch (\Exception $e) { @@ -127,7 +127,7 @@ class ApiIntegration extends ResourceController $response = $this->save_sales_details($records, $wordpress_order_id); $this->logger->info("Api CallMethods : response = ".$response['status']); } else { - throw new \Exception('The "id" field is not set or empty.'); + throw new \Exception('The "id" (which means wp_api_order_id) field is not set or empty.'); } } catch (\Exception $e) { // $this->logger->error("Api CallMethods : ".str_replace("_", " ",ucwords($api_request_text))." {exception}", ['exception' => $e]); @@ -199,7 +199,7 @@ class ApiIntegration extends ResourceController public function save_book_details($records, $wordpress_book_id) { - $this->logger->info("Api SaveBookDetails : records count = ".count($records).", wordpress Product Ref ID = ".$wordpress_book_id); + $this->logger->info("Api SaveBookDetails : records count = ".count($records).", wordpress Product (Ref ID) = ".$wordpress_book_id); $api_model = new ApiIntegrationModel(); $extensive_correspondence = ""; $basic_message = ""; @@ -299,7 +299,8 @@ class ApiIntegration extends ResourceController } $this->logger->info("Api SaveBookDetails : Book images count : " .count($images)); - if (count($images) > 0 && $last_insert_book_id != "") { + if ($last_insert_book_id != "") { + // if (count($images) > 0 && $last_insert_book_id != "") { $extensive_correspondence .= " Its have " . count($images) . " Images ,"; $basic_message .= " Its have " . count($images) . " Images"; $this->logger->info("Api SaveBookDetails : ".$extensive_correspondence); @@ -347,36 +348,40 @@ class ApiIntegration extends ResourceController $lastestActiveBookImageData = $api_model->getData('book_images', $where, $whereIn); $this->logger->info("Api SaveBookDetails : lastest Active Book Image Data Count " .count($lastestActiveBookImageData)); $book_image_data = []; - $i = 0; - foreach ($images as $img) { - $bookImgId = null; - // Iterate through the array and find the 'book_img_id' based on 'wp_api_img_id' - if (!empty($lastestActiveBookImageData)) { - foreach ($lastestActiveBookImageData as $item) { - if ($item->wp_api_img_id == $img->id) { - $bookImgId = $item->book_img_id; - break; // Found the match, no need to continue looping + $book_image_inx = 0; + if(count($images) > 0){ + foreach ($images as $img) { + $bookImgId = null; + // Iterate through the array and find the 'book_img_id' based on 'wp_api_img_id' + if (!empty($lastestActiveBookImageData)) { + foreach ($lastestActiveBookImageData as $item) { + if ($item->wp_api_img_id == $img->id) { + $bookImgId = $item->book_img_id; + break; // Found the match, no need to continue looping + } } } - } - $book_image_data[$i]['img_name'] =isset($img->name) && !empty($img->name) ? $img->name : NULL; - $book_image_data[$i]['wp_img_url'] = isset($img->src) && !empty($img->src) ? $img->src : NULL; - $book_image_data[$i]['is_cover'] = (!empty($bookImgId) && $i == 0) ? 1 : 0 ; - $book_image_data[$i]['type'] = 2; - $book_image_data[$i]['book_id'] = $last_insert_book_id; - $book_image_data[$i]['isactive'] = 1; - $book_image_data[$i]['wp_api_img_id'] = isset($img->id) && !empty($img->id) ? $img->id : NULL; - $book_image_data[$i]['book_img_id'] = $bookImgId; - $i++; - $this->logger->info("Api SaveBookDetails : Image Data = $i Request data = ".json_encode($book_image_data)); - } // image loop closed - $extensive_correspondence .= (!empty($book_image_data) ? $api_model->insertAndUpdateChildDetails($book_image_data, 'book_images', 'book_img_id') : ""); + $book_image_data[$book_image_inx]['img_name'] =isset($img->name) && !empty($img->name) ? $img->name : NULL; + $book_image_data[$book_image_inx]['wp_img_url'] = isset($img->src) && !empty($img->src) ? $img->src : NULL; + $book_image_data[$book_image_inx]['is_cover'] = (!empty($bookImgId) && $book_image_inx == 0) ? 1 : 0 ; + $book_image_data[$book_image_inx]['type'] = 2; + $book_image_data[$book_image_inx]['book_id'] = $last_insert_book_id; + $book_image_data[$book_image_inx]['isactive'] = 1; + $book_image_data[$book_image_inx]['wp_api_img_id'] = isset($img->id) && !empty($img->id) ? $img->id : NULL; + $book_image_data[$book_image_inx]['book_img_id'] = $bookImgId; + $book_image_inx++; + $this->logger->info("Api SaveBookDetails : Image Data = $book_image_inx Request data = ".json_encode($book_image_data)); + } // image loop closed + $extensive_correspondence .= (!empty($book_image_data) ? $api_model->insertAndUpdateChildDetails($book_image_data, 'book_images', 'book_img_id') : ""); + } $this->logger->info("Api SaveBookDetails : ".$extensive_correspondence); } //image count closed else{ - $this->logger->error("Api SaveBookDetails : Err = Image Data Not Found"); + $this->logger->error("Api SaveBookDetails : Err = Book Image Data Not Found"); } - if (count($categories) > 0 && $last_insert_book_id != "") { + // if (count($categories) > 0 && $last_insert_book_id != "") { + if ($last_insert_book_id != "") { + ## To save/update categories master details $this->save_categories_master_details($categories); $extensive_correspondence .= " Its have " . count($categories) . " Categories"; $basic_message .= " And Its have " . count($categories) . " Categories"; @@ -423,7 +428,7 @@ class ApiIntegration extends ResourceController $where2 = ['isactive' => 1]; $whereIn2[0] = 'category_id'; $whereIn2[1] = $book_categories_to_inactive; - $data_to_batch_update2 = ['isactive' => 0,'updated_by'=>$this->global_variable_user_id]; + $data_to_batch_update2 = ['isactive' => 0,'updated_by'=>$this->global_variable_user_id,'book_id'=>$last_insert_book_id]; $api_model->inactiveMissingDetails('book_categories',$data_to_batch_update2, $where2, $whereIn2); //echo $api_model->getlastQuery(); } @@ -441,7 +446,7 @@ class ApiIntegration extends ResourceController $this->logger->info("Api SaveBookDetails : Common Categories Wp Id " .implode(",",$common_category_element)." Are already inserted data"); $this->logger->info("Api SaveBookDetails : available category data count = " .count($request_categories)); $book_categories_data = []; - $j = 0; + $book_categories_inx = 0; foreach ($categories as $cate) { $categories_id = null; $book_category_primarykey = null; @@ -457,10 +462,10 @@ class ApiIntegration extends ResourceController } } } - $book_categories_data[$j]['id'] = $book_category_primarykey; - $book_categories_data[$j]['category_id'] = (int)$categories_id; - $book_categories_data[$j]['book_id'] = $last_insert_book_id; - $j++; + $book_categories_data[$book_categories_inx]['id'] = $book_category_primarykey; + $book_categories_data[$book_categories_inx]['category_id'] = (int)$categories_id; + $book_categories_data[$book_categories_inx]['book_id'] = $last_insert_book_id; + $book_categories_inx++; } // loop closed $extensive_correspondence .= (!empty($book_categories_data) ? $api_model->insertAndUpdateChildDetails($book_categories_data, 'book_categories', 'id') : ""); $this->logger->info("Api SaveBookDetails : ".$extensive_correspondence); @@ -1123,8 +1128,8 @@ class ApiIntegration extends ResourceController } $lastestActiveMasterCategoriesData = $api_model->getData('category', $where, $where_in); $this->logger->info("Api SaveCategoriesMasterDetails : lastest Active Book Categories Data Count " . count($lastestActiveMasterCategoriesData)); - $book_category_data = []; - $a = 0; + $master_categories_data = []; + $master_categories_inx = 0; foreach ($request_master_categories as $r) { $masterCategoryId = null; if (!empty($lastestActiveMasterCategoriesData)) { @@ -1135,15 +1140,15 @@ class ApiIntegration extends ResourceController } } } - $book_category_data[$a]['id'] = $masterCategoryId; - $book_category_data[$a]['name'] = $r->name; - $book_category_data[$a]['wp_api_category_id'] = $r->id; - $book_category_data[$a]['business_id'] = $this->global_variable_business_id; - $book_category_data[$a]['parent_wp_api_category_id'] = NULL; - $a++; - $this->logger->info("Api SaveCategoriesMasterDetails : Categories Data = $a Request data to save on master category = " . json_encode($book_category_data)); + $master_categories_data[$master_categories_inx]['id'] = $masterCategoryId; + $master_categories_data[$master_categories_inx]['name'] = $r->name; + $master_categories_data[$master_categories_inx]['wp_api_category_id'] = $r->id; + $master_categories_data[$master_categories_inx]['business_id'] = $this->global_variable_business_id; + $master_categories_data[$master_categories_inx]['parent_wp_api_category_id'] = NULL; + $master_categories_inx++; + $this->logger->info("Api SaveCategoriesMasterDetails : Categories Data = $master_categories_inx Request data to save on master category = " . json_encode($master_categories_data)); } // loop closed - $message = (!empty($book_category_data) ? $api_model->insertAndUpdateChildDetails($book_category_data, 'category', 'id') : "Info : New Categories Not Avaialble to Save"); + $message = (!empty($master_categories_data) ? $api_model->insertAndUpdateChildDetails($master_categories_data, 'category', 'id') : "Info : New Categories Not Avaialble to Save"); $this->logger->info("Api SaveCategoriesMasterDetails : " . $message); } // count closed else {