dolibarr 18.0.6
api_memberstypes.class.php
1<?php
2/* Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18use Luracast\Restler\RestException;
19
20require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent_type.class.php';
21
29{
33 public static $FIELDS = array(
34 'label',
35 );
36
40 public function __construct()
41 {
42 global $db, $conf;
43 $this->db = $db;
44 }
45
56 public function get($id)
57 {
58 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
59 throw new RestException(401);
60 }
61
62 $membertype = new AdherentType($this->db);
63 $result = $membertype->fetch($id);
64 if (!$result) {
65 throw new RestException(404, 'member type not found');
66 }
67
68 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
69 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
70 }
71
72 return $this->_cleanObjectDatas($membertype);
73 }
74
89 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
90 {
91 global $db, $conf;
92
93 $obj_ret = array();
94
95 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
96 throw new RestException(401);
97 }
98
99 $sql = "SELECT t.rowid";
100 $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type AS t LEFT JOIN ".MAIN_DB_PREFIX."adherent_type_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
101 $sql .= ' WHERE t.entity IN ('.getEntity('member_type').')';
102
103 // Add sql filters
104 if ($sqlfilters) {
105 $errormessage = '';
106 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
107 if ($errormessage) {
108 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
109 }
110 }
111
112 $sql .= $this->db->order($sortfield, $sortorder);
113 if ($limit) {
114 if ($page < 0) {
115 $page = 0;
116 }
117 $offset = $limit * $page;
118
119 $sql .= $this->db->plimit($limit + 1, $offset);
120 }
121
122 $result = $this->db->query($sql);
123 if ($result) {
124 $i = 0;
125 $num = $this->db->num_rows($result);
126 $min = min($num, ($limit <= 0 ? $num : $limit));
127 while ($i < $min) {
128 $obj = $this->db->fetch_object($result);
129 $membertype = new AdherentType($this->db);
130 if ($membertype->fetch($obj->rowid)) {
131 $obj_ret[] = $this->_cleanObjectDatas($membertype);
132 }
133 $i++;
134 }
135 } else {
136 throw new RestException(503, 'Error when retrieve member type list : '.$this->db->lasterror());
137 }
138 if (!count($obj_ret)) {
139 throw new RestException(404, 'No member type found');
140 }
141
142 return $obj_ret;
143 }
144
151 public function post($request_data = null)
152 {
153 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
154 throw new RestException(401);
155 }
156 // Check mandatory fields
157 $result = $this->_validate($request_data);
158
159 $membertype = new AdherentType($this->db);
160 foreach ($request_data as $field => $value) {
161 $membertype->$field = $value;
162 }
163 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
164 throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
165 }
166 return $membertype->id;
167 }
168
176 public function put($id, $request_data = null)
177 {
178 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
179 throw new RestException(401);
180 }
181
182 $membertype = new AdherentType($this->db);
183 $result = $membertype->fetch($id);
184 if (!$result) {
185 throw new RestException(404, 'member type not found');
186 }
187
188 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
189 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
190 }
191
192 foreach ($request_data as $field => $value) {
193 if ($field == 'id') {
194 continue;
195 }
196 if ($field == 'array_options' && is_array($value)) {
197 foreach ($value as $index => $val) {
198 $membertype->array_options[$index] = $this->_checkValForAPI($field, $val, $membertype);
199 }
200 continue;
201 }
202 // Process the status separately because it must be updated using
203 // the validate(), resiliate() and exclude() methods of the class AdherentType.
204 $membertype->$field = $value;
205 }
206
207 // If there is no error, update() returns the number of affected rows
208 // so if the update is a no op, the return value is zero.
209 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
210 return $this->get($id);
211 } else {
212 throw new RestException(500, 'Error when updating member type: '.$membertype->error);
213 }
214 }
215
222 public function delete($id)
223 {
224 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
225 throw new RestException(401);
226 }
227 $membertype = new AdherentType($this->db);
228 $result = $membertype->fetch($id);
229 if (!$result) {
230 throw new RestException(404, 'member type not found');
231 }
232
233 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
234 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
235 }
236
237 $res = $membertype->delete();
238 if ($res < 0) {
239 throw new RestException(500, "Can't delete, error occurs");
240 } elseif ($res == 0) {
241 throw new RestException(409, "Can't delete, that product is probably used");
242 }
243
244 return array(
245 'success' => array(
246 'code' => 200,
247 'message' => 'Member type deleted'
248 )
249 );
250 }
251
260 private function _validate($data)
261 {
262 $membertype = array();
263 foreach (MembersTypes::$FIELDS as $field) {
264 if (!isset($data[$field])) {
265 throw new RestException(400, "$field field missing");
266 }
267 $membertype[$field] = $data[$field];
268 }
269 return $membertype;
270 }
271
272 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
279 protected function _cleanObjectDatas($object)
280 {
281 // phpcs:enable
282 $object = parent::_cleanObjectDatas($object);
283
284 unset($object->array_options);
285 unset($object->linkedObjectsIds);
286 unset($object->context);
287 unset($object->canvas);
288 unset($object->fk_project);
289 unset($object->contact);
290 unset($object->contact_id);
291 unset($object->thirdparty);
292 unset($object->user);
293 unset($object->origin);
294 unset($object->origin_id);
295 unset($object->ref_ext);
296 unset($object->country);
297 unset($object->country_id);
298 unset($object->country_code);
299 unset($object->barcode_type);
300 unset($object->barcode_type_code);
301 unset($object->barcode_type_label);
302 unset($object->barcode_type_coder);
303 unset($object->mode_reglement_id);
304 unset($object->cond_reglement_id);
305 unset($object->cond_reglement);
306 unset($object->fk_delivery_address);
307 unset($object->shipping_method_id);
308 unset($object->model_pdf);
309 unset($object->fk_account);
310 unset($object->note_public);
311 unset($object->note_private);
312 unset($object->fk_incoterms);
313 unset($object->label_incoterms);
314 unset($object->location_incoterms);
315 unset($object->name);
316 unset($object->lastname);
317 unset($object->firstname);
318 unset($object->civility_id);
319 unset($object->total_ht);
320 unset($object->total_tva);
321 unset($object->total_localtax1);
322 unset($object->total_localtax2);
323 unset($object->total_ttc);
324
325 return $object;
326 }
327}
Class to manage members type.
Class for API REST v1.
Definition api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:86
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List members types.
__construct()
Constructor.
post($request_data=null)
Create member type object.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before creating an object.
put($id, $request_data=null)
Update member type.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria