PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.6.7
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.6.7
1.17.10 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 All 144 releases
erp / modules / hrm / includes / api / class-announcements-controller.php

class-announcements-controller.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.7, at modules/hrm/includes/api/class-announcements-controller.php

352 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WeDevs\ERP\HRM\API;
4
5 use DateTime;
6 use WeDevs\ERP\API\REST_Controller;
7 use WP_Error;
8 use WP_REST_Response;
9 use WP_REST_Server;
10
11 class Announcements_Controller extends REST_Controller {
12
13 /**
14 * Endpoint namespace.
15 *
16 * @var string
17 */
18 protected $namespace = 'erp/v1';
19
20 /**
21 * Route base.
22 *
23 * @var string
24 */
25 protected $rest_base = 'hrm/announcements';
26
27 /**
28 * Register the routes for the objects of the controller.
29 */
30 public function register_routes() {
31 register_rest_route( $this->namespace, '/' . $this->rest_base, [
32 [
33 'methods' => WP_REST_Server::READABLE,
34 'callback' => [ $this, 'get_announcements' ],
35 'args' => $this->get_collection_params(),
36 'permission_callback' => function ( $request ) {
37 return current_user_can( 'erp_manage_announcement' );
38 },
39 ],
40 [
41 'methods' => WP_REST_Server::CREATABLE,
42 'callback' => [ $this, 'create_announcement' ],
43 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
44 'permission_callback' => function ( $request ) {
45 return current_user_can( 'erp_manage_announcement' );
46 },
47 ],
48 'schema' => [ $this, 'get_public_item_schema' ],
49 ] );
50
51 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [
52 [
53 'methods' => WP_REST_Server::READABLE,
54 'callback' => [ $this, 'get_announcement' ],
55 'args' => [
56 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
57 ],
58 'permission_callback' => function ( $request ) {
59 return current_user_can( 'erp_manage_announcement' );
60 },
61 ],
62 [
63 'methods' => WP_REST_Server::EDITABLE,
64 'callback' => [ $this, 'update_announcement' ],
65 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
66 'permission_callback' => function ( $request ) {
67 return current_user_can( 'erp_manage_announcement' );
68 },
69 ],
70 [
71 'methods' => WP_REST_Server::DELETABLE,
72 'callback' => [ $this, 'delete_announcement' ],
73 'permission_callback' => function ( $request ) {
74 return current_user_can( 'erp_manage_announcement' );
75 },
76 ],
77 'schema' => [ $this, 'get_public_item_schema' ],
78 ] );
79 }
80
81 /**
82 * Get a collection of announcements
83 *
84 * @param WP_REST_Request $request
85 *
86 * @return WP_Error|WP_REST_Response
87 */
88 public function get_announcements( $request ) {
89 $args = [
90 'posts_per_page' => $request['per_page'],
91 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
92 ];
93
94 $args['post_type'] = 'erp_hr_announcement';
95
96 $items = get_posts( $args );
97
98 $count_items = wp_count_posts( $args['post_type'] );
99 $total_items = (int) $count_items->publish;
100
101 $formated_items = [];
102
103 foreach ( $items as $item ) {
104 $item->id = $item->ID;
105 $data = $this->prepare_item_for_response( $item, $request );
106 $formated_items[] = $this->prepare_response_for_collection( $data );
107 }
108
109 $response = rest_ensure_response( $formated_items );
110 $response = $this->format_collection_response( $response, $request, $total_items );
111
112 return $response;
113 }
114
115 /**
116 * Get a specific announcement
117 *
118 * @param WP_REST_Request $request
119 *
120 * @return WP_Error|WP_REST_Response
121 */
122 public function get_announcement( $request ) {
123 $id = (int) $request['id'];
124 $item = get_post( $id );
125 $item->id = $item->ID;
126
127 if ( empty( $id ) || empty( $item->id ) ) {
128 return new WP_Error( 'rest_announcement_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
129 }
130
131 $item = $this->prepare_item_for_response( $item, $request );
132 $response = rest_ensure_response( $item );
133
134 return $response;
135 }
136
137 /**
138 * Create an announcement
139 *
140 * @param WP_REST_Request $request
141 *
142 * @return WP_Error|WP_REST_Request
143 */
144 public function create_announcement( $request ) {
145 $item = $this->prepare_item_for_database( $request );
146
147 $id = wp_insert_post( $item );
148
149 $type = ( $request['recipient_type'] == 'all_employees' ) ? 'all_employee' : 'selected_employee';
150
151 $employees = [];
152
153 if ( $type == 'selected_employee' ) {
154 $employees = explode( ',', str_replace( ' ', '', $request['employees'] ) );
155 }
156
157 erp_hr_assign_announcements_to_employees( $id, $type, $employees );
158
159 $announcement = get_post( $id );
160 $announcement->id = $announcement->ID;
161
162 $request->set_param( 'context', 'edit' );
163 $response = $this->prepare_item_for_response( $announcement, $request );
164 $response = rest_ensure_response( $response );
165 $response->set_status( 201 );
166 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
167
168 return $response;
169 }
170
171 /**
172 * Update an announcement
173 *
174 * @param WP_REST_Request $request
175 *
176 * @return WP_Error|WP_REST_Request
177 */
178 public function update_announcement( $request ) {
179 $id = (int) $request['id'];
180
181 $announcement = get_post( $id );
182
183 if ( empty( $id ) || empty( $announcement->ID ) ) {
184 return new WP_Error( 'rest_announcement_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
185 }
186
187 $item = $this->prepare_item_for_database( $request );
188
189 $id = wp_insert_post( $item );
190 $announcement = get_post( $id );
191
192 $type = ( $request['recipient_type'] == 'all_employees' ) ? 'all_employee' : 'selected_employee';
193
194 $employees = [];
195
196 if ( $type == 'selected_employee' ) {
197 $employees = explode( ',', str_replace( ' ', '', $request['employees'] ) );
198 }
199
200 erp_hr_assign_announcements_to_employees( $id, $type, $employees );
201
202 $announcement->id = $announcement->ID;
203
204 $request->set_param( 'context', 'edit' );
205 $response = $this->prepare_item_for_response( $announcement, $request );
206 $response = rest_ensure_response( $response );
207 $response->set_status( 201 );
208 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
209
210 return $response;
211 }
212
213 /**
214 * Delete an announcement
215 *
216 * @param WP_REST_Request $request
217 *
218 * @return WP_Error|WP_REST_Request
219 */
220 public function delete_announcement( $request ) {
221 $id = (int) $request['id'];
222
223 $force_delete = true;
224 wp_delete_post( $id );
225
226 return new WP_REST_Response( true, 204 );
227 }
228
229 /**
230 * Prepare a single item for create or update
231 *
232 * @param WP_REST_Request $request request object
233 *
234 * @return array $prepared_item
235 */
236 protected function prepare_item_for_database( $request ) {
237 $prepared_item = [];
238
239 // required arguments.
240 if ( isset( $request['title'] ) ) {
241 $prepared_item['post_title'] = $request['title'];
242 }
243
244 if ( isset( $request['body'] ) ) {
245 $prepared_item['post_content'] = $request['body'];
246 }
247
248 // optional arguments.
249 if ( isset( $request['id'] ) ) {
250 $prepared_item['ID'] = absint( $request['id'] );
251 }
252
253 if ( isset( $request['status'] ) ) {
254 $prepared_item['post_status'] = $request['status'];
255 }
256
257 $prepared_item['post_type'] = 'erp_hr_announcement';
258
259 return $prepared_item;
260 }
261
262 /**
263 * Prepare a single user output for response
264 *
265 * @param object $item
266 * @param WP_REST_Request $request request object
267 * @param array $additional_fields (optional)
268 *
269 * @return WP_REST_Response $response response data
270 */
271 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
272 $post_date = new DateTime( $item->post_date );
273 $post_author = get_user_by( 'id', $item->post_author );
274
275 $data = [
276 'id' => (int) $item->id,
277 'title' => $item->post_title,
278 'body' => $item->post_content,
279 'status' => $item->post_status,
280 'date' => $post_date->format( 'Y-m-d' ),
281 'author' => $post_author->user_login,
282 ];
283
284 $data = array_merge( $data, $additional_fields );
285
286 // Wrap the data in a response object
287 $response = rest_ensure_response( $data );
288
289 $response = $this->add_links( $response, $item );
290
291 return $response;
292 }
293
294 /**
295 * Get the User's schema, conforming to JSON Schema
296 *
297 * @return array
298 */
299 public function get_item_schema() {
300 $schema = [
301 '$schema' => 'http://json-schema.org/draft-04/schema#',
302 'title' => 'announcement',
303 'type' => 'object',
304 'properties' => [
305 'id' => [
306 'description' => __( 'Unique identifier for the resource.' ),
307 'type' => 'integer',
308 'context' => [ 'embed', 'view', 'edit' ],
309 'readonly' => true,
310 ],
311 'title' => [
312 'description' => __( 'Title for the resource.' ),
313 'type' => 'string',
314 'context' => [ 'edit' ],
315 'arg_options' => [
316 'sanitize_callback' => 'sanitize_text_field',
317 ],
318 'required' => true,
319 ],
320 'body' => [
321 'description' => __( 'Body for the resource.' ),
322 'type' => 'string',
323 'context' => [ 'edit' ],
324 'arg_options' => [
325 'sanitize_callback' => 'sanitize_text_field',
326 ],
327 'required' => true,
328 ],
329 'status' => [
330 'description' => __( 'Status for the resource.' ),
331 'type' => 'string',
332 'context' => [ 'edit' ],
333 'arg_options' => [
334 'sanitize_callback' => 'sanitize_text_field',
335 ],
336 ],
337 'recipient_type' => [
338 'description' => __( 'Recipient type for the resource.' ),
339 'type' => 'string',
340 'context' => [ 'edit' ],
341 'arg_options' => [
342 'sanitize_callback' => 'sanitize_text_field',
343 ],
344 'required' => true,
345 ],
346 ],
347 ];
348
349 return $schema;
350 }
351 }
352