Files
2025-10-29 11:09:43 +01:00

421 lines
19 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
/** @noinspection PhpIncludeInspection */
require __DIR__ . '/REST_Controller.php';
/**
* This is an example of a few basic user interaction methods you could use
* all done with a hardcoded array
*
* @package CodeIgniter
* @subpackage Rest Server
* @category Controller
* @author Phil Sturgeon, Chris Kacerguis
* @license MIT
* @link https://github.com/chriskacerguis/codeigniter-restserver
*/
class Customers extends REST_Controller {
function __construct() {
// Construct the parent class
parent::__construct();
}
/**
* @api {get} api/customers/:id Request customer information
* @apiName GetCustomer
* @apiGroup Customers
*
* @apiHeader {String} authtoken Authentication token, generated from admin area
*
* @apiParam {Number} id customer unique ID.
*
* @apiSuccess {Object} customer information.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "id": "28",
* "name": "Test1",
* "description": null,
* "status": "1",
* "clientid": "11",
* "billing_type": "3",
* "start_date": "2019-04-19",
* "deadline": "2019-08-30",
* "customer_created": "2019-07-16",
* "date_finished": null,
* "progress": "0",
* "progress_from_tasks": "1",
* "customer_cost": "0.00",
* "customer_rate_per_hour": "0.00",
* "estimated_hours": "0.00",
* "addedfrom": "5",
* "rel_type": "customer",
* "potential_revenue": "0.00",
* "potential_margin": "0.00",
* "external": "E",
* ...
* }
*
* @apiError {Boolean} status Request status.
* @apiError {String} message No data were found.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "status": false,
* "message": "No data were found"
* }
*/
public function data_get($id = '') {
// If the id parameter doesn't exist return all the
$data = $this->Api_model->get_table('clients', $id);
// Check if the data store contains
if ($data) {
$data = $this->Api_model->get_api_custom_data($data, "customers", $id);
// Set the response and exit
$this->response($data, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
} else {
// Set the response and exit
$this->response(['status' => FALSE, 'message' => 'No data were found'], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
/**
* @api {get} api/customers/search/:keysearch Search Customer Information
* @apiName GetCustomerSearch
* @apiGroup Customers
*
* @apiHeader {String} authtoken Authentication token, generated from admin area
*
* @apiParam {String} keysearch Search Keywords.
*
* @apiSuccess {Object} customer information.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "id": "28",
* "name": "Test1",
* "description": null,
* "status": "1",
* "clientid": "11",
* "billing_type": "3",
* "start_date": "2019-04-19",
* "deadline": "2019-08-30",
* "customer_created": "2019-07-16",
* "date_finished": null,
* "progress": "0",
* "progress_from_tasks": "1",
* "customer_cost": "0.00",
* "customer_rate_per_hour": "0.00",
* "estimated_hours": "0.00",
* "addedfrom": "5",
* "rel_type": "customer",
* "potential_revenue": "0.00",
* "potential_margin": "0.00",
* "external": "E",
* ...
* }
*
* @apiError {Boolean} status Request status.
* @apiError {String} message No data were found.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "status": false,
* "message": "No data were found"
* }
*/
public function data_search_get($key = '') {
// If the id parameter doesn't exist return all the
$data = $this->Api_model->search('customer', $key);
// Check if the data store contains
if ($data) {
$data = $this->Api_model->get_api_custom_data($data, "customers");
// Set the response and exit
$this->response($data, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
} else {
// Set the response and exit
$this->response(['status' => FALSE, 'message' => 'No data were found'], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
/**
* @api {post} api/customers Add New Customer
* @apiName PostCustomer
* @apiGroup Customers
*
* @apiHeader {String} authtoken Authentication token, generated from admin area
*
* @apiParam {String} company Mandatory Customer company.
* @apiParam {String} [vat] Optional Vat.
* @apiParam {String} [phonenumber] Optional Customer Phone.
* @apiParam {String} [website] Optional Customer Website.
* @apiParam {Number[]} [groups_in] Optional Customer groups.
* @apiParam {String} [default_language] Optional Customer Default Language.
* @apiParam {String} [default_currency] Optional default currency.
* @apiParam {String} [address] Optional Customer address.
* @apiParam {String} [city] Optional Customer City.
* @apiParam {String} [state] Optional Customer state.
* @apiParam {String} [zip] Optional Zip Code.
* @apiParam {String} [partnership_type] Optional Customer partnership type.
* @apiParam {String} [country] Optional country.
* @apiParam {String} [billing_street] Optional Billing Address: Street.
* @apiParam {String} [billing_city] Optional Billing Address: City.
* @apiParam {Number} [billing_state] Optional Billing Address: State.
* @apiParam {String} [billing_zip] Optional Billing Address: Zip.
* @apiParam {String} [billing_country] Optional Billing Address: Country.
* @apiParam {String} [shipping_street] Optional Shipping Address: Street.
* @apiParam {String} [shipping_city] Optional Shipping Address: City.
* @apiParam {String} [shipping_state] Optional Shipping Address: State.
* @apiParam {String} [shipping_zip] Optional Shipping Address: Zip.
* @apiParam {String} [shipping_country] Optional Shipping Address: Country.
*
* @apiParamExample {Multipart Form} Request-Example:
* array (size=22)
* 'company' => string 'Themesic Interactive' (length=38)
* 'vat' => string '123456789' (length=9)
* 'phonenumber' => string '123456789' (length=9)
* 'website' => string 'AAA.com' (length=7)
* 'groups_in' =>
* array (size=2)
* 0 => string '1' (length=1)
* 1 => string '4' (length=1)
* 'default_currency' => string '3' (length=1)
* 'default_language' => string 'english' (length=7)
* 'address' => string '1a The Alexander Suite Silk Point' (length=27)
* 'city' => string 'London' (length=14)
* 'state' => string 'London' (length=14)
* 'zip' => string '700000' (length=6)
* 'country' => string '243' (length=3)
* 'billing_street' => string '1a The Alexander Suite Silk Point' (length=27)
* 'billing_city' => string 'London' (length=14)
* 'billing_state' => string 'London' (length=14)
* 'billing_zip' => string '700000' (length=6)
* 'billing_country' => string '243' (length=3)
* 'shipping_street' => string '1a The Alexander Suite Silk Point' (length=27)
* 'shipping_city' => string 'London' (length=14)
* 'shipping_state' => string 'London' (length=14)
* 'shipping_zip' => string '700000' (length=6)
* 'shipping_country' => string '243' (length=3)
*
*
* @apiSuccess {Boolean} status Request status.
* @apiSuccess {String} message Customer add successful.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "status": true,
* "message": "Customer add successful."
* }
*
* @apiError {Boolean} status Request status.
* @apiError {String} message Customer add fail.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "status": false,
* "message": "Customer add fail."
* }
*
*/
public function data_post() {
\modules\api\core\Apiinit::the_da_vinci_code('api');
// form validation
$this->form_validation->set_rules('company', 'Company', 'trim|required|max_length[600]', array('is_unique' => 'This %s already exists please enter another Company'));
if ($this->form_validation->run() == FALSE) {
// form validation error
$message = array('status' => FALSE, 'error' => $this->form_validation->error_array(), 'message' => validation_errors());
$this->response($message, REST_Controller::HTTP_NOT_FOUND);
} else {
$groups_in = $this->Api_model->value($this->input->post('groups_in', TRUE));
$insert_data = ['company' => $this->input->post('company', TRUE), 'vat' => $this->Api_model->value($this->input->post('vat', TRUE)), 'phonenumber' => $this->Api_model->value($this->input->post('phonenumber', TRUE)), 'website' => $this->Api_model->value($this->input->post('website', TRUE)), 'default_currency' => $this->Api_model->value($this->input->post('default_currency', TRUE)), 'default_language' => $this->Api_model->value($this->input->post('default_language', TRUE)), 'address' => $this->Api_model->value($this->input->post('address', TRUE)), 'city' => $this->Api_model->value($this->input->post('city', TRUE)), 'state' => $this->Api_model->value($this->input->post('state', TRUE)), 'zip' => $this->Api_model->value($this->input->post('zip', TRUE)), 'country' => $this->Api_model->value($this->input->post('country', TRUE)), 'billing_street' => $this->Api_model->value($this->input->post('billing_street', TRUE)), 'billing_city' => $this->Api_model->value($this->input->post('billing_city', TRUE)), 'billing_state' => $this->Api_model->value($this->input->post('billing_state', TRUE)), 'billing_zip' => $this->Api_model->value($this->input->post('billing_zip', TRUE)), 'billing_country' => $this->Api_model->value($this->input->post('billing_country', TRUE)), 'shipping_street' => $this->Api_model->value($this->input->post('shipping_street', TRUE)), 'shipping_city' => $this->Api_model->value($this->input->post('shipping_city', TRUE)), 'shipping_state' => $this->Api_model->value($this->input->post('shipping_state', TRUE)), 'shipping_zip' => $this->Api_model->value($this->input->post('shipping_zip', TRUE)), 'shipping_country' => $this->Api_model->value($this->input->post('shipping_country', TRUE)) ];
if (!empty($this->input->post('custom_fields', TRUE))) {
$insert_data['custom_fields'] = $this->Api_model->value($this->input->post('custom_fields', TRUE));
}
if ($groups_in != '') {
$insert_data['groups_in'] = $groups_in;
}
// insert data
$this->load->model('clients_model');
$output = $this->clients_model->add($insert_data);
if ($output > 0 && !empty($output)) {
// success
$message = array(
'status' => TRUE,
'message' => 'Client add successful.',
'record_id' => $output // Εδώ επιστρέφουμε το ID της νέας εγγραφής
);
$this->response($message, REST_Controller::HTTP_OK);
} else {
// error
$message = array('status' => FALSE, 'message' => 'Client add fail.');
$this->response($message, REST_Controller::HTTP_NOT_FOUND);
}
}
}
/**
* @api {delete} api/delete/customers/:id Delete a Customer
* @apiName DeleteCustomer
* @apiGroup Customers
*
* @apiHeader {String} authtoken Authentication token, generated from admin area
*
* @apiParam {Number} id Customer unique ID.
*
* @apiSuccess {String} status Request status.
* @apiSuccess {String} message Customer Delete Successful.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "status": true,
* "message": "Customer Delete Successful."
* }
*
* @apiError {Boolean} status Request status.
* @apiError {String} message Customer Delete Fail.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "status": false,
* "message": "Customer Delete Fail."
* }
*/
public function data_delete($id = '') {
$id = $this->security->xss_clean($id);
if (empty($id) && !is_numeric($id)) {
$message = array('status' => FALSE, 'message' => 'Invalid Customer ID');
$this->response($message, REST_Controller::HTTP_NOT_FOUND);
} else {
// delete data
$this->load->model('clients_model');
$output = $this->clients_model->delete($id);
if ($output === TRUE) {
// success
$message = array('status' => TRUE, 'message' => 'Customer Delete Successful.');
$this->response($message, REST_Controller::HTTP_OK);
} else {
// error
$message = array('status' => FALSE, 'message' => 'Customer Delete Fail.');
$this->response($message, REST_Controller::HTTP_NOT_FOUND);
}
}
}
/**
* @api {put} api/customers/:id Update a Customer
* @apiName PutCustomer
* @apiGroup Customers
*
* @apiHeader {String} authtoken Authentication token, generated from admin area
*
* @apiParam {String} company Mandatory Customer company.
* @apiParam {String} [vat] Optional Vat.
* @apiParam {String} [phonenumber] Optional Customer Phone.
* @apiParam {String} [website] Optional Customer Website.
* @apiParam {Number[]} [groups_in] Optional Customer groups.
* @apiParam {String} [default_language] Optional Customer Default Language.
* @apiParam {String} [default_currency] Optional default currency.
* @apiParam {String} [address] Optional Customer address.
* @apiParam {String} [city] Optional Customer City.
* @apiParam {String} [state] Optional Customer state.
* @apiParam {String} [zip] Optional Zip Code.
* @apiParam {String} [country] Optional country.
* @apiParam {String} [billing_street] Optional Billing Address: Street.
* @apiParam {String} [billing_city] Optional Billing Address: City.
* @apiParam {Number} [billing_state] Optional Billing Address: State.
* @apiParam {String} [billing_zip] Optional Billing Address: Zip.
* @apiParam {String} [billing_country] Optional Billing Address: Country.
* @apiParam {String} [shipping_street] Optional Shipping Address: Street.
* @apiParam {String} [shipping_city] Optional Shipping Address: City.
* @apiParam {String} [shipping_state] Optional Shipping Address: State.
* @apiParam {String} [shipping_zip] Optional Shipping Address: Zip.
* @apiParam {String} [shipping_country] Optional Shipping Address: Country.
*
* @apiParamExample {json} Request-Example:
* {
* "company": "Công ty A",
* "vat": "",
* "phonenumber": "0123456789",
* "website": "",
* "default_language": "",
* "default_currency": "0",
* "country": "243",
* "city": "TP London",
* "zip": "700000",
* "state": "Quận 12",
* "address": "hẻm 71, số 34\/3 Đường TA 16, Phường Thới An, Quận 12",
* "billing_street": "hẻm 71, số 34\/3 Đường TA 16, Phường Thới An, Quận 12",
* "billing_city": "TP London",
* "billing_state": "Quận 12",
* "billing_zip": "700000",
* "billing_country": "243",
* "shipping_street": "",
* "shipping_city": "",
* "shipping_state": "",
* "shipping_zip": "",
* "shipping_country": "0"
* }
*
* @apiSuccess {Boolean} status Request status.
* @apiSuccess {String} message Customer Update Successful.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "status": true,
* "message": "Customer Update Successful."
* }
*
* @apiError {Boolean} status Request status.
* @apiError {String} message Customer Update Fail.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "status": false,
* "message": "Customer Update Fail."
* }
*/
public function data_put($id = '') {
// JSON data is now automatically parsed in REST_Controller
if (empty($_POST) || !isset($_POST)) {
$message = array('status' => FALSE, 'message' => 'Data Not Acceptable OR Not Provided');
$this->response($message, REST_Controller::HTTP_NOT_ACCEPTABLE);
}
if (empty($id) && !is_numeric($id)) {
$message = array('status' => FALSE, 'message' => 'Invalid Customers ID');
$this->response($message, REST_Controller::HTTP_NOT_FOUND);
} else {
$update_data = $this->input->post();
// update data
$this->load->model('clients_model');
$output = $this->clients_model->update($update_data, $id);
if ($output > 0 && !empty($output)) {
// success
$message = array('status' => TRUE, 'message' => 'Customers Update Successful.');
$this->response($message, REST_Controller::HTTP_OK);
} else {
// error
$message = array('status' => FALSE, 'message' => 'Customers Update Fail.');
$this->response($message, REST_Controller::HTTP_NOT_FOUND);
}
}
}
}