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-designations-controller.php

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

285 lines 9.3 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 Designations_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 = 'hrm/designations';
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_designations' ],
31 'args' => $this->get_collection_params(),
32 'permission_callback' => function ( $request ) {
33 return current_user_can( 'erp_manage_designation' );
34 },
35 ],
36 [
37 'methods' => WP_REST_Server::CREATABLE,
38 'callback' => [ $this, 'create_designation' ],
39 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
40 'permission_callback' => function ( $request ) {
41 return current_user_can( 'erp_manage_designation' );
42 },
43 ],
44 'schema' => [ $this, 'get_public_item_schema' ],
45 ] );
46
47 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [
48 [
49 'methods' => WP_REST_Server::READABLE,
50 'callback' => [ $this, 'get_designation' ],
51 'args' => [
52 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
53 ],
54 'permission_callback' => function ( $request ) {
55 return current_user_can( 'erp_manage_designation' );
56 },
57 ],
58 [
59 'methods' => WP_REST_Server::EDITABLE,
60 'callback' => [ $this, 'update_designation' ],
61 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
62 'permission_callback' => function ( $request ) {
63 return current_user_can( 'erp_manage_designation' );
64 },
65 ],
66 [
67 'methods' => WP_REST_Server::DELETABLE,
68 'callback' => [ $this, 'delete_designation' ],
69 'permission_callback' => function ( $request ) {
70 return current_user_can( 'erp_manage_designation' );
71 },
72 ],
73 'schema' => [ $this, 'get_public_item_schema' ],
74 ] );
75 }
76
77 /**
78 * Get a collection of designations
79 *
80 * @param WP_REST_Request $request
81 *
82 * @return WP_Error|WP_REST_Response
83 */
84 public function get_designations( $request ) {
85 $args = [
86 'number' => $request['per_page'],
87 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
88 ];
89
90 $items = erp_hr_get_designations( $args );
91 $total_items = erp_hr_count_designation();
92
93 $formated_items = [];
94 foreach ( $items as $item ) {
95 $data = $this->prepare_item_for_response( $item, $request );
96 $formated_items[] = $this->prepare_response_for_collection( $data );
97 }
98
99 $response = rest_ensure_response( $formated_items );
100 $response = $this->format_collection_response( $response, $request, $total_items );
101
102 return $response;
103 }
104
105 /**
106 * Get a specific designation
107 *
108 * @param WP_REST_Request $request
109 *
110 * @return WP_Error|WP_REST_Response
111 */
112 public function get_designation( $request ) {
113 $id = (int) $request['id'];
114 $item = new \WeDevs\ERP\HRM\Designation( $id );
115
116 if ( empty( $id ) || empty( $item->id ) ) {
117 return new WP_Error( 'rest_designation_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
118 }
119
120 $item = $this->prepare_item_for_response( $item, $request );
121 $response = rest_ensure_response( $item );
122
123 return $response;
124 }
125
126 /**
127 * Create a designation
128 *
129 * @param WP_REST_Request $request
130 *
131 * @return WP_Error|WP_REST_Request
132 */
133 public function create_designation( $request ) {
134 $item = $this->prepare_item_for_database( $request );
135 $id = erp_hr_create_designation( $item );
136
137 $designation = new \WeDevs\ERP\HRM\Designation( $id );
138
139 $request->set_param( 'context', 'edit' );
140 $response = $this->prepare_item_for_response( $designation, $request );
141 $response = rest_ensure_response( $response );
142 $response->set_status( 201 );
143 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
144
145 return $response;
146 }
147
148 /**
149 * Update a designation
150 *
151 * @param WP_REST_Request $request
152 *
153 * @return WP_Error|WP_REST_Request
154 */
155 public function update_designation( $request ) {
156 $id = (int) $request['id'];
157
158 $designation = new \WeDevs\ERP\HRM\Designation( $id );
159 if ( ! $designation ) {
160 return new WP_Error( 'rest_designation_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
161 }
162
163 $item = $this->prepare_item_for_database( $request );
164 $id = erp_hr_create_designation( $item );
165
166 $request->set_param( 'context', 'edit' );
167 $response = $this->prepare_item_for_response( $designation, $request );
168 $response = rest_ensure_response( $response );
169 $response->set_status( 201 );
170 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
171
172 return $response;
173 }
174
175 /**
176 * Delete a designation
177 *
178 * @param WP_REST_Request $request
179 *
180 * @return WP_Error|WP_REST_Request
181 */
182 public function delete_designation( $request ) {
183 $id = (int) $request['id'];
184
185 erp_hr_delete_designation( $id );
186
187 return new WP_REST_Response( true, 204 );
188 }
189
190 /**
191 * Prepare a single item for create or update
192 *
193 * @param WP_REST_Request $request Request object.
194 *
195 * @return array $prepared_item
196 */
197 protected function prepare_item_for_database( $request ) {
198 $prepared_item = [];
199
200 // required arguments.
201 if ( isset( $request['title'] ) ) {
202 $prepared_item['title'] = $request['title'];
203 }
204
205 // optional arguments.
206 if ( isset( $request['id'] ) ) {
207 $prepared_item['id'] = absint( $request['id'] );
208 }
209
210 if ( isset( $request['description'] ) ) {
211 $prepared_item['description'] = $request['description'];
212 }
213
214 return $prepared_item;
215 }
216
217 /**
218 * Prepare a single user output for response
219 *
220 * @param object $item
221 * @param WP_REST_Request $request Request object.
222 * @param array $additional_fields (optional)
223 *
224 * @return WP_REST_Response $response Response data.
225 */
226 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
227 $total_employees = \WeDevs\ERP\HRM\Models\Employee::where( array( 'status' => 'active', 'designation' => $item->id ) )->count();
228
229 $data = [
230 'id' => (int) $item->id,
231 'title' => $item->title,
232 'description' => $item->description,
233 'total_employees' => $total_employees,
234 ];
235
236 $data = array_merge( $data, $additional_fields );
237
238 // Wrap the data in a response object
239 $response = rest_ensure_response( $data );
240
241 $response = $this->add_links( $response, $item );
242
243 return $response;
244 }
245
246 /**
247 * Get the User's schema, conforming to JSON Schema
248 *
249 * @return array
250 */
251 public function get_item_schema() {
252 $schema = [
253 '$schema' => 'http://json-schema.org/draft-04/schema#',
254 'title' => 'designation',
255 'type' => 'object',
256 'properties' => [
257 'id' => [
258 'description' => __( 'Unique identifier for the resource.' ),
259 'type' => 'integer',
260 'context' => [ 'embed', 'view', 'edit' ],
261 'readonly' => true,
262 ],
263 'title' => [
264 'description' => __( 'Title for the resource.' ),
265 'type' => 'string',
266 'context' => [ 'edit' ],
267 'arg_options' => [
268 'sanitize_callback' => 'sanitize_text_field',
269 ],
270 'required' => true,
271 ],
272 'description' => [
273 'description' => __( 'Description for the resource.' ),
274 'type' => 'string',
275 'context' => [ 'edit' ],
276 'arg_options' => [
277 'sanitize_callback' => 'sanitize_text_field',
278 ],
279 ],
280 ],
281 ];
282
283 return $schema;
284 }
285 }