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 / modules / hrm / includes / api / class-announcements-controller.php

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

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