PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.2.6
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.2.6
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / api / class-contacts-controller.php

class-contacts-controller.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.2.6, at includes/api/class-contacts-controller.php

569 lines 20.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WeDevs\ERP\API;
3
4 use WP_REST_Server;
5 use WP_REST_Response;
6 use WP_Error;
7
8 class Contacts_Controller extends REST_Controller {
9 /**
10 * Endpoint namespace.
11 *
12 * @var string
13 */
14 protected $namespace = 'erp/v1';
15
16 /**
17 * Route base.
18 *
19 * @var string
20 */
21 protected $rest_base = 'crm/contacts';
22
23 /**
24 * Register the routes for the objects of the controller.
25 */
26 public function register_routes() {
27 register_rest_route( $this->namespace, '/' . $this->rest_base, [
28 [
29 'methods' => WP_REST_Server::READABLE,
30 'callback' => [ $this, 'get_contacts' ],
31 'args' => $this->get_collection_params(),
32 'permission_callback' => function ( $request ) {
33 return current_user_can( 'erp_crm_list_contact' );
34 },
35 ],
36 [
37 'methods' => WP_REST_Server::CREATABLE,
38 'callback' => [ $this, 'create_contact' ],
39 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
40 'permission_callback' => function ( $request ) {
41 return current_user_can( 'erp_crm_add_contact' );
42 },
43 ],
44 'schema' => [ $this, 'get_public_item_schema' ],
45 ] );
46
47 register_rest_route( $this->namespace, '/' . $this->rest_base . '/bulk', [
48 [
49 'methods' => WP_REST_Server::CREATABLE,
50 'callback' => [ $this, 'create_contacts' ],
51 'permission_callback' => function ( $request ) {
52 return current_user_can( 'erp_crm_add_contact' );
53 },
54 ],
55 'schema' => [ $this, 'get_public_item_schema' ],
56 ] );
57
58 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [
59 [
60 'methods' => WP_REST_Server::READABLE,
61 'callback' => [ $this, 'get_contact' ],
62 'args' => [
63 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
64 ],
65 'permission_callback' => function ( $request ) {
66 return current_user_can( 'erp_crm_list_contact' );
67 },
68 ],
69 [
70 'methods' => WP_REST_Server::EDITABLE,
71 'callback' => [ $this, 'update_contact' ],
72 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
73 'permission_callback' => function ( $request ) {
74 return current_user_can( 'erp_crm_edit_contact', $request['id'] );
75 },
76 ],
77 [
78 'methods' => WP_REST_Server::DELETABLE,
79 'callback' => [ $this, 'delete_contact' ],
80 'permission_callback' => function ( $request ) {
81 return current_user_can( 'erp_crm_delete_contact', $request['id'] );
82 },
83 ],
84 'schema' => [ $this, 'get_public_item_schema' ],
85 ] );
86 }
87
88 /**
89 * Get a collection of contacts
90 *
91 * @param WP_REST_Request $request
92 *
93 * @return WP_Error|WP_REST_Response
94 */
95 public function get_contacts( $request ) {
96 $args = [
97 'number' => $request['per_page'],
98 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
99 ];
100
101 $items = erp_get_peoples( $args );
102 $total_items = erp_get_peoples( [ 'count' => true ] );
103
104 $formated_items = [];
105 foreach ( $items as $item ) {
106 $additional_fields = [];
107
108 if ( isset( $request['include'] ) ) {
109 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
110
111 if ( in_array( 'owner', $include_params ) ) {
112 $contact_owner_id = ( $item->user_id ) ? get_user_meta( $item->user_id, 'contact_owner', true ) : erp_people_get_meta( $item->id, 'contact_owner', true );
113
114 $item->owner = $this->get_user( $contact_owner_id );
115 $additional_fields = ['owner' => $item->owner];
116 }
117 }
118
119 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
120 $formated_items[] = $this->prepare_response_for_collection( $data );
121 }
122
123 $response = rest_ensure_response( $formated_items );
124 $response = $this->format_collection_response( $response, $request, $total_items );
125
126 return $response;
127 }
128
129 /**
130 * Get a specific contact
131 *
132 * @param WP_REST_Request $request
133 *
134 * @return WP_Error|WP_REST_Response
135 */
136 public function get_contact( $request ) {
137 $id = (int) $request['id'];
138 $item = erp_get_people( $id );
139
140 if ( empty( $id ) || empty( $item->id ) ) {
141 return new WP_Error( 'rest_contact_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
142 }
143
144 $additional_fields = [];
145 if ( isset( $request['include'] ) ) {
146 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
147
148 if ( in_array( 'owner', $include_params ) ) {
149 $contact_owner_id = ( $item->user_id ) ? get_user_meta( $item->user_id, 'contact_owner', true ) : erp_people_get_meta( $item->id, 'contact_owner', true );
150
151 $item->owner = $this->get_user( $contact_owner_id );
152 $additional_fields = ['owner' => $item->owner];
153 }
154 }
155
156 $item = $this->prepare_item_for_response( $item, $request, $additional_fields );
157 $response = rest_ensure_response( $item );
158
159 return $response;
160 }
161
162 /**
163 * Create a contact
164 *
165 * @param WP_REST_Request $request
166 *
167 * @return WP_Error|WP_REST_Request
168 */
169 public function create_contact( $request ) {
170 $item = $this->prepare_item_for_database( $request );
171 $id = erp_insert_people( $item );
172
173 $contact = erp_get_people( $id );
174
175 $request->set_param( 'context', 'edit' );
176 $response = $this->prepare_item_for_response( $contact, $request );
177 $response = rest_ensure_response( $response );
178 $response->set_status( 201 );
179 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
180
181 return $response;
182 }
183
184 /**
185 * Create contacts
186 *
187 * @param WP_REST_Request $request
188 *
189 * @return WP_Error|WP_REST_Request
190 */
191 public function create_contacts( $request ) {
192 $contacts = json_decode( $request->get_body(), true );
193
194 foreach ( $contacts as $contact ) {
195 $item = $this->prepare_item_for_database( $contact );
196 $id = erp_insert_people( $item );
197
198 if ( is_wp_error( $id ) ) {
199 return $id;
200 }
201 }
202
203 return new WP_REST_Response( true, 201 );
204 }
205
206 /**
207 * Update a contact
208 *
209 * @param WP_REST_Request $request
210 *
211 * @return WP_Error|WP_REST_Request
212 */
213 public function update_contact( $request ) {
214 $id = (int) $request['id'];
215
216 $item = erp_get_people( $id );
217 if ( ! $item ) {
218 return new WP_Error( 'rest_contact_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
219 }
220
221 if ( isset( $request['email'] ) ) {
222 unset( $request['email'] );
223 }
224
225 if ( isset( $request['type'] ) ) {
226 unset( $request['type'] );
227 }
228
229 $item = $this->prepare_item_for_database( $request );
230
231 erp_insert_people( $item );
232
233 $contact = erp_get_people( $id );
234
235 $response = $this->prepare_item_for_response( $contact, $request );
236 $response = rest_ensure_response( $response );
237 $response->set_status( 201 );
238 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
239
240 return $response;
241 }
242
243 /**
244 * Delete a contact
245 *
246 * @param WP_REST_Request $request
247 *
248 * @return WP_Error|WP_REST_Request
249 */
250 public function delete_contact( $request ) {
251 $id = (int) $request['id'];
252
253 $data = [
254 'id' => $id,
255 'hard' => false,
256 'type' => 'contact'
257 ];
258
259 erp_delete_people( $data );
260
261 return new WP_REST_Response( true, 204 );
262 }
263
264 /**
265 * Prepare a single item for create or update
266 *
267 * @param WP_REST_Request $request Request object.
268 *
269 * @return array $prepared_item
270 */
271 protected function prepare_item_for_database( $request ) {
272 $prepared_item = [];
273
274 // required arguments.
275 if ( isset( $request['first_name'] ) ) {
276 $prepared_item['first_name'] = $request['first_name'];
277 }
278 if ( isset( $request['last_name'] ) ) {
279 $prepared_item['last_name'] = $request['last_name'];
280 }
281 if ( isset( $request['email'] ) ) {
282 $prepared_item['email'] = $request['email'];
283 }
284
285 // optional arguments.
286 if ( isset( $request['id'] ) ) {
287 $prepared_item['id'] = absint( $request['id'] );
288 }
289 if ( isset( $request['company'] ) ) {
290 $prepared_item['company'] = $request['company'];
291 }
292 if ( isset( $request['phone'] ) ) {
293 $prepared_item['phone'] = $request['phone'];
294 }
295 if ( isset( $request['mobile'] ) ) {
296 $prepared_item['mobile'] = $request['mobile'];
297 }
298 if ( isset( $request['other'] ) ) {
299 $prepared_item['other'] = $request['other'];
300 }
301 if ( isset( $request['website'] ) ) {
302 $prepared_item['website'] = $request['website'];
303 }
304 if ( isset( $request['fax'] ) ) {
305 $prepared_item['fax'] = $request['fax'];
306 }
307 if ( isset( $request['notes'] ) ) {
308 $prepared_item['notes'] = $request['notes'];
309 }
310 if ( isset( $request['street_1'] ) ) {
311 $prepared_item['street_1'] = $request['street_1'];
312 }
313 if ( isset( $request['street_2'] ) ) {
314 $prepared_item['street_2'] = $request['street_2'];
315 }
316 if ( isset( $request['city'] ) ) {
317 $prepared_item['city'] = $request['city'];
318 }
319 if ( isset( $request['state'] ) ) {
320 $prepared_item['state'] = $request['state'];
321 }
322 if ( isset( $request['postal_code'] ) ) {
323 $prepared_item['postal_code'] = $request['postal_code'];
324 }
325 if ( isset( $request['country'] ) ) {
326 $prepared_item['country'] = $request['country'];
327 }
328 if ( isset( $request['currency'] ) ) {
329 $prepared_item['currency'] = $request['currency'];
330 }
331 if ( isset( $request['type'] ) ) {
332 $prepared_item['type'] = $request['type'];
333 }
334 if ( isset( $request['owner'] ) ) {
335 $prepared_item['contact_owner'] = $request['owner'];
336 }
337 if ( isset( $request['life_stage'] ) ) {
338 $prepared_item['life_stage'] = $request['life_stage'];
339 }
340
341 return $prepared_item;
342 }
343
344 /**
345 * Prepare a single user output for response
346 *
347 * @param object $item
348 * @param WP_REST_Request $request Request object.
349 * @param array $additional_fields (optional)
350 *
351 * @return WP_REST_Response $response Response data.
352 */
353 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
354 $data = [
355 'id' => (int) $item->id,
356 'first_name' => $item->first_name,
357 'last_name' => $item->last_name,
358 'email' => $item->email,
359 'company' => $item->company,
360 'phone' => $item->phone,
361 'mobile' => $item->mobile,
362 'other' => $item->other,
363 'website' => $item->website,
364 'fax' => $item->fax,
365 'notes' => $item->notes,
366 'street_1' => $item->street_1,
367 'street_2' => $item->street_2,
368 'city' => $item->city,
369 'state' => $item->state,
370 'postal_code' => $item->postal_code,
371 'country' => $item->country,
372 'currency' => $item->currency,
373 'types' => $item->types,
374 'user_id' => (int) $item->user_id,
375 'life_stage' => $item->life_stage,
376 ];
377
378 $data = array_merge( $data, $additional_fields );
379
380 // Wrap the data in a response object
381 $response = rest_ensure_response( $data );
382
383 $response = $this->add_links( $response, $item );
384
385 return $response;
386 }
387
388 /**
389 * Get the User's schema, conforming to JSON Schema
390 *
391 * @return array
392 */
393 public function get_item_schema() {
394 $schema = [
395 '$schema' => 'http://json-schema.org/draft-04/schema#',
396 'title' => 'contact',
397 'type' => 'object',
398 'properties' => [
399 'id' => [
400 'description' => __( 'Unique identifier for the resource.' ),
401 'type' => 'integer',
402 'context' => [ 'embed', 'view', 'edit' ],
403 'readonly' => true,
404 ],
405 'first_name' => [
406 'description' => __( 'First name for the resource.' ),
407 'type' => 'string',
408 'context' => [ 'edit' ],
409 'arg_options' => [
410 'sanitize_callback' => 'sanitize_text_field',
411 ],
412 'required' => true,
413 ],
414 'last_name' => [
415 'description' => __( 'Last name for the resource.' ),
416 'type' => 'string',
417 'context' => [ 'edit' ],
418 'arg_options' => [
419 'sanitize_callback' => 'sanitize_text_field',
420 ],
421 'required' => true,
422 ],
423 'email' => [
424 'description' => __( 'The email address for the resource.' ),
425 'type' => 'string',
426 'format' => 'email',
427 'context' => [ 'edit' ],
428 'required' => true,
429 ],
430 'company' => [
431 'description' => __( 'Company for the resource.' ),
432 'type' => 'string',
433 'context' => [ 'edit' ],
434 'arg_options' => [
435 'sanitize_callback' => 'sanitize_text_field',
436 ],
437 ],
438 'phone' => [
439 'description' => __( 'Phone for the resource.' ),
440 'type' => 'string',
441 'context' => [ 'edit' ],
442 'arg_options' => [
443 'sanitize_callback' => 'sanitize_text_field',
444 ],
445 ],
446 'mobile' => [
447 'description' => __( 'Mobile for the resource.' ),
448 'type' => 'string',
449 'context' => [ 'edit' ],
450 'arg_options' => [
451 'sanitize_callback' => 'sanitize_text_field',
452 ],
453 ],
454 'other' => [
455 'description' => __( 'Other for the resource.' ),
456 'type' => 'string',
457 'context' => [ 'edit' ],
458 'arg_options' => [
459 'sanitize_callback' => 'sanitize_text_field',
460 ],
461 ],
462 'website' => [
463 'description' => __( 'Website of the resource.' ),
464 'type' => 'string',
465 'format' => 'uri',
466 'context' => [ 'embed', 'view', 'edit' ],
467 ],
468 'fax' => [
469 'description' => __( 'Fax of the resource.' ),
470 'type' => 'string',
471 'context' => [ 'embed', 'view', 'edit' ],
472 'arg_options' => [
473 'sanitize_callback' => 'sanitize_text_field',
474 ],
475 ],
476 'notes' => [
477 'description' => __( 'Notes of the resource.' ),
478 'type' => 'string',
479 'context' => [ 'embed', 'view', 'edit' ],
480 'arg_options' => [
481 'sanitize_callback' => 'sanitize_text_field',
482 ],
483 ],
484 'street_1' => [
485 'description' => __( 'Street 1 of the resource.' ),
486 'type' => 'string',
487 'context' => [ 'embed', 'view', 'edit' ],
488 'arg_options' => [
489 'sanitize_callback' => 'sanitize_text_field',
490 ],
491 ],
492 'street_2' => [
493 'description' => __( 'Street 1 of the resource.' ),
494 'type' => 'string',
495 'context' => [ 'embed', 'view', 'edit' ],
496 'arg_options' => [
497 'sanitize_callback' => 'sanitize_text_field',
498 ],
499 ],
500 'city' => [
501 'description' => __( 'City of the resource.' ),
502 'type' => 'string',
503 'context' => [ 'embed', 'view', 'edit' ],
504 'arg_options' => [
505 'sanitize_callback' => 'sanitize_text_field',
506 ],
507 ],
508 'state' => [
509 'description' => __( 'State of the resource.' ),
510 'type' => 'string',
511 'context' => [ 'embed', 'view', 'edit' ],
512 'arg_options' => [
513 'sanitize_callback' => 'sanitize_text_field',
514 ],
515 ],
516 'postal_code' => [
517 'description' => __( 'Postal Code of the resource.' ),
518 'type' => 'string',
519 'context' => [ 'embed', 'view', 'edit' ],
520 'arg_options' => [
521 'sanitize_callback' => 'sanitize_text_field',
522 ],
523 ],
524 'country' => [
525 'description' => __( 'Country of the resource.' ),
526 'type' => 'string',
527 'context' => [ 'embed', 'view', 'edit' ],
528 'arg_options' => [
529 'sanitize_callback' => 'sanitize_text_field',
530 ],
531 ],
532 'currency' => [
533 'description' => __( 'Currency of the resource.' ),
534 'type' => 'string',
535 'context' => [ 'embed', 'view', 'edit' ],
536 'arg_options' => [
537 'sanitize_callback' => 'sanitize_text_field',
538 ],
539 ],
540 'type' => [
541 'description' => __( 'Type of the resource.' ),
542 'type' => 'string',
543 'context' => [ 'embed', 'view', 'edit' ],
544 'arg_options' => [
545 'sanitize_callback' => 'sanitize_text_field',
546 ],
547 ],
548 'owner' => [
549 'description' => __( 'Owner of the resource.' ),
550 'type' => 'integer',
551 'context' => [ 'embed', 'view', 'edit' ],
552 'required' => true,
553 ],
554 'life_stage' => [
555 'description' => __( 'Life stage of the resource.' ),
556 'type' => 'string',
557 'context' => [ 'embed', 'view', 'edit' ],
558 'arg_options' => [
559 'sanitize_callback' => 'sanitize_text_field',
560 ],
561 'required' => true,
562 ],
563 ],
564 ];
565
566 return $schema;
567 }
568 }
569