dolibarr 24.0.0-beta
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-2025 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 ($field === 'caller') {
384 // 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
385 $member->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
386 continue;
387 }
388
389 $member->$field = $this->_checkValForAPI($field, $value, $member);
390 }
391 if ($member->create(DolibarrApiAccess::$user) < 0) {
392 throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
393 }
394 return $member->id;
395 }
396
410 public function put($id, $request_data = null)
411 {
412 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
413 throw new RestException(403);
414 }
415
416 $member = new Adherent($this->db);
417 $result = $member->fetch($id);
418 if (!$result) {
419 throw new RestException(404, 'member not found');
420 }
421
422 if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
423 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
424 }
425
426 foreach ($request_data as $field => $value) {
427 if ($field == 'id') {
428 continue;
429 }
430 if ($field === 'caller') {
431 // 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
432 $member->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
433 continue;
434 }
435 if ($field == 'array_options' && is_array($value)) {
436 foreach ($value as $index => $val) {
437 $member->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $member);
438 }
439 continue;
440 }
441 // Process the status separately because it must be updated using
442 // the validate(), resiliate() and exclude() methods of the class Adherent.
443 if ($field == 'statut') {
444 if ($value == '0') {
445 $result = $member->resiliate(DolibarrApiAccess::$user);
446 if ($result < 0) {
447 throw new RestException(500, 'Error when resiliating member: '.$member->error);
448 }
449 } elseif ($value == '1') {
450 $result = $member->validate(DolibarrApiAccess::$user);
451 if ($result < 0) {
452 throw new RestException(500, 'Error when validating member: '.$member->error);
453 }
454 } elseif ($value == '-2') {
455 $result = $member->exclude(DolibarrApiAccess::$user);
456 if ($result < 0) {
457 throw new RestException(500, 'Error when excluding member: '.$member->error);
458 }
459 }
460 } else {
461 $member->$field = $this->_checkValForAPI($field, $value, $member);
462 }
463 }
464
465 // If there is no error, update() returns the number of affected rows
466 // so if the update is a no op, the return value is zero.
467 if ($member->update(DolibarrApiAccess::$user) >= 0) {
468 return $this->get($id);
469 } else {
470 throw new RestException(500, 'Error when updating member: '.$member->error);
471 }
472 }
473
486 public function delete($id)
487 {
488 if (!DolibarrApiAccess::$user->hasRight('adherent', 'supprimer')) {
489 throw new RestException(403);
490 }
491 $member = new Adherent($this->db);
492 $result = $member->fetch($id);
493 if (!$result) {
494 throw new RestException(404, 'member not found');
495 }
496
497 if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
498 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
499 }
500
501
502 $res = $member->delete(DolibarrApiAccess::$user);
503 if ($res < 0) {
504 throw new RestException(500, "Can't delete, error occurs");
505 }
506
507 return array(
508 'success' => array(
509 'code' => 200,
510 'message' => 'Member deleted'
511 )
512 );
513 }
514
524 private function _validate($data)
525 {
526 if ($data === null) {
527 $data = array();
528 }
529 $member = array();
530
531 $mandatoryfields = array(
532 'morphy',
533 'typeid'
534 );
535 foreach ($mandatoryfields as $field) {
536 if (!isset($data[$field])) {
537 throw new RestException(400, "$field field missing");
538 }
539 $member[$field] = $data[$field];
540 }
541 return $member;
542 }
543
544 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
554 public function _cleanObjectDatas($object)
555 {
556 // phpcs:enable
557 $object = parent::_cleanObjectDatas($object);
558
559 // Remove the subscriptions because they are handled as a subresource.
560 if ($object instanceof Adherent) {
561 unset($object->subscriptions);
562 unset($object->fk_incoterms);
563 unset($object->label_incoterms);
564 unset($object->location_incoterms);
565 unset($object->fk_delivery_address);
566 unset($object->shipping_method_id);
567
568 unset($object->total_ht);
569 unset($object->total_ttc);
570 unset($object->total_tva);
571 unset($object->total_localtax1);
572 unset($object->total_localtax2);
573 }
574
575 if ($object instanceof AdherentType) {
576 unset($object->linkedObjectsIds);
577 unset($object->context);
578 unset($object->canvas);
579 unset($object->fk_project);
580 unset($object->contact);
581 unset($object->contact_id);
582 unset($object->thirdparty);
583 unset($object->user);
584 unset($object->origin);
585 unset($object->origin_id);
586 unset($object->ref_ext);
587 unset($object->country);
588 unset($object->country_id);
589 unset($object->country_code);
590 unset($object->barcode_type);
591 unset($object->barcode_type_code);
592 unset($object->barcode_type_label);
593 unset($object->barcode_type_coder);
594 unset($object->mode_reglement_id);
595 unset($object->cond_reglement_id);
596 unset($object->cond_reglement);
597 unset($object->fk_delivery_address);
598 unset($object->shipping_method_id);
599 unset($object->model_pdf);
600 unset($object->fk_account);
601 unset($object->note_public);
602 unset($object->note_private);
603 unset($object->fk_incoterms);
604 unset($object->label_incoterms);
605 unset($object->location_incoterms);
606 unset($object->name);
607 unset($object->lastname);
608 unset($object->firstname);
609 unset($object->civility_id);
610 unset($object->total_ht);
611 unset($object->total_tva);
612 unset($object->total_localtax1);
613 unset($object->total_localtax2);
614 unset($object->total_ttc);
615 }
616
617 // Expose POST-friendly aliases on the Subscription GET response so the
618 // payload returned by GET /members/{id}/subscriptions matches the field
619 // names POST /members/{id}/subscriptions expects (see issue #38279).
620 // $dateh / $datef stay in the response for backward compatibility with
621 // existing consumers; date_start / date_end are the documented names
622 // used by the rest of the codebase (e.g. tasks, expenses, holidays).
623 if ($object instanceof Subscription) {
624 $object->date_start = $object->dateh;
625 $object->date_end = $object->datef;
626 }
627
628 return $object;
629 }
630
646 public function getSubscriptions($id)
647 {
648 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
649 throw new RestException(403);
650 }
651
652 $member = new Adherent($this->db);
653 $result = $member->fetch($id);
654 if (!$result) {
655 throw new RestException(404, 'member not found');
656 }
657
658 $obj_ret = array();
659 foreach ($member->subscriptions as $subscription) {
660 $obj_ret[] = $this->_cleanObjectDatas($subscription);
661 }
662 return $obj_ret;
663 }
664
682 public function createSubscription($id, $start_date, $end_date, $amount, $label = '')
683 {
684 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
685 throw new RestException(403);
686 }
687 if (!is_numeric($start_date) || !is_numeric($end_date) || !is_numeric($amount)) {
688 throw new RestException(422, 'Malformed data: subscription start or end date, or subscription amount, is not numeric');
689 }
690 if ($start_date > $end_date) {
691 throw new RestException(422, 'Malformed data: subscription start is not larger than end date');
692 }
693
694 $member = new Adherent($this->db);
695 $result = $member->fetch($id);
696 if (!$result) {
697 throw new RestException(404, 'member not found');
698 }
699
700 $result = $member->subscription((int) $start_date, (float) $amount, 0, '', $label, '', '', '', (int) $end_date);
701 if ($result < 1) {
702 throw new RestException(500, $member->error);
703 } else {
704 return $result;
705 }
706 }
707
725 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
726 {
727 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
728 throw new RestException(403);
729 }
730
731 $member = new Adherent($this->db);
732 $result = $member->fetch($id);
733 if (0 === $result) {
734 throw new RestException(404, 'Member not found');
735 }
736
737 $categories = new Categorie($this->db);
738
739 $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
740
741 if ($result < 0) {
742 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
743 }
744
745 return $result;
746 }
747
748
749
750
764 public function getType($id)
765 {
766 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
767 throw new RestException(403);
768 }
769
770 $membertype = new AdherentType($this->db);
771 $result = $membertype->fetch($id);
772 if (!$result) {
773 throw new RestException(404, 'member type not found');
774 }
775
776 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
777 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
778 }
779
780 return $this->_cleanObjectDatas($membertype);
781 }
782
805 public function indexType($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
806 {
807 $obj_ret = array();
808
809 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
810 throw new RestException(403);
811 }
812
813 $sql = "SELECT t.rowid";
814 $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
815 $sql .= ' WHERE t.entity IN ('.getEntity('member_type').')';
816
817 // Add sql filters
818 if ($sqlfilters) {
819 $errormessage = '';
820 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
821 if ($errormessage) {
822 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
823 }
824 }
825
826 //this query will return total orders with the filters given
827 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
828
829 $sql .= $this->db->order($sortfield, $sortorder);
830 if ($limit) {
831 if ($page < 0) {
832 $page = 0;
833 }
834 $offset = $limit * $page;
835
836 $sql .= $this->db->plimit($limit + 1, $offset);
837 }
838
839 $result = $this->db->query($sql);
840 if ($result) {
841 $i = 0;
842 $num = $this->db->num_rows($result);
843 $min = min($num, ($limit <= 0 ? $num : $limit));
844 while ($i < $min) {
845 $obj = $this->db->fetch_object($result);
846 $membertype = new AdherentType($this->db);
847 if ($membertype->fetch($obj->rowid)) {
848 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($membertype), $properties);
849 }
850 $i++;
851 }
852 } else {
853 throw new RestException(503, 'Error when retrieve member type list : '.$this->db->lasterror());
854 }
855
856 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
857 if ($pagination_data) {
858 $totalsResult = $this->db->query($sqlTotals);
859 $total = $this->db->fetch_object($totalsResult)->total;
860
861 $tmp = $obj_ret;
862 $obj_ret = [];
863
864 $obj_ret['data'] = $tmp;
865 $obj_ret['pagination'] = [
866 'total' => (int) $total,
867 'page' => $page, //count starts from 0
868 'page_count' => ceil((int) $total / $limit),
869 'limit' => $limit
870 ];
871 }
872
873 return $obj_ret;
874 }
875
889 public function postType($request_data = null)
890 {
891 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
892 throw new RestException(403);
893 }
894 // Check mandatory fields
895 $result = $this->_validateType($request_data);
896
897 $membertype = new AdherentType($this->db);
898 foreach ($request_data as $field => $value) {
899 if ($field === 'caller') {
900 // 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
901 $membertype->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
902 continue;
903 }
904
905 $membertype->$field = $this->_checkValForAPI($field, $value, $membertype);
906 }
907 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
908 throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
909 }
910 return $membertype->id;
911 }
912
928 public function putType($id, $request_data = null)
929 {
930 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
931 throw new RestException(403);
932 }
933
934 $membertype = new AdherentType($this->db);
935 $result = $membertype->fetch($id);
936 if (!$result) {
937 throw new RestException(404, 'member type not found');
938 }
939
940 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
941 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
942 }
943
944 foreach ($request_data as $field => $value) {
945 if ($field == 'id') {
946 continue;
947 }
948 if ($field === 'caller') {
949 // 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
950 $membertype->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
951 continue;
952 }
953 if ($field == 'array_options' && is_array($value)) {
954 foreach ($value as $index => $val) {
955 $membertype->array_options[$index] = $val;
956 }
957 continue;
958 }
959 // Process the status separately because it must be updated using
960 // the validate(), resiliate() and exclude() methods of the class AdherentType.
961 $membertype->$field = $this->_checkValForAPI($field, $value, $membertype);
962 }
963
964 // If there is no error, update() returns the number of affected rows
965 // so if the update is a no op, the return value is zero.
966 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
967 return $this->get($id);
968 } else {
969 throw new RestException(500, 'Error when updating member type: '.$membertype->error);
970 }
971 }
972
987 public function deleteType($id)
988 {
989 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
990 throw new RestException(403);
991 }
992 $membertype = new AdherentType($this->db);
993 $result = $membertype->fetch($id);
994 if ($result < 1) {
995 throw new RestException(404, 'member type not found');
996 }
997
998 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
999 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1000 }
1001
1002 $res = $membertype->delete(DolibarrApiAccess::$user);
1003 if ($res < 0) {
1004 throw new RestException(500, "Can't delete, error occurs");
1005 }
1006
1007 return array(
1008 'success' => array(
1009 'code' => 200,
1010 'message' => 'Member type deleted'
1011 )
1012 );
1013 }
1014
1029 public function getNbByMonth($year, $format = 0)
1030 {
1031 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1032 throw new RestException(403);
1033 }
1034
1035 return $this->memberstats->getNbByMonth($year, $format);
1036 }
1037
1048 public function getNbByYear()
1049 {
1050 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1051 throw new RestException(403);
1052 }
1053
1054 return $this->memberstats->getNbByYear();
1055 }
1056
1069 public function getAmountByMonth($year, $format = 0)
1070 {
1071 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1072 throw new RestException(403);
1073 }
1074
1075 return $this->memberstats->getAmountByMonth($year, $format);
1076 }
1077
1091 public function getLastModifiedMembers($max)
1092 {
1093 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1094 throw new RestException(403);
1095 }
1096
1097 return $this->memberstats->getLastModifiedMembers($max);
1098 }
1099
1108 private function _validateType($data)
1109 {
1110 $membertype = array();
1111
1112 $mandatoryfields = array('label');
1113
1114 foreach ($mandatoryfields as $field) {
1115 if (!isset($data[$field])) {
1116 throw new RestException(400, "$field field missing");
1117 }
1118 $membertype[$field] = $data[$field];
1119 }
1120 return $membertype;
1121 }
1122}
$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.
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.
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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.