| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\API; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use WP_Error; |
| 7 |
use WP_REST_Response; |
| 8 |
use WP_REST_Server; |
| 9 |
|
| 10 |
class ActivitiesController extends REST_Controller { |
| 11 |
|
| 12 |
/** |
| 13 |
* Endpoint namespace. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
protected $namespace = 'erp/v1'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Route base. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $rest_base = 'crm/activities'; |
| 25 |
|
| 26 |
public function __construct() { |
| 27 |
$this->activity_types = [ |
| 28 |
'note' => 'new_note', |
| 29 |
'log' => 'log_activity', |
| 30 |
'schedule' => 'schedule', |
| 31 |
'task' => 'tasks', |
| 32 |
'email' => 'email', |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Register the routes for the objects of the controller. |
| 38 |
*/ |
| 39 |
public function register_routes() { |
| 40 |
register_rest_route( $this->namespace, '/' . $this->rest_base, [ |
| 41 |
[ |
| 42 |
'methods' => WP_REST_Server::READABLE, |
| 43 |
'callback' => [ $this, 'get_activities' ], |
| 44 |
'args' => $this->get_collection_params(), |
| 45 |
'permission_callback' => function ( $request ) { |
| 46 |
return current_user_can( 'erp_crm_manage_activites' ); |
| 47 |
}, |
| 48 |
], |
| 49 |
[ |
| 50 |
'methods' => WP_REST_Server::CREATABLE, |
| 51 |
'callback' => [ $this, 'create_activity' ], |
| 52 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 53 |
'permission_callback' => function ( $request ) { |
| 54 |
return current_user_can( 'erp_crm_manage_activites' ); |
| 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_activity' ], |
| 64 |
'args' => [ |
| 65 |
'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
| 66 |
], |
| 67 |
'permission_callback' => function ( $request ) { |
| 68 |
return current_user_can( 'erp_crm_manage_activites' ); |
| 69 |
}, |
| 70 |
], |
| 71 |
[ |
| 72 |
'methods' => WP_REST_Server::EDITABLE, |
| 73 |
'callback' => [ $this, 'update_activity' ], |
| 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_manage_activites' ); |
| 77 |
}, |
| 78 |
], |
| 79 |
[ |
| 80 |
'methods' => WP_REST_Server::DELETABLE, |
| 81 |
'callback' => [ $this, 'delete_activity' ], |
| 82 |
'permission_callback' => function ( $request ) { |
| 83 |
return current_user_can( 'erp_crm_manage_activites' ); |
| 84 |
}, |
| 85 |
], |
| 86 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 87 |
] ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get a collection of activities |
| 92 |
* |
| 93 |
* @param WP_REST_Request $request |
| 94 |
* |
| 95 |
* @return WP_Error|WP_REST_Response |
| 96 |
*/ |
| 97 |
public function get_activities( $request ) { |
| 98 |
$args = [ |
| 99 |
'type' => $this->activity_types[ $request['type'] ], |
| 100 |
'limit' => $request['per_page'], |
| 101 |
'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ), |
| 102 |
]; |
| 103 |
|
| 104 |
$items = erp_crm_get_feed_activity( $args ); |
| 105 |
$items = erp_array_to_object( $items ); |
| 106 |
|
| 107 |
$args['count'] = true; |
| 108 |
$total_items = erp_crm_get_feed_activity( $args ); |
| 109 |
|
| 110 |
$formated_items = []; |
| 111 |
|
| 112 |
foreach ( $items as $item ) { |
| 113 |
$additional_fields = []; |
| 114 |
|
| 115 |
$data = $this->prepare_item_for_response( $item, $request, $additional_fields ); |
| 116 |
$formated_items[] = $this->prepare_response_for_collection( $data ); |
| 117 |
} |
| 118 |
|
| 119 |
$response = rest_ensure_response( $formated_items ); |
| 120 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 121 |
|
| 122 |
return $response; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get a specific activity |
| 127 |
* |
| 128 |
* @param WP_REST_Request $request |
| 129 |
* |
| 130 |
* @return WP_Error|WP_REST_Response |
| 131 |
*/ |
| 132 |
public function get_activity( $request ) { |
| 133 |
$activity_id = (int) $request['id']; |
| 134 |
$activity = (object) erp_crm_customer_get_single_activity_feed( $activity_id ); |
| 135 |
$activity = $this->prepare_item_for_response( $activity, $request ); |
| 136 |
$response = rest_ensure_response( $activity ); |
| 137 |
|
| 138 |
return $response; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Create a activity |
| 143 |
* |
| 144 |
* @param WP_REST_Request $request |
| 145 |
* |
| 146 |
* @return WP_Error|WP_REST_Request |
| 147 |
*/ |
| 148 |
public function create_activity( $request ) { |
| 149 |
$item = $this->prepare_item_for_database( $request ); |
| 150 |
|
| 151 |
switch ( $item['type'] ) { |
| 152 |
case 'note': |
| 153 |
case 'email': |
| 154 |
$item['type'] = $this->activity_types[ $item['type'] ]; |
| 155 |
$saved_data = erp_crm_save_customer_feed_data( $item ); |
| 156 |
break; |
| 157 |
|
| 158 |
case 'log': |
| 159 |
$extra_data = [ |
| 160 |
'invite_contact' => $item['invited_user'], |
| 161 |
]; |
| 162 |
$item['start_date'] = gmdate( 'Y-m-d H:i:s', strtotime( $item['start_date'] ) ); |
| 163 |
$item['extra'] = base64_encode( wp_json_encode( $extra_data ) ); |
| 164 |
|
| 165 |
$item['type'] = $this->activity_types[ $item['type'] ]; |
| 166 |
$saved_data = erp_crm_save_customer_feed_data( $item ); |
| 167 |
break; |
| 168 |
|
| 169 |
case 'schedule': |
| 170 |
$item['type'] = $this->activity_types[ $item['type'] ]; |
| 171 |
|
| 172 |
$extra_data = [ |
| 173 |
'schedule_title' => $item['email_subject'], |
| 174 |
'all_day' => isset( $item['all_day'] ) ? (string) $item['all_day'] : 'false', |
| 175 |
'allow_notification' => isset( $item['allow_notification'] ) ? (string) $item['allow_notification'] : 'false', |
| 176 |
'invite_contact' => ! empty( $item['invite_contact'] ) ? $item['invite_contact'] : [], |
| 177 |
]; |
| 178 |
|
| 179 |
if ( $item['allow_notification'] == 'true' ) { |
| 180 |
$notify_date = new DateTime( $item['start_date'] ); |
| 181 |
$notify_date->modify( '-' . $item['notification_time_interval'] . ' ' . $item['notification_time'] ); |
| 182 |
$extra_data['notification_datetime'] = $notify_date->format( 'Y-m-d H:i:s' ); |
| 183 |
} else { |
| 184 |
$extra_data['notification_datetime'] = ''; |
| 185 |
} |
| 186 |
|
| 187 |
$post_data = [ |
| 188 |
'type' => 'log_activity', |
| 189 |
'log_type' => $item['schedule_type'], |
| 190 |
'message' => $item['message'], |
| 191 |
'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( $item['start_date'] ) ), |
| 192 |
'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( $item['end_date'] ) ), |
| 193 |
'user_id' => $item['user_id'], |
| 194 |
'created_by' => $item['created_by'], |
| 195 |
'extra' => base64_encode( wp_json_encode( $extra_data ) ), |
| 196 |
]; |
| 197 |
|
| 198 |
$saved_data = erp_crm_save_customer_feed_data( $post_data ); |
| 199 |
|
| 200 |
break; |
| 201 |
|
| 202 |
case 'task': |
| 203 |
$extra_data = [ |
| 204 |
'task_title' => $item['task_title'], |
| 205 |
'invite_contact' => ! empty( $item['invite_contact'] ) ? $item['invite_contact'] : [], |
| 206 |
]; |
| 207 |
|
| 208 |
$post_data = [ |
| 209 |
'type' => 'tasks', |
| 210 |
'message' => $item['message'], |
| 211 |
'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( $item['start_date'] ) ), |
| 212 |
'user_id' => $item['user_id'], |
| 213 |
'created_by' => $item['created_by'], |
| 214 |
'extra' => base64_encode( wp_json_encode( $extra_data ) ), |
| 215 |
]; |
| 216 |
|
| 217 |
$saved_data = erp_crm_save_customer_feed_data( $post_data ); |
| 218 |
|
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
$activity = (object) erp_crm_customer_get_single_activity_feed( $saved_data['id'] ); |
| 223 |
$response = $this->prepare_item_for_response( $activity, ['id' => $saved_data['id']] ); |
| 224 |
|
| 225 |
$response->set_status( 201 ); |
| 226 |
|
| 227 |
return $response; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Update a activity |
| 232 |
* |
| 233 |
* @param WP_REST_Request $request |
| 234 |
* |
| 235 |
* @return WP_Error|WP_REST_Request |
| 236 |
*/ |
| 237 |
public function update_activity( $request ) { |
| 238 |
return new WP_REST_Response( true, 201 ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Delete a activity |
| 243 |
* |
| 244 |
* @param WP_REST_Request $request |
| 245 |
* |
| 246 |
* @return WP_Error|WP_REST_Request |
| 247 |
*/ |
| 248 |
public function delete_activity( $request ) { |
| 249 |
$activity_id = (int) $request['id']; |
| 250 |
|
| 251 |
erp_crm_customer_delete_activity_feed( $activity_id ); |
| 252 |
|
| 253 |
return new WP_REST_Response( true, 204 ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Prepare a single item for create or update |
| 258 |
* |
| 259 |
* @param WP_REST_Request $request request object |
| 260 |
* |
| 261 |
* @return array $prepared_item |
| 262 |
*/ |
| 263 |
protected function prepare_item_for_database( $request ) { |
| 264 |
$prepared_item = [ |
| 265 |
'created_by' => get_current_user_id(), |
| 266 |
]; |
| 267 |
|
| 268 |
if ( isset( $request['id'] ) ) { |
| 269 |
$prepared_item['id'] = absint( $request['id'] ); |
| 270 |
} |
| 271 |
|
| 272 |
if ( isset( $request['type'] ) ) { |
| 273 |
$prepared_item['type'] = $request['type']; |
| 274 |
} |
| 275 |
|
| 276 |
if ( isset( $request['contact_id'] ) ) { |
| 277 |
$prepared_item['user_id'] = absint( $request['contact_id'] ); |
| 278 |
} |
| 279 |
|
| 280 |
switch ( $request['type'] ) { |
| 281 |
case 'note': |
| 282 |
if ( isset( $request['content'] ) ) { |
| 283 |
$prepared_item['message'] = $request['content']; |
| 284 |
} |
| 285 |
|
| 286 |
break; |
| 287 |
|
| 288 |
case 'email': |
| 289 |
if ( isset( $request['subject'] ) ) { |
| 290 |
$prepared_item['email_subject'] = $request['subject']; |
| 291 |
$prepared_item['message'] = $request['body']; |
| 292 |
} |
| 293 |
break; |
| 294 |
|
| 295 |
case 'log': |
| 296 |
if ( isset( $request['date_time'] ) ) { |
| 297 |
$prepared_item['start_date'] = $request['date_time']; |
| 298 |
} |
| 299 |
|
| 300 |
if ( isset( $request['content'] ) ) { |
| 301 |
$prepared_item['message'] = $request['content']; |
| 302 |
} |
| 303 |
|
| 304 |
if ( isset( $request['log_type'] ) ) { |
| 305 |
$prepared_item['log_type'] = $request['log_type']; |
| 306 |
|
| 307 |
if ( $request['log_type'] == 'meeting' && isset( $request['employee_ids'] ) ) { |
| 308 |
$prepared_item['invited_user'] = $request['employee_ids']; |
| 309 |
} |
| 310 |
|
| 311 |
if ( $request['log_type'] == 'email' && isset( $request['subject'] ) ) { |
| 312 |
$prepared_item['email_subject'] = $request['subject']; |
| 313 |
} |
| 314 |
} |
| 315 |
break; |
| 316 |
|
| 317 |
case 'schedule': |
| 318 |
if ( isset( $request['title'] ) ) { |
| 319 |
$prepared_item['email_subject'] = $request['title']; |
| 320 |
} |
| 321 |
|
| 322 |
if ( isset( $request['start_date_time'] ) ) { |
| 323 |
$prepared_item['start_date'] = $request['start_date_time']; |
| 324 |
} |
| 325 |
|
| 326 |
if ( isset( $request['end_date_time'] ) ) { |
| 327 |
$prepared_item['end_date'] = $request['end_date_time']; |
| 328 |
} |
| 329 |
|
| 330 |
if ( isset( $request['all_day'] ) ) { |
| 331 |
$prepared_item['all_day'] = $request['all_day']; |
| 332 |
} |
| 333 |
|
| 334 |
if ( isset( $request['content'] ) ) { |
| 335 |
$prepared_item['message'] = $request['content']; |
| 336 |
} |
| 337 |
|
| 338 |
if ( isset( $request['employee_ids'] ) ) { |
| 339 |
$prepared_item['invite_contact'] = explode( ',', str_replace( ' ', '', $request['employee_ids'] ) ); |
| 340 |
} |
| 341 |
|
| 342 |
if ( isset( $request['schedule_type'] ) ) { |
| 343 |
$prepared_item['schedule_type'] = $request['schedule_type']; |
| 344 |
} |
| 345 |
|
| 346 |
if ( isset( $request['allow_notification'] ) ) { |
| 347 |
$prepared_item['allow_notification'] = $request['allow_notification']; |
| 348 |
} |
| 349 |
|
| 350 |
if ( isset( $request['notification_via'] ) ) { |
| 351 |
$prepared_item['notification_via'] = $request['notification_via']; |
| 352 |
} |
| 353 |
|
| 354 |
if ( isset( $request['notification_time'] ) ) { |
| 355 |
$prepared_item['notification_time'] = $request['notification_time']; |
| 356 |
} |
| 357 |
|
| 358 |
if ( isset( $request['notification_time_interval'] ) ) { |
| 359 |
$prepared_item['notification_time_interval'] = $request['notification_time_interval']; |
| 360 |
} |
| 361 |
break; |
| 362 |
|
| 363 |
case 'task': |
| 364 |
if ( isset( $request['title'] ) ) { |
| 365 |
$prepared_item['task_title'] = $request['title']; |
| 366 |
} |
| 367 |
|
| 368 |
if ( isset( $request['employee_ids'] ) ) { |
| 369 |
$prepared_item['invite_contact'] = explode( ',', str_replace( ' ', '', $request['employee_ids'] ) ); |
| 370 |
} |
| 371 |
|
| 372 |
if ( isset( $request['date_time'] ) ) { |
| 373 |
$prepared_item['start_date'] = $request['date_time']; |
| 374 |
} |
| 375 |
|
| 376 |
if ( isset( $request['content'] ) ) { |
| 377 |
$prepared_item['message'] = $request['content']; |
| 378 |
} |
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
return $prepared_item; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Prepare a single user output for response |
| 387 |
* |
| 388 |
* @param object $item |
| 389 |
* @param WP_REST_Request $request request object |
| 390 |
* @param array $additional_fields (optional) |
| 391 |
* |
| 392 |
* @return WP_REST_Response $response response data |
| 393 |
*/ |
| 394 |
public function prepare_item_for_response( $item, $request, $additional_fields = [] ) { |
| 395 |
$types = array_flip( $this->activity_types ); |
| 396 |
|
| 397 |
// Convert to a standard type |
| 398 |
$item->type = $types[ $item->type ]; |
| 399 |
|
| 400 |
$common_fields = [ |
| 401 |
'id' => (int) $item->id, |
| 402 |
'type' => $item->type, |
| 403 |
'contact_id' => (int) $item->user_id, |
| 404 |
'created_by' => $item->created_by, |
| 405 |
]; |
| 406 |
|
| 407 |
switch ( $item->type ) { |
| 408 |
case 'note': |
| 409 |
$fields = [ |
| 410 |
'content' => $item->message, |
| 411 |
]; |
| 412 |
|
| 413 |
$fields = array_merge( $common_fields, $fields ); |
| 414 |
break; |
| 415 |
|
| 416 |
case 'email': |
| 417 |
$fields = [ |
| 418 |
'subject' => $item->email_subject, |
| 419 |
'body' => $item->message, |
| 420 |
]; |
| 421 |
|
| 422 |
$fields = array_merge( $common_fields, $fields ); |
| 423 |
break; |
| 424 |
|
| 425 |
case 'log': |
| 426 |
$fields = [ |
| 427 |
'date_time' => $item->start_date, |
| 428 |
'log_type' => $item->log_type, |
| 429 |
'content' => $item->message, |
| 430 |
]; |
| 431 |
|
| 432 |
if ( $item->log_type == 'meeting' ) { |
| 433 |
$fields['employee_ids'] = wp_list_pluck( $item->extra['invited_user'], 'id' ); |
| 434 |
} |
| 435 |
|
| 436 |
if ( $item->log_type == 'email' ) { |
| 437 |
$fields['subject'] = $item->email_subject; |
| 438 |
} |
| 439 |
|
| 440 |
$fields = array_merge( $common_fields, $fields ); |
| 441 |
break; |
| 442 |
|
| 443 |
case 'schedule': |
| 444 |
$fields = [ |
| 445 |
'title' => $item->email_subject, |
| 446 |
'start_date_time' => $item->start_date, |
| 447 |
'end_date_time' => $item->end_date, |
| 448 |
'all_day' => $item->extra['all_day'], |
| 449 |
'content' => $item->message, |
| 450 |
'employee_ids' => wp_list_pluck( $item->extra['invited_user'], 'id' ), |
| 451 |
'schedule_type' => $item->extra['schedule_type'], |
| 452 |
'allow_notification' => $item->extra['allow_notification'], |
| 453 |
'notification_datetime' => $item->extra['notification_datetime'], |
| 454 |
]; |
| 455 |
|
| 456 |
$fields = array_merge( $common_fields, $fields ); |
| 457 |
break; |
| 458 |
|
| 459 |
case 'task': |
| 460 |
$fields = [ |
| 461 |
'title' => $item->extra['task_title'], |
| 462 |
'employee_ids' => wp_list_pluck( $item->extra['invited_user'], 'id' ), |
| 463 |
'date_time' => $item->start_date, |
| 464 |
'content' => $item->message, |
| 465 |
]; |
| 466 |
|
| 467 |
$fields = array_merge( $common_fields, $fields ); |
| 468 |
break; |
| 469 |
} |
| 470 |
|
| 471 |
// Wrap the data in a response object |
| 472 |
$response = rest_ensure_response( $fields ); |
| 473 |
|
| 474 |
$response = $this->add_links( $response, $item ); |
| 475 |
|
| 476 |
return $response; |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Get the query params for collections. |
| 481 |
* |
| 482 |
* @return array |
| 483 |
*/ |
| 484 |
public function get_collection_params() { |
| 485 |
return [ |
| 486 |
'context' => $this->get_context_param(), |
| 487 |
'type' => [ |
| 488 |
'description' => __( 'The type of activities.', 'erp' ), |
| 489 |
'type' => 'string', |
| 490 |
'default' => 'log', |
| 491 |
], |
| 492 |
'page' => [ |
| 493 |
'description' => __( 'Current page of the collection.', 'erp' ), |
| 494 |
'type' => 'integer', |
| 495 |
'default' => 1, |
| 496 |
'sanitize_callback' => 'absint', |
| 497 |
'validate_callback' => 'rest_validate_request_arg', |
| 498 |
'minimum' => 1, |
| 499 |
], |
| 500 |
'per_page' => [ |
| 501 |
'description' => __( 'Maximum number of items to be returned in result set.', 'erp' ), |
| 502 |
'type' => 'integer', |
| 503 |
'default' => 20, |
| 504 |
'minimum' => 1, |
| 505 |
'maximum' => 100, |
| 506 |
'sanitize_callback' => 'absint', |
| 507 |
'validate_callback' => 'rest_validate_request_arg', |
| 508 |
], |
| 509 |
]; |
| 510 |
} |
| 511 |
} |
| 512 |
|