forked from metallurgical/codeigniter-restserver-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.php
307 lines (243 loc) · 11.3 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
/**
* PHP version 5
* Codeigniter RestServer
* Used with phil sturgeon library = codeigniter-resetserver
*
* @category models
* @package api.php
* @author aa6my <[email protected]> @ aa6my
* @author Norlihazmey <[email protected]> @metallurgical
* @license https://ellislab.com/codeigniter/user-guide/license.html
* @copyright 2015
*/
class Api extends REST_Controller
{
public $url;
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->url = current_url();
/**
* Set header for cross origin request(CORS)
* Allow (POST, GET, OPTIONS, PUT, DELETE) method for operations, by default is not available
* Allow authorization basic authentication
*/
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Method: POST, GET, OPTIONS, PUT, DELETE');
header("Access-Control-Allow-Headers: X-Custom-Header, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
}
/************** THIS FUNCTION WILL CHANGE DEPEND ON COMPLEXITY OF CLIENT SIDE REQUESTED ****************************
* @param [type] [parameter]
* [value][table name]
* eg : crm/type/invoices
*
* @param [key] [parameter]
* [value][table primary key or unique value]
* if use this must be include also the val paremeter
* [val][parameter]
* [value][value for the primary key]
* eg : crm/key/invoice_id/val/42
*
* example Full url : crm/type/invoice/key/invoice_id/val/12
*
* ----------------join table(optional-if dont want to use, no need to include join set paramter)---------
* @param [joinid] [parameter]
* [value][id for the table that need to join]
* eg : joinid/invoice_id @@ if want to add more than one join, separeted by '-' respectively
* if use this must be include also the jointo paremeter and type parameter(mainly for first table to select)
* [jointo][parameter]
* [value][name of the table that need to join]
* ex : jointo/invoice_payments
* eg : if more than one - crm/type/invoices/joinid/invoice_id-customer_id/jointo/invoice_payments-customers
*
**************************************************************************************************/
function dataAll_get()
{
/************** THIS FUNCTION WILL CHANGE DEPEND ON COMPLEXITY OF CLIENT SIDE REQUESTED ****************************
*
*
*/
if(($this->get('val') && !$this->get('key')) || ($this->get('key') && !$this->get('val')))
{
$this->response(array('error' => 'The key parameter and value parameter must have'), 400);
}
if(($this->get('joinid') && !$this->get('jointo')) || ($this->get('jointo') && !$this->get('joinid')))
{
$this->response(array('error' => 'The joinid parameter and jointo parameter must have'), 400);
}
$type = $this->get('type'); // get type of table need to fetch data eg:|customers(user/type/customers)|
$key = $this->get('key'); // UNIQUE ID in table to fetch from eg : |customers(user/type/customers/fetch/all@specified/key/customer_id)
$table = $type; // asign type into table variable
$join_id = $this->get('joinid');
$join_id = explode('-', $join_id);
$join_to = $this->get('jointo');
$join_to = explode('-', $join_to);
if (false !== strpos($this->url,'joinid')) //if joinid name in url variable exist
{
$value = $this->get('val');
if($value!="none") // return join table with condition applied
{
$where = array($table.".".$key => $value);
$data[$table] = $this->api_model->get_data_join($table,$where, $join_to, $join_id);
}
}
else
{
/**
* if specified need and value for $key to fetch from eg : |customers(user/type/customers/val/2/key) == if no unique id just put none as a value
* @var [type]
*/
$value = $this->get('val');
$where = &$same_where;
$where = array($key => $value);
if (false !== strpos($this->url,'val')) //if have val string in url - must be include the key parameter also
{
$data[$table] = $this->api_model->get_all_rows($table,$same_where);
}
else
{
$data[$table] = $this->api_model->get_all_rows($table);
}
}
/*========================================== RESULTS RESPOND =========================================
*
* if got the data in query, return respond from the server to the client using exact format
*/
if($data)
{
$this->response($data, 200); // 200 being the HTTP response code
}
else
{
$this->response(array('error' => 'User could not be found'), 404);
}
/*========================================= END RESULT ================================================*/
}
public function dataAll_options(){
/**
* OPTIONS requested set header and status to OK
* This method majority applied for DELETE and PUT operations
*/
header( "HTTP/1.1 200 OK" );
exit();
}
public function dataAll_post()
{
/************** THIS FUNCTION WILL CHANGE DEPEND ON COMPLEXITY OF CLIENT SIDE REQUESTED ****************************
*
* Multiple or single table inserted
* Usage(client side) :
* For the table : (multiple table inserted) just seperated with '-' symbol
* (single table inserted) just write down one name only
* $formData = array(
array('name'=>'emi'), -----> data for table customers
array('subject_name'=>'akhlak') -----> data for table subjects
);
$post = array('type'=>'customers-subjects', 'formData'=>$formData);
* p/s : Data and table name must be write down in sequence order
*
* Will changes time to time in order to create function for dynamic function
* PEACE
*/
// get table value from client side
// get data value from client side
// set variable loop as a boolean to false for initial start
// set variable loop as a boolean true value if multiple table detected
$type = $this->post('type');
$arrayData = $this->post('formData');
$loop = false;
// only for multiple table delete, if have this set to true
if (false !== strpos($type,'-')){ // if '-' existed
$loop = true; // set the loop value to TRUE
$table = explode('-', $type); // and then explode those '-' into array value
}
// if '-' symbol detected
// count the array size
// make insert function inside loop
// loop iteration depend on arraysize
if($loop == true){
$bil = count($table);
for($i = 0; $i < $bil; $i++){
$doAdd = $this->api_model->insert_new_data($arrayData[$i],$table[$i]);
}
$this->response(array('Respone'=> 'Multiple table Insert into table'), 200);
}
// if no '-' detected
// single table inserted function will triggered
// use the array index 0
else{
$table = $type;
$doAdd = $this->api_model->insert_new_data($arrayData[0],$table);
$this->response(array('Respone'=> 'Single table Insert into table'), 200);
}
}
public function dataAll_delete()
{
/************** THIS FUNCTION WILL CHANGE DEPEND ON COMPLEXITY OF CLIENT SIDE REQUESTED ****************************
* This function can accept multiple and single table to delete
* How this can happen?
* If want to make multiple delete operation, separate it by '-' :
* example : type/customer-vendors/key/customer_id-vendor_id/12-30
* Just separate by '-' symbols to delete multiple table
* Single table example :
* example : type/customers/key/customer_id/val/12
*
* Multiple table delete notes :
* - The 'type','key' and 'val' values must be in order form respectively
* - The number of values also must be the same
* - If the number of value in 'type' have 3 value, so in 'key' and 'val' also need 3 value otherwise, error will returned
*/
$loop = false; // only for multiple table delete, if have this set to true
$type = $this->get('type'); // get type value in url
if (false !== strpos($type,'-')){ // if '-' existed
$loop = true; // set the loop value to TRUE
$type = explode('-', $type); // and then explode those '-' into array value
}
$key = $this->get('key'); // same with type but no need to set loop to true because already set in 'type'
if (false !== strpos($key,'-')){
$key = explode('-', $key);
}
$val = $this->get('val'); // same with type but no need to set loop to true because already set in 'type'
if (false !== strpos($val,'-')){
$val = explode('-', $val);
}
// multiple tables delete
if($loop == true){
$bil = count($type);
for($i = 0; $i < $bil; $i++){
$table = $type[$i];
$where = array($key[$i] => $val[$i]);
$doDelete = $this->api_model->delete_data($table, $where);
}
$this->response(array('Multiple tables Delete Success'), 200);
}
// single table delete
else{
$table = $type;
$where = array($key => $val);
$doDelete = $this->api_model->delete_data($table, $where);
$this->response(array('Single table Delete Success'), 200);
}
}
public function dataAll_put(){
/************** THIS FUNCTION WILL CHANGE DEPEND ON COMPLEXITY OF CLIENT SIDE REQUESTED ****************************
*
*/
$tableToUpdate = $this->put('type');
$pk = $this->put('primaryKey');
$pkVal = $this->put('primaryKeyVal');
$columnToUpdate = $this->put('formData');
$usingCondition = array($pk => $pkVal);
$kk = $this->api_model->update_data($columnToUpdate, $tableToUpdate, $usingCondition);
$this->response(array('Requestsuccess'), 200);
}
public function send_post()
{
var_dump($this->request->body);
}
}
/* End of file api.php */
/* Location: ./application/controllers/api.php */