1 |
<?php |
2 |
|
3 |
/* |
4 |
* Treats user requests that arrive via REST services. |
5 |
* |
6 |
* @author: Mirna Limic <[email protected]> |
7 |
* @uses ./restler The RESTLER framework. When using PUT and POST http methods |
8 |
* the function receiving the incomming data MUST contain a parameter called |
9 |
* request_data. The RESTLER framework stores the incoming JSON string converted |
10 |
* into an array in the request_data parameter. |
11 |
* |
12 |
* Date: 18/10/2012 |
13 |
* Follow-up: |
14 |
* Bugs: Unknown |
15 |
* |
16 |
*/ |
17 |
|
18 |
// L'ordre des includes est important |
19 |
include_once ('./REST/services.php'); |
20 |
include_once('./metier/usermanager.php'); |
21 |
|
22 |
class utilisateurs extends Services { |
23 |
|
24 |
/* |
25 |
* Constructor |
26 |
* |
27 |
* Calls its parent's constructor and fills the contents array with the |
28 |
* keys that are to be searched for in the incoming JSON data. |
29 |
* |
30 |
*/ |
31 |
public function __construct() { |
32 |
parent::__construct(); |
33 |
$contelem_strs = array("nom", "email","login","pwd","om_profil", |
34 |
"om_collectivite","om_type"); |
35 |
foreach($contelem_strs as $elem) { |
36 |
$this->contents[$elem] = ''; |
37 |
} |
38 |
$this->metier_manager = new UserManager(); |
39 |
} |
40 |
|
41 |
|
42 |
/* |
43 |
* Destructor |
44 |
* |
45 |
* Call's its parent's destructor. |
46 |
*/ |
47 |
public function __destruct() { |
48 |
parent::__destruct(); |
49 |
} |
50 |
|
51 |
|
52 |
/* |
53 |
* Called when a POST http request is made to insert a user. |
54 |
* |
55 |
* Checks the validity of the format, and the presence of |
56 |
* content (if mandatory) of the data received with the request. |
57 |
* @param mixed $request_data The incoming JSON data as an array. |
58 |
* Note: the parameter has to be named this way due to RESTLER's |
59 |
* parameter mapping of JSON data onto $request_data. |
60 |
* @return mixed The array contining the return code. Where |
61 |
* 400 is sent if data is missing, and 500 is sent on other error. |
62 |
* 200 is sent on success. |
63 |
*/ |
64 |
public function post($request_data) { |
65 |
// check that the request contains all the data |
66 |
if (!$this->requestValid($request_data, null)) { |
67 |
return $this->sendHttpCode(400, "Missing mandatory data."); |
68 |
} |
69 |
|
70 |
// appel de la fonction qui va faire l'ajout des donnees |
71 |
$ret = $this->metier_manager->ajouter($request_data); |
72 |
// send the reply |
73 |
return $this->sendReply($ret, 'Could not insert user data.'); |
74 |
} |
75 |
|
76 |
|
77 |
/* |
78 |
* Called when a GET http request is made on a user ID. |
79 |
* @param string $id The ID of a user. |
80 |
* @return mixed The array contining the return code. Where 400 |
81 |
* is sent if user ID is not found, and 500 is sent on other error. |
82 |
* 200 is sent on success. |
83 |
*/ |
84 |
public function get($id) { |
85 |
// appel a la fonction qui va recuperer des donnees |
86 |
if (!$id || empty($id)) { |
87 |
return $this->sendHttpCode(400, 'No ID found.'); |
88 |
} |
89 |
$ret = $this->metier_manager->extraire($id); |
90 |
if ($ret != 'OK') { |
91 |
return $this->sendReply($ret, 'Could not retreive user data.'); |
92 |
} |
93 |
// send the reply |
94 |
return $this->metier_manager->getReturnDataArray(); |
95 |
} |
96 |
|
97 |
|
98 |
/* |
99 |
* Called when a PUT http request is made to change a user's data. |
100 |
* |
101 |
* Checks the validity of the format, and the presence of |
102 |
* content (if mandatory) of the data received with the request. |
103 |
* @param mixed $request_data The incoming JSON data as an array. |
104 |
* Note: the parameter has to be named this way due to RESTLER's |
105 |
* parameter mapping of JSON data onto $request_data. |
106 |
* @param string $id The ID of a user. |
107 |
* @return mixed The array contining the return code. Where 400 is |
108 |
* sent if data is missing or bad, and 500 is sent on other error. |
109 |
* 200 is sent on success. |
110 |
*/ |
111 |
public function put($request_data, $id) { |
112 |
|
113 |
if (!$id || empty($id)) { |
114 |
return $this->sendHttpCode(400, 'No ID found.'); |
115 |
} |
116 |
|
117 |
$this->mdtry_grps = array("nom", "email","login","pwd","om_profil", |
118 |
"om_collectivite","om_type"); |
119 |
$group_idx = $this->requestMdtrGroup($request_data); |
120 |
if ($group_idx < 0) { |
121 |
return $this->sendHttpCode(400, 'Bad data.'); |
122 |
} |
123 |
|
124 |
// appel a la fonction qui va faire modification des donnees |
125 |
$ret = $this->metier_manager->modifier($request_data, $id); |
126 |
// send the reply |
127 |
return $this->sendReply($ret, 'Could not update user data.'); |
128 |
} |
129 |
|
130 |
|
131 |
/* |
132 |
* Called when a DELETE http request is made to delete a user. |
133 |
* @param string $id The ID of a user. |
134 |
* @return mixed The array contining the return code. Where 400 |
135 |
* is sent if user ID is not found, and 500 is sent on other error. |
136 |
* 200 is sent on success. |
137 |
*/ |
138 |
public function delete($id) { |
139 |
//return array("ok"); |
140 |
if (empty($id)) { |
141 |
return $this->sendHttpCode(400, 'No ID found.'); |
142 |
} |
143 |
// appel a la fonction qui va supprimer des donnees |
144 |
$ret = $this->metier_manager->supprimer($id); |
145 |
// send the reply |
146 |
return $this->sendReply($ret, 'Could not delete user data.'); |
147 |
} |
148 |
|
149 |
} |
150 |
|
151 |
?> |