Api New controller : ps

This commit is contained in:
VE10-Sanjeev 2023-09-05 19:07:48 +05:30
parent dbcaf13478
commit 88baff883b
9 changed files with 265 additions and 179 deletions

View File

@ -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');

View File

@ -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");
});
/*
* --------------------------------------------------------------------

View File

@ -0,0 +1,180 @@
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
use App\Models\ApiIntegrationModel;
class ApiIntegration extends ResourceController
{
use ResponseTrait;
public function save_book_details()
{
$request = \Config\Services::request();
$api_model = new ApiIntegrationModel();
$message = "";
if ($request->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) . " <br/>";
// echo "book ID : ".$lastInsertId." have Imgs".count($images)." <br/>";
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 <br/>";
// echo "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Img Batch Inserted Done. ".$i. " <br/>";
} //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)." <br/>";
$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)." <br/>";
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 .= "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Billing address has a value and Inserted ".$i." <br/>";
}else{
$message .= "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Keys are available but Billing address has no value; <br/>";
}
}
(!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 .= "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Shipping address has a value and Inserted ".$j." <br/>";
}
else{
$message .= "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Keys are available but Shipping address has no value <br/>";
}
}
(!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);
}
}

View File

@ -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'])." <br/>";
echo "Note : This For CrossCheck Purpose 1ly <br/>";
echo "Total Book API Reponse Count : " . count($response['response']) . " <br/>";
$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)." <br/>";
echo "book ID : ".$lastInsertId." have Imgs".count($images)." <br/>";
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 <br/>";
echo "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Img Batch Inserted Done. ".$i. " <br/>";
}//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 "<center><a href=".$go_to_list_page.">go to list page</a></center>";
}
}

View File

@ -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'])." <br/>";
echo "Note : This For CrossCheck Purpose 1ly <br/>";
echo "Total Customer API Reponse Count : " . count($response['response']) . " <br/>";
$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)." <br/>";
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 "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Billing address has a value and Inserted ".$i." <br/>";
}else{
echo "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Keys are available but Billing address has no value; <br/>";
}
}
(!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 "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Shipping address has a value and Inserted ".$j." <br/>";
}
else{
echo "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Keys are available but Shipping address has no value <br/>";
}
}
(!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 "<center><a href=".$go_to_list_page.">go to list page</a></center>";
// return $message;
}
}

View File

@ -0,0 +1,74 @@
<?php
namespace App\Models;
use CodeIgniter\Model;
class ApiIntegrationModel extends Model
{
protected $table;
public function setTable($tableName)
{
$this->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;
}
}

View File

@ -4,7 +4,6 @@
<div class="card-body">
<div class="float-right">
<a href="<?= base_url() . "book_page/0"; ?>" class="btn btn-primary waves-effect"> <span> <i class="mdi mdi-book-plus"></i></span> Add New </a>
<a href="<?= base_url()."book_api_integration"; ?>" class="btn btn-success waves-effect"> <span> <i class="mdi mdi-gesture-swipe-down"></i></span> Api integration </a>
</div>
<br>
<h4 class="header-title mb-3"><?= $page_name; ?></h4>

View File

@ -4,7 +4,6 @@
<div class="card-body">
<div class="float-right">
<a href="new_customer/0" class="btn btn-primary"><i class="ri-map-pin-user-fill"></i> Add New </a>
<a href="<?= base_url()."customer_api_integration"; ?>" class="btn btn-success waves-effect"> <span> <i class="mdi mdi-gesture-swipe-down"></i></span> Api integration </a>
</div><!-- end col-->
<br>
<h4 class="header-title mb-3"><?= $page_name; ?></h4>

View File

@ -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();