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 / accounting / api / class-rest-api-product-cats.php

class-rest-api-product-cats.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.7, at modules/accounting/api/class-rest-api-product-cats.php

345 lines 11.2 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\Accounting\API;
4
5 use WP_Error;
6 use WP_REST_Response;
7 use WP_REST_Server;
8
9 class Inventory_Product_Cats_Controller extends \WeDevs\ERP\API\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 = 'accounting/v1/product-cats';
24
25 /**
26 * Register the routes for the objects of the controller.
27 */
28 public function register_routes() {
29 register_rest_route(
30 $this->namespace,
31 '/' . $this->rest_base,
32 [
33 [
34 'methods' => WP_REST_Server::READABLE,
35 'callback' => [ $this, 'get_all_inventory_product_cats' ],
36 'args' => $this->get_collection_params(),
37 'permission_callback' => function ( $request ) {
38 return current_user_can( 'erp_ac_manager' );
39 },
40 ],
41 [
42 'methods' => WP_REST_Server::CREATABLE,
43 'callback' => [ $this, 'create_inventory_product_cat' ],
44 'args' => $this->get_collection_params(),
45 'permission_callback' => function ( $request ) {
46 return current_user_can( 'erp_ac_manager' );
47 },
48 ],
49 'schema' => [ $this, 'get_item_schema' ],
50 ]
51 );
52 register_rest_route(
53 $this->namespace,
54 '/' . $this->rest_base . '/(?P<id>[\d]+)',
55 [
56 [
57 'methods' => WP_REST_Server::READABLE,
58 'callback' => [ $this, 'get_inventory_product_cat' ],
59 'args' => [
60 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
61 ],
62 'permission_callback' => function ( $request ) {
63 return current_user_can( 'erp_ac_manager' );
64 },
65 ],
66 [
67 'methods' => WP_REST_Server::EDITABLE,
68 'callback' => [ $this, 'update_inventory_product_cat' ],
69 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
70 'permission_callback' => function ( $request ) {
71 return current_user_can( 'erp_ac_manager' );
72 },
73 ],
74 [
75 'methods' => WP_REST_Server::DELETABLE,
76 'callback' => [ $this, 'delete_inventory_product_cat' ],
77 'permission_callback' => function ( $request ) {
78 return current_user_can( 'erp_ac_manager' );
79 },
80 ],
81 'schema' => [ $this, 'get_item_schema' ],
82 ]
83 );
84
85 register_rest_route(
86 $this->namespace,
87 '/' . $this->rest_base . '/delete/(?P<ids>[\d,?]+)',
88 [
89 [
90 'methods' => WP_REST_Server::DELETABLE,
91 'callback' => [ $this, 'bulk_delete_cat' ],
92 'args' => [
93 'ids' => [ 'required' => true ],
94 ],
95 'permission_callback' => function ( $request ) {
96 return current_user_can( 'erp_ac_manager' );
97 },
98 ],
99 'schema' => [ $this, 'get_item_schema' ],
100 ]
101 );
102 }
103
104 /**
105 * Get a collection of inventory product categories
106 *
107 * @param WP_REST_Request $request
108 *
109 * @return WP_Error|WP_REST_Response
110 */
111 public function get_all_inventory_product_cats( $request ) {
112 $formatted_items = [];
113 $additional_fields = [];
114
115 $additional_fields['namespace'] = $this->namespace;
116 $additional_fields['rest_base'] = $this->rest_base;
117
118 $product_cats = erp_acct_get_all_product_cats();
119
120 $total_items = is_array( $product_cats ) ? count( $product_cats ) : 1;
121
122 foreach ( $product_cats as $item ) {
123 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
124 $formatted_items[] = $this->prepare_response_for_collection( $data );
125 }
126
127 $response = rest_ensure_response( $formatted_items );
128 $response = $this->format_collection_response( $response, $request, $total_items );
129 $response->set_status( 200 );
130
131 return $response;
132 }
133
134 /**
135 * Get a specific inventory product category
136 *
137 * @param WP_REST_Request $request
138 *
139 * @return WP_Error|WP_REST_Response
140 */
141 public function get_inventory_product_cat( $request ) {
142 $id = (int) $request['id'];
143
144 if ( empty( $id ) ) {
145 return new WP_Error( 'rest_inventory_product_cat_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
146 }
147
148 $item = erp_acct_get_product_cat( $id );
149
150 $additional_fields['namespace'] = $this->namespace;
151 $additional_fields['rest_base'] = $this->rest_base;
152 $item = $this->prepare_item_for_response( $item, $request, $additional_fields );
153 $response = rest_ensure_response( $item );
154
155 $response->set_status( 200 );
156
157 return $response;
158 }
159
160 /**
161 * Create an inventory product
162 *
163 * @param WP_REST_Request $request
164 *
165 * @return WP_Error|WP_REST_Request
166 */
167 public function create_inventory_product_cat( $request ) {
168 $item = $this->prepare_item_for_database( $request );
169 $id = erp_acct_insert_product_cat( $item );
170 $item['id'] = $id;
171
172 $additional_fields['namespace'] = $this->namespace;
173 $additional_fields['rest_base'] = $this->rest_base;
174
175 $response = $this->prepare_item_for_response( $item, $request, $additional_fields );
176 $response = rest_ensure_response( $response );
177 $response->set_status( 201 );
178
179 return $response;
180 }
181
182 /**
183 * Update an inventory product category
184 *
185 * @param WP_REST_Request $request
186 *
187 * @return WP_Error|WP_REST_Request
188 */
189 public function update_inventory_product_cat( $request ) {
190 $id = (int) $request['id'];
191
192 $item = $this->prepare_item_for_database( $request );
193
194 if ( empty( $id ) ) {
195 return new WP_Error( 'rest_inventory_product_cat_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
196 }
197 $id = erp_acct_update_product_cat( $item, $id );
198 $item['id'] = $id;
199
200 $additional_fields['namespace'] = $this->namespace;
201 $additional_fields['rest_base'] = $this->rest_base;
202
203 $item = $this->prepare_item_for_response( $item, $request, $additional_fields );
204 $response = rest_ensure_response( $item );
205 $response->set_status( 200 );
206
207 return $response;
208 }
209
210 /**
211 * Delete an inventory product category
212 *
213 * @param WP_REST_Request $request
214 *
215 * @return WP_Error|WP_REST_Request
216 */
217 public function delete_inventory_product_cat( $request ) {
218 $term_id = (int) $request['id'];
219
220 erp_acct_delete_product_cat( $term_id );
221
222 return new WP_REST_Response( true, 204 );
223 }
224
225 /**
226 * Bulk delete action
227 *
228 * @param object $request
229 *
230 * @return object
231 */
232 public function bulk_delete_cat( $request ) {
233 $ids = $request['ids'];
234 $ids = explode( ',', $ids );
235
236 if ( ! $ids ) {
237 return;
238 }
239
240 foreach ( $ids as $id ) {
241 erp_acct_delete_product_cat( $id );
242 }
243
244 return new WP_REST_Response( true, 204 );
245 }
246
247 /**
248 * Prepare a single item for create or update
249 *
250 * @param WP_REST_Request $request request object
251 *
252 * @return array $prepared_item
253 */
254 protected function prepare_item_for_database( $request ) {
255 $prepared_item = [];
256
257 if ( isset( $request['name'] ) ) {
258 $prepared_item['name'] = $request['name'];
259 }
260
261 if ( isset( $request['parent'] ) ) {
262 $prepared_item['parent'] = $request['parent'];
263 }
264
265 return $prepared_item;
266 }
267
268 /**
269 * Prepare a single user output for response
270 *
271 * @param array|object $item
272 * @param WP_REST_Request $request request object
273 * @param array $additional_fields (optional)
274 *
275 * @return WP_REST_Response $response response data
276 */
277 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
278 $item = (object) $item;
279
280 $data = [
281 'id' => $item->id,
282 'name' => $item->name,
283 'parent' => $item->parent,
284 ];
285
286 $data = array_merge( $data, $additional_fields );
287
288 // Wrap the data in a response object
289 $response = rest_ensure_response( $data );
290
291 $response = $this->add_links( $response, $item, $additional_fields );
292
293 return $response;
294 }
295
296 /**
297 * Get the User's schema, conforming to JSON Schema
298 *
299 * @return array
300 */
301 public function get_item_schema() {
302 $schema = [
303 '$schema' => 'http://json-schema.org/draft-04/schema#',
304 'title' => 'erp_inv_product_cat',
305 'type' => 'object',
306 'properties' => [
307 'id' => [
308 'description' => __( 'Unique identifier for the resource.' ),
309 'type' => 'integer',
310 'context' => [ 'embed', 'view', 'edit' ],
311 'readonly' => true,
312 ],
313 'name' => [
314 'description' => __( 'Name for the resource.' ),
315 'type' => 'string',
316 'context' => [ 'edit' ],
317 'arg_options' => [
318 'sanitize_callback' => 'sanitize_text_field',
319 ],
320 'required' => true,
321 ],
322 'parent' => [
323 'description' => __( 'Parent for the resource.', 'erp' ),
324 'type' => 'object',
325 'context' => [ 'view', 'edit' ],
326 'properties' => [
327 'id' => [
328 'description' => __( 'Unique identifier for the resource.', 'erp' ),
329 'type' => 'integer',
330 'context' => [ 'view', 'edit' ],
331 ],
332 'name' => [
333 'description' => __( 'Name for the resource.', 'erp' ),
334 'type' => 'string',
335 'context' => [ 'view', 'edit' ],
336 ],
337 ],
338 ],
339 ],
340 ];
341
342 return $schema;
343 }
344 }
345