| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\HRM\API; |
| 3 |
|
| 4 |
use WP_REST_Server; |
| 5 |
use WP_REST_Response; |
| 6 |
use WP_Error; |
| 7 |
use WeDevs\ERP\API\REST_Controller; |
| 8 |
|
| 9 |
class Leave_Policies_Controller extends REST_Controller { |
| 10 |
/** |
| 11 |
* Endpoint namespace. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $namespace = 'erp/v1'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Route base. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $rest_base = 'hrm/leaves/policies'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Activate types of leave policy. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $activate_types = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* Class constructor. |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
$this->activate_types = [ |
| 36 |
1 => 'immediately', |
| 37 |
2 => 'after_days', |
| 38 |
3 => 'manually', |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Register the routes for the objects of the controller. |
| 44 |
*/ |
| 45 |
public function register_routes() { |
| 46 |
register_rest_route( $this->namespace, '/' . $this->rest_base, [ |
| 47 |
[ |
| 48 |
'methods' => WP_REST_Server::READABLE, |
| 49 |
'callback' => [ $this, 'get_policies' ], |
| 50 |
'args' => $this->get_collection_params(), |
| 51 |
'permission_callback' => function ( $request ) { |
| 52 |
return current_user_can( 'erp_leave_manage' ); |
| 53 |
}, |
| 54 |
], |
| 55 |
[ |
| 56 |
'methods' => WP_REST_Server::CREATABLE, |
| 57 |
'callback' => [ $this, 'create_policy' ], |
| 58 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 59 |
'permission_callback' => function ( $request ) { |
| 60 |
return current_user_can( 'erp_leave_manage' ); |
| 61 |
}, |
| 62 |
], |
| 63 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 64 |
] ); |
| 65 |
|
| 66 |
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [ |
| 67 |
[ |
| 68 |
'methods' => WP_REST_Server::READABLE, |
| 69 |
'callback' => [ $this, 'get_policy' ], |
| 70 |
'args' => [ |
| 71 |
'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
| 72 |
], |
| 73 |
'permission_callback' => function ( $request ) { |
| 74 |
return current_user_can( 'erp_leave_manage' ); |
| 75 |
}, |
| 76 |
], |
| 77 |
[ |
| 78 |
'methods' => WP_REST_Server::EDITABLE, |
| 79 |
'callback' => [ $this, 'update_policy' ], |
| 80 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 81 |
'permission_callback' => function ( $request ) { |
| 82 |
return current_user_can( 'erp_leave_manage' ); |
| 83 |
}, |
| 84 |
], |
| 85 |
[ |
| 86 |
'methods' => WP_REST_Server::DELETABLE, |
| 87 |
'callback' => [ $this, 'delete_policy' ], |
| 88 |
'permission_callback' => function ( $request ) { |
| 89 |
return current_user_can( 'erp_leave_manage' ); |
| 90 |
}, |
| 91 |
], |
| 92 |
'schema' => [ $this, 'get_public_item_schema' ], |
| 93 |
] ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get a collection of policies |
| 98 |
* |
| 99 |
* @param WP_REST_Request $request |
| 100 |
* |
| 101 |
* @return WP_Error|WP_REST_Response |
| 102 |
*/ |
| 103 |
public function get_policies( $request ) { |
| 104 |
$args = [ |
| 105 |
'number' => $request['per_page'], |
| 106 |
'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ), |
| 107 |
]; |
| 108 |
|
| 109 |
$items = erp_hr_leave_get_policies( $args ); |
| 110 |
$total_items = erp_hr_count_leave_policies(); |
| 111 |
|
| 112 |
$formated_items = []; |
| 113 |
foreach ( $items as $item ) { |
| 114 |
$data = $this->prepare_item_for_response( $item, $request ); |
| 115 |
$formated_items[] = $this->prepare_response_for_collection( $data ); |
| 116 |
} |
| 117 |
|
| 118 |
$response = rest_ensure_response( $formated_items ); |
| 119 |
$response = $this->format_collection_response( $response, $request, $total_items ); |
| 120 |
|
| 121 |
return $response; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get a specific policy |
| 126 |
* |
| 127 |
* @param WP_REST_Request $request |
| 128 |
* |
| 129 |
* @return WP_Error|WP_REST_Response |
| 130 |
*/ |
| 131 |
public function get_policy( $request ) { |
| 132 |
$id = (int) $request['id']; |
| 133 |
$item = erp_hr_leave_get_policy( $id ); |
| 134 |
|
| 135 |
if ( empty( $id ) || empty( $item->id ) ) { |
| 136 |
return new WP_Error( 'rest_policy_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] ); |
| 137 |
} |
| 138 |
|
| 139 |
$item = $this->prepare_item_for_response( $item, $request ); |
| 140 |
$response = rest_ensure_response( $item ); |
| 141 |
|
| 142 |
return $response; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Create an policy |
| 147 |
* |
| 148 |
* @since 1.1.10 |
| 149 |
* @since 1.2.0 erp_hr_leave_insert_policy is now returns Leave_Policies model |
| 150 |
* |
| 151 |
* @param WP_REST_Request $request |
| 152 |
* |
| 153 |
* @return WP_Error|WP_REST_Request |
| 154 |
*/ |
| 155 |
public function create_policy( $request ) { |
| 156 |
$item = $this->prepare_item_for_database( $request ); |
| 157 |
$policy = erp_hr_leave_insert_policy( $item ); |
| 158 |
|
| 159 |
$request->set_param( 'context', 'edit' ); |
| 160 |
$response = $this->prepare_item_for_response( $policy, $request ); |
| 161 |
$response = rest_ensure_response( $response ); |
| 162 |
$response->set_status( 201 ); |
| 163 |
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) ); |
| 164 |
|
| 165 |
return $response; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Update an policy |
| 170 |
* |
| 171 |
* @param WP_REST_Request $request |
| 172 |
* |
| 173 |
* @return WP_Error|WP_REST_Request |
| 174 |
*/ |
| 175 |
public function update_policy( $request ) { |
| 176 |
$id = (int) $request['id']; |
| 177 |
|
| 178 |
$policy = erp_hr_leave_get_policy( $id ); |
| 179 |
if ( empty( $id ) || empty( $policy->id ) ) { |
| 180 |
return new WP_Error( 'rest_policy_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] ); |
| 181 |
} |
| 182 |
|
| 183 |
$item = $this->prepare_item_for_database( $request ); |
| 184 |
|
| 185 |
$policy_id = erp_hr_leave_insert_policy( $item ); |
| 186 |
$policy = erp_hr_leave_get_policy( $policy_id ); |
| 187 |
|
| 188 |
$request->set_param( 'context', 'edit' ); |
| 189 |
$response = $this->prepare_item_for_response( $policy, $request ); |
| 190 |
$response = rest_ensure_response( $response ); |
| 191 |
$response->set_status( 201 ); |
| 192 |
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) ); |
| 193 |
|
| 194 |
return $response; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Delete an policy |
| 199 |
* |
| 200 |
* @param WP_REST_Request $request |
| 201 |
* |
| 202 |
* @return WP_Error|WP_REST_Request |
| 203 |
*/ |
| 204 |
public function delete_policy( $request ) { |
| 205 |
$id = (int) $request['id']; |
| 206 |
|
| 207 |
erp_hr_leave_policy_delete( $id ); |
| 208 |
|
| 209 |
return new WP_REST_Response( true, 204 ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Prepare a single item for create or update |
| 214 |
* |
| 215 |
* @param WP_REST_Request $request Request object. |
| 216 |
* |
| 217 |
* @return array $prepared_item |
| 218 |
*/ |
| 219 |
protected function prepare_item_for_database( $request ) { |
| 220 |
$prepared_item = []; |
| 221 |
|
| 222 |
// required arguments. |
| 223 |
if ( isset( $request['name'] ) ) { |
| 224 |
$prepared_item['name'] = $request['name']; |
| 225 |
} |
| 226 |
|
| 227 |
if ( isset( $request['days'] ) ) { |
| 228 |
$prepared_item['value'] = absint( $request['days'] ); |
| 229 |
} |
| 230 |
|
| 231 |
$prepared_item['color'] = isset( $request['color'] ) ? $request['color'] : '#fafafa'; |
| 232 |
|
| 233 |
// optional arguments. |
| 234 |
if ( isset( $request['id'] ) ) { |
| 235 |
$prepared_item['id'] = absint( $request['id'] ); |
| 236 |
} |
| 237 |
|
| 238 |
if ( isset( $request['department'] ) ) { |
| 239 |
$prepared_item['department'] = absint( $request['department'] ); |
| 240 |
} |
| 241 |
|
| 242 |
if ( isset( $request['designation'] ) ) { |
| 243 |
$prepared_item['designation'] = absint( $request['designation'] ); |
| 244 |
} |
| 245 |
|
| 246 |
if ( isset( $request['gender'] ) ) { |
| 247 |
$prepared_item['gender'] = $request['gender']; |
| 248 |
} |
| 249 |
|
| 250 |
if ( isset( $request['marital'] ) ) { |
| 251 |
$prepared_item['marital'] = $request['marital']; |
| 252 |
} |
| 253 |
|
| 254 |
if ( isset( $request['activate'] ) ) { |
| 255 |
$activate_types = array_flip( $this->activate_types ); |
| 256 |
$prepared_item['activate'] = $activate_types[ $request['activate'] ]; |
| 257 |
} |
| 258 |
|
| 259 |
if ( isset( $request['execute_day'] ) ) { |
| 260 |
$prepared_item['execute_day'] = absint( $request['execute_day'] ); |
| 261 |
} |
| 262 |
|
| 263 |
if ( isset( $request['effective_date'] ) ) { |
| 264 |
$prepared_item['effective_date'] = date( 'Y-m-d', strtotime( $request['effective_date'] ) ); |
| 265 |
} |
| 266 |
|
| 267 |
if ( isset( $request['location'] ) ) { |
| 268 |
$prepared_item['location'] = absint( $request['location'] ); |
| 269 |
} |
| 270 |
|
| 271 |
if ( isset( $request['description'] ) ) { |
| 272 |
$prepared_item['description'] = $request['description']; |
| 273 |
} |
| 274 |
|
| 275 |
return $prepared_item; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Prepare a single user output for response |
| 280 |
* |
| 281 |
* @param object $item |
| 282 |
* @param WP_REST_Request $request Request object. |
| 283 |
* @param array $additional_fields (optional) |
| 284 |
* |
| 285 |
* @return WP_REST_Response $response Response data. |
| 286 |
*/ |
| 287 |
public function prepare_item_for_response( $item, $request, $additional_fields = [] ) { |
| 288 |
$data = [ |
| 289 |
'id' => (int) $item->id, |
| 290 |
'name' => $item->name, |
| 291 |
'days' => (int) $item->value, |
| 292 |
'color' => $item->color, |
| 293 |
'gender' => ( $item->gender != -1 ) ? $item->gender : null, |
| 294 |
'marital' => ( $item->marital != -1 ) ? $item->marital : null, |
| 295 |
'activate' => $this->activate_types[ $item->activate ], |
| 296 |
'execute_day' => (int) $item->execute_day, |
| 297 |
'effective_date' => date( 'Y-m-d', strtotime( $item->effective_date ) ), |
| 298 |
'location' => ( $item->location != -1 ) ? $item->location : null, |
| 299 |
'description' => $item->description, |
| 300 |
]; |
| 301 |
|
| 302 |
if ( isset( $request['include'] ) ) { |
| 303 |
$include_params = explode( ',', str_replace( ' ', '', $request['include'] ) ); |
| 304 |
|
| 305 |
if ( in_array( 'department', $include_params ) ) { |
| 306 |
$departments_controller = new Departments_Controller(); |
| 307 |
|
| 308 |
$department_id = (int) $item->department; |
| 309 |
$data['department'] = null; |
| 310 |
|
| 311 |
if ( $department_id ) { |
| 312 |
$department = $departments_controller->get_department( ['id' => $department_id ] ); |
| 313 |
$data['department'] = ! is_wp_error( $department ) ? $department->get_data() : null; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
if ( in_array( 'designation', $include_params ) ) { |
| 318 |
$designations_controller = new Designations_Controller(); |
| 319 |
|
| 320 |
$designation_id = (int) $item->designation; |
| 321 |
$data['designation'] = null; |
| 322 |
|
| 323 |
if ( $designation_id ) { |
| 324 |
$designation = $designations_controller->get_designation( ['id' => $designation_id ] ); |
| 325 |
$data['designation'] = ! is_wp_error( $designation ) ? $designation->get_data() : null; |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
$data = array_merge( $data, $additional_fields ); |
| 331 |
|
| 332 |
// Wrap the data in a response object |
| 333 |
$response = rest_ensure_response( $data ); |
| 334 |
|
| 335 |
$response = $this->add_links( $response, $item ); |
| 336 |
|
| 337 |
return $response; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Get the User's schema, conforming to JSON Schema |
| 342 |
* |
| 343 |
* @return array |
| 344 |
*/ |
| 345 |
public function get_item_schema() { |
| 346 |
$schema = [ |
| 347 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 348 |
'title' => 'policy', |
| 349 |
'type' => 'object', |
| 350 |
'properties' => [ |
| 351 |
'id' => [ |
| 352 |
'description' => __( 'Unique identifier for the resource.' ), |
| 353 |
'type' => 'integer', |
| 354 |
'context' => [ 'embed', 'view', 'edit' ], |
| 355 |
'readonly' => true, |
| 356 |
], |
| 357 |
'name' => [ |
| 358 |
'description' => __( 'Name for the resource.' ), |
| 359 |
'type' => 'string', |
| 360 |
'context' => [ 'edit' ], |
| 361 |
'arg_options' => [ |
| 362 |
'sanitize_callback' => 'sanitize_text_field', |
| 363 |
], |
| 364 |
'required' => true, |
| 365 |
], |
| 366 |
'days' => [ |
| 367 |
'description' => __( 'Days for the resource.' ), |
| 368 |
'type' => 'integer', |
| 369 |
'context' => [ 'embed', 'view', 'edit' ], |
| 370 |
'required' => true, |
| 371 |
], |
| 372 |
'color' => [ |
| 373 |
'description' => __( 'Color for the resource.' ), |
| 374 |
'type' => 'string', |
| 375 |
'context' => [ 'edit' ], |
| 376 |
'arg_options' => [ |
| 377 |
'sanitize_callback' => 'sanitize_text_field', |
| 378 |
], |
| 379 |
], |
| 380 |
], |
| 381 |
]; |
| 382 |
|
| 383 |
return $schema; |
| 384 |
} |
| 385 |
} |
| 386 |
|