|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\rest; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\cache\CacheManager; |
|
6
|
|
|
use Ubiquity\orm\DAO; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Abstract base class for Simple Rest controllers. |
|
10
|
|
|
* Ubiquity\controllers\rest$SimpleRestController |
|
11
|
|
|
* This class is part of Ubiquity |
|
12
|
|
|
* |
|
13
|
|
|
* @author jcheron <[email protected]> |
|
14
|
|
|
* @version 1.0.1 |
|
15
|
|
|
* @since Ubiquity 2.1.1 |
|
16
|
|
|
*/ |
|
17
|
|
|
class SimpleRestController extends RestBaseController implements HasResourceInterface { |
|
18
|
|
|
|
|
19
|
6 |
|
public function initialize() { |
|
20
|
6 |
|
$thisClass = \get_class ( $this ); |
|
21
|
6 |
|
if (! isset ( $this->model )) |
|
22
|
6 |
|
$this->model = CacheManager::getRestResource ( $thisClass ); |
|
23
|
6 |
|
if (! isset ( $this->model )) { |
|
24
|
|
|
$modelsNS = $this->config ["mvcNS"] ["models"]; |
|
25
|
|
|
$this->model = $modelsNS . "\\" . $this->_getResponseFormatter ()->getModel ( $thisClass ); |
|
26
|
|
|
} |
|
27
|
6 |
|
parent::initialize (); |
|
28
|
6 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
* @see \Ubiquity\controllers\rest\RestBaseController::index() |
|
34
|
|
|
* @route("/links","methods"=>["get"],"priority"=>3000) |
|
35
|
|
|
*/ |
|
36
|
|
|
public function index() { |
|
37
|
|
|
parent::index (); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns all the instances from the model $this->model. |
|
42
|
|
|
* Query parameters: |
|
43
|
|
|
* - **include**: A string of associated members to load, comma separated (e.g. users,groups,organization...), or a boolean: true for all members, false for none (default: true). |
|
44
|
|
|
* - **filter**: The filter to apply to the query (where part of an SQL query) (default: 1=1). |
|
45
|
|
|
* - **page[number]**: The page to display (in this case, the page size is set to 1). |
|
46
|
|
|
* - **page[size]**: The page size (count of instance per page) (default: 1). |
|
47
|
|
|
* |
|
48
|
|
|
* @route("/","methods"=>["get"],"priority"=>0) |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function getAll_() { |
|
51
|
4 |
|
$filter = $this->getCondition($this->getRequestParam ( 'filter', '1=1' )); |
|
52
|
4 |
|
$pages = null; |
|
53
|
4 |
|
if (isset ( $_GET ['page'] )) { |
|
54
|
2 |
|
$pageNumber = $_GET ['page'] ['number']; |
|
55
|
2 |
|
$pageSize = $_GET ['page'] ['size'] ?? 1; |
|
56
|
2 |
|
$pages = $this->generatePagination ( $filter, $pageNumber, $pageSize ); |
|
57
|
|
|
} |
|
58
|
4 |
|
$datas = DAO::getAll ( $this->model, $filter, $this->getInclude ( $this->getRequestParam ( 'include', true ) ) ); |
|
59
|
4 |
|
echo $this->_getResponseFormatter ()->get ( $datas, $pages ); |
|
60
|
4 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the first object corresponding to the $keyValues |
|
64
|
|
|
* Query parameters: |
|
65
|
|
|
* - **include**: A string of associated members to load, comma separated (e.g. |
|
66
|
|
|
* users,groups,organization...), or a boolean: true for all members, false for none (default: true). |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $id primary key(s) value(s) or condition |
|
69
|
|
|
* @route("{id}/","methods"=>["get"],"priority"=>1000) |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function getOne($id) { |
|
72
|
2 |
|
$this->_getOne ( $id, $this->getRequestParam ( 'include', true ) ); |
|
73
|
2 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Update an instance of $model selected by the primary key $keyValues |
|
77
|
|
|
* Require members values in $_POST array |
|
78
|
|
|
* Requires an authorization with access token |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $keyValues |
|
81
|
|
|
* @authorization |
|
82
|
|
|
* @route("/{keyValues}","methods"=>["patch"],"priority"=>0) |
|
83
|
|
|
*/ |
|
84
|
|
|
public function update(...$keyValues) { |
|
85
|
|
|
$this->_update ( ...$keyValues ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* |
|
90
|
|
|
* @route("/","methods"=>["options"],"priority"=>3000) |
|
91
|
|
|
*/ |
|
92
|
|
|
public function options(...$resource) { |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Insert a new instance of $model |
|
97
|
|
|
* Require members values in $_POST array |
|
98
|
|
|
* Requires an authorization with access token |
|
99
|
|
|
* |
|
100
|
|
|
* @authorization |
|
101
|
|
|
* @route("/","methods"=>["post"],"priority"=>0) |
|
102
|
|
|
*/ |
|
103
|
|
|
public function add() { |
|
104
|
|
|
$this->_add (); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Delete the instance of $model selected by the primary key $keyValues |
|
109
|
|
|
* Requires an authorization with access token |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $keyValues |
|
112
|
|
|
* @route("/{keyValues}","methods"=>["delete"],"priority"=>30) |
|
113
|
|
|
* @authorization |
|
114
|
|
|
*/ |
|
115
|
|
|
public function delete(...$keyValues) { |
|
116
|
|
|
$this->_delete ( ...$keyValues ); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|