dolibarr 25.0.0-alpha
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-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22use Luracast\Restler\RestException;
23
24require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
25require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
26require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
27require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
28require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent_type.class.php';
29require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherentstats.class.php';
30
31
38class Members extends DolibarrApi
39{
43 public static $FIELDS = array(
44 'morphy',
45 'typeid'
46 );
47
51 public $memberstats;
52
56 public function __construct()
57 {
58 global $db;
59 $this->db = $db;
60 $this->memberstats = new AdherentStats($this->db, DolibarrApiAccess::$user->socid, DolibarrApiAccess::$user->id);
61 }
62
74 public function get($id)
75 {
76 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
77 throw new RestException(403);
78 }
79
80 $member = new Adherent($this->db);
81 if ($id == 0) {
82 $result = $member->initAsSpecimen();
83 } else {
84 $result = $member->fetch($id);
85 }
86 if (!$result) {
87 throw new RestException(404, 'member not found');
88 }
89
90 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id) && $id > 0) {
91 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
92 }
93
94 return $this->_cleanObjectDatas($member);
95 }
96
111 public function getByThirdparty($thirdparty)
112 {
113 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
114 throw new RestException(403);
115 }
116
117 $member = new Adherent($this->db);
118 $result = $member->fetch(0, '', $thirdparty);
119 if (!$result) {
120 throw new RestException(404, 'member not found');
121 }
122
123 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
124 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
125 }
126
127 return $this->_cleanObjectDatas($member);
128 }
129
142 public function getByThirdpartyAccounts($site, $key_account)
143 {
144 if (!DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
145 throw new RestException(403);
146 }
147
148 $sql = "SELECT rowid, fk_soc as socid, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX."societe_account";
149 $sql .= " WHERE site = '".$this->db->escape($site)."' AND key_account = '".$this->db->escape($key_account)."'";
150 $sql .= " AND entity IN (".getEntity('adherent').")";
151
152 $result = $this->db->query($sql);
153
154 if ($result && $this->db->num_rows($result) == 1) {
155 $obj = $this->db->fetch_object($result);
156 $thirdparty = new Societe($this->db);
157 $result = $thirdparty->fetch($obj->socid);
158
159 if ($result <= 0) {
160 throw new RestException(404, 'thirdparty not found');
161 }
162
163 $member = new Adherent($this->db);
164 $result = $member->fetch(0, '', $thirdparty->id);
165 if (!$result) {
166 throw new RestException(404, 'member not found');
167 }
168 } else {
169 throw new RestException(404, 'This account have many thirdparties attached or does not exist.');
170 }
171
172 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
173 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
174 }
175
176 return $this->_cleanObjectDatas($member);
177 }
178
193 public function getByThirdpartyEmail($email)
194 {
195 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
196 throw new RestException(403);
197 }
198
199 $thirdparty = new Societe($this->db);
200 $result = $thirdparty->fetch(0, '', '', '', '', '', '', '', '', '', $email);
201 if (!$result) {
202 throw new RestException(404, 'thirdparty not found');
203 }
204
205 $member = new Adherent($this->db);
206 $result = $member->fetch(0, '', $thirdparty->id);
207 if (!$result) {
208 throw new RestException(404, 'member not found');
209 }
210
211 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
212 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
213 }
214
215 return $this->_cleanObjectDatas($member);
216 }
217
232 public function getByThirdpartyBarcode($barcode)
233 {
234 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
235 throw new RestException(403);
236 }
237
238 $thirdparty = new Societe($this->db);
239 $result = $thirdparty->fetch(0, '', '', $barcode);
240 if (!$result) {
241 throw new RestException(404, 'thirdparty not found');
242 }
243
244 $member = new Adherent($this->db);
245 $result = $member->fetch(0, '', $thirdparty->id);
246 if (!$result) {
247 throw new RestException(404, 'member not found');
248 }
249
250 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
251 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
252 }
253
254 return $this->_cleanObjectDatas($member);
255 }
256
281 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $typeid = '', $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
282 {
283 $obj_ret = array();
284
285 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
286 throw new RestException(403);
287 }
288
289 $sql = "SELECT t.rowid";
290 $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
291 if ($category > 0) {
292 $sql .= ", ".MAIN_DB_PREFIX."categorie_member as c";
293 }
294 $sql .= ' WHERE t.entity IN ('.getEntity('adherent').')';
295 if (!empty($typeid)) {
296 $sql .= ' AND t.fk_adherent_type='.((int) $typeid);
297 }
298 // Select members of given category
299 if ($category > 0) {
300 $sql .= " AND c.fk_categorie = ".((int) $category);
301 $sql .= " AND c.fk_member = t.rowid";
302 }
303 // Add sql filters
304 if ($sqlfilters) {
305 $errormessage = '';
306 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
307 if ($errormessage) {
308 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
309 }
310 }
311
312 //this query will return total orders with the filters given
313 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
314
315 $sql .= $this->db->order($sortfield, $sortorder);
316 if ($limit) {
317 if ($page < 0) {
318 $page = 0;
319 }
320 $offset = $limit * $page;
321
322 $sql .= $this->db->plimit($limit + 1, $offset);
323 }
324
325 $result = $this->db->query($sql);
326 if ($result) {
327 $i = 0;
328 $num = $this->db->num_rows($result);
329 $min = min($num, ($limit <= 0 ? $num : $limit));
330 while ($i < $min) {
331 $obj = $this->db->fetch_object($result);
332 $member = new Adherent($this->db);
333 if ($member->fetch($obj->rowid)) {
334 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($member), $properties);
335 }
336 $i++;
337 }
338 } else {
339 throw new RestException(503, 'Error when retrieve member list : '.$this->db->lasterror());
340 }
341
342 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
343 if ($pagination_data) {
344 $totalsResult = $this->db->query($sqlTotals);
345 $total = $this->db->fetch_object($totalsResult)->total;
346
347 $tmp = $obj_ret;
348 $obj_ret = [];
349
350 $obj_ret['data'] = $tmp;
351 $obj_ret['pagination'] = [
352 'total' => (int) $total,
353 'page' => $page, //count starts from 0
354 'page_count' => ceil((int) $total / $limit),
355 'limit' => $limit
356 ];
357 }
358
359 return $obj_ret;
360 }
361
373 public function post($request_data = null)
374 {
375 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
376 throw new RestException(403);
377 }
378 // Check mandatory fields
379 $result = $this->_validate($request_data);
380
381 $member = new Adherent($this->db);
382 foreach ($request_data as $field => $value) {
383 if (in_array($field, array('pass', 'pass_crypted', 'pass_indatabase', 'pass_indatabase_crypted', 'pass_temp', 'api_key'))) {
384 // This properties can't be set/modified with API
385 throw new RestException(405, 'The property '.$field." can't be set/modified using the APIs");
386 }
387 if (in_array($field, array('user_id')) && !DolibarrApiAccess::$user->hasRight('user', 'user', 'creer')) {
388 // This properties can't be set/modified with API without permission user->user->creer
389 throw new RestException(405, 'The property '.$field." can't be set/modified using the APIs without permission user->user->create");
390 }
391 if ($field === 'caller') {
392 // 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
393 $member->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
394 continue;
395 }
396
397 $member->$field = $this->_checkValForAPI($field, $value, $member);
398 }
399 if ($member->create(DolibarrApiAccess::$user) < 0) {
400 throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
401 }
402 return $member->id;
403 }
404
418 public function put($id, $request_data = null)
419 {
420 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
421 throw new RestException(403);
422 }
423
424 $member = new Adherent($this->db);
425 $result = $member->fetch($id);
426 if (!$result) {
427 throw new RestException(404, 'member not found');
428 }
429
430 if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
431 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
432 }
433
434 $sav_user_id = $member->user_id;
435
436 foreach ($request_data as $field => $value) {
437 if (in_array($field, array('pass', 'pass_crypted', 'pass_indatabase', 'pass_indatabase_crypted', 'pass_temp', 'api_key'))) {
438 // This properties can't be set/modified with API
439 throw new RestException(405, 'The property '.$field." can't be set/modified using the APIs");
440 }
441 if (in_array($field, array('user_id')) && !DolibarrApiAccess::$user->hasRight('user', 'user', 'creer')) {
442 // This properties can't be set/modified with API without permission user->user->creer
443 throw new RestException(405, 'The property '.$field." can't be set/modified using the APIs without the permission user->user->create");
444 }
445 if ($field == 'id') {
446 continue;
447 }
448 if ($field === 'caller') {
449 // 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
450 $member->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
451 continue;
452 }
453 if ($field == 'array_options' && is_array($value)) {
454 foreach ($value as $index => $val) {
455 $member->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $member);
456 }
457 continue;
458 }
459 // Process the status separately because it must be updated using
460 // the validate(), resiliate() and exclude() methods of the class Adherent.
461 if ($field == 'statut') {
462 if ($value == '0') {
463 $result = $member->resiliate(DolibarrApiAccess::$user);
464 if ($result < 0) {
465 throw new RestException(500, 'Error when resiliating member: '.$member->error);
466 }
467 } elseif ($value == '1') {
468 $result = $member->validate(DolibarrApiAccess::$user);
469 if ($result < 0) {
470 throw new RestException(500, 'Error when validating member: '.$member->error);
471 }
472 } elseif ($value == '-2') {
473 $result = $member->exclude(DolibarrApiAccess::$user);
474 if ($result < 0) {
475 throw new RestException(500, 'Error when excluding member: '.$member->error);
476 }
477 }
478 } else {
479 $member->$field = $this->_checkValForAPI($field, $value, $member);
480 }
481 }
482
483 // If there is no error, update() returns the number of affected rows
484 // so if the update is a no op, the return value is zero.
485 $nosyncuser = 0;
486 $nosyncpassword = 1; // Disable password sync. Management of password must be done using the user API only.
487 if ($member->update(DolibarrApiAccess::$user, 0, $nosyncuser, $nosyncpassword) >= 0) {
488 return $this->get($id);
489 } else {
490 throw new RestException(500, 'Error when updating member: '.$member->error);
491 }
492 }
493
506 public function delete($id)
507 {
508 if (!DolibarrApiAccess::$user->hasRight('adherent', 'supprimer')) {
509 throw new RestException(403);
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 if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
518 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
519 }
520
521
522 $res = $member->delete(DolibarrApiAccess::$user);
523 if ($res < 0) {
524 throw new RestException(500, "Can't delete, error occurs");
525 }
526
527 return array(
528 'success' => array(
529 'code' => 200,
530 'message' => 'Member deleted'
531 )
532 );
533 }
534
544 private function _validate($data)
545 {
546 if ($data === null) {
547 $data = array();
548 }
549 $member = array();
550
551 $mandatoryfields = array(
552 'morphy',
553 'typeid'
554 );
555 foreach ($mandatoryfields as $field) {
556 if (!isset($data[$field])) {
557 throw new RestException(400, "$field field missing");
558 }
559 $member[$field] = $data[$field];
560 }
561 return $member;
562 }
563
564 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
574 public function _cleanObjectDatas($object)
575 {
576 // phpcs:enable
577 $object = parent::_cleanObjectDatas($object);
578
579 // Remove the subscriptions because they are handled as a subresource.
580 if ($object instanceof Adherent) {
581 unset($object->subscriptions);
582 unset($object->fk_incoterms);
583 unset($object->label_incoterms);
584 unset($object->location_incoterms);
585 unset($object->fk_delivery_address);
586 unset($object->shipping_method_id);
587
588 unset($object->pass);
589 unset($object->pass_crypted);
590
591 unset($object->total_ht);
592 unset($object->total_ttc);
593 unset($object->total_tva);
594 unset($object->total_localtax1);
595 unset($object->total_localtax2);
596 }
597
598 if ($object instanceof AdherentType) {
599 unset($object->linkedObjectsIds);
600 unset($object->context);
601 unset($object->canvas);
602 unset($object->fk_project);
603 unset($object->contact);
604 unset($object->contact_id);
605 unset($object->thirdparty);
606 unset($object->user);
607 unset($object->origin);
608 unset($object->origin_id);
609 unset($object->ref_ext);
610 unset($object->country);
611 unset($object->country_id);
612 unset($object->country_code);
613 unset($object->barcode_type);
614 unset($object->barcode_type_code);
615 unset($object->barcode_type_label);
616 unset($object->barcode_type_coder);
617 unset($object->mode_reglement_id);
618 unset($object->cond_reglement_id);
619 unset($object->cond_reglement);
620 unset($object->fk_delivery_address);
621 unset($object->shipping_method_id);
622 unset($object->model_pdf);
623 unset($object->fk_account);
624 unset($object->note_public);
625 unset($object->note_private);
626 unset($object->fk_incoterms);
627 unset($object->label_incoterms);
628 unset($object->location_incoterms);
629 unset($object->name);
630 unset($object->lastname);
631 unset($object->firstname);
632 unset($object->civility_id);
633 unset($object->total_ht);
634 unset($object->total_tva);
635 unset($object->total_localtax1);
636 unset($object->total_localtax2);
637 unset($object->total_ttc);
638 }
639
640 // Expose POST-friendly aliases on the Subscription GET response so the
641 // payload returned by GET /members/{id}/subscriptions matches the field
642 // names POST /members/{id}/subscriptions expects (see issue #38279).
643 // $dateh / $datef stay in the response for backward compatibility with
644 // existing consumers; date_start / date_end are the documented names
645 // used by the rest of the codebase (e.g. tasks, expenses, holidays).
646 if ($object instanceof Subscription) {
647 $object->date_start = $object->dateh;
648 $object->date_end = $object->datef;
649 }
650
651 return $object;
652 }
653
669 public function getSubscriptions($id)
670 {
671 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
672 throw new RestException(403);
673 }
674
675 $member = new Adherent($this->db);
676 $result = $member->fetch($id);
677 if (!$result) {
678 throw new RestException(404, 'member not found');
679 }
680
681 $obj_ret = array();
682 foreach ($member->subscriptions as $subscription) {
683 $obj_ret[] = $this->_cleanObjectDatas($subscription);
684 }
685 return $obj_ret;
686 }
687
705 public function createSubscription($id, $start_date, $end_date, $amount, $label = '')
706 {
707 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
708 throw new RestException(403);
709 }
710 if (!is_numeric($start_date) || !is_numeric($end_date) || !is_numeric($amount)) {
711 throw new RestException(422, 'Malformed data: subscription start or end date, or subscription amount, is not numeric');
712 }
713 if ($start_date > $end_date) {
714 throw new RestException(422, 'Malformed data: subscription start is not larger than end date');
715 }
716
717 $member = new Adherent($this->db);
718 $result = $member->fetch($id);
719 if (!$result) {
720 throw new RestException(404, 'member not found');
721 }
722
723 $result = $member->subscription((int) $start_date, (float) $amount, 0, '', $label, '', '', '', (int) $end_date);
724 if ($result < 1) {
725 throw new RestException(500, $member->error);
726 } else {
727 return $result;
728 }
729 }
730
748 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
749 {
750 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
751 throw new RestException(403);
752 }
753
754 $member = new Adherent($this->db);
755 $result = $member->fetch($id);
756 if (0 === $result) {
757 throw new RestException(404, 'Member not found');
758 }
759
760 $categories = new Categorie($this->db);
761
762 $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
763
764 if ($result < 0) {
765 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
766 }
767
768 return $result;
769 }
770
771
772
773
787 public function getType($id)
788 {
789 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
790 throw new RestException(403);
791 }
792
793 $membertype = new AdherentType($this->db);
794 $result = $membertype->fetch($id);
795 if (!$result) {
796 throw new RestException(404, 'member type not found');
797 }
798
799 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
800 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
801 }
802
803 return $this->_cleanObjectDatas($membertype);
804 }
805
828 public function indexType($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
829 {
830 $obj_ret = array();
831
832 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
833 throw new RestException(403);
834 }
835
836 $sql = "SELECT t.rowid";
837 $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
838 $sql .= ' WHERE t.entity IN ('.getEntity('member_type').')';
839
840 // Add sql filters
841 if ($sqlfilters) {
842 $errormessage = '';
843 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
844 if ($errormessage) {
845 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
846 }
847 }
848
849 //this query will return total orders with the filters given
850 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
851
852 $sql .= $this->db->order($sortfield, $sortorder);
853 if ($limit) {
854 if ($page < 0) {
855 $page = 0;
856 }
857 $offset = $limit * $page;
858
859 $sql .= $this->db->plimit($limit + 1, $offset);
860 }
861
862 $result = $this->db->query($sql);
863 if ($result) {
864 $i = 0;
865 $num = $this->db->num_rows($result);
866 $min = min($num, ($limit <= 0 ? $num : $limit));
867 while ($i < $min) {
868 $obj = $this->db->fetch_object($result);
869 $membertype = new AdherentType($this->db);
870 if ($membertype->fetch($obj->rowid)) {
871 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($membertype), $properties);
872 }
873 $i++;
874 }
875 } else {
876 throw new RestException(503, 'Error when retrieve member type list : '.$this->db->lasterror());
877 }
878
879 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
880 if ($pagination_data) {
881 $totalsResult = $this->db->query($sqlTotals);
882 $total = $this->db->fetch_object($totalsResult)->total;
883
884 $tmp = $obj_ret;
885 $obj_ret = [];
886
887 $obj_ret['data'] = $tmp;
888 $obj_ret['pagination'] = [
889 'total' => (int) $total,
890 'page' => $page, //count starts from 0
891 'page_count' => ceil((int) $total / $limit),
892 'limit' => $limit
893 ];
894 }
895
896 return $obj_ret;
897 }
898
912 public function postType($request_data = null)
913 {
914 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
915 throw new RestException(403);
916 }
917 // Check mandatory fields
918 $result = $this->_validateType($request_data);
919
920 $membertype = new AdherentType($this->db);
921 foreach ($request_data as $field => $value) {
922 if ($field === 'caller') {
923 // 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
924 $membertype->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
925 continue;
926 }
927
928 $membertype->$field = $this->_checkValForAPI($field, $value, $membertype);
929 }
930 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
931 throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
932 }
933 return $membertype->id;
934 }
935
951 public function putType($id, $request_data = null)
952 {
953 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
954 throw new RestException(403);
955 }
956
957 $membertype = new AdherentType($this->db);
958 $result = $membertype->fetch($id);
959 if (!$result) {
960 throw new RestException(404, 'member type not found');
961 }
962
963 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
964 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
965 }
966
967 foreach ($request_data as $field => $value) {
968 if ($field == 'id') {
969 continue;
970 }
971 if ($field === 'caller') {
972 // 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
973 $membertype->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
974 continue;
975 }
976 if ($field == 'array_options' && is_array($value)) {
977 foreach ($value as $index => $val) {
978 $membertype->array_options[$index] = $val;
979 }
980 continue;
981 }
982 // Process the status separately because it must be updated using
983 // the validate(), resiliate() and exclude() methods of the class AdherentType.
984 $membertype->$field = $this->_checkValForAPI($field, $value, $membertype);
985 }
986
987 // If there is no error, update() returns the number of affected rows
988 // so if the update is a no op, the return value is zero.
989 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
990 return $this->get($id);
991 } else {
992 throw new RestException(500, 'Error when updating member type: '.$membertype->error);
993 }
994 }
995
1010 public function deleteType($id)
1011 {
1012 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
1013 throw new RestException(403);
1014 }
1015 $membertype = new AdherentType($this->db);
1016 $result = $membertype->fetch($id);
1017 if ($result < 1) {
1018 throw new RestException(404, 'member type not found');
1019 }
1020
1021 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
1022 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1023 }
1024
1025 $res = $membertype->delete(DolibarrApiAccess::$user);
1026 if ($res < 0) {
1027 throw new RestException(500, "Can't delete, error occurs");
1028 }
1029
1030 return array(
1031 'success' => array(
1032 'code' => 200,
1033 'message' => 'Member type deleted'
1034 )
1035 );
1036 }
1037
1052 public function getNbByMonth($year, $format = 0)
1053 {
1054 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1055 throw new RestException(403);
1056 }
1057
1058 return $this->memberstats->getNbByMonth($year, $format);
1059 }
1060
1071 public function getNbByYear()
1072 {
1073 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1074 throw new RestException(403);
1075 }
1076
1077 return $this->memberstats->getNbByYear();
1078 }
1079
1092 public function getAmountByMonth($year, $format = 0)
1093 {
1094 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1095 throw new RestException(403);
1096 }
1097
1098 return $this->memberstats->getAmountByMonth($year, $format);
1099 }
1100
1114 public function getLastModifiedMembers($max)
1115 {
1116 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1117 throw new RestException(403);
1118 }
1119
1120 return $this->memberstats->getLastModifiedMembers($max);
1121 }
1122
1131 private function _validateType($data)
1132 {
1133 $membertype = array();
1134
1135 $mandatoryfields = array('label');
1136
1137 foreach ($mandatoryfields as $field) {
1138 if (!isset($data[$field])) {
1139 throw new RestException(400, "$field field missing");
1140 }
1141 $membertype[$field] = $data[$field];
1142 }
1143 return $membertype;
1144 }
1145}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage members of a foundation.
Class to manage statistics of members.
Class to manage members type.
Class to manage categories.
Class for API REST v1.
Definition api.class.php:35
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
put($id, $request_data=null)
Update member.
getLastModifiedMembers($max)
Last Modified Members.
getType($id)
Get properties of a member type object.
createSubscription($id, $start_date, $end_date, $amount, $label='')
Add a subscription for a member.
indexType($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List members types.
_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.
deleteType($id)
Delete member type.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $typeid='', $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List members.
__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 @phpstan-template T.
getAmountByMonth($year, $format=0)
Return the number of subscriptions by month for a given year.
_validateType($data)
Validate fields before creating an object.
putType($id, $request_data=null)
Update member type.
getNbByYear()
Return an array with the number of subscriptions by year.
getSubscriptions($id)
List subscriptions of a member.
post($request_data=null)
Create member object.
getByThirdpartyAccounts($site, $key_account)
Get properties of a member object by linked thirdparty account.
getNbByMonth($year, $format=0)
Return an array with the number of members by month for a given year.
postType($request_data=null)
Create member type object.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage subscriptions of foundation members.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.