| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\API; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use WP_REST_Response; |
| 7 |
use WP_REST_Server; |
| 8 |
|
| 9 |
class ContactsGroupsController extends REST_Controller { |
| 10 |
|
| 11 |
/** |
| 12 |
* Endpoint namespace. |
| 13 |
* |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
protected $namespace = 'erp/v1'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Route base. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $rest_base = 'crm/contacts/groups'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Register the routes for the objects of the controller. |
| 27 |
*/ |
| 28 |
public function register_routes() { |
| 29 |
register_rest_route( $this->namespace, '/' . $this->rest_base, [ |
| 30 |
[ |
| 31 |
'methods' => WP_REST_Server::READABLE, |
| 32 |
'callback' => [ $this, 'get_groups' ], |
| 33 |
'args' => $this->get_collection_params(), |
| 34 |
'permission_callback' => function ( $request ) { |
| 35 |
return current_user_can( 'erp_crm_manage_groups' ); |
| 36 |
}, |
| 37 |
], |
| 38 |
[ |
| 39 |
'methods' => WP_REST_Server::CREATABLE, |
| 40 |
'callback' => [ $this, 'create_group' ], |
| 41 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 42 |
'permission_callback' => function ( $request ) { |
| 43 |
return current_user_can( 'erp_crm_create_groups' ); |
| 44 |
}, |
| 45 |
], |
| 46 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 47 |
] ); |
| 48 |
|
| 49 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [ |
| 50 |
[ |
| 51 |
'methods' => WP_REST_Server::READABLE, |
| 52 |
'callback' => [ $this, 'get_group' ], |
| 53 |
'permission_callback' => function ( $request ) { |
| 54 |
return current_user_can( 'erp_crm_manage_groups' ); |
| 55 |
}, |
| 56 |
], |
| 57 |
[ |
| 58 |
'methods' => WP_REST_Server::EDITABLE, |
| 59 |
'callback' => [ $this, 'update_group' ], |
| 60 |
'permission_callback' => function ( $request ) { |
| 61 |
return current_user_can( 'erp_crm_create_groups' ); |
| 62 |
}, |
| 63 |
], |
| 64 |
[ |
| 65 |
'methods' => WP_REST_Server::DELETABLE, |
| 66 |
'callback' => [ $this, 'delete_group' ], |
| 67 |
'permission_callback' => function ( $request ) { |
| 68 |
return current_user_can( 'erp_crm_delete_groups' ); |
| 69 |
}, |
| 70 |
], |
| 71 |
] ); |
| 72 |
|
| 73 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)' . '/subscribes', [ |
| 74 |
[ |
| 75 |
'methods' => WP_REST_Server::READABLE, |
| 76 |
'callback' => [ $this, 'get_subscribed_contacts' ], |
| 77 |
'args' => $this->get_collection_params(), |
| 78 |
'permission_callback' => function ( $request ) { |
| 79 |
return current_user_can( 'erp_crm_manage_groups' ); |
| 80 |
}, |
| 81 |
], |
| 82 |
[ |
| 83 |
'methods' => WP_REST_Server::CREATABLE, |
| 84 |
'callback' => [ $this, 'subscribe_contact' ], |
| 85 |
'permission_callback' => function ( $request ) { |
| 86 |
return current_user_can( 'erp_crm_manage_groups' ); |
| 87 |
}, |
| 88 |
], |
| 89 |
] ); |
| 90 |
|
| 91 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<group_id>[\d]+)' . '/subscribes' . '/(?P<contact_id>[\d]+)', [ |
| 92 |
[ |
| 93 |
'methods' => WP_REST_Server::DELETABLE, |
| 94 |
'callback' => [ $this, 'delete_subscribed_contact' ], |
| 95 |
'permission_callback' => function ( $request ) { |
| 96 |
return current_user_can( 'erp_crm_manage_groups' ); |
| 97 |
}, |
| 98 |
], |
| 99 |
] ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get a collection of groups |
| 104 |
* |
| 105 |
* @param WP_REST_Request $request |
| 106 |
* |
| 107 |
* @return WP_Error|WP_REST_Response |
| 108 |
*/ |
| 109 |
public function get_groups( $request ) { |
| 110 |
$args = [ |
| 111 |
'number' => $request['per_page'], |
| 112 |
'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ), |
| 113 |
'orderby' => $request['orderby'], |
| 114 |
'order' => $request['order'] |
| 115 |
]; |
| 116 |
|
| 117 |
$items = erp_crm_get_contact_groups( $args ); |
| 118 |
$total_items = erp_crm_get_contact_groups( [ 'count' => true ] ); |
| 119 |
|
| 120 |
$formated_items = []; |
| 121 |
|
| 122 |
foreach ( $items as $item ) { |
| 123 |
$data = $this->prepare_item_for_response( $item, $request ); |
| 124 |
$formated_items[] = $this->prepare_response_for_collection( $data ); |
| 125 |
} |
| 126 |
|
| 127 |
$response = rest_ensure_response( $formated_items ); |
| 128 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 129 |
|
| 130 |
return $response; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get a specific group |
| 135 |
* |
| 136 |
* @param WP_REST_Request $request |
| 137 |
* |
| 138 |
* @return WP_Error|WP_REST_Response |
| 139 |
*/ |
| 140 |
public function get_group( $request ) { |
| 141 |
$id = (int) $request['id']; |
| 142 |
$item = (object) erp_crm_get_contact_group_by_id( $id ); |
| 143 |
|
| 144 |
if ( empty( $id ) || empty( $item->id ) ) { |
| 145 |
return new WP_Error( 'rest_group_invalid_id', __( 'Invalid resource id.', 'erp' ), [ 'status' => 404 ] ); |
| 146 |
} |
| 147 |
|
| 148 |
$item = $this->prepare_item_for_response( $item, $request ); |
| 149 |
$response = rest_ensure_response( $item ); |
| 150 |
|
| 151 |
return $response; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Create a group |
| 156 |
* |
| 157 |
* @param WP_REST_Request $request |
| 158 |
* |
| 159 |
* @return WP_Error|WP_REST_Request |
| 160 |
*/ |
| 161 |
public function create_group( $request ) { |
| 162 |
$data = [ |
| 163 |
'name' => $request['name'], |
| 164 |
'description' => $request['description'], |
| 165 |
]; |
| 166 |
|
| 167 |
$group = erp_crm_save_contact_group( $data ); |
| 168 |
|
| 169 |
return new WP_REST_Response( $group, 201 ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Update a group |
| 174 |
* |
| 175 |
* @param WP_REST_Request $request |
| 176 |
* |
| 177 |
* @return WP_Error|WP_REST_Request |
| 178 |
*/ |
| 179 |
public function update_group( $request ) { |
| 180 |
$data = [ |
| 181 |
'id' => intval( $request['id'] ), |
| 182 |
'name' => $request['name'], |
| 183 |
'description' => $request['description'], |
| 184 |
]; |
| 185 |
|
| 186 |
$group = erp_crm_save_contact_group( $data ); |
| 187 |
|
| 188 |
return new WP_REST_Response( $group_id, 201 ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Delete a group |
| 193 |
* |
| 194 |
* @param WP_REST_Request $request |
| 195 |
* |
| 196 |
* @return WP_Error|WP_REST_Request |
| 197 |
*/ |
| 198 |
public function delete_group( $request ) { |
| 199 |
$group_id = (int) $request['id']; |
| 200 |
|
| 201 |
erp_crm_contact_group_delete( $group_id ); |
| 202 |
|
| 203 |
return new WP_REST_Response( true, 204 ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get a collection of subscribed contacts |
| 208 |
* |
| 209 |
* @param WP_REST_Request $request |
| 210 |
* |
| 211 |
* @return WP_Error|WP_REST_Response |
| 212 |
*/ |
| 213 |
public function get_subscribed_contacts( $request ) { |
| 214 |
$args = [ |
| 215 |
'group_id' => intval( $request['id'] ), |
| 216 |
'number' => $request['per_page'], |
| 217 |
'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ), |
| 218 |
]; |
| 219 |
|
| 220 |
$subscribers = erp_crm_get_subscriber_contact( $args ); |
| 221 |
$args['count'] = true; |
| 222 |
$total_items = erp_crm_get_subscriber_contact( $args ); |
| 223 |
|
| 224 |
$item_ids = []; |
| 225 |
|
| 226 |
foreach ( $subscribers as $subscriber ) { |
| 227 |
$item_ids[] = $subscriber->user_id; |
| 228 |
} |
| 229 |
|
| 230 |
$items = []; |
| 231 |
|
| 232 |
if ( ! empty( $item_ids ) ) { |
| 233 |
$items = erp_get_people_by( 'id', $item_ids ); |
| 234 |
} |
| 235 |
|
| 236 |
$contacts_controller = new ContactsController(); |
| 237 |
|
| 238 |
$formated_items = []; |
| 239 |
|
| 240 |
foreach ( $items as $item ) { |
| 241 |
$data = $contacts_controller->prepare_item_for_response( $item, $request ); |
| 242 |
$formated_items[] = $this->prepare_response_for_collection( $data ); |
| 243 |
} |
| 244 |
|
| 245 |
$response = rest_ensure_response( $formated_items ); |
| 246 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 247 |
|
| 248 |
return $response; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Subscribe a contact to a group |
| 253 |
* |
| 254 |
* @param WP_REST_Request $request |
| 255 |
* |
| 256 |
* @return WP_Error|WP_REST_Request |
| 257 |
*/ |
| 258 |
public function subscribe_contact( $request ) { |
| 259 |
if ( ! isset( $request['contact_ids'] ) || empty( $request['contact_ids'] ) ) { |
| 260 |
return new WP_Error( 'rest_group_invalid_contact_ids', __( 'Required contact ids.', 'erp' ), [ 'status' => 400 ] ); |
| 261 |
} |
| 262 |
|
| 263 |
$contact_ids = explode( ',', str_replace( ' ', '', $request['contact_ids'] ) ); |
| 264 |
|
| 265 |
foreach ( $contact_ids as $contact_id ) { |
| 266 |
$data = [ |
| 267 |
'group_id' => intval( $request['id'] ), |
| 268 |
'user_id' => intval( $contact_id ), |
| 269 |
]; |
| 270 |
|
| 271 |
$result = erp_crm_create_new_contact_subscriber( $data ); |
| 272 |
} |
| 273 |
|
| 274 |
return new WP_REST_Response( true, 201 ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Unsubscribe a contact from a group |
| 279 |
* |
| 280 |
* @param WP_REST_Request $request |
| 281 |
* |
| 282 |
* @return WP_Error|WP_REST_Request |
| 283 |
*/ |
| 284 |
public function delete_subscribed_contact( $request ) { |
| 285 |
$contact_id = intval( $request['contact_id'] ); |
| 286 |
$group_id = intval( $request['group_id'] ); |
| 287 |
|
| 288 |
erp_crm_contact_subscriber_delete( $contact_id, $group_id ); |
| 289 |
|
| 290 |
return new WP_REST_Response( true, 204 ); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Prepare a single item for create or update |
| 295 |
* |
| 296 |
* @param WP_REST_Request $request request object |
| 297 |
* |
| 298 |
* @return array $prepared_item |
| 299 |
*/ |
| 300 |
protected function prepare_item_for_database( $request ) { |
| 301 |
$prepared_item = []; |
| 302 |
|
| 303 |
// required arguments. |
| 304 |
if ( isset( $request['name'] ) ) { |
| 305 |
$prepared_item['name'] = $request['name']; |
| 306 |
} |
| 307 |
|
| 308 |
if ( isset( $request['description'] ) ) { |
| 309 |
$prepared_item['description'] = $request['description']; |
| 310 |
} |
| 311 |
|
| 312 |
// optional arguments. |
| 313 |
if ( isset( $request['id'] ) ) { |
| 314 |
$prepared_item['id'] = absint( $request['id'] ); |
| 315 |
} |
| 316 |
|
| 317 |
return $prepared_item; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Prepare a single user output for response |
| 322 |
* |
| 323 |
* @param object $item |
| 324 |
* @param WP_REST_Request $request request object |
| 325 |
* |
| 326 |
* @return WP_REST_Response $response response data |
| 327 |
*/ |
| 328 |
public function prepare_item_for_response( $item, $request ) { |
| 329 |
$data = [ |
| 330 |
'id' => (int) $item->id, |
| 331 |
'name' => $item->name, |
| 332 |
'description' => $item->description, |
| 333 |
]; |
| 334 |
|
| 335 |
// Wrap the data in a response object |
| 336 |
$response = rest_ensure_response( $data ); |
| 337 |
|
| 338 |
$response = $this->add_links( $response, $item ); |
| 339 |
|
| 340 |
return $response; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Get the User's schema, conforming to JSON Schema |
| 345 |
* |
| 346 |
* @return array |
| 347 |
*/ |
| 348 |
public function get_item_schema() { |
| 349 |
$schema = [ |
| 350 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 351 |
'title' => 'contact group', |
| 352 |
'type' => 'object', |
| 353 |
'properties' => [ |
| 354 |
'id' => [ |
| 355 |
'description' => __( 'Unique identifier for the resource.', 'erp' ), |
| 356 |
'type' => 'integer', |
| 357 |
'context' => [ 'embed', 'view', 'edit' ], |
| 358 |
'readonly' => true, |
| 359 |
], |
| 360 |
'name' => [ |
| 361 |
'description' => __( 'Name for the resource.', 'erp' ), |
| 362 |
'type' => 'string', |
| 363 |
'context' => [ 'edit' ], |
| 364 |
'arg_options' => [ |
| 365 |
'sanitize_callback' => 'sanitize_text_field', |
| 366 |
], |
| 367 |
'required' => true, |
| 368 |
], |
| 369 |
], |
| 370 |
]; |
| 371 |
|
| 372 |
return $schema; |
| 373 |
} |
| 374 |
} |
| 375 |
|