22use Luracast\Restler\RestException;
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';
43 public static $FIELDS = array(
60 $this->memberstats =
new AdherentStats($this->db, DolibarrApiAccess::$user->socid, DolibarrApiAccess::$user->
id);
74 public function get(
$id)
76 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
77 throw new RestException(403);
82 $result = $member->initAsSpecimen();
84 $result = $member->fetch(
$id);
87 throw new RestException(404,
'member not found');
91 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
113 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
114 throw new RestException(403);
118 $result = $member->fetch(0,
'', $thirdparty);
120 throw new RestException(404,
'member not found');
124 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
144 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
145 throw new RestException(403);
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').
")";
152 $result = $this->db->query($sql);
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);
160 throw new RestException(404,
'thirdparty not found');
164 $result = $member->fetch(0,
'', $thirdparty->id);
166 throw new RestException(404,
'member not found');
169 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
173 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
195 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
196 throw new RestException(403);
199 $thirdparty =
new Societe($this->db);
200 $result = $thirdparty->fetch(0,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
202 throw new RestException(404,
'thirdparty not found');
206 $result = $member->fetch(0,
'', $thirdparty->id);
208 throw new RestException(404,
'member not found');
212 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
234 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
235 throw new RestException(403);
238 $thirdparty =
new Societe($this->db);
239 $result = $thirdparty->fetch(0,
'',
'', $barcode);
241 throw new RestException(404,
'thirdparty not found');
245 $result = $member->fetch(0,
'', $thirdparty->id);
247 throw new RestException(404,
'member not found');
251 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
281 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $typeid =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
285 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
286 throw new RestException(403);
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)";
292 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_member as c";
294 $sql .=
' WHERE t.entity IN ('.getEntity(
'adherent').
')';
295 if (!empty($typeid)) {
296 $sql .=
' AND t.fk_adherent_type='.((int) $typeid);
300 $sql .=
" AND c.fk_categorie = ".((int) $category);
301 $sql .=
" AND c.fk_member = t.rowid";
308 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
313 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
315 $sql .= $this->db->order($sortfield, $sortorder);
320 $offset = $limit * $page;
322 $sql .= $this->db->plimit($limit + 1, $offset);
325 $result = $this->db->query($sql);
328 $num = $this->db->num_rows($result);
329 $min = min($num, ($limit <= 0 ? $num : $limit));
331 $obj = $this->db->fetch_object($result);
333 if ($member->fetch($obj->rowid)) {
339 throw new RestException(503,
'Error when retrieve member list : '.$this->db->lasterror());
343 if ($pagination_data) {
344 $totalsResult = $this->db->query($sqlTotals);
345 $total = $this->db->fetch_object($totalsResult)->total;
350 $obj_ret[
'data'] = $tmp;
351 $obj_ret[
'pagination'] = [
352 'total' => (int) $total,
354 'page_count' => ceil((
int) $total / $limit),
373 public function post($request_data =
null)
375 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
376 throw new RestException(403);
379 $result = $this->
_validate($request_data);
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'))) {
385 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs");
387 if (in_array($field, array(
'user_id')) && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
389 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs without permission user->user->create");
391 if ($field ===
'caller') {
393 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
399 if ($member->create(DolibarrApiAccess::$user) < 0) {
400 throw new RestException(500,
'Error creating member', array_merge(array($member->error), $member->errors));
418 public function put(
$id, $request_data =
null)
420 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
421 throw new RestException(403);
425 $result = $member->fetch(
$id);
427 throw new RestException(404,
'member not found');
431 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
434 $sav_user_id = $member->user_id;
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'))) {
439 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs");
441 if (in_array($field, array(
'user_id')) && !DolibarrApiAccess::$user->hasRight(
'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");
445 if ($field ==
'id') {
448 if ($field ===
'caller') {
450 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
453 if ($field ==
'array_options' && is_array($value)) {
454 foreach ($value as $index => $val) {
461 if ($field ==
'statut') {
463 $result = $member->resiliate(DolibarrApiAccess::$user);
465 throw new RestException(500,
'Error when resiliating member: '.$member->error);
467 } elseif ($value ==
'1') {
468 $result = $member->validate(DolibarrApiAccess::$user);
470 throw new RestException(500,
'Error when validating member: '.$member->error);
472 } elseif ($value ==
'-2') {
473 $result = $member->exclude(DolibarrApiAccess::$user);
475 throw new RestException(500,
'Error when excluding member: '.$member->error);
487 if ($member->update(DolibarrApiAccess::$user, 0, $nosyncuser, $nosyncpassword) >= 0) {
488 return $this->
get(
$id);
490 throw new RestException(500,
'Error when updating member: '.$member->error);
506 public function delete(
$id)
508 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'supprimer')) {
509 throw new RestException(403);
512 $result = $member->fetch(
$id);
514 throw new RestException(404,
'member not found');
518 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
522 $res = $member->delete(DolibarrApiAccess::$user);
524 throw new RestException(500,
"Can't delete, error occurs");
530 'message' =>
'Member deleted'
546 if ($data ===
null) {
551 $mandatoryfields = array(
555 foreach ($mandatoryfields as $field) {
556 if (!isset($data[$field])) {
557 throw new RestException(400,
"$field field missing");
559 $member[$field] = $data[$field];
583 unset(
$object->label_incoterms);
584 unset(
$object->location_incoterms);
585 unset(
$object->fk_delivery_address);
586 unset(
$object->shipping_method_id);
594 unset(
$object->total_localtax1);
595 unset(
$object->total_localtax2);
599 unset(
$object->linkedObjectsIds);
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);
627 unset(
$object->label_incoterms);
628 unset(
$object->location_incoterms);
635 unset(
$object->total_localtax1);
636 unset(
$object->total_localtax2);
671 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
672 throw new RestException(403);
676 $result = $member->fetch(
$id);
678 throw new RestException(404,
'member not found');
682 foreach ($member->subscriptions as $subscription) {
707 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
708 throw new RestException(403);
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');
713 if ($start_date > $end_date) {
714 throw new RestException(422,
'Malformed data: subscription start is not larger than end date');
718 $result = $member->fetch(
$id);
720 throw new RestException(404,
'member not found');
723 $result = $member->subscription((
int) $start_date, (
float) $amount, 0,
'', $label,
'',
'',
'', (
int) $end_date);
725 throw new RestException(500, $member->error);
748 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
750 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
751 throw new RestException(403);
755 $result = $member->fetch(
$id);
757 throw new RestException(404,
'Member not found');
762 $result = $categories->getListForItem(
$id,
'member', $sortfield, $sortorder, $limit, $page);
765 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
789 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
790 throw new RestException(403);
794 $result = $membertype->fetch(
$id);
796 throw new RestException(404,
'member type not found');
800 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
828 public function indexType($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
832 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
833 throw new RestException(403);
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)";
838 $sql .=
' WHERE t.entity IN ('.getEntity(
'member_type').
')';
845 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
850 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
852 $sql .= $this->db->order($sortfield, $sortorder);
857 $offset = $limit * $page;
859 $sql .= $this->db->plimit($limit + 1, $offset);
862 $result = $this->db->query($sql);
865 $num = $this->db->num_rows($result);
866 $min = min($num, ($limit <= 0 ? $num : $limit));
868 $obj = $this->db->fetch_object($result);
870 if ($membertype->fetch($obj->rowid)) {
876 throw new RestException(503,
'Error when retrieve member type list : '.$this->db->lasterror());
880 if ($pagination_data) {
881 $totalsResult = $this->db->query($sqlTotals);
882 $total = $this->db->fetch_object($totalsResult)->total;
887 $obj_ret[
'data'] = $tmp;
888 $obj_ret[
'pagination'] = [
889 'total' => (int) $total,
891 'page_count' => ceil((
int) $total / $limit),
914 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
915 throw new RestException(403);
921 foreach ($request_data as $field => $value) {
922 if ($field ===
'caller') {
924 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
928 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
930 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
931 throw new RestException(500,
'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
933 return $membertype->id;
953 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
954 throw new RestException(403);
958 $result = $membertype->fetch(
$id);
960 throw new RestException(404,
'member type not found');
964 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
967 foreach ($request_data as $field => $value) {
968 if ($field ==
'id') {
971 if ($field ===
'caller') {
973 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
976 if ($field ==
'array_options' && is_array($value)) {
977 foreach ($value as $index => $val) {
978 $membertype->array_options[$index] = $val;
984 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
989 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
990 return $this->
get(
$id);
992 throw new RestException(500,
'Error when updating member type: '.$membertype->error);
1012 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
1013 throw new RestException(403);
1016 $result = $membertype->fetch(
$id);
1018 throw new RestException(404,
'member type not found');
1022 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1025 $res = $membertype->delete(DolibarrApiAccess::$user);
1027 throw new RestException(500,
"Can't delete, error occurs");
1033 'message' =>
'Member type deleted'
1054 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1055 throw new RestException(403);
1058 return $this->memberstats->getNbByMonth($year, $format);
1073 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1074 throw new RestException(403);
1077 return $this->memberstats->getNbByYear();
1094 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1095 throw new RestException(403);
1098 return $this->memberstats->getAmountByMonth($year, $format);
1116 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
1117 throw new RestException(403);
1120 return $this->memberstats->getLastModifiedMembers($max);
1133 $membertype = array();
1135 $mandatoryfields = array(
'label');
1137 foreach ($mandatoryfields as $field) {
1138 if (!isset($data[$field])) {
1139 throw new RestException(400,
"$field field missing");
1141 $membertype[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage members of a foundation.
Class to manage statistics of members.
Class to manage members type.
Class to manage categories.
_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.
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.