PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.6.7
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.6.7
1.17.10 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 All 144 releases
erp / modules / hrm / includes / api / class-employees-controller.php

class-employees-controller.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.7, at modules/hrm/includes/api/class-employees-controller.php

1,743 lines 61.2 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\HRM\API;
4
5 use Exception;
6 use WeDevs\ERP\API\REST_Controller;
7 use WeDevs\ERP\HRM\Employee;
8 use WeDevs\ERP\HRM\Models\Department;
9 use WeDevs\ERP\HRM\Models\Designation;
10 use WP_Error;
11 use WP_REST_Request;
12 use WP_REST_Response;
13 use WP_REST_Server;
14
15 class Employees_Controller extends REST_Controller {
16
17 /**
18 * Endpoint namespace.
19 *
20 * @var string
21 */
22 protected $namespace = 'erp/v1';
23
24 /**
25 * Route base.
26 *
27 * @var string
28 */
29 protected $rest_base = 'hrm/employees';
30
31 /**
32 * Register the routes for the objects of the controller.
33 */
34 public function register_routes() {
35 register_rest_route( $this->namespace, '/' . $this->rest_base, [
36 [
37 'methods' => WP_REST_Server::READABLE,
38 'callback' => [ $this, 'get_employees' ],
39 'args' => $this->get_collection_params(),
40 'permission_callback' => function ( $request ) {
41 return current_user_can( 'erp_view_list' );
42 },
43 ],
44 [
45 'methods' => WP_REST_Server::CREATABLE,
46 'callback' => [ $this, 'create_employee' ],
47 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
48 'permission_callback' => function ( $request ) {
49 return current_user_can( 'erp_create_employee' );
50 },
51 ],
52 'schema' => [ $this, 'get_public_item_schema' ],
53 ] );
54
55 register_rest_route( $this->namespace, '/' . $this->rest_base . '/bulk', [
56 [
57 'methods' => WP_REST_Server::CREATABLE,
58 'callback' => [ $this, 'create_employees' ],
59 'permission_callback' => function ( $request ) {
60 return current_user_can( 'erp_create_employee' );
61 },
62 ],
63 'schema' => [ $this, 'get_public_item_schema' ],
64 ] );
65
66 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)', [
67 [
68 'methods' => WP_REST_Server::READABLE,
69 'callback' => [ $this, 'get_employee' ],
70 'args' => [
71 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
72 ],
73 'permission_callback' => function ( $request ) {
74 return current_user_can( 'erp_list_employee' );
75 },
76 ],
77 [
78 'methods' => WP_REST_Server::EDITABLE,
79 'callback' => [ $this, 'update_employee' ],
80 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
81 'permission_callback' => function ( $request ) {
82 return current_user_can( 'erp_edit_employee', $request['user_id'] );
83 },
84 ],
85 [
86 'methods' => WP_REST_Server::DELETABLE,
87 'callback' => [ $this, 'delete_employee' ],
88 'permission_callback' => function ( $request ) {
89 return current_user_can( 'erp_delete_employee' );
90 },
91 ],
92 'schema' => [ $this, 'get_public_item_schema' ],
93 ] );
94
95 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/experiences', [
96 [
97 'methods' => WP_REST_Server::READABLE,
98 'callback' => [ $this, 'get_experiences' ],
99 'permission_callback' => function ( $request ) {
100 return current_user_can( 'erp_view_experience', $request['user_id'] );
101 },
102 ],
103 [
104 'methods' => WP_REST_Server::CREATABLE,
105 'callback' => [ $this, 'create_experience' ],
106 'permission_callback' => function ( $request ) {
107 return current_user_can( 'erp_create_experience', $request['user_id'] );
108 },
109 ],
110 ] );
111
112 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/experiences' . '/(?P<id>[\d]+)', [
113 [
114 'methods' => WP_REST_Server::READABLE,
115 'callback' => [ $this, 'get_experience' ],
116 'permission_callback' => function ( $request ) {
117 return current_user_can( 'erp_view_experience', $request['user_id'] );
118 },
119 ],
120 [
121 'methods' => WP_REST_Server::EDITABLE,
122 'callback' => [ $this, 'update_experience' ],
123 'permission_callback' => function ( $request ) {
124 return current_user_can( 'erp_edit_experience', $request['user_id'] );
125 },
126 ],
127 [
128 'methods' => WP_REST_Server::DELETABLE,
129 'callback' => [ $this, 'delete_experience' ],
130 'permission_callback' => function ( $request ) {
131 return current_user_can( 'erp_delete_experience', $request['user_id'] );
132 },
133 ],
134 ] );
135
136 //education
137 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/educations', [
138 [
139 'methods' => WP_REST_Server::READABLE,
140 'callback' => [ $this, 'get_educations' ],
141 'permission_callback' => function ( $request ) {
142 return current_user_can( 'erp_view_education', $request['user_id'] );
143 },
144 ],
145 [
146 'methods' => WP_REST_Server::CREATABLE,
147 'callback' => [ $this, 'create_education' ],
148 'permission_callback' => function ( $request ) {
149 return current_user_can( 'erp_create_education', $request['user_id'] );
150 },
151 ],
152 ] );
153
154 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/educations' . '/(?P<id>[\d]+)', [
155 [
156 'methods' => WP_REST_Server::EDITABLE,
157 'callback' => [ $this, 'update_education' ],
158 'permission_callback' => function ( $request ) {
159 return current_user_can( 'erp_edit_education', $request['user_id'] );
160 },
161 ],
162 [
163 'methods' => WP_REST_Server::DELETABLE,
164 'callback' => [ $this, 'delete_education' ],
165 'permission_callback' => function ( $request ) {
166 return current_user_can( 'erp_delete_education', $request['user_id'] );
167 },
168 ],
169 ] );
170
171 //dependents
172 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/dependents', [
173 [
174 'methods' => WP_REST_Server::READABLE,
175 'callback' => [ $this, 'get_dependents' ],
176 'permission_callback' => function ( $request ) {
177 return current_user_can( 'erp_view_dependent', $request['user_id'] );
178 },
179 ],
180 [
181 'methods' => WP_REST_Server::CREATABLE,
182 'callback' => [ $this, 'create_dependent' ],
183 'permission_callback' => function ( $request ) {
184 return current_user_can( 'erp_create_dependent', $request['user_id'] );
185 },
186 ],
187 ] );
188
189 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/dependents' . '/(?P<id>[\d]+)', [
190 [
191 'methods' => WP_REST_Server::EDITABLE,
192 'callback' => [ $this, 'update_dependent' ],
193 'permission_callback' => function ( $request ) {
194 return current_user_can( 'erp_edit_dependent', $request['user_id'] );
195 },
196 ],
197 [
198 'methods' => WP_REST_Server::DELETABLE,
199 'callback' => [ $this, 'delete_dependent' ],
200 'permission_callback' => function ( $request ) {
201 return current_user_can( 'erp_delete_dependent', $request['user_id'] );
202 },
203 ],
204 ] );
205
206 //job histories
207 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/job_histories', [
208 [
209 'methods' => WP_REST_Server::READABLE,
210 'callback' => [ $this, 'get_histories' ],
211 'permission_callback' => function ( $request ) {
212 return current_user_can( 'erp_view_jobinfo', $request['user_id'] );
213 },
214 ],
215 [
216 'methods' => WP_REST_Server::CREATABLE,
217 'callback' => [ $this, 'create_history' ],
218 'permission_callback' => function ( $request ) {
219 return current_user_can( 'erp_manage_jobinfo' );
220 },
221 ],
222 ] );
223
224 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/job_histories' . '/(?P<id>[\d]+)', [
225 [
226 'methods' => WP_REST_Server::DELETABLE,
227 'callback' => [ $this, 'delete_history' ],
228 'permission_callback' => function ( $request ) {
229 return current_user_can( 'erp_manage_jobinfo' );
230 },
231 ],
232 ] );
233
234 //performances
235
236 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/performances', [
237 [
238 'methods' => WP_REST_Server::READABLE,
239 'callback' => [ $this, 'get_performances' ],
240 'permission_callback' => function ( $request ) {
241 return current_user_can( 'erp_view_jobinfo', $request['user_id'] );
242 },
243 ],
244 [
245 'methods' => WP_REST_Server::CREATABLE,
246 'callback' => [ $this, 'create_performance' ],
247 'permission_callback' => function ( $request ) {
248 return current_user_can( 'erp_manage_jobinfo' );
249 },
250 ],
251 ] );
252
253 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/performances' . '/(?P<id>[\d]+)', [
254 [
255 'methods' => WP_REST_Server::DELETABLE,
256 'callback' => [ $this, 'delete_performance' ],
257 'permission_callback' => function ( $request ) {
258 return current_user_can( 'erp_manage_jobinfo' );
259 },
260 ],
261 ] );
262
263 //events
264 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/events', [
265 [
266 'methods' => WP_REST_Server::READABLE,
267 'callback' => [ $this, 'get_events' ],
268 'permission_callback' => function ( $request ) {
269 return current_user_can( 'erp_edit_employee', $request['user_id'] );
270 },
271 ],
272 ] );
273
274 //terminate
275 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/terminate', [
276 [
277 'methods' => WP_REST_Server::EDITABLE,
278 'callback' => [ $this, 'create_terminate' ],
279 'permission_callback' => function ( $request ) {
280 return current_user_can( 'erp_can_terminate' );
281 },
282 ],
283 ] );
284
285 //announcements
286 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/announcements', [
287 [
288 'methods' => WP_REST_Server::READABLE,
289 'callback' => [ $this, 'get_announcements' ],
290 'permission_callback' => function ( $request ) {
291 return current_user_can( 'erp_view_announcement', $request['user_id'] );
292 },
293 ],
294 ] );
295
296 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/announcements', [
297 [
298 'methods' => WP_REST_Server::READABLE,
299 'callback' => [ $this, 'get_announcements' ],
300 'permission_callback' => function ( $request ) {
301 return current_user_can( 'erp_view_announcement', $request['user_id'] );
302 },
303 ],
304 [
305 'methods' => WP_REST_Server::EDITABLE,
306 'callback' => [ $this, 'update_status' ],
307 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
308 'permission_callback' => function ( $request ) {
309 return current_user_can( 'erp_view_announcement', $request['user_id'] );
310 },
311 ],
312 ] );
313
314 //policies
315 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/policies', [
316 [
317 'methods' => WP_REST_Server::READABLE,
318 'callback' => [ $this, 'get_policies' ],
319 'permission_callback' => function ( $request ) {
320 return current_user_can( 'erp_edit_employee', $request['user_id'] );
321 },
322 ],
323 ] );
324
325 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/leaves', [
326 [
327 'methods' => WP_REST_Server::READABLE,
328 'callback' => [ $this, 'get_leaves' ],
329 'permission_callback' => function ( $request ) {
330 return current_user_can( 'erp_edit_employee', $request['user_id'] );
331 },
332 ],
333 [
334 'methods' => WP_REST_Server::CREATABLE,
335 'callback' => [ $this, 'create_leave' ],
336 'permission_callback' => function ( $request ) {
337 return current_user_can( 'erp_edit_employee', $request['user_id'] );
338 },
339 ],
340 ] );
341
342 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/notes', [
343 [
344 'methods' => WP_REST_Server::READABLE,
345 'callback' => [ $this, 'get_notes' ],
346 'permission_callback' => function ( $request ) {
347 return current_user_can( 'erp_manage_review' );
348 },
349 ],
350 [
351 'methods' => WP_REST_Server::CREATABLE,
352 'callback' => [ $this, 'create_note' ],
353 'permission_callback' => function ( $request ) {
354 return current_user_can( 'erp_manage_review' );
355 },
356 ],
357 ] );
358 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/notes' . '/(?P<note_id>[\d]+)', [
359 [
360 'methods' => WP_REST_Server::DELETABLE,
361 'callback' => [ $this, 'delete_note' ],
362 'permission_callback' => function ( $request ) {
363 return current_user_can( 'erp_edit_employee' );
364 },
365 ],
366 ] );
367
368 //roles
369 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<user_id>[\d]+)' . '/roles', [
370 [
371 'methods' => WP_REST_Server::READABLE,
372 'callback' => [ $this, 'get_roles' ],
373 'permission_callback' => function ( $request ) {
374 return current_user_can( erp_hr_get_manager_role() );
375 },
376 ],
377 [
378 'methods' => WP_REST_Server::EDITABLE,
379 'callback' => [ $this, 'update_role' ],
380 'permission_callback' => function ( $request ) {
381 return current_user_can( erp_hr_get_manager_role() );
382 },
383 ],
384 ] );
385
386 // Upload Photo
387 register_rest_route( $this->namespace, '/' . $this->rest_base . '/upload', [
388 [
389 'methods' => WP_REST_Server::CREATABLE,
390 'callback' => [ $this, 'upload_photo' ],
391 'permission_callback' => function ( $request ) {
392 return current_user_can( 'erp_create_employee' );
393 },
394 ],
395 [
396 'methods' => WP_REST_Server::EDITABLE,
397 'callback' => [ $this, 'update_photo' ],
398 'permission_callback' => function () {
399 return current_user_can( 'erp_create_employee' );
400 },
401 ],
402 ] );
403 }
404
405 /**
406 * Upload employee photo
407 *
408 * @return array
409 */
410 public function upload_photo( WP_REST_Request $request ) {
411 $file = isset( $_FILES['image'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_FILES['image'] ) ) : [];
412
413 if ( ! $file ) {
414 return;
415 }
416
417 require_once ABSPATH . 'wp-admin/includes/image.php';
418 require_once ABSPATH . 'wp-admin/includes/file.php';
419 require_once ABSPATH . 'wp-admin/includes/media.php';
420
421 $attachment_id = media_handle_upload( 'image', 0 );
422
423 $response = [
424 'photo_id' => $attachment_id,
425 ];
426
427 return $response;
428 }
429
430 /**
431 * Update Photo
432 *
433 * @return bool
434 */
435 public function update_photo( WP_REST_Request $request ) {
436 $photo_id = isset( $request['photo_id'] ) ? $request['photo_id'] : 0;
437 $user_id = isset( $request['user_id'] ) ? $request['user_id'] : 0;
438
439 update_user_meta( $user_id, 'photo_id', $photo_id );
440 }
441
442 /**
443 * Get a collection of employees
444 *
445 * @return mixed|object|WP_REST_Response
446 */
447 public function get_employees( WP_REST_Request $request ) {
448 $args = [
449 'number' => $request['per_page'],
450 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
451 'status' => ( $request['status'] ) ? $request['status'] : 'active',
452 'department' => ( $request['department'] ) ? $request['department'] : '-1',
453 'designation' => ( $request['designation'] ) ? $request['designation'] : '-1',
454 'location' => ( $request['location'] ) ? $request['location'] : '-1',
455 'type' => ( $request['type'] ) ? $request['type'] : '-1',
456 's' => ( $request['s'] ) ? $request['s'] : '',
457 ];
458
459 $items = erp_hr_get_employees( $args );
460
461 $args['count'] = true;
462 $total_items = erp_hr_get_employees( $args );
463
464 $formatted_items = [];
465
466 foreach ( $items as $item ) {
467 $additional_fields = [];
468 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
469 $formatted_items[] = $this->prepare_response_for_collection( $data );
470 }
471 $response = rest_ensure_response( $formatted_items );
472 $response = $this->format_collection_response( $response, $request, (int) $total_items );
473
474 return $response;
475 }
476
477 /**
478 * Get a specific employee
479 *
480 * @return WP_Error|WP_REST_Response
481 */
482 public function get_employee( WP_REST_Request $request ) {
483 $user_id = (int) $request['user_id'];
484 $item = new Employee( $user_id );
485
486 if ( ! $item->is_employee() ) {
487 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
488 }
489
490 $item = $this->prepare_item_for_response( $item, $request );
491 $response = rest_ensure_response( $item );
492
493 return $response;
494 }
495
496 /**
497 * Create employees
498 *
499 * @param $request
500 *
501 * @return $this|int|WP_Error|WP_REST_Response
502 */
503 public function create_employees( WP_REST_Request $request ) {
504 $employees = json_decode( $request->get_body(), true );
505
506 foreach ( $employees as $employee ) {
507 $item_data = $this->prepare_item_for_database( $employee );
508 $item = new Employee( null );
509 $created = $item->create_employee( $item_data );
510
511 if ( is_wp_error( $created ) ) {
512 return $created;
513 }
514 }
515
516 return new WP_REST_Response( true, 201 );
517 }
518
519 /**
520 * Create an employee
521 *
522 * @param WP_REST_Request $request
523 *
524 * @return WP_Error|WP_REST_Request
525 */
526 public function create_employee( $request ) {
527 $item_data = $this->prepare_item_for_database( $request );
528
529 $employee = new Employee( null );
530 $created = $employee->create_employee( $item_data );
531
532 if ( is_wp_error( $created ) ) {
533 return $created;
534 }
535 $request->set_param( 'context', 'edit' );
536 $item = new Employee( $created->user_id );
537
538 // User Notification
539 if ( isset( $request['user_notification'] ) && $request['user_notification'] == true ) {
540 $emailer = wperp()->emailer->get_email( 'New_Employee_Welcome' );
541 $send_login = isset( $request['login_info'] ) ? true : false;
542
543 if ( is_a( $emailer, '\WeDevs\ERP\Email' ) ) {
544 $emailer->trigger( $employee->get_user_id(), $send_login );
545 }
546 }
547
548 $response = $this->prepare_item_for_response( $item, $request );
549 $response = rest_ensure_response( $response );
550 $response->set_status( 201 );
551 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $employee->get_user_id() ) ) );
552
553 return $response;
554 }
555
556 /**
557 * Update an employee
558 *
559 * @return $this|mixed|object|WP_Error|WP_REST_Response
560 */
561 public function update_employee( WP_REST_Request $request ) {
562 $id = (int) $request['user_id'];
563
564 $employee = new Employee( $id );
565
566 if ( ! $employee ) {
567 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
568 }
569
570 $data = $this->prepare_item_for_database( $request );
571 $updated = $employee->update_employee( $data );
572
573 if ( is_wp_error( $updated ) ) {
574 return $updated;
575 }
576
577 $request->set_param( 'context', 'edit' );
578 $updated_user = new Employee( $updated->user_id );
579 $response = $this->prepare_item_for_response( $updated_user, $request );
580
581 $response = rest_ensure_response( $response );
582 $response->set_status( 201 );
583 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
584
585 return $response;
586 }
587
588 /**
589 * Delete an employee
590 *
591 * @param $request
592 *
593 * @return WP_REST_Response
594 */
595 public function delete_employee( WP_REST_Request $request ) {
596 $id = (int) $request['user_id'];
597
598 erp_employee_delete( $id );
599 $response = rest_ensure_response( true );
600
601 return new WP_REST_Response( $response, 204 );
602 }
603
604 /**
605 * Get a collection of employee's experiences
606 *
607 * @return mixed|object|WP_Error|WP_REST_Response
608 */
609 public function get_experiences( WP_REST_Request $request ) {
610 $employee_id = (int) $request['user_id'];
611 $employee = new Employee( $employee_id );
612
613 if ( ! $employee->is_employee() ) {
614 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
615 }
616
617 $items = $employee->get_experiences();
618
619 $total_items = $employee->get_erp_user()->experiences()->count();
620 $response = rest_ensure_response( $items );
621 $response = $this->format_collection_response( $response, $request, $total_items );
622
623 return $response;
624 }
625
626 /**
627 * Create an experience
628 *
629 * @return WP_Error|WP_REST_Request
630 */
631 public function create_experience( WP_REST_Request $request ) {
632 $employee_id = (int) $request['user_id'];
633 $employee = new Employee( $employee_id );
634
635 if ( ! $employee->is_employee() ) {
636 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
637 }
638 $experience = $employee->add_experience( $request->get_params() );
639
640 if ( is_wp_error( $experience ) ) {
641 return $experience;
642 }
643
644 $request->set_param( 'context', 'edit' );
645 $response = rest_ensure_response( $experience );
646 $response->set_status( 201 );
647 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $experience['id'] ) ) );
648
649 return $response;
650 }
651
652 /**
653 * Update an experience
654 *
655 * @return WP_Error|WP_REST_Request
656 */
657 public function update_experience( WP_REST_Request $request ) {
658 $employee_id = (int) $request['user_id'];
659 $exp_id = (int) $request['id'];
660 $employee = new Employee( $employee_id );
661
662 if ( ! $employee->is_employee() ) {
663 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
664 }
665 $args = $request->get_params();
666 $args['id'] = $exp_id;
667
668 $experience = $employee->add_experience( $args );
669
670 if ( is_wp_error( $experience ) ) {
671 return $experience;
672 }
673
674 $request->set_param( 'context', 'edit' );
675 $response = rest_ensure_response( $experience );
676 $response->set_status( 201 );
677 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $experience['id'] ) ) );
678
679 return $response;
680 }
681
682 /**
683 * Delete an experience
684 *
685 * @param $request
686 *
687 * @return WP_Error|WP_REST_Response
688 */
689 public function delete_experience( $request ) {
690 $employee_id = (int) $request['user_id'];
691 $employee = new Employee( $employee_id );
692
693 if ( ! $employee->is_employee() ) {
694 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
695 }
696 $result = $employee->delete_experience( $request['id'] );
697 $response = rest_ensure_response( $result );
698
699 return $response;
700 }
701
702 /**
703 * Get a collection of employee's educations
704 *
705 * @param $request
706 *
707 * @return mixed|object|WP_Error|WP_REST_Response
708 */
709 public function get_educations( $request ) {
710 $employee_id = (int) $request['user_id'];
711 $employee = new Employee( $employee_id );
712
713 if ( ! $employee->is_employee() ) {
714 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
715 }
716 $items = $employee->get_educations();
717 $total = $employee->get_erp_user()->educations()->count();
718 $response = rest_ensure_response( $items );
719 $response = $this->format_collection_response( $response, $request, $total );
720
721 return $response;
722 }
723
724 /**
725 * Create an education
726 *
727 * @param WP_REST_Request $request
728 *
729 * @return WP_Error|WP_REST_Request
730 */
731 public function create_education( $request ) {
732 $employee_id = (int) $request['user_id'];
733 $employee = new Employee( $employee_id );
734
735 if ( ! $employee->is_employee() ) {
736 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
737 }
738 $education = $employee->add_education( $request->get_params() );
739
740 if ( is_wp_error( $education ) ) {
741 return $education;
742 }
743
744 $request->set_param( 'context', 'edit' );
745
746 $response = rest_ensure_response( $education );
747 $response->set_status( 201 );
748 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $education['id'] ) ) );
749
750 return $response;
751 }
752
753 /**
754 * Update an education
755 *
756 * @param WP_REST_Request $request
757 *
758 * @return WP_Error|WP_REST_Request
759 */
760 public function update_education( $request ) {
761 $employee_id = (int) $request['user_id'];
762 $edu_id = (int) $request['id'];
763 $employee = new Employee( $employee_id );
764
765 if ( ! $employee->is_employee() ) {
766 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
767 }
768
769 $args['id'] = $edu_id;
770 $education = $employee->add_education( $request->get_params() );
771
772 if ( is_wp_error( $education ) ) {
773 return $education;
774 }
775
776 $request->set_param( 'context', 'edit' );
777
778 $response = rest_ensure_response( $education );
779 $response->set_status( 201 );
780 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $education['id'] ) ) );
781
782 return $response;
783 }
784
785 /**
786 * Delete an education
787 *
788 * @param WP_REST_Request $request
789 *
790 * @return WP_Error|WP_REST_Request
791 */
792 public function delete_education( $request ) {
793 $employee_id = (int) $request['user_id'];
794 $employee = new Employee( $employee_id );
795
796 if ( ! $employee->is_employee() ) {
797 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
798 }
799 $result = $employee->delete_education( $request['id'] );
800 $response = rest_ensure_response( $result );
801
802 return $response;
803 }
804
805 /**
806 * Get a collection of employee's dependents
807 *
808 * @param WP_REST_Request $request
809 *
810 * @return WP_Error|WP_REST_Response|mixed
811 */
812 public function get_dependents( $request ) {
813 $employee_id = (int) $request['user_id'];
814 $employee = new Employee( $employee_id );
815
816 if ( ! $employee->is_employee() ) {
817 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
818 }
819 $items = $employee->get_dependents();
820 $total = $employee->get_erp_user()->dependents()->count();
821 $response = rest_ensure_response( $items );
822 $response = $this->format_collection_response( $response, $request, $total );
823
824 return $response;
825 }
826
827 /**
828 * Create a dependent
829 *
830 * @param WP_REST_Request $request
831 *
832 * @return WP_Error|WP_REST_Request
833 */
834 public function create_dependent( $request ) {
835 $employee_id = (int) $request['user_id'];
836 $employee = new Employee( $employee_id );
837
838 if ( ! $employee->is_employee() ) {
839 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
840 }
841 $dependent = $employee->add_dependent( $request->get_params() );
842
843 if ( is_wp_error( $dependent ) ) {
844 return $dependent;
845 }
846
847 $request->set_param( 'context', 'edit' );
848
849 $response = rest_ensure_response( $dependent );
850 $response->set_status( 201 );
851 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $dependent['id'] ) ) );
852
853 return $response;
854 }
855
856 /**
857 * Update a dependent
858 *
859 * @param WP_REST_Request $request
860 *
861 * @return WP_Error|WP_REST_Request
862 */
863 public function update_dependent( $request ) {
864 $employee_id = (int) $request['user_id'];
865 $depen_id = (int) $request['id'];
866 $employee = new Employee( $employee_id );
867
868 if ( ! $employee->is_employee() ) {
869 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
870 }
871
872 $args['id'] = $depen_id;
873 $dependent = $employee->add_dependent( $request->get_params() );
874
875 if ( is_wp_error( $dependent ) ) {
876 return $dependent;
877 }
878
879 $request->set_param( 'context', 'edit' );
880
881 $response = rest_ensure_response( $dependent );
882 $response->set_status( 201 );
883 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $dependent['id'] ) ) );
884
885 return $response;
886 }
887
888 /**
889 * Delete a dependent
890 *
891 * @param WP_REST_Request $request
892 *
893 * @return WP_Error|WP_REST_Request
894 */
895 public function delete_dependent( $request ) {
896 $employee_id = (int) $request['user_id'];
897 $employee = new Employee( $employee_id );
898
899 if ( ! $employee->is_employee() ) {
900 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
901 }
902 $result = $employee->delete_dependent( $request['id'] );
903 $response = rest_ensure_response( $result );
904
905 return $response;
906 }
907
908 /**
909 * Get employee histories
910 *
911 * @since 1.3.0
912 *
913 * @param $request
914 *
915 * @return mixed|object|WP_Error|WP_REST_Response
916 */
917 public function get_histories( $request ) {
918 $employee_id = (int) $request['user_id'];
919 $employee = new Employee( $employee_id );
920
921 if ( ! $employee->is_employee() ) {
922 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
923 }
924 $module = ! empty( $request['module'] ) ? sanitize_key( $request['module'] ) : 'all';
925
926 $histories = $employee->get_job_histories( $module );
927
928 for ( $i = 0; $i < count( $histories['job'] ); $i ++ ) {
929 $reports_to = new Employee( $histories['job'][ $i ]['reporting_to'] );
930
931 if ( $employee->is_employee() ) {
932 $histories['job'][ $i ]['reporting_to_full_name'] = $reports_to->display_name;
933 }
934 }
935
936 $total = $employee->get_erp_user()->histories()->count();
937 $response = rest_ensure_response( $histories );
938 $response = $this->format_collection_response( $response, $request, $total );
939
940 return $response;
941 }
942
943 /**
944 * Create employee history
945 *
946 * @since 1.3.0
947 *
948 * @param $request
949 *
950 * @return mixed|WP_Error|WP_REST_Response
951 */
952 public function create_history( WP_REST_Request $request ) {
953 $employee_id = (int) $request['user_id'];
954 $module = ! empty( $request['module'] ) ? sanitize_key( $request['module'] ) : 0;
955 $employee = new Employee( $employee_id );
956
957 if ( ! $employee ) {
958 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
959 }
960
961 if ( empty( $module ) || ( ! in_array( $module, [ 'employment', 'compensation', 'job' ] ) ) ) {
962 return new WP_Error( 'rest_no_module_type', __( 'Invalid/No module type' ), [ 'status' => 404 ] );
963 }
964
965 $history = new WP_Error();
966
967 if ( $request['module'] == 'employment' ) {
968 $history = $employee->update_employment_status( $request->get_params() );
969 }
970
971 if ( $request['module'] == 'compensation' ) {
972 $history = $employee->update_compensation( $request->get_params() );
973 }
974
975 if ( $request['module'] == 'job' ) {
976 $history = $employee->update_job_info( $request->get_params() );
977 }
978
979 if ( is_wp_error( $history ) ) {
980 return $history;
981 }
982 $response = rest_ensure_response( $history );
983
984 return $response;
985 }
986
987 /**
988 * Delete a history
989 *
990 * @since 1.3.0
991 *
992 * @param $request
993 *
994 * @return WP_Error|WP_REST_Response
995 *
996 * @throws Exception
997 */
998 public function delete_history( $request ) {
999 $employee_id = (int) $request['user_id'];
1000 $employee = new Employee( $employee_id );
1001
1002 if ( ! $employee->is_employee() ) {
1003 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
1004 }
1005 $result = $employee->delete_job_history( $request['id'] );
1006 $response = rest_ensure_response( $result );
1007
1008 return $response;
1009 }
1010
1011 /**
1012 * Get all performance of a single employee
1013 *
1014 * @since 1.3.0
1015 *
1016 * @param $request
1017 *
1018 * @return mixed|object|WP_Error|WP_REST_Response
1019 */
1020 public function get_performances( WP_REST_Request $request ) {
1021 $employee_id = (int) $request['user_id'];
1022 $employee = new Employee( $employee_id );
1023
1024 if ( ! $employee->is_employee() ) {
1025 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid Employee id.' ), [ 'status' => 400 ] );
1026 }
1027 $items = $employee->get_performances( $request->get_params() );
1028
1029 foreach ( $items as $item ) {
1030 foreach ( $item as $performance ) {
1031 $user_id = 0;
1032
1033 if ( $performance['reporting_to'] ) {
1034 $user_id = $performance['reporting_to'];
1035 } elseif ( $performance['reviewer'] ) {
1036 $user_id = $performance['reviewer'];
1037 } elseif ( $performance['supervisor'] ) {
1038 $user_id = $performance['supervisor'];
1039 }
1040
1041 $associate_employee = new Employee( $user_id );
1042
1043 if ( $associate_employee->is_employee() ) {
1044 $performance->reporting_to_full_name = $associate_employee->display_name;
1045 $performance->supervisor_full_name = $associate_employee->display_name;
1046 $performance->reviewer_full_name = $associate_employee->display_name;
1047 }
1048 }
1049 }
1050
1051 $total = $employee->get_erp_user()->performances()->count();
1052 $response = rest_ensure_response( $items );
1053 $response = $this->format_collection_response( $response, $request, $total );
1054
1055 return $response;
1056 }
1057
1058 /**
1059 * Create a performance
1060 *
1061 * @param WP_REST_Request $request
1062 *
1063 * @return WP_Error|WP_REST_Request
1064 */
1065 public function create_performance( $request ) {
1066 $employee_id = (int) trim( $request['user_id'] );
1067 $employee = new Employee( $employee_id );
1068
1069 if ( ! $employee ) {
1070 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1071 }
1072
1073 $performance = $employee->add_performance( $request->get_params() );
1074
1075 if ( is_wp_error( $performance ) ) {
1076 return $performance;
1077 }
1078 $request->set_param( 'context', 'edit' );
1079 $response = rest_ensure_response( $performance );
1080 $response->set_status( 201 );
1081 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $performance['id'] ) ) );
1082
1083 return $response;
1084 }
1085
1086 /**
1087 * Delete performance
1088 *
1089 * @since 1.3.0
1090 *
1091 * @param $request
1092 *
1093 * @return mixed|WP_Error|WP_REST_Response
1094 */
1095 public function delete_performance( $request ) {
1096 $employee_id = (int) $request['user_id'];
1097 $employee = new Employee( $employee_id );
1098
1099 if ( ! $employee->is_employee() ) {
1100 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
1101 }
1102 $result = $employee->delete_performance( $request['id'] );
1103 $response = rest_ensure_response( $result );
1104
1105 return $response;
1106 }
1107
1108 /**
1109 * Get all the events of a single user
1110 *
1111 * @since 1.3.0
1112 *
1113 * @param $request
1114 *
1115 * @return mixed|object|WP_Error|WP_REST_Response
1116 */
1117 public function get_events( $request ) {
1118 $user_id = (int) $request['user_id'];
1119 $start = ! empty( $request['start'] ) ? $request['start'] : date( 'Y-01-01' );
1120 $end = ! empty( $request['end'] ) ? $request['end'] : date( 'Y-12-31' );
1121
1122 $employee = new Employee( $user_id );
1123
1124 if ( ! $employee ) {
1125 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1126 }
1127 $event_data = $employee->get_calender_events( ['start' => $start, 'end' => $end] );
1128
1129 $response = rest_ensure_response( $event_data );
1130 $response = $this->format_collection_response( $response, $request, count( $event_data ) );
1131
1132 return $response;
1133 }
1134
1135 /**
1136 * Get Available leaves policies of a single employee
1137 *
1138 * @since 1.3.0
1139 *
1140 * @param $request
1141 *
1142 * @return mixed|object|WP_Error|WP_REST_Response
1143 */
1144 public function get_policies( $request ) {
1145 $user_id = (int) $request['user_id'];
1146 $employee = new Employee( $user_id );
1147
1148 if ( ! $employee ) {
1149 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1150 }
1151 $policies = $employee->get_leave_summary();
1152
1153 foreach ( $policies as &$policy ) {
1154 if ( $policy->available == 0 && $policy->extra_leave > 0 ) {
1155 $policy->available = - $policy->extra_leave;
1156 }
1157 }
1158 $response = rest_ensure_response( $policies );
1159 $response = $this->format_collection_response( $response, $request, count( $policies ) );
1160
1161 return $response;
1162 }
1163
1164 /**
1165 * Get all leaves of a single employee
1166 *
1167 * @since 1.3.0
1168 *
1169 * @return array|WP_Error|object
1170 */
1171 public function get_leaves( WP_REST_Request $request ) {
1172 $user_id = (int) $request['user_id'];
1173 $employee = new Employee( $user_id );
1174
1175 if ( ! $employee ) {
1176 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1177 }
1178
1179 $f_year = erp_hr_get_financial_year_from_date();
1180
1181 if ( empty( $f_year ) ) {
1182 return new WP_Error( 'rest_invalid_financial_year', __( 'No financial year defined for current year.' ), [ 'status' => 404 ] );
1183 }
1184
1185 $args = [
1186 'user_id' => $user_id,
1187 'f_year' => $f_year->id,
1188 'status' => 1,
1189 'orderby' => 'created_at',
1190 'policy_id' => 0,
1191 'number' => -1,
1192 'offset' => 0,
1193 ];
1194 $leaves = erp_hr_get_leave_requests( $args );
1195
1196 $response = rest_ensure_response( $leaves['data'] );
1197 $response = $this->format_collection_response( $response, $request, $leaves['total'] );
1198
1199 return $response;
1200 }
1201
1202 /**
1203 * Create leave request
1204 *
1205 * @since 1.3.0
1206 *
1207 * @param WP_REST_Request $request
1208 *
1209 * @return array|WP_Error|object
1210 */
1211 public function create_leave( $request ) {
1212 $id = (int) $request['user_id'];
1213 $employee = new Employee( $id );
1214
1215 if ( ! $employee ) {
1216 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1217 }
1218
1219 $request_id = erp_hr_leave_insert_request(
1220 [
1221 'user_id' => $request['user_id'],
1222 'leave_policy' => $request['policy_id'],
1223 'start_date' => $request['start_date'],
1224 'end_date' => $request['end_date'],
1225 'reason' => $request['reason'],
1226 'status' => 0,
1227 ]
1228 );
1229
1230 if ( ! is_wp_error( $request_id ) ) {
1231 // notification email
1232 $emailer = wperp()->emailer->get_email( 'New_Leave_Request' );
1233
1234 if ( is_a( $emailer, '\WeDevs\ERP\Email' ) ) {
1235 $emailer->trigger( $request_id );
1236 }
1237 }
1238
1239 $response = rest_ensure_response( $request_id );
1240 $response->set_status( 201 );
1241 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $request_id ) ) );
1242
1243 return $response;
1244 }
1245
1246 /**
1247 * Get all notes of a single employee
1248 *
1249 * @since 1.3.0
1250 *
1251 * @param $request
1252 *
1253 * @return mixed|object|WP_Error|WP_REST_Response
1254 */
1255 public function get_notes( $request ) {
1256 $args = [
1257 'total' => isset( $request['perpage'] ) ? $request['perpage'] : 20,
1258 'offset' => isset( $request['offset'] ) ? $request['offset'] : 0,
1259 ];
1260
1261 $id = (int) $request['user_id'];
1262 $employee = new Employee( $id );
1263
1264 if ( ! $employee ) {
1265 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1266 }
1267
1268 $notes = $employee->get_notes( $args['total'], $args['offset'] );
1269
1270 foreach ( $notes as $note ) {
1271 $user = get_user_by( 'id', $note->comment_by );
1272 $note->comment_by_display_name = $user->display_name;
1273 $note->comment_by_avatar_url = get_avatar_url( $note->comment_by );
1274 }
1275
1276 $total = $employee->get_erp_user()->notes()->count();
1277 $response = rest_ensure_response( $notes );
1278 $response = $this->format_collection_response( $response, $request, $total );
1279
1280 return $response;
1281 }
1282
1283 /**
1284 * Create a note for employee
1285 *
1286 * @since 1.3.0
1287 *
1288 * @param $request
1289 *
1290 * @return array|mixed|WP_Error|WP_REST_Response
1291 */
1292 public function create_note( $request ) {
1293 $id = (int) $request['user_id'];
1294 $employee = new Employee( $id );
1295
1296 if ( ! $employee ) {
1297 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1298 }
1299 $note = $employee->add_note( $request['note'], null, true );
1300 $note->comment_by_avatar_url = get_avatar_url( $note->comment_by );
1301 $request->set_param( 'context', 'edit' );
1302 $response = rest_ensure_response( $note );
1303 $response->set_status( 201 );
1304 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $note['id'] ) ) );
1305
1306 return $response;
1307 }
1308
1309 /**
1310 * Delete a note
1311 *
1312 * @since 1.3.0
1313 *
1314 * @param $request
1315 *
1316 * @return WP_REST_Response|WP_Error
1317 */
1318 public function delete_note( $request ) {
1319 $employee_id = (int) $request['user_id'];
1320 $note_id = (int) $request['note_id'];
1321 $employee = new Employee( $employee_id );
1322
1323 if ( ! $employee->is_employee() ) {
1324 return new WP_Error( 'rest_employee_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
1325 }
1326 $result = $employee->delete_note( $note_id );
1327 $response = rest_ensure_response( $result );
1328
1329 return $response;
1330 }
1331
1332 /**
1333 * Get roles of an employee
1334 *
1335 * @since 1.3.0
1336 *
1337 * @param $request
1338 *
1339 * @return mixed|WP_Error|WP_REST_Response
1340 */
1341 public function get_roles( $request ) {
1342 $employee_id = (int) trim( $request['user_id'] );
1343 $employee = new Employee( $employee_id );
1344
1345 if ( ! $employee ) {
1346 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1347 }
1348 $response = rest_ensure_response( $employee->get_roles() );
1349
1350 return $response;
1351 }
1352
1353 /**
1354 * Update employee roles
1355 * accepts associative array eg. ['erp_hr_manager' => true, 'erp_crm_manager' => false ]
1356 *
1357 * @since 1.3.0
1358 *
1359 * @param $request
1360 *
1361 * @return array|mixed|WP_Error|WP_REST_Response
1362 */
1363 public function update_role( $request ) {
1364 $hr_manager_role = erp_hr_get_manager_role();
1365
1366 if ( ! current_user_can( $hr_manager_role ) ) {
1367 return new WP_Error( 'rest_invalid_user_permission', __( 'User do not have permission for the action.' ), [ 'status' => 404 ] );
1368 }
1369 $employee_id = (int) trim( $request['user_id'] );
1370 $employee = new Employee( $employee_id );
1371
1372 if ( ! $employee ) {
1373 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1374 }
1375
1376 if ( ! is_array( $request['roles'] ) || empty( $request['roles'] ) ) {
1377 return new WP_Error( 'rest_performance_invalid_permission_type', __( 'Invalid role format' ), [ 'status' => 400 ] );
1378 }
1379
1380 $roles = $employee->update_role( $request['roles'] )->get_roles();
1381 $request->set_param( 'context', 'edit' );
1382 $response = rest_ensure_response( $roles );
1383 $response->set_status( 201 );
1384 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $employee_id ) ) );
1385
1386 return $response;
1387 }
1388
1389 /**
1390 * Get announcement of an employee
1391 *
1392 * @since 1.3.0
1393 *
1394 * @param $request
1395 *
1396 * @return mixed|object|WP_Error|WP_REST_Response
1397 */
1398 public function get_announcements( $request ) {
1399 $user_id = (int) $request['user_id'];
1400 $employee = new Employee( $user_id );
1401
1402 if ( ! $employee ) {
1403 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1404 }
1405 $announcements = $employee->get_announcements();
1406
1407 foreach ( $announcements as $announcement ) {
1408 $user = get_user_by( 'id', $announcement['post_author'] );
1409 $announcement['post_author'] = $user->data->display_name;
1410 }
1411
1412 $response = rest_ensure_response( $announcements );
1413 $response = $this->format_collection_response( $response, $request, count( $announcements ) );
1414
1415 return $response;
1416 }
1417
1418 /**
1419 * Terminate the employee
1420 *
1421 * @since 1.3.0
1422 *
1423 * @param $request
1424 *
1425 * @return mixed|WP_Error|WP_REST_Response
1426 */
1427 public function create_terminate( $request ) {
1428 $user_id = (int) $request['user_id'];
1429 $employee = new Employee( $user_id );
1430
1431 if ( ! $employee ) {
1432 return new WP_Error( 'rest_invalid_employee_id', __( 'Invalid Employee id.' ), [ 'status' => 404 ] );
1433 }
1434
1435 $employee_id = isset( $request['user_id'] ) ? intval( $request['user_id'] ) : 0;
1436 $terminate_date = ( empty( $request['terminate_date'] ) ) ? current_time( 'mysql' ) : $request['terminate_date'];
1437 $termination_type = isset( $request['termination_type'] ) ? $request['termination_type'] : '';
1438 $termination_reason = isset( $request['termination_reason'] ) ? $request['termination_reason'] : '';
1439 $eligible_for_rehire = isset( $request['eligible_for_rehire'] ) ? $request['eligible_for_rehire'] : '';
1440
1441 $fields = [
1442 'employee_id' => $employee_id,
1443 'terminate_date' => $terminate_date,
1444 'termination_type' => $termination_type,
1445 'termination_reason' => $termination_reason,
1446 'eligible_for_rehire' => $eligible_for_rehire,
1447 ];
1448 $result = $employee->terminate( $fields );
1449
1450 if ( is_wp_error( $result ) ) {
1451 return new WP_Error( 'rest_insufficient_data', $result->get_error_messages(), [ 'status' => 401 ] );
1452 }
1453
1454 $request->set_param( 'context', 'edit' );
1455 $response = rest_ensure_response( true );
1456 $response->set_status( 201 );
1457 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $user_id ) ) );
1458
1459 return $response;
1460 }
1461
1462 /**
1463 * Prepare a single user output for response
1464 *
1465 * @param array $additional_fields
1466 *
1467 * @return mixed|object|WP_REST_Response
1468 */
1469 public function prepare_item_for_response( Employee $item, WP_REST_Request $request = null, $additional_fields = [] ) {
1470 $default = [
1471 'user_id' => '',
1472 'employee_id' => '',
1473 'first_name' => '',
1474 'middle_name' => '',
1475 'last_name' => '',
1476 'full_name' => '',
1477 'location' => '',
1478 'date_of_birth' => '',
1479 'pay_rate' => '',
1480 'pay_type' => '',
1481 'hiring_source' => '',
1482 'hiring_date' => '',
1483 'type' => '',
1484 'status' => '',
1485 'other_email' => '',
1486 'phone' => '',
1487 'work_phone' => '',
1488 'mobile' => '',
1489 'blood_group' => '',
1490 'address' => '',
1491 'gender' => '',
1492 'marital_status' => '',
1493 'nationality' => '',
1494 'driving_license' => '',
1495 'hobbies' => '',
1496 'user_url' => '',
1497 'description' => '',
1498 'street_1' => '',
1499 'street_2' => '',
1500 'city' => '',
1501 'country' => '',
1502 'state' => '',
1503 'postal_code' => '',
1504 ];
1505
1506 $data = wp_parse_args( $item->get_data( [], true ), $default );
1507
1508 if ( isset( $request['include'] ) ) {
1509 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
1510
1511 if ( in_array( 'department', $include_params ) && ! empty( $item->get_department() ) ) {
1512 $data['department'] = Department::find( $item->get_department() );
1513 }
1514
1515 if ( in_array( 'designation', $include_params ) && ! empty( $item->get_designation() ) ) {
1516 $data['designation'] = Designation::find( $item->get_designation() );
1517 }
1518
1519 if ( in_array( 'reporting_to', $include_params ) && $item->get_reporting_to() ) {
1520 $reporting_to = new Employee( $item->get_reporting_to() );
1521
1522 if ( $reporting_to->is_employee() ) {
1523 $data['reporting_to'] = $this->prepare_item_for_response( $reporting_to );
1524 }
1525 }
1526
1527 if ( in_array( 'avatar', $include_params ) ) {
1528 $data['avatar_url'] = $item->get_avatar_url( 80 );
1529 }
1530
1531 if ( in_array( 'roles', $include_params ) ) {
1532 $data['roles'] = $item->get_roles();
1533 }
1534 }
1535
1536 $data = array_merge( $data, $additional_fields );
1537
1538 // Wrap the data in a response object
1539 $response = rest_ensure_response( $data );
1540
1541 $response = $this->add_links( $response, $item );
1542
1543 return $response;
1544 }
1545
1546 /**
1547 * Prepare a single item for create or update
1548 *
1549 * @param WP_REST_Request $request request object
1550 *
1551 * @return array $prepared_item
1552 */
1553 protected function prepare_item_for_database( $request ) {
1554 $prepared_item = [];
1555
1556 // required arguments.
1557 if ( isset( $request['first_name'] ) ) {
1558 $prepared_item['personal']['first_name'] = $request['first_name'];
1559 }
1560
1561 if ( isset( $request['last_name'] ) ) {
1562 $prepared_item['personal']['last_name'] = $request['last_name'];
1563 }
1564
1565 if ( isset( $request['employee_id'] ) ) {
1566 $prepared_item['work']['employee_id'] = $request['employee_id'];
1567 }
1568
1569 if ( isset( $request['email'] ) ) {
1570 $prepared_item['user_email'] = $request['email'];
1571 }
1572
1573 // optional arguments.
1574 if ( isset( $request['user_id'] ) ) {
1575 $prepared_item['user_id'] = absint( $request['user_id'] );
1576 }
1577
1578 if ( isset( $request['middle_name'] ) ) {
1579 $prepared_item['personal']['middle_name'] = $request['middle_name'];
1580 }
1581
1582 if ( isset( $request['designation'] ) ) {
1583 $prepared_item['work']['designation'] = $request['designation'];
1584 }
1585
1586 if ( isset( $request['department'] ) ) {
1587 $prepared_item['work']['department'] = $request['department'];
1588 }
1589
1590 if ( isset( $request['reporting_to'] ) ) {
1591 $prepared_item['work']['reporting_to'] = $request['reporting_to'];
1592 }
1593
1594 if ( isset( $request['location'] ) ) {
1595 $prepared_item['work']['location'] = $request['location'];
1596 }
1597
1598 if ( isset( $request['hiring_source'] ) ) {
1599 $prepared_item['work']['hiring_source'] = $request['hiring_source'];
1600 }
1601
1602 if ( isset( $request['hiring_date'] ) ) {
1603 $prepared_item['work']['hiring_date'] = $request['hiring_date'];
1604 }
1605
1606 if ( isset( $request['date_of_birth'] ) ) {
1607 $prepared_item['work']['date_of_birth'] = $request['date_of_birth'];
1608 }
1609
1610 if ( isset( $request['pay_rate'] ) ) {
1611 $prepared_item['work']['pay_rate'] = $request['pay_rate'];
1612 }
1613
1614 if ( isset( $request['pay_type'] ) ) {
1615 $prepared_item['work']['pay_type'] = $request['pay_type'];
1616 }
1617
1618 if ( isset( $request['type'] ) ) {
1619 $prepared_item['work']['type'] = $request['type'];
1620 }
1621
1622 if ( isset( $request['status'] ) ) {
1623 $prepared_item['work']['status'] = $request['status'];
1624 }
1625
1626 if ( isset( $request['other_email'] ) ) {
1627 $prepared_item['personal']['other_email'] = $request['other_email'];
1628 }
1629
1630 if ( isset( $request['phone'] ) ) {
1631 $prepared_item['personal']['phone'] = $request['phone'];
1632 }
1633
1634 if ( isset( $request['work_phone'] ) ) {
1635 $prepared_item['personal']['work_phone'] = $request['work_phone'];
1636 }
1637
1638 if ( isset( $request['mobile'] ) ) {
1639 $prepared_item['personal']['mobile'] = $request['mobile'];
1640 }
1641
1642 if ( isset( $request['blood_group'] ) ) {
1643 $prepared_item['personal']['blood_group'] = $request['blood_group'];
1644 }
1645
1646 if ( isset( $request['address'] ) ) {
1647 $prepared_item['personal']['address'] = $request['address'];
1648 }
1649
1650 if ( isset( $request['gender'] ) ) {
1651 $prepared_item['personal']['gender'] = $request['gender'];
1652 }
1653
1654 if ( isset( $request['marital_status'] ) ) {
1655 $prepared_item['personal']['marital_status'] = $request['marital_status'];
1656 }
1657
1658 if ( isset( $request['nationality'] ) ) {
1659 $prepared_item['personal']['nationality'] = $request['nationality'];
1660 }
1661
1662 if ( isset( $request['driving_license'] ) ) {
1663 $prepared_item['personal']['driving_license'] = $request['driving_license'];
1664 }
1665
1666 if ( isset( $request['hobbies'] ) ) {
1667 $prepared_item['personal']['hobbies'] = $request['hobbies'];
1668 }
1669
1670 if ( isset( $request['user_url'] ) ) {
1671 $prepared_item['personal']['user_url'] = $request['user_url'];
1672 }
1673
1674 if ( isset( $request['description'] ) ) {
1675 $prepared_item['personal']['description'] = $request['description'];
1676 }
1677
1678 if ( isset( $request['street_1'] ) ) {
1679 $prepared_item['personal']['street_1'] = $request['street_1'];
1680 }
1681
1682 if ( isset( $request['street_2'] ) ) {
1683 $prepared_item['personal']['street_2'] = $request['street_2'];
1684 }
1685
1686 if ( isset( $request['city'] ) ) {
1687 $prepared_item['personal']['city'] = $request['city'];
1688 }
1689
1690 if ( isset( $request['country'] ) ) {
1691 $prepared_item['personal']['country'] = $request['country'];
1692 }
1693
1694 if ( isset( $request['state'] ) ) {
1695 $prepared_item['personal']['state'] = $request['state'];
1696 }
1697
1698 if ( isset( $request['postal_code'] ) ) {
1699 $prepared_item['personal']['postal_code'] = $request['postal_code'];
1700 }
1701
1702 if ( isset( $request['photo_id'] ) ) {
1703 $prepared_item['personal']['photo_id'] = $request['photo_id'];
1704 }
1705
1706 return $prepared_item;
1707 }
1708
1709 /**
1710 * Get the query params for collections.
1711 *
1712 * @return array
1713 */
1714 public function get_collection_params() {
1715 return [
1716 'context' => $this->get_context_param(),
1717 'page' => [
1718 'description' => __( 'Current page of the collection.' ),
1719 'type' => 'integer',
1720 'default' => 1,
1721 'sanitize_callback' => 'absint',
1722 'validate_callback' => 'rest_validate_request_arg',
1723 'minimum' => 1,
1724 ],
1725 'per_page' => [
1726 'description' => __( 'Maximum number of items to be returned in result set.' ),
1727 'type' => 'integer',
1728 'default' => 20,
1729 'minimum' => 1,
1730 'maximum' => 100,
1731 'sanitize_callback' => 'absint',
1732 'validate_callback' => 'rest_validate_request_arg',
1733 ],
1734 'search' => [
1735 'description' => __( 'Limit results to those matching a string.' ),
1736 'type' => 'string',
1737 'sanitize_callback' => 'sanitize_text_field',
1738 'validate_callback' => 'rest_validate_request_arg',
1739 ],
1740 ];
1741 }
1742 }
1743