dolibarr 19.0.3
api_members.class.php
1<?php
2/* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
3 * Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2020 Thibault FOUCART<support@ptibogxiv.net>
5 * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21use Luracast\Restler\RestException;
22
23require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
24require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
25require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
26require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
27
34class Members extends DolibarrApi
35{
39 public static $FIELDS = array(
40 'morphy',
41 'typeid'
42 );
43
47 public function __construct()
48 {
49 global $db, $conf;
50 $this->db = $db;
51 }
52
63 public function get($id)
64 {
65 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
66 throw new RestException(401);
67 }
68
69 $member = new Adherent($this->db);
70 if ($id == 0) {
71 $result = $member->initAsSpecimen();
72 } else {
73 $result = $member->fetch($id);
74 }
75 if (!$result) {
76 throw new RestException(404, 'member not found');
77 }
78
79 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id) && $id > 0) {
80 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 }
82
83 return $this->_cleanObjectDatas($member);
84 }
85
100 public function getByThirdparty($thirdparty)
101 {
102 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
103 throw new RestException(401);
104 }
105
106 $member = new Adherent($this->db);
107 $result = $member->fetch('', '', $thirdparty);
108 if (!$result) {
109 throw new RestException(404, 'member not found');
110 }
111
112 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
113 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
114 }
115
116 return $this->_cleanObjectDatas($member);
117 }
118
133 public function getByThirdpartyEmail($email)
134 {
135 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
136 throw new RestException(401);
137 }
138
139 $thirdparty = new Societe($this->db);
140 $result = $thirdparty->fetch('', '', '', '', '', '', '', '', '', '', $email);
141 if (!$result) {
142 throw new RestException(404, 'thirdparty not found');
143 }
144
145 $member = new Adherent($this->db);
146 $result = $member->fetch('', '', $thirdparty->id);
147 if (!$result) {
148 throw new RestException(404, 'member not found');
149 }
150
151 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
152 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
153 }
154
155 return $this->_cleanObjectDatas($member);
156 }
157
172 public function getByThirdpartyBarcode($barcode)
173 {
174 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
175 throw new RestException(401);
176 }
177
178 $thirdparty = new Societe($this->db);
179 $result = $thirdparty->fetch('', '', '', $barcode);
180 if (!$result) {
181 throw new RestException(404, 'thirdparty not found');
182 }
183
184 $member = new Adherent($this->db);
185 $result = $member->fetch('', '', $thirdparty->id);
186 if (!$result) {
187 throw new RestException(404, 'member not found');
188 }
189
190 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
191 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
192 }
193
194 return $this->_cleanObjectDatas($member);
195 }
196
215 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $typeid = '', $category = 0, $sqlfilters = '', $properties = '')
216 {
217 global $db, $conf;
218
219 $obj_ret = array();
220
221 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
222 throw new RestException(401);
223 }
224
225 $sql = "SELECT t.rowid";
226 $sql .= " FROM ".MAIN_DB_PREFIX."adherent AS t LEFT JOIN ".MAIN_DB_PREFIX."adherent_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call
227 if ($category > 0) {
228 $sql .= ", ".MAIN_DB_PREFIX."categorie_member as c";
229 }
230 $sql .= ' WHERE t.entity IN ('.getEntity('adherent').')';
231 if (!empty($typeid)) {
232 $sql .= ' AND t.fk_adherent_type='.((int) $typeid);
233 }
234 // Select members of given category
235 if ($category > 0) {
236 $sql .= " AND c.fk_categorie = ".((int) $category);
237 $sql .= " AND c.fk_member = t.rowid";
238 }
239 // Add sql filters
240 if ($sqlfilters) {
241 $errormessage = '';
242 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
243 if ($errormessage) {
244 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
245 }
246 }
247
248 $sql .= $this->db->order($sortfield, $sortorder);
249 if ($limit) {
250 if ($page < 0) {
251 $page = 0;
252 }
253 $offset = $limit * $page;
254
255 $sql .= $this->db->plimit($limit + 1, $offset);
256 }
257
258 $result = $this->db->query($sql);
259 if ($result) {
260 $i = 0;
261 $num = $this->db->num_rows($result);
262 $min = min($num, ($limit <= 0 ? $num : $limit));
263 while ($i < $min) {
264 $obj = $this->db->fetch_object($result);
265 $member = new Adherent($this->db);
266 if ($member->fetch($obj->rowid)) {
267 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($member), $properties);
268 }
269 $i++;
270 }
271 } else {
272 throw new RestException(503, 'Error when retrieve member list : '.$this->db->lasterror());
273 }
274
275 return $obj_ret;
276 }
277
284 public function post($request_data = null)
285 {
286 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
287 throw new RestException(401);
288 }
289 // Check mandatory fields
290 $result = $this->_validate($request_data);
291
292 $member = new Adherent($this->db);
293 foreach ($request_data as $field => $value) {
294 if ($field === 'caller') {
295 // 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 whith the caller
296 $member->context['caller'] = $request_data['caller'];
297 continue;
298 }
299
300 $member->$field = $value;
301 }
302 if ($member->create(DolibarrApiAccess::$user) < 0) {
303 throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
304 }
305 return $member->id;
306 }
307
315 public function put($id, $request_data = null)
316 {
317 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
318 throw new RestException(401);
319 }
320
321 $member = new Adherent($this->db);
322 $result = $member->fetch($id);
323 if (!$result) {
324 throw new RestException(404, 'member not found');
325 }
326
327 if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
328 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
329 }
330
331 foreach ($request_data as $field => $value) {
332 if ($field == 'id') {
333 continue;
334 }
335 if ($field === 'caller') {
336 // 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 whith the caller
337 $member->context['caller'] = $request_data['caller'];
338 continue;
339 }
340
341 // Process the status separately because it must be updated using
342 // the validate(), resiliate() and exclude() methods of the class Adherent.
343 if ($field == 'statut') {
344 if ($value == '0') {
345 $result = $member->resiliate(DolibarrApiAccess::$user);
346 if ($result < 0) {
347 throw new RestException(500, 'Error when resiliating member: '.$member->error);
348 }
349 } elseif ($value == '1') {
350 $result = $member->validate(DolibarrApiAccess::$user);
351 if ($result < 0) {
352 throw new RestException(500, 'Error when validating member: '.$member->error);
353 }
354 } elseif ($value == '-2') {
355 $result = $member->exclude(DolibarrApiAccess::$user);
356 if ($result < 0) {
357 throw new RestException(500, 'Error when excluding member: '.$member->error);
358 }
359 }
360 } else {
361 $member->$field = $value;
362 }
363 }
364
365 // If there is no error, update() returns the number of affected rows
366 // so if the update is a no op, the return value is zero.
367 if ($member->update(DolibarrApiAccess::$user) >= 0) {
368 return $this->get($id);
369 } else {
370 throw new RestException(500, 'Error when updating member: '.$member->error);
371 }
372 }
373
380 public function delete($id)
381 {
382 if (!DolibarrApiAccess::$user->hasRight('adherent', 'supprimer')) {
383 throw new RestException(401);
384 }
385 $member = new Adherent($this->db);
386 $result = $member->fetch($id);
387 if (!$result) {
388 throw new RestException(404, 'member not found');
389 }
390
391 if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
392 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
393 }
394
395
396 $res = $member->delete($member->id, DolibarrApiAccess::$user);
397 if ($res < 0) {
398 throw new RestException(500, "Can't delete, error occurs");
399 } elseif ($res == 0) {
400 throw new RestException(409, "Can't delete, that product is probably used");
401 }
402
403 return array(
404 'success' => array(
405 'code' => 200,
406 'message' => 'Member deleted'
407 )
408 );
409 }
410
419 private function _validate($data)
420 {
421 $member = array();
422 foreach (Members::$FIELDS as $field) {
423 if (!isset($data[$field])) {
424 throw new RestException(400, "$field field missing");
425 }
426 $member[$field] = $data[$field];
427 }
428 return $member;
429 }
430
431 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
438 protected function _cleanObjectDatas($object)
439 {
440 // phpcs:enable
441 $object = parent::_cleanObjectDatas($object);
442
443 // Remove the subscriptions because they are handled as a subresource.
444 unset($object->subscriptions);
445 unset($object->fk_incoterms);
446 unset($object->label_incoterms);
447 unset($object->location_incoterms);
448 unset($object->fk_delivery_address);
449 unset($object->shipping_method_id);
450
451 unset($object->total_ht);
452 unset($object->total_ttc);
453 unset($object->total_tva);
454 unset($object->total_localtax1);
455 unset($object->total_localtax2);
456
457 return $object;
458 }
459
472 public function getSubscriptions($id)
473 {
474 $obj_ret = array();
475
476 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
477 throw new RestException(401);
478 }
479
480 $member = new Adherent($this->db);
481 $result = $member->fetch($id);
482 if (!$result) {
483 throw new RestException(404, 'member not found');
484 }
485
486 $obj_ret = array();
487 foreach ($member->subscriptions as $subscription) {
488 $obj_ret[] = $this->_cleanObjectDatas($subscription);
489 }
490 return $obj_ret;
491 }
492
505 public function createSubscription($id, $start_date, $end_date, $amount, $label = '')
506 {
507 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
508 throw new RestException(401);
509 }
510
511 $member = new Adherent($this->db);
512 $result = $member->fetch($id);
513 if (!$result) {
514 throw new RestException(404, 'member not found');
515 }
516
517 return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
518 }
519
533 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
534 {
535 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
536 throw new RestException(401);
537 }
538
539 $categories = new Categorie($this->db);
540
541 $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
542
543 if ($result < 0) {
544 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
545 }
546
547 return $result;
548 }
549}
Class to manage members of a foundation.
Class to manage categories.
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.
put($id, $request_data=null)
Update member.
createSubscription($id, $start_date, $end_date, $amount, $label='')
Add a subscription for a member.
_validate($data)
Validate fields before creating an object.
getByThirdparty($thirdparty)
Get properties of a member object by linked thirdparty.
getByThirdpartyBarcode($barcode)
Get properties of a member object by linked thirdparty barcode.
__construct()
Constructor.
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a member.
getByThirdpartyEmail($email)
Get properties of a member object by linked thirdparty email.
_cleanObjectDatas($object)
Clean sensible object datas.
getSubscriptions($id)
List subscriptions of a member.
post($request_data=null)
Create member object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $typeid='', $category=0, $sqlfilters='', $properties='')
List members.
Class to manage third parties objects (customers, suppliers, prospects...)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria