From 88baff883bfb49d76d09ce697cc5b92d895dae36 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Tue, 5 Sep 2023 19:07:48 +0530 Subject: [PATCH] Api New controller : ps --- app/Config/Constants.php | 6 +- app/Config/Routes.php | 6 +- app/Controllers/ApiIntegration.php | 180 +++++++++++++++++++++++++++++ app/Controllers/Books.php | 73 ------------ app/Controllers/Customer.php | 99 ---------------- app/Models/ApiIntegrationModel.php | 74 ++++++++++++ app/Views/book_list.php | 1 - app/Views/customer_list.php | 1 - index.php | 4 + 9 files changed, 265 insertions(+), 179 deletions(-) create mode 100644 app/Controllers/ApiIntegration.php create mode 100644 app/Models/ApiIntegrationModel.php diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 4f636275..d3df0db2 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -96,8 +96,8 @@ define('EVENT_PRIORITY_HIGH', 10); /** * Constants For Api Integration. */ -define('VB_APIURL', 'https://vbp.venbait.in/wp-json/wc/v3/'); -define('VB_BOOKS', VB_APIURL.'products'); -define('VB_CUSTOMERS', VB_APIURL.'customers'); +// define('VB_APIURL', 'https://vbp.venbait.in/wp-json/wc/v3/'); +// define('VB_BOOKS', VB_APIURL.'products'); +// define('VB_CUSTOMERS', VB_APIURL.'customers'); diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 709ed250..0864bbc5 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -84,8 +84,10 @@ $routes->post("insert_scheme/", "Scheme::insert_scheme"); $routes->get("delete_scheme/(:any)", "Scheme::delete_scheme/$1"); # Api integration Routes -$routes->get("book_api_integration", "Books::apiintegration"); -$routes->get("customer_api_integration", "Customer::apiintegration"); +$routes->group("api", function ($routes) { + $routes->post("create_products/", "ApiIntegration::save_book_details"); + $routes->post("create_customers/", "ApiIntegration::save_customer_details"); +}); /* * -------------------------------------------------------------------- diff --git a/app/Controllers/ApiIntegration.php b/app/Controllers/ApiIntegration.php new file mode 100644 index 00000000..8afc8314 --- /dev/null +++ b/app/Controllers/ApiIntegration.php @@ -0,0 +1,180 @@ +is('post')) { + $records = (array)$request->getVar(); + if (count($records) > 0 && $records['status'] == "publish" && !empty($records['id'])) { + $where = ['wp_api_id' => $records['id']]; + $countAll = $api_model->countAll('books', $where); + if ((int)$countAll == 0) { + $publisher = ''; + $language = ''; + $attributes = $records['attributes']; + if (count($attributes) > 0) { + foreach ($attributes as $att) { + if ($att->name == "Publisher") { + $publisher = implode(", ", $att->options); + } + if ($att->name == "Book Author") { + $publisher .= implode(", ", $att->options) . "(Book Author)"; + } + if ($att->name == "Book Language") { + $language = implode(", ", $att->options); + } + } + } + $insertion_data['wp_api_id'] = $records['id']; + $insertion_data['title'] = $records['name']; + $insertion_data['publication_date'] = $records['date_created']; + $insertion_data['publisher'] = $publisher; + $insertion_data['genre'] = $records['description']; + $insertion_data['language'] = $language; + $insertion_data['description'] = $records['short_description']; + $insertion_data['page_count'] = ''; + $insertion_data['price'] = $records['sale_price'] != '' ? $records['sale_price'] : $records['regular_price']; + $insertion_data['created_by'] = 77; + $insertion_data['isactive'] = 1; + $insertion_data['business_id'] = 1; + $images = $records['images']; + $i = 0; + $lastInsertId = $api_model->insertData('books', $insertion_data); + $insertion_img_data = []; + $message .= "book ID : " . $lastInsertId . " have Imgs" . count($images) . "
"; + // echo "book ID : ".$lastInsertId." have Imgs".count($images)."
"; + if (count($images) > 0) { + foreach ($images as $img) { + $insertion_img_data[$i]['img_name'] = $img->src; + $insertion_img_data[$i]['is_cover'] = 0; + $insertion_img_data[$i]['type'] = 2; + $insertion_img_data[$i]['book_id'] = $lastInsertId; + $insertion_img_data[$i]['created_by'] = 777; + $insertion_img_data[$i]['isactive'] = 1; + $i++; + } // image loop closed + $api_model->insertImagesBatch($insertion_img_data); + $message .= "book ID : " . $lastInsertId . " Img. Batch Inserted Done
"; + // echo "       Img Batch Inserted Done. ".$i. "
"; + } //image count closed + $response = ['status' => 200, 'message' => $message, 'data' => array()]; + } else { + $response = ['status' => 204, 'message' => 'Data Already Exist', 'data' => array()]; + } + } //if cond. closed + } else { + $response = ['status' => 204, 'message' => 'Invaild Method', 'data' => array()]; + } + return $this->respond($response); + } + + public function save_customer_details() + { + + $request = \Config\Services::request(); + $api_model = new ApiIntegrationModel(); + $message = ""; + + if ($request->is('post')) { + $records = (array)$request->getVar(); + $message = "Reponse Count : ".count($records)."
"; + $x=0; + $records = (array)$request->getVar(); + if (count($records) > 0 && !empty($records['id'])) { + $where = ['wp_api_id' => $records['id']]; + $countAll = $api_model->countAll('customers', $where); + if ((int)$countAll == 0) { + $insertion_data['wp_api_id'] = $records['id']; + $insertion_data['first_name'] = $records['first_name']; + $insertion_data['last_name'] = $records['last_name']; + $insertion_data['type'] = $records['role']; + $insertion_data['email'] = $records['email']; + $insertion_data['mode'] = 'online'; + $insertion_data['created_by'] = 777; + $insertion_data['business_id'] = 7; + $billing = $records['billing']; + $shipping = $records['shipping']; + $lastInsertId = $api_model->insertData('customers', $insertion_data); + $insertion_baddress_data = [];$insertion_saddress_data = []; + $i = 0; $j = 0; + + if(isset($billing) && gettype($billing) === 'object') {$billing = [$billing];} + if(isset($shipping) && gettype($shipping) === 'object') {$shipping = [$shipping];} + $message .= "Customer ID : ".$lastInsertId." have Billing address = ".count($billing)." and Shipping address = ".count($shipping)."
"; + if (count($billing) > 0) { + foreach ($billing as $bill) { + if ($bill->first_name !== '') { + $insertion_baddress_data[$i]['first_name'] = $bill->first_name; + $insertion_baddress_data[$i]['last_name'] = $bill->last_name; + $insertion_baddress_data[$i]['company'] = $bill->company; + $insertion_baddress_data[$i]['address_1'] = $bill->address_1; + $insertion_baddress_data[$i]['address_2'] = $bill->address_2; + $insertion_baddress_data[$i]['city'] = $bill->city; + $insertion_baddress_data[$i]['state'] = $bill->state; + $insertion_baddress_data[$i]['country'] = $bill->country; + $insertion_baddress_data[$i]['postal_code'] = $bill->postcode; + $insertion_baddress_data[$i]['customer_id'] = $lastInsertId; + $insertion_baddress_data[$i]['address_type'] = 1; + $insertion_baddress_data[$i]['created_by'] = 777; + $insertion_baddress_data[$i]['email'] = isset($bill->email) ? $bill->email : ''; + $insertion_baddress_data[$i]['mobile_no'] = isset($bill->phone) ? $bill->phone : ''; + $i++; + $message .= "       Billing address has a value and Inserted ".$i."
"; + }else{ + $message .= "       Keys are available but Billing address has no value;
"; + } + } + (!empty($insertion_baddress_data) ? $api_model->insertAddressBatch($insertion_baddress_data) : ""); + } + if (count($shipping) > 0) { + foreach ($shipping as $ship) { + if ($ship->first_name !== '') { + $insertion_saddress_data[$j]['first_name'] = $ship->first_name; + $insertion_saddress_data[$j]['last_name'] = $ship->last_name; + $insertion_saddress_data[$j]['company'] = $ship->company; + $insertion_saddress_data[$j]['address_1'] = $ship->address_1; + $insertion_saddress_data[$j]['address_2'] = $ship->address_2; + $insertion_saddress_data[$j]['city'] = $ship->city; + $insertion_saddress_data[$j]['state'] = $ship->state; + $insertion_saddress_data[$j]['country'] = $ship->country; + $insertion_saddress_data[$j]['postal_code'] = $ship->postcode; + $insertion_saddress_data[$j]['email'] = isset($ship->email)?$ship->email:""; + $insertion_saddress_data[$j]['mobile_no'] = isset($ship->phone)?$ship->phone:""; + $insertion_saddress_data[$j]['customer_id'] = $lastInsertId; + $insertion_saddress_data[$j]['address_type'] = 2; + $insertion_saddress_data[$j]['created_by'] = 777; + $j++; + $message .= "        Shipping address has a value and Inserted ".$j."
"; + } + else{ + $message .= "        Keys are available but Shipping address has no value
"; + } + } + (!empty($insertion_saddress_data) ? $api_model->insertAddressBatch($insertion_saddress_data) : ""); + } + $response = ['status' => 200, 'message' => $message, 'data' => array()]; + } else { + $response = ['status' => 204, 'message' => 'Data Already Exist', 'data' => array()]; + } + } //reponse count if closed + }else{ + $response = ['status' => 204, 'message' => 'Invaild Method', 'data' => array()]; + } + return $this->respond($response); + } +} diff --git a/app/Controllers/Books.php b/app/Controllers/Books.php index 5eaeaa06..f20be53f 100755 --- a/app/Controllers/Books.php +++ b/app/Controllers/Books.php @@ -149,77 +149,4 @@ class Books extends BaseController return redirect()->route('book_list'); } - ## ApiIntegration For Book. - public function apiintegration() { - helper('apiIntegration'); - helper('session'); - $session_role = get_user_role(); - $session_uid = get_logged_user_id(); - $response = perform_http_request('GET', VB_BOOKS); - $message = ""; - if(count($response['response'])>0){ - //$message = "Reponse Count : ".count($response['response'])."
"; - echo "Note : This For CrossCheck Purpose 1ly
"; - echo "Total Book API Reponse Count : " . count($response['response']) . "
"; - $BooksModel = new BooksModel(); - foreach($response['response'] as $row){ - if($row->status == "publish"){ - $publisher = ''; - $language = ''; - $attributes = $row->attributes; - if(count($attributes)>0){ - foreach ($attributes as $att){ - if($att->name == "Publisher"){ $publisher = implode(", ",$att->options); } - if($att->name == "Book Author"){ $publisher .= implode(", ",$att->options)."(Book Author)"; } - if($att->name == "Book Language"){ $language = implode(", ",$att->options); } - } - } - // echo $row->permalink ; - $insertion_data['title'] = $row->name ; - $insertion_data['publication_date'] = $row->date_created; - $insertion_data['publisher'] = $publisher; - $insertion_data['genre'] = $row->description; - $insertion_data['language'] = $language; - $insertion_data['description'] = $row->short_description; - $insertion_data['page_count']=''; - $insertion_data['price'] = $row->sale_price != '' ? $row->sale_price : $row->regular_price; - $insertion_data['created_by'] = $session_uid; - $insertion_data['isactive'] = 1; - $insertion_data['business_id'] = 1; - $images = $row->images; - $i = 0; - $BooksModel->insert($insertion_data); - $lastInsertId = $BooksModel->insertID(); - $insertion_img_data = []; - // $message .= "book ID : ".$lastInsertId." have Imgs".count($images)."
"; - echo "book ID : ".$lastInsertId." have Imgs".count($images)."
"; - if(count($images)>0){ - foreach ($images as $img){ - $insertion_img_data[$i]['img_name'] = $img->src; - $insertion_img_data[$i]['is_cover'] = 0; - $insertion_img_data[$i]['type'] = 2; - $insertion_img_data[$i]['book_id'] = $lastInsertId; - $insertion_img_data[$i]['created_by'] = $session_uid; - $insertion_img_data[$i]['isactive'] = 1; - $i++; - } // image loop closed - $BooksModel->insertImagesBatch($insertion_img_data); - //$message .= "book ID : ".$lastInsertId." Img. Batch Inserted Done
"; - echo "       Img Batch Inserted Done. ".$i. "
"; - }//image count closed - }//if cond. closed - }//reponse foreach closed - }//reponse count if closed - if(!empty($response['error'])){ - echo $response['error']; - echo $response['error_msg']; - $message .= $response['error'].$response['error_msg']; - echo $response['error'].$response['error_msg']; - echo "Error :".$response['error'] . $response['error_msg']; - }else{ - echo "Done"; - } - $go_to_list_page = base_url()."book_list"; - echo "
go to list page
"; - } } diff --git a/app/Controllers/Customer.php b/app/Controllers/Customer.php index c5e0c06e..a2931183 100644 --- a/app/Controllers/Customer.php +++ b/app/Controllers/Customer.php @@ -191,103 +191,4 @@ class Customer extends BaseController $address_details = $model->where($where)->findAll(); return $address_details; } - ## ApiIntegration For Customer. - public function apiintegration() - { - helper('apiIntegration'); - helper('session'); - $session_bid = get_business_id(); - $session_uid = get_logged_user_id(); - $response = perform_http_request('GET', VB_CUSTOMERS); - $message = ""; - if (count($response['response']) > 0) { - //$message = "Reponse Count : ".count($response['response'])."
"; - echo "Note : This For CrossCheck Purpose 1ly
"; - echo "Total Customer API Reponse Count : " . count($response['response']) . "
"; - $CustomerModel = new CustomerModel(); - $x=0; - foreach ($response['response'] as $row) { - $insertion_data['first_name'] = $row->first_name; - $insertion_data['last_name'] = $row->last_name; - $insertion_data['type'] = $row->role; - $insertion_data['email'] = $row->email; - // $insertion_data['profile_picture'] = $row->avatar_url; - $insertion_data['mode'] = 'online'; - $insertion_data['created_by'] = $session_uid; - $insertion_data['business_id'] = $session_bid; - $billing = $row->billing; - $shipping = $row->shipping; - $CustomerModel->insert($insertion_data); - $lastInsertId = $CustomerModel->insertID(); - $insertion_baddress_data = [];$insertion_saddress_data = []; - $i = 0; $j = 0; - - if(isset($billing) && gettype($billing) === 'object') {$billing = [$billing];} - if(isset($shipping) && gettype($shipping) === 'object') {$shipping = [$shipping];} - echo "Customer ID : ".$lastInsertId." have Billing address = ".count($billing)." and Shipping address = ".count($shipping)."
"; - if (count($billing) > 0) { - foreach ($billing as $bill) { - if ($bill->first_name !== '') { - $insertion_baddress_data[$i]['first_name'] = $bill->first_name; - $insertion_baddress_data[$i]['last_name'] = $bill->last_name; - $insertion_baddress_data[$i]['company'] = $bill->company; - $insertion_baddress_data[$i]['address_1'] = $bill->address_1; - $insertion_baddress_data[$i]['address_2'] = $bill->address_2; - $insertion_baddress_data[$i]['city'] = $bill->city; - $insertion_baddress_data[$i]['state'] = $bill->state; - $insertion_baddress_data[$i]['country'] = $bill->country; - $insertion_baddress_data[$i]['postal_code'] = $bill->postcode; - $insertion_baddress_data[$i]['customer_id'] = $lastInsertId; - $insertion_baddress_data[$i]['address_type'] = 1; - $insertion_baddress_data[$i]['created_by'] = $session_uid; - $insertion_baddress_data[$i]['email'] = isset($bill->email) ? $bill->email : ''; - $insertion_baddress_data[$i]['mobile_no'] = isset($bill->phone) ? $bill->phone : ''; - $i++; - echo "       Billing address has a value and Inserted ".$i."
"; - }else{ - echo "       Keys are available but Billing address has no value;
"; - } - } - (!empty($insertion_baddress_data) ? $CustomerModel->insertAddressBatch($insertion_baddress_data) : ""); - } - if (count($shipping) > 0) { - foreach ($shipping as $ship) { - if ($ship->first_name !== '') { - $insertion_saddress_data[$j]['first_name'] = $ship->first_name; - $insertion_saddress_data[$j]['last_name'] = $ship->last_name; - $insertion_saddress_data[$j]['company'] = $ship->company; - $insertion_saddress_data[$j]['address_1'] = $ship->address_1; - $insertion_saddress_data[$j]['address_2'] = $ship->address_2; - $insertion_saddress_data[$j]['city'] = $ship->city; - $insertion_saddress_data[$j]['state'] = $ship->state; - $insertion_saddress_data[$j]['country'] = $ship->country; - $insertion_saddress_data[$j]['postal_code'] = $ship->postcode; - $insertion_saddress_data[$j]['email'] = isset($ship->email)?$ship->email:""; - $insertion_saddress_data[$j]['mobile_no'] = isset($ship->phone)?$ship->phone:""; - $insertion_saddress_data[$j]['customer_id'] = $lastInsertId; - $insertion_saddress_data[$j]['address_type'] = 2; - $insertion_saddress_data[$j]['created_by'] = $session_uid; - $j++; - echo "        Shipping address has a value and Inserted ".$j."
"; - } - else{ - echo "        Keys are available but Shipping address has no value
"; - } - } - (!empty($insertion_saddress_data) ? $CustomerModel->insertAddressBatch($insertion_saddress_data) : ""); - } - } //reponse foreach closed - } //reponse count if closed - if (!empty($response['error'])) { - echo $response['error']; - echo $response['error_msg']; - $message .= $response['error'] . $response['error_msg']; - echo "Error :".$response['error'] . $response['error_msg']; - }else{ - echo "Done"; - } - $go_to_list_page = base_url()."customer_list"; - echo "
go to list page
"; - // return $message; - } } diff --git a/app/Models/ApiIntegrationModel.php b/app/Models/ApiIntegrationModel.php new file mode 100644 index 00000000..4f75e024 --- /dev/null +++ b/app/Models/ApiIntegrationModel.php @@ -0,0 +1,74 @@ +table = $tableName; + return $this; + } + + public function insertData($table, $data) + { + $this->db->table($table)->insert($data); + $last_insert_id = $this->db->insertID(); + return $last_insert_id; + + } + + public function updateData($table, $data, $where) + { + $this->db->table($table)->update($data, $where); + $affected_rows = $this->db->affectedRows(); + return $affected_rows; + } + + public function countAll($table, $where = null) + { + $query = $this->db->table($table); + if ($where) { + $query->where($where); + } + return $query->countAllResults(); + } + + + public function getData($table, $where = null) + { + $query = $this->db->table($table); + if ($where) { + $query->where($where); + } + return $query->get()->getResult(); + } + + public function insertImagesBatch($imgDataArray) { + $this->db->table('book_images')->insertBatch($imgDataArray); + } + + public function saveAddressDetails($data){ + $i = 0; + $statement = []; + foreach ($data as $row) { + $id = $row['customer_address_id']; + if($id != ''){ + unset($row['customer_address_id']); // Remove the id from the data to avoid updating it + unset($row['created_by']); // bcoz here data Updating here. + $this->db->table('customer_addresses')->where('customer_address_id', $id)->update($row);// Update the row with the specified id + $affectedRows = $this->db->affectedRows(); + $statement[$i] = "address - ".$id." ".$affectedRows ? " Updated":" Not Updated"; + }else{ + + $this->db->table('customer_addresses')->insert($row); + $insertID = $this->db->insertID(); + $statement[$i] = "address - ".$insertID." Inserted"; + } + } + return $statement; + } +} diff --git a/app/Views/book_list.php b/app/Views/book_list.php index 8b9276db..10323748 100644 --- a/app/Views/book_list.php +++ b/app/Views/book_list.php @@ -4,7 +4,6 @@

diff --git a/app/Views/customer_list.php b/app/Views/customer_list.php index 70a37e4b..f3c14b6a 100644 --- a/app/Views/customer_list.php +++ b/app/Views/customer_list.php @@ -4,7 +4,6 @@

diff --git a/index.php b/index.php index 8688206c..cb4ec830 100755 --- a/index.php +++ b/index.php @@ -52,6 +52,10 @@ require_once SYSTEMPATH . 'Config/DotEnv.php'; * the application run, and does all the dirty work to get * the pieces all working together. */ +header('Access-Control-Allow-Origin: *'); +header("Access-Control-Allow-Headers: *"); +header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE"); +$method = $_SERVER['REQUEST_METHOD']; $app = Config\Services::codeigniter(); $app->initialize();