| 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 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
class Company_Controller extends \WeDevs\ERP\API\REST_Controller { |
| 14 |
|
| 15 |
/** |
| 16 |
* Endpoint namespace. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $namespace = 'erp/v1'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Route base. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $rest_base = 'accounting/v1/company'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Register the routes for the objects of the controller. |
| 31 |
*/ |
| 32 |
public function register_routes() { |
| 33 |
register_rest_route( |
| 34 |
$this->namespace, |
| 35 |
'/' . $this->rest_base, |
| 36 |
[ |
| 37 |
[ |
| 38 |
'methods' => WP_REST_Server::READABLE, |
| 39 |
'callback' => [ $this, 'get_company' ], |
| 40 |
'args' => [], |
| 41 |
'permission_callback' => function ( $request ) { |
| 42 |
return current_user_can( 'erp_view_list' ); |
| 43 |
}, |
| 44 |
], |
| 45 |
'schema' => [ $this, 'get_item_schema' ], |
| 46 |
] |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get a company logo |
| 52 |
* |
| 53 |
* @param WP_REST_Request $request |
| 54 |
* |
| 55 |
* @return WP_Error|WP_REST_Response |
| 56 |
*/ |
| 57 |
public function get_company( $request ) { |
| 58 |
$company = new \WeDevs\ERP\Company(); |
| 59 |
|
| 60 |
$logo_id = (int) $company->logo; |
| 61 |
|
| 62 |
if ( ! $logo_id ) { |
| 63 |
$url = $company->placeholder_logo(); |
| 64 |
} else { |
| 65 |
$image = wp_get_attachment_image_src( $logo_id, 'medium' ); |
| 66 |
$url = $image[0]; |
| 67 |
} |
| 68 |
|
| 69 |
$response = rest_ensure_response( |
| 70 |
[ |
| 71 |
'logo' => $url, |
| 72 |
'name' => $company->name, |
| 73 |
'address' => $company->address, |
| 74 |
] |
| 75 |
); |
| 76 |
|
| 77 |
$response->set_status( 200 ); |
| 78 |
|
| 79 |
return $response; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get the schema, conforming to JSON Schema |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function get_item_schema() { |
| 88 |
$schema = [ |
| 89 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 90 |
'title' => 'company', |
| 91 |
'type' => 'object', |
| 92 |
'properties' => [ |
| 93 |
'logo' => [ |
| 94 |
'description' => __( 'Company logo for the resource.' ), |
| 95 |
'type' => 'string', |
| 96 |
'context' => [ 'embed', 'view' ], |
| 97 |
], |
| 98 |
'name' => [ |
| 99 |
'description' => __( 'Company name for the resource.' ), |
| 100 |
'type' => 'string', |
| 101 |
'context' => [ 'view' ], |
| 102 |
], |
| 103 |
'address' => [ |
| 104 |
'description' => __( 'Address data.', 'erp' ), |
| 105 |
'type' => 'object', |
| 106 |
'context' => [ 'view' ], |
| 107 |
'properties' => [ |
| 108 |
'address_1' => [ |
| 109 |
'description' => __( 'Company address 1 for the resource.', 'erp' ), |
| 110 |
'type' => 'string', |
| 111 |
'context' => [ 'view' ], |
| 112 |
], |
| 113 |
'address_2' => [ |
| 114 |
'description' => __( 'Company address 2 for the resource.', 'erp' ), |
| 115 |
'type' => 'string', |
| 116 |
'context' => [ 'view' ], |
| 117 |
], |
| 118 |
], |
| 119 |
], |
| 120 |
], |
| 121 |
]; |
| 122 |
|
| 123 |
return $schema; |
| 124 |
} |
| 125 |
} |
| 126 |
|