PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
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-activities-controller.php

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

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