dolibarr 21.0.4
api_memberstypes.class.php
1<?php
2/* Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
3 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent_type.class.php';
23
31{
35 public static $FIELDS = array(
36 'label',
37 );
38
42 public function __construct()
43 {
44 global $db, $conf;
45 $this->db = $db;
46 }
47
58 public function get($id)
59 {
60 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
61 throw new RestException(401);
62 }
63
64 $membertype = new AdherentType($this->db);
65 $result = $membertype->fetch($id);
66 if (!$result) {
67 throw new RestException(404, 'member type not found');
68 }
69
70 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
71 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
72 }
73
74 return $this->_cleanObjectDatas($membertype);
75 }
76
94 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
95 {
96 global $db, $conf;
97
98 $obj_ret = array();
99
100 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
101 throw new RestException(401);
102 }
103
104 $sql = "SELECT t.rowid";
105 $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
106 $sql .= ' WHERE t.entity IN ('.getEntity('member_type').')';
107
108 // Add sql filters
109 if ($sqlfilters) {
110 $errormessage = '';
111 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
112 if ($errormessage) {
113 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
114 }
115 }
116
117 $sql .= $this->db->order($sortfield, $sortorder);
118 if ($limit) {
119 if ($page < 0) {
120 $page = 0;
121 }
122 $offset = $limit * $page;
123
124 $sql .= $this->db->plimit($limit + 1, $offset);
125 }
126
127 $result = $this->db->query($sql);
128 if ($result) {
129 $i = 0;
130 $num = $this->db->num_rows($result);
131 $min = min($num, ($limit <= 0 ? $num : $limit));
132 while ($i < $min) {
133 $obj = $this->db->fetch_object($result);
134 $membertype = new AdherentType($this->db);
135 if ($membertype->fetch($obj->rowid)) {
136 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($membertype), $properties);
137 }
138 $i++;
139 }
140 } else {
141 throw new RestException(503, 'Error when retrieve member type list : '.$this->db->lasterror());
142 }
143
144 return $obj_ret;
145 }
146
155 public function post($request_data = null)
156 {
157 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
158 throw new RestException(401);
159 }
160 // Check mandatory fields
161 $result = $this->_validate($request_data);
162
163 $membertype = new AdherentType($this->db);
164 foreach ($request_data as $field => $value) {
165 if ($field === 'caller') {
166 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
167 $membertype->context['caller'] = $request_data['caller'];
168 continue;
169 }
170
171 $membertype->$field = $value;
172 }
173 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
174 throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
175 }
176 return $membertype->id;
177 }
178
188 public function put($id, $request_data = null)
189 {
190 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
191 throw new RestException(401);
192 }
193
194 $membertype = new AdherentType($this->db);
195 $result = $membertype->fetch($id);
196 if (!$result) {
197 throw new RestException(404, 'member type not found');
198 }
199
200 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
201 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
202 }
203
204 foreach ($request_data as $field => $value) {
205 if ($field == 'id') {
206 continue;
207 }
208 if ($field === 'caller') {
209 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
210 $membertype->context['caller'] = $request_data['caller'];
211 continue;
212 }
213 if ($field == 'array_options' && is_array($value)) {
214 foreach ($value as $index => $val) {
215 $membertype->array_options[$index] = $this->_checkValForAPI($field, $val, $membertype);
216 }
217 continue;
218 }
219 // Process the status separately because it must be updated using
220 // the validate(), resiliate() and exclude() methods of the class AdherentType.
221 $membertype->$field = $this->_checkValForAPI($field, $value, $membertype);
222 }
223
224 // If there is no error, update() returns the number of affected rows
225 // so if the update is a no op, the return value is zero.
226 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
227 return $this->get($id);
228 } else {
229 throw new RestException(500, 'Error when updating member type: '.$membertype->error);
230 }
231 }
232
241 public function delete($id)
242 {
243 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
244 throw new RestException(401);
245 }
246 $membertype = new AdherentType($this->db);
247 $result = $membertype->fetch($id);
248 if (!$result) {
249 throw new RestException(404, 'member type not found');
250 }
251
252 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
253 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
254 }
255
256 $res = $membertype->delete(DolibarrApiAccess::$user);
257 if ($res < 0) {
258 throw new RestException(500, "Can't delete, error occurs");
259 } elseif ($res == 0) {
260 throw new RestException(409, "Can't delete, that product is probably used");
261 }
262
263 return array(
264 'success' => array(
265 'code' => 200,
266 'message' => 'Member type deleted'
267 )
268 );
269 }
270
279 private function _validate($data)
280 {
281 $membertype = array();
282 foreach (MembersTypes::$FIELDS as $field) {
283 if (!isset($data[$field])) {
284 throw new RestException(400, "$field field missing");
285 }
286 $membertype[$field] = $data[$field];
287 }
288 return $membertype;
289 }
290
291 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
298 protected function _cleanObjectDatas($object)
299 {
300 // phpcs:enable
301 $object = parent::_cleanObjectDatas($object);
302
303 unset($object->array_options);
304 unset($object->linkedObjectsIds);
305 unset($object->context);
306 unset($object->canvas);
307 unset($object->fk_project);
308 unset($object->contact);
309 unset($object->contact_id);
310 unset($object->thirdparty);
311 unset($object->user);
312 unset($object->origin);
313 unset($object->origin_id);
314 unset($object->ref_ext);
315 unset($object->country);
316 unset($object->country_id);
317 unset($object->country_code);
318 unset($object->barcode_type);
319 unset($object->barcode_type_code);
320 unset($object->barcode_type_label);
321 unset($object->barcode_type_coder);
322 unset($object->mode_reglement_id);
323 unset($object->cond_reglement_id);
324 unset($object->cond_reglement);
325 unset($object->fk_delivery_address);
326 unset($object->shipping_method_id);
327 unset($object->model_pdf);
328 unset($object->fk_account);
329 unset($object->note_public);
330 unset($object->note_private);
331 unset($object->fk_incoterms);
332 unset($object->label_incoterms);
333 unset($object->location_incoterms);
334 unset($object->name);
335 unset($object->lastname);
336 unset($object->firstname);
337 unset($object->civility_id);
338 unset($object->total_ht);
339 unset($object->total_tva);
340 unset($object->total_localtax1);
341 unset($object->total_localtax2);
342 unset($object->total_ttc);
343
344 return $object;
345 }
346}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage members type.
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
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:96
__construct()
Constructor.
post($request_data=null)
Create member type object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List members types.
_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
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79