dolibarr 23.0.4
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->_checkValForAPI($field, $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 return $object;
618 }
619
635 public function getSubscriptions($id)
636 {
637 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
638 throw new RestException(403);
639 }
640
641 $member = new Adherent($this->db);
642 $result = $member->fetch($id);
643 if (!$result) {
644 throw new RestException(404, 'member not found');
645 }
646
647 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
648 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
649 }
650
651 $obj_ret = array();
652 foreach ($member->subscriptions as $subscription) {
653 $obj_ret[] = $this->_cleanObjectDatas($subscription);
654 }
655 return $obj_ret;
656 }
657
675 public function createSubscription($id, $start_date, $end_date, $amount, $label = '')
676 {
677 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
678 throw new RestException(403);
679 }
680 if (!is_numeric($start_date) || !is_numeric($end_date) || !is_numeric($amount)) {
681 throw new RestException(422, 'Malformed data: subscription start or end date, or subscription amount, is not numeric');
682 }
683 if ($start_date > $end_date) {
684 throw new RestException(422, 'Malformed data: subscription start is not larger than end date');
685 }
686
687 $member = new Adherent($this->db);
688 $result = $member->fetch($id);
689 if (!$result) {
690 throw new RestException(404, 'member not found');
691 }
692
693 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
694 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
695 }
696
697 $result = $member->subscription((int) $start_date, (float) $amount, 0, '', $label, '', '', '', (int) $end_date);
698 if ($result < 1) {
699 throw new RestException(500, $member->error);
700 } else {
701 return $result;
702 }
703 }
704
722 public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
723 {
724 if (!DolibarrApiAccess::$user->hasRight('categorie', 'lire')) {
725 throw new RestException(403);
726 }
727
728 $member = new Adherent($this->db);
729 $result = $member->fetch($id);
730 if (0 === $result) {
731 throw new RestException(404, 'Member not found');
732 }
733
734 if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
735 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
736 }
737
738 $categories = new Categorie($this->db);
739
740 $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
741
742 if ($result < 0) {
743 throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
744 }
745
746 return $result;
747 }
748
749
750
751
765 public function getType($id)
766 {
767 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
768 throw new RestException(403);
769 }
770
771 $membertype = new AdherentType($this->db);
772 $result = $membertype->fetch($id);
773 if (!$result) {
774 throw new RestException(404, 'member type not found');
775 }
776
777 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
778 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
779 }
780
781 return $this->_cleanObjectDatas($membertype);
782 }
783
806 public function indexType($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
807 {
808 $obj_ret = array();
809
810 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
811 throw new RestException(403);
812 }
813
814 $sql = "SELECT t.rowid";
815 $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
816 $sql .= ' WHERE t.entity IN ('.getEntity('member_type').')';
817
818 // Add sql filters
819 if ($sqlfilters) {
820 $errormessage = '';
821 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
822 if ($errormessage) {
823 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
824 }
825 }
826
827 //this query will return total orders with the filters given
828 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
829
830 $sql .= $this->db->order($sortfield, $sortorder);
831 if ($limit) {
832 if ($page < 0) {
833 $page = 0;
834 }
835 $offset = $limit * $page;
836
837 $sql .= $this->db->plimit($limit + 1, $offset);
838 }
839
840 $result = $this->db->query($sql);
841 if ($result) {
842 $i = 0;
843 $num = $this->db->num_rows($result);
844 $min = min($num, ($limit <= 0 ? $num : $limit));
845 while ($i < $min) {
846 $obj = $this->db->fetch_object($result);
847 $membertype = new AdherentType($this->db);
848 if ($membertype->fetch($obj->rowid)) {
849 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($membertype), $properties);
850 }
851 $i++;
852 }
853 } else {
854 throw new RestException(503, 'Error when retrieve member type list : '.$this->db->lasterror());
855 }
856
857 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
858 if ($pagination_data) {
859 $totalsResult = $this->db->query($sqlTotals);
860 $total = $this->db->fetch_object($totalsResult)->total;
861
862 $tmp = $obj_ret;
863 $obj_ret = [];
864
865 $obj_ret['data'] = $tmp;
866 $obj_ret['pagination'] = [
867 'total' => (int) $total,
868 'page' => $page, //count starts from 0
869 'page_count' => ceil((int) $total / $limit),
870 'limit' => $limit
871 ];
872 }
873
874 return $obj_ret;
875 }
876
890 public function postType($request_data = null)
891 {
892 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
893 throw new RestException(403);
894 }
895 // Check mandatory fields
896 $result = $this->_validateType($request_data);
897
898 $membertype = new AdherentType($this->db);
899 foreach ($request_data as $field => $value) {
900 if ($field === 'caller') {
901 // 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
902 $membertype->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
903 continue;
904 }
905
906 $membertype->$field = $this->_checkValForAPI($field, $value, $membertype);
907 }
908 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
909 throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
910 }
911 return $membertype->id;
912 }
913
929 public function putType($id, $request_data = null)
930 {
931 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
932 throw new RestException(403);
933 }
934
935 $membertype = new AdherentType($this->db);
936 $result = $membertype->fetch($id);
937 if (!$result) {
938 throw new RestException(404, 'member type not found');
939 }
940
941 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
942 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
943 }
944
945 foreach ($request_data as $field => $value) {
946 if ($field == 'id') {
947 continue;
948 }
949 if ($field === 'caller') {
950 // 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
951 $membertype->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
952 continue;
953 }
954 if ($field == 'array_options' && is_array($value)) {
955 foreach ($value as $index => $val) {
956 $membertype->array_options[$index] = $val;
957 }
958 continue;
959 }
960 // Process the status separately because it must be updated using
961 // the validate(), resiliate() and exclude() methods of the class AdherentType.
962 $membertype->$field = $this->_checkValForAPI($field, $value, $membertype);
963 }
964
965 // If there is no error, update() returns the number of affected rows
966 // so if the update is a no op, the return value is zero.
967 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
968 return $this->get($id);
969 } else {
970 throw new RestException(500, 'Error when updating member type: '.$membertype->error);
971 }
972 }
973
988 public function deleteType($id)
989 {
990 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
991 throw new RestException(403);
992 }
993 $membertype = new AdherentType($this->db);
994 $result = $membertype->fetch($id);
995 if ($result < 1) {
996 throw new RestException(404, 'member type not found');
997 }
998
999 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
1000 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
1001 }
1002
1003 $res = $membertype->delete(DolibarrApiAccess::$user);
1004 if ($res < 0) {
1005 throw new RestException(500, "Can't delete, error occurs");
1006 }
1007
1008 return array(
1009 'success' => array(
1010 'code' => 200,
1011 'message' => 'Member type deleted'
1012 )
1013 );
1014 }
1015
1030 public function getNbByMonth($year, $format = 0)
1031 {
1032 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1033 throw new RestException(403);
1034 }
1035
1036 return $this->memberstats->getNbByMonth($year, $format);
1037 }
1038
1049 public function getNbByYear()
1050 {
1051 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1052 throw new RestException(403);
1053 }
1054
1055 return $this->memberstats->getNbByYear();
1056 }
1057
1070 public function getAmountByMonth($year, $format = 0)
1071 {
1072 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1073 throw new RestException(403);
1074 }
1075
1076 return $this->memberstats->getAmountByMonth($year, $format);
1077 }
1078
1092 public function getLastModifiedMembers($max)
1093 {
1094 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
1095 throw new RestException(403);
1096 }
1097
1098 return $this->memberstats->getLastModifiedMembers($max);
1099 }
1100
1109 private function _validateType($data)
1110 {
1111 $membertype = array();
1112
1113 $mandatoryfields = array('label');
1114
1115 foreach ($mandatoryfields as $field) {
1116 if (!isset($data[$field])) {
1117 throw new RestException(400, "$field field missing");
1118 }
1119 $membertype[$field] = $data[$field];
1120 }
1121 return $membertype;
1122 }
1123}
$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:33
_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:98
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...)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.