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