| 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 Schedules_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/schedules'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Register the routes for the objects of the controller. |
| 25 |
*/ |
| 26 |
public function register_routes() { |
| 27 |
register_rest_route( $this->namespace, '/' . $this->rest_base, [ |
| 28 |
[ |
| 29 |
'methods' => WP_REST_Server::READABLE, |
| 30 |
'callback' => [ $this, 'get_schedules' ], |
| 31 |
'args' => $this->get_collection_params(), |
| 32 |
'permission_callback' => function ( $request ) { |
| 33 |
return current_user_can( 'erp_crm_manage_schedules' ); |
| 34 |
}, |
| 35 |
], |
| 36 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 37 |
] ); |
| 38 |
|
| 39 |
register_rest_route( $this->namespace, '/crm/todays-schedules', [ |
| 40 |
[ |
| 41 |
'methods' => WP_REST_Server::READABLE, |
| 42 |
'callback' => [ $this, 'get_todays_schedules' ], |
| 43 |
'args' => $this->get_collection_params(), |
| 44 |
'permission_callback' => function ( $request ) { |
| 45 |
return current_user_can( 'erp_crm_manage_schedules' ); |
| 46 |
}, |
| 47 |
], |
| 48 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 49 |
] ); |
| 50 |
|
| 51 |
register_rest_route( $this->namespace, '/crm/upcoming-schedules', [ |
| 52 |
[ |
| 53 |
'methods' => WP_REST_Server::READABLE, |
| 54 |
'callback' => [ $this, 'get_upcoming_schedules' ], |
| 55 |
'args' => $this->get_collection_params(), |
| 56 |
'permission_callback' => function ( $request ) { |
| 57 |
return current_user_can( 'erp_crm_manage_schedules' ); |
| 58 |
}, |
| 59 |
], |
| 60 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 61 |
] ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get a collection of user's schedules |
| 66 |
* |
| 67 |
* @param WP_REST_Request $request |
| 68 |
* |
| 69 |
* @return WP_Error|WP_REST_Response |
| 70 |
*/ |
| 71 |
public function get_schedules( $request ) { |
| 72 |
$args = [ |
| 73 |
'limit' => $request['per_page'], |
| 74 |
'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ), |
| 75 |
'type' => 'log_activity', |
| 76 |
]; |
| 77 |
|
| 78 |
$items = erp_crm_get_feed_activity( $args ); |
| 79 |
$items = erp_array_to_object( $items ); |
| 80 |
$total_items = erp_crm_get_feed_activity( ['count' => true] ); |
| 81 |
|
| 82 |
$formated_items = []; |
| 83 |
foreach ( $items as $item ) { |
| 84 |
$additional_fields = []; |
| 85 |
|
| 86 |
$data = $this->prepare_item_for_response( $item, $request, $additional_fields ); |
| 87 |
$formated_items[] = $this->prepare_response_for_collection( $data ); |
| 88 |
} |
| 89 |
|
| 90 |
$response = rest_ensure_response( $formated_items ); |
| 91 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 92 |
|
| 93 |
|
| 94 |
return $response; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get a collection of today's schedules |
| 99 |
* |
| 100 |
* @param WP_REST_Request $request |
| 101 |
* |
| 102 |
* @return WP_Error|WP_REST_Response |
| 103 |
*/ |
| 104 |
public function get_todays_schedules( $request ) { |
| 105 |
$items = erp_crm_get_todays_schedules_activity( 1 ); |
| 106 |
$items = erp_array_to_object( $items ); |
| 107 |
$total_items = null; |
| 108 |
|
| 109 |
$formated_items = []; |
| 110 |
foreach ( $items as $item ) { |
| 111 |
$additional_fields = []; |
| 112 |
|
| 113 |
$data = $this->prepare_item_for_response( $item, $request, $additional_fields ); |
| 114 |
$formated_items[] = $this->prepare_response_for_collection( $data ); |
| 115 |
} |
| 116 |
|
| 117 |
$response = rest_ensure_response( $formated_items ); |
| 118 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 119 |
|
| 120 |
return $response; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Get a collection of upcoming schedules |
| 125 |
* |
| 126 |
* @param WP_REST_Request $request |
| 127 |
* |
| 128 |
* @return WP_Error|WP_REST_Response |
| 129 |
*/ |
| 130 |
public function get_upcoming_schedules( $request ) { |
| 131 |
$items = erp_crm_get_next_seven_day_schedules_activities( 1 ); |
| 132 |
|
| 133 |
$items = erp_array_to_object( $items ); |
| 134 |
$total_items = null; |
| 135 |
|
| 136 |
$formated_items = []; |
| 137 |
foreach ( $items as $item ) { |
| 138 |
$additional_fields = []; |
| 139 |
|
| 140 |
$data = $this->prepare_item_for_response( $item, $request, $additional_fields ); |
| 141 |
$formated_items[] = $this->prepare_response_for_collection( $data ); |
| 142 |
} |
| 143 |
|
| 144 |
$response = rest_ensure_response( $formated_items ); |
| 145 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 146 |
|
| 147 |
return $response; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Prepare a single user output for response |
| 152 |
* |
| 153 |
* @param object $item |
| 154 |
* @param WP_REST_Request $request Request object. |
| 155 |
* @param array $additional_fields (optional) |
| 156 |
* |
| 157 |
* @return WP_REST_Response $response Response data. |
| 158 |
*/ |
| 159 |
public function prepare_item_for_response( $item, $request, $additional_fields = [] ) { |
| 160 |
// Convert to a standard type |
| 161 |
$item->type = 'log'; |
| 162 |
|
| 163 |
$common_fields = [ |
| 164 |
'id' => (int) $item->id, |
| 165 |
'type' => $item->type, |
| 166 |
'contact_id' => (int) $item->user_id, |
| 167 |
'created_by' => $item->created_by, |
| 168 |
]; |
| 169 |
|
| 170 |
$fields = [ |
| 171 |
'date_time' => $item->start_date, |
| 172 |
'log_type' => $item->log_type, |
| 173 |
'content' => $item->message, |
| 174 |
]; |
| 175 |
|
| 176 |
if ( $item->log_type == 'meeting' ) { |
| 177 |
$fields['employee_ids'] = wp_list_pluck( $item->extra['invited_user'], 'id' ); |
| 178 |
} |
| 179 |
|
| 180 |
if ( $item->log_type == 'email' ) { |
| 181 |
$fields['subject'] = $item->email_subject; |
| 182 |
} |
| 183 |
|
| 184 |
$fields = array_merge( $common_fields, $fields ); |
| 185 |
|
| 186 |
// Wrap the data in a response object |
| 187 |
$response = rest_ensure_response( $fields ); |
| 188 |
|
| 189 |
$response = $this->add_links( $response, $item ); |
| 190 |
|
| 191 |
return $response; |
| 192 |
} |
| 193 |
} |