api Book Category fixes

This commit is contained in:
VE10-Sanjeev 2023-10-07 18:04:00 +05:30
parent 330f9a19bb
commit 6663703cfe

View File

@ -200,22 +200,22 @@ class ApiIntegration extends ResourceController
$page_count = NULL;
$genre = NULL;
$author = NULL;
$category = 1; // Book
$category = NULL; // Book
$attributes = $records['attributes'];
$categories = $records['categories'];
if (count($categories) > 0) {
$categoryNames = [];
foreach ($categories as $category) {
if (isset($category->name) && $category->name != "") {
if($category->name !== "Membership"){
$categoryNames[] = $category->name; }
else{ $category = 2; }
}
}
if (!empty($categoryNames)) {
$genre = implode(", ", $categoryNames);
}
}
// if (count($categories) > 0) {
// $categoryNames = [];
// foreach ($categories as $category) {
// if (isset($category->name) && $category->name != "") {
// if($category->name !== "Membership"){
// $category = 1;$categoryNames[] = $category->name; }
// else{ $category = 2; }
// }
// }
// if (!empty($categoryNames)) {
// $genre = implode(", ", $categoryNames);
// }
// }
if (count($attributes) > 0) {
foreach ($attributes as $att) {
if (isset($att->name) && $att->name == "Publisher") {
@ -340,8 +340,8 @@ class ApiIntegration extends ResourceController
$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;
$this->logger->info("Api SaveBookDetails : Image Data = $i ");
$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') : "");
$this->logger->info("Api SaveBookDetails : ".$extensive_correspondence);
@ -349,6 +349,97 @@ class ApiIntegration extends ResourceController
else{
$this->logger->error("Api SaveBookDetails : Err = Image Data Not Found");
}
if (count($categories) > 0 && $last_insert_book_id != "") {
//$this->save_categories_master_details($categories);
$extensive_correspondence .= " Its have " . count($categories) . " Categories";
$basic_message .= " And Its have " . count($categories) . " Categories";
$this->logger->info("Api SaveBookDetails : ".$extensive_correspondence);
$master_categories = $api_model->getData('category', ['isactive' => 1]);
$this->logger->info("Api SaveBookDetails : total master categories count = ".count($master_categories));
$where = ['book_id' => (int)$last_insert_book_id, 'isactive' => 1 ];
$existing_categories = $api_model->getData('book_categories', $where);
$this->logger->info("Api SaveBookDetails : existing categories count = ".count($existing_categories));
$request_categories = (array)$categories;
$filtering_existing_categories_wpApiId = [];
$filtering_requested_categories_wpApiId = [];
$missing_category_values = [];
$common_category_element = [];
if (!empty($existing_categories)) {
for ($z = 0; $z < count($request_categories); $z++) {
$filtering_requested_categories_wpApiId[$z] = $request_categories[$z]->id;
}
for ($y = 0; $y < count($master_categories); $y++) {
$filtering_existing_categories_wpApiId[$y] = $master_categories[$y]->wp_api_category_id;
}
$filtering_existing_categories_wpApiId = array_diff($filtering_existing_categories_wpApiId, array(""));
if (!empty($filtering_requested_categories_wpApiId)) {
$this->logger->info("Api SaveBookDetails : Incoming Categories Wp Id " .implode(",",$filtering_requested_categories_wpApiId));
$this->logger->info("Api SaveBookDetails : Existing Categories Wp Id " .implode(",",$filtering_existing_categories_wpApiId));
$A = $filtering_existing_categories_wpApiId;
$B = $filtering_requested_categories_wpApiId;
$missing_category_values = array_diff($A, $B); //Find difference element in Existing Images only
$common_category_element = array_intersect($A, $B);
$this->logger->info("Api SaveBookDetails : Missing Categories Wp Id " .implode(",",$missing_category_values)." Are inactived");
if (!empty($missing_category_values)) {
$this->logger->info("Api SaveBookDetails : Missing Categories Wp Id " .implode(",",$missing_category_values)." Are inactived");
$where1 = ['isactive' => 1];
$whereIn1[0] = 'wp_api_category_id';
$whereIn1[1] = array_filter($missing_category_values);
$get_master_category_id_to_inactive = $api_model->getData('category', $where1, $whereIn1);
for ($x = 0; $x < count($get_master_category_id_to_inactive); $x++) {
$book_categories_to_inactive[$x] = $get_master_category_id_to_inactive[$x]->id;
}
$this->logger->info("Api SaveBookDetails : Missing Categories Wp Id " .implode(",",$missing_category_values)." catgory pkid => ".implode(",",$book_categories_to_inactive). "Are inactived");
$where2 = ['isactive' => 1];
$whereIn2[0] = 'category_id';
$whereIn2[1] = $book_categories_to_inactive;
$api_model->inactiveMissingDetails('book_categories', $where2, $whereIn2);
//echo $api_model->getlastQuery();
}
} // loop closed
}
$where = ['isactive' => 1];
if (!empty($common_category_element)) {
$whereIn[0] = 'wp_api_category_id';
$whereIn[1] = $common_category_element;
} else {
$whereIn = null;
}
$get_master_category_based_WPid = $api_model->getData('category', $where, $whereIn);
$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;
foreach ($categories as $cate) {
$categories_id = null;
$book_category_primarykey = null;
// Iterate through the array and find the 'book_img_id' based on 'wp_api_img_id'
if (!empty($get_master_category_based_WPid)) {
foreach ($get_master_category_based_WPid as $master) {
if ($master->wp_api_category_id == $cate->id) {
$categories_id = $master->id;
$exist_categories = $api_model->getData('book_categories', ['isactive'=>1,'category_id'=>$categories_id,'book_id' => (int)$last_insert_book_id]);
$book_category_primarykey = isset($exist_categories) && !empty($exist_categories) ? $exist_categories[0]->id : NULL;
$this->logger->info("Api SaveBookDetails : Master Category ID = " .$categories_id." => WP CategoryID = ".$master->wp_api_category_id);
break; // Found the match, no need to continue looping
}
}
}
$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++;
} // loop closed
$extensive_correspondence .= (!empty($book_categories_data) ? $api_model->insertAndUpdateChildDetails($book_categories_data, 'book_categories', 'id') : "");
$this->logger->info("Api SaveBookDetails : ".$extensive_correspondence);
} //category count closed
else{
$this->logger->error("Api SaveBookDetails : Err = Category Data Not Found");
}
$response = ['status' => 200, 'message' => $basic_message ,'indetails' => $extensive_correspondence];
} //if cond. closed
else {
@ -891,7 +982,78 @@ class ApiIntegration extends ResourceController
$response = ['status' => 204, 'message' => 'Data No Found'];
}
return $response;
}
}
public function save_categories_master_details($records)
{
$api_model = new ApiIntegrationModel();
if (count($records) > 0) {
$this->logger->info("Api SaveCategoriesMasterDetails : Categories Count = ".count($records));
$existing_categories = $api_model->getData('category', ['isactive' => 1]);
$request_categories = (array)$records;
$filteringExistingCategoriesWpApiId = [];
$filteringRequestedCategoriesWpApiId = [];
$commonCategoriesElement = [];
if (!empty($existing_categories)) {
for ($z = 0; $z < count($request_categories); $z++) {
$filteringRequestedCategoriesWpApiId[$z] = $request_categories[$z]->id;
}
for ($y = 0; $y < count($existing_categories); $y++) {
$filteringExistingCategoriesWpApiId[$y] = $existing_categories[$y]->wp_api_category_id;
}
if (!empty($filteringExistingCategoriesWpApiId)) {
$this->logger->info("Api SaveCategoriesMasterDetails : Incoming Categories Wp Id " .implode(",",$filteringRequestedCategoriesWpApiId));
$this->logger->info("Api SaveCategoriesMasterDetails : Existing Categories Wp Id " .implode(",",$filteringExistingCategoriesWpApiId));
$A = $filteringExistingCategoriesWpApiId;
$B = $filteringRequestedCategoriesWpApiId;
$missingCategoriesValues = array_diff($A, $B); //Find difference element in Existing Images only
$commonCategoriesElement = array_intersect($A, $B);
if (!empty($missingCategoriesValues)) {
$this->logger->info("Api SaveCategoriesMasterDetails : Missing Categories Wp Id " .implode(",",$missingCategoriesValues)." Are inactived");
$inactive_where = ['isactive' => 1];
$inactive_whereIn[0] = 'wp_api_category_id';
$inactive_whereIn[1] = $missingCategoriesValues;
$api_model->inactiveMissingDetails('category', $inactive_where, $inactive_whereIn);
}
}
}
$where = ['isactive' => 1];
if (!empty($commonCategoriesElement)) {
$whereIn[0] = 'wp_api_category_id';
$whereIn[1] = $commonCategoriesElement;
} else {
$whereIn = null;
}
$lastestActiveCategoriesData = $api_model->getData('category', $where, $whereIn);
$this->logger->info("Api SaveCategoriesMasterDetails : lastest Active Book Categories Data Count " .count($lastestActiveCategoriesData));
$book_category_data = [];
$a = 0;
foreach ($records as $r) {
$categoryId = 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_category_id == $r->id) {
$categoryId = $item->id;
break; // Found the match, no need to continue looping
}
}
}
$book_category_data[$a]['id'] = $categoryId;
$book_category_data[$a]['name'] = $r->name;
$book_category_data[$a]['wp_api_category_id'] = $r->id;
$book_category_data[$a]['parent_wp_api_category_id'] =NULL;
$a++;
$this->logger->info("Api SaveCategoriesMasterDetails : Categories Data = $a Request data to save master = ".json_encode($book_category_data));
} // loop closed
$extensive_correspondence = (!empty($book_image_data) ? $api_model->insertAndUpdateChildDetails($book_category_data, 'category', 'id') : "");
$this->logger->info("Api SaveCategoriesMasterDetails : ".$extensive_correspondence);
} // count closed
else{
$this->logger->error("Api SaveCategoriesMasterDetails : Err = Categories Data Not Found");
}
}
}