PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.13.2
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.13.2
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 / ContactsController.php

ContactsController.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.13.2, at includes/API/ContactsController.php

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