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';
42 public static $FIELDS = array(
67 public function get(
$id)
69 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
70 throw new RestException(403);
75 $result = $member->initAsSpecimen();
77 $result = $member->fetch(
$id);
80 throw new RestException(404,
'member not found');
84 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
106 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
107 throw new RestException(403);
111 $result = $member->fetch(0,
'', $thirdparty);
113 throw new RestException(404,
'member not found');
117 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
137 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
138 throw new RestException(403);
141 $sql =
"SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM ".MAIN_DB_PREFIX.
"societe_account";
142 $sql .=
" WHERE site = '".$this->db->escape($site).
"' AND key_account = '".$this->db->escape($key_account).
"'";
143 $sql .=
" AND entity IN (".getEntity(
'adherent').
")";
145 $result = $this->db->query($sql);
147 if ($result && $this->db->num_rows($result) == 1) {
148 $obj = $this->db->fetch_object($result);
149 $thirdparty =
new Societe($this->db);
150 $result = $thirdparty->fetch($obj->fk_soc);
153 throw new RestException(404,
'thirdparty not found');
157 $result = $member->fetch(0,
'', $thirdparty->id);
159 throw new RestException(404,
'member not found');
162 throw new RestException(404,
'This account have many thirdparties attached or does not exist.');
166 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
188 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
189 throw new RestException(403);
192 $thirdparty =
new Societe($this->db);
193 $result = $thirdparty->fetch(0,
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
195 throw new RestException(404,
'thirdparty not found');
199 $result = $member->fetch(0,
'', $thirdparty->id);
201 throw new RestException(404,
'member not found');
205 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
227 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
228 throw new RestException(403);
231 $thirdparty =
new Societe($this->db);
232 $result = $thirdparty->fetch(0,
'',
'', $barcode);
234 throw new RestException(404,
'thirdparty not found');
238 $result = $member->fetch(0,
'', $thirdparty->id);
240 throw new RestException(404,
'member not found');
244 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
274 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $typeid =
'', $category = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
278 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
279 throw new RestException(403);
282 $sql =
"SELECT t.rowid";
283 $sql .=
" FROM ".MAIN_DB_PREFIX.
"adherent AS t LEFT JOIN ".MAIN_DB_PREFIX.
"adherent_extrafields AS ef ON (ef.fk_object = t.rowid)";
285 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_member as c";
287 $sql .=
' WHERE t.entity IN ('.getEntity(
'adherent').
')';
288 if (!empty($typeid)) {
289 $sql .=
' AND t.fk_adherent_type='.((int) $typeid);
293 $sql .=
" AND c.fk_categorie = ".((int) $category);
294 $sql .=
" AND c.fk_member = t.rowid";
301 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
306 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
308 $sql .= $this->db->order($sortfield, $sortorder);
313 $offset = $limit * $page;
315 $sql .= $this->db->plimit($limit + 1, $offset);
318 $result = $this->db->query($sql);
321 $num = $this->db->num_rows($result);
322 $min = min($num, ($limit <= 0 ? $num : $limit));
324 $obj = $this->db->fetch_object($result);
326 if ($member->fetch($obj->rowid)) {
332 throw new RestException(503,
'Error when retrieve member list : '.$this->db->lasterror());
336 if ($pagination_data) {
337 $totalsResult = $this->db->query($sqlTotals);
338 $total = $this->db->fetch_object($totalsResult)->total;
343 $obj_ret[
'data'] = $tmp;
344 $obj_ret[
'pagination'] = [
345 'total' => (int) $total,
347 'page_count' => ceil((
int) $total / $limit),
366 public function post($request_data =
null)
368 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
369 throw new RestException(403);
372 $result = $this->
_validate($request_data);
375 foreach ($request_data as $field => $value) {
376 if ($field ===
'caller') {
378 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
384 if ($member->create(DolibarrApiAccess::$user) < 0) {
385 throw new RestException(500,
'Error creating member', array_merge(array($member->error), $member->errors));
403 public function put(
$id, $request_data =
null)
405 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
406 throw new RestException(403);
410 $result = $member->fetch(
$id);
412 throw new RestException(404,
'member not found');
416 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
419 foreach ($request_data as $field => $value) {
420 if ($field ==
'id') {
423 if ($field ===
'caller') {
425 $member->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
428 if ($field ==
'array_options' && is_array($value)) {
429 foreach ($value as $index => $val) {
430 $member->array_options[$index] = $this->
_checkValForAPI($field, $val, $member);
436 if ($field ==
'statut') {
438 $result = $member->resiliate(DolibarrApiAccess::$user);
440 throw new RestException(500,
'Error when resiliating member: '.$member->error);
442 } elseif ($value ==
'1') {
443 $result = $member->validate(DolibarrApiAccess::$user);
445 throw new RestException(500,
'Error when validating member: '.$member->error);
447 } elseif ($value ==
'-2') {
448 $result = $member->exclude(DolibarrApiAccess::$user);
450 throw new RestException(500,
'Error when excluding member: '.$member->error);
460 if ($member->update(DolibarrApiAccess::$user) >= 0) {
461 return $this->
get(
$id);
463 throw new RestException(500,
'Error when updating member: '.$member->error);
479 public function delete(
$id)
481 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'supprimer')) {
482 throw new RestException(403);
485 $result = $member->fetch(
$id);
487 throw new RestException(404,
'member not found');
491 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 $res = $member->delete(DolibarrApiAccess::$user);
497 throw new RestException(500,
"Can't delete, error occurs");
503 'message' =>
'Member deleted'
519 if ($data ===
null) {
524 $mandatoryfields = array(
528 foreach ($mandatoryfields as $field) {
529 if (!isset($data[$field])) {
530 throw new RestException(400,
"$field field missing");
532 $member[$field] = $data[$field];
553 unset(
$object->label_incoterms);
554 unset(
$object->location_incoterms);
555 unset(
$object->fk_delivery_address);
556 unset(
$object->shipping_method_id);
561 unset(
$object->total_localtax1);
562 unset(
$object->total_localtax2);
566 unset(
$object->linkedObjectsIds);
581 unset(
$object->barcode_type_code);
582 unset(
$object->barcode_type_label);
583 unset(
$object->barcode_type_coder);
584 unset(
$object->mode_reglement_id);
585 unset(
$object->cond_reglement_id);
586 unset(
$object->cond_reglement);
587 unset(
$object->fk_delivery_address);
588 unset(
$object->shipping_method_id);
594 unset(
$object->label_incoterms);
595 unset(
$object->location_incoterms);
602 unset(
$object->total_localtax1);
603 unset(
$object->total_localtax2);
627 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
628 throw new RestException(403);
632 $result = $member->fetch(
$id);
634 throw new RestException(404,
'member not found');
638 foreach ($member->subscriptions as $subscription) {
663 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
664 throw new RestException(403);
666 if (!is_numeric($start_date) || !is_numeric($end_date) || !is_numeric($amount)) {
667 throw new RestException(422,
'Malformed data: subscription start or end date, or subscription amount, is not numeric');
669 if ($start_date > $end_date) {
670 throw new RestException(422,
'Malformed data: subscription start is not larger than end date');
674 $result = $member->fetch(
$id);
676 throw new RestException(404,
'member not found');
679 $result = $member->subscription((
int) $start_date, (
float) $amount, 0,
'', $label,
'',
'',
'', (
int) $end_date);
681 throw new RestException(500, $member->error);
704 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
706 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
707 throw new RestException(403);
711 $result = $member->fetch(
$id);
713 throw new RestException(404,
'Member not found');
718 $result = $categories->getListForItem(
$id,
'member', $sortfield, $sortorder, $limit, $page);
721 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
745 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
746 throw new RestException(403);
750 $result = $membertype->fetch(
$id);
752 throw new RestException(404,
'member type not found');
756 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
784 public function indexType($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
788 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
789 throw new RestException(403);
792 $sql =
"SELECT t.rowid";
793 $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)";
794 $sql .=
' WHERE t.entity IN ('.getEntity(
'member_type').
')';
801 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
806 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
808 $sql .= $this->db->order($sortfield, $sortorder);
813 $offset = $limit * $page;
815 $sql .= $this->db->plimit($limit + 1, $offset);
818 $result = $this->db->query($sql);
821 $num = $this->db->num_rows($result);
822 $min = min($num, ($limit <= 0 ? $num : $limit));
824 $obj = $this->db->fetch_object($result);
826 if ($membertype->fetch($obj->rowid)) {
832 throw new RestException(503,
'Error when retrieve member type list : '.$this->db->lasterror());
836 if ($pagination_data) {
837 $totalsResult = $this->db->query($sqlTotals);
838 $total = $this->db->fetch_object($totalsResult)->total;
843 $obj_ret[
'data'] = $tmp;
844 $obj_ret[
'pagination'] = [
845 'total' => (int) $total,
847 'page_count' => ceil((
int) $total / $limit),
870 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
871 throw new RestException(403);
877 foreach ($request_data as $field => $value) {
878 if ($field ===
'caller') {
880 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
884 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
886 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
887 throw new RestException(500,
'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
889 return $membertype->id;
909 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
910 throw new RestException(403);
914 $result = $membertype->fetch(
$id);
916 throw new RestException(404,
'member type not found');
920 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
923 foreach ($request_data as $field => $value) {
924 if ($field ==
'id') {
927 if ($field ===
'caller') {
929 $membertype->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
932 if ($field ==
'array_options' && is_array($value)) {
933 foreach ($value as $index => $val) {
934 $membertype->array_options[$index] = $val;
940 $membertype->$field = $this->
_checkValForAPI($field, $value, $membertype);
945 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
946 return $this->
get(
$id);
948 throw new RestException(500,
'Error when updating member type: '.$membertype->error);
968 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
969 throw new RestException(403);
972 $result = $membertype->fetch(
$id);
974 throw new RestException(404,
'member type not found');
978 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
981 $res = $membertype->delete(DolibarrApiAccess::$user);
983 throw new RestException(500,
"Can't delete, error occurs");
989 'message' =>
'Member type deleted'
1004 $membertype = array();
1006 $mandatoryfields = array(
'label');
1008 foreach ($mandatoryfields as $field) {
1009 if (!isset($data[$field])) {
1010 throw new RestException(400,
"$field field missing");
1012 $membertype[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage members of a foundation.
Class to manage members type.
Class to manage categories.
_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.
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.
_validateType($data)
Validate fields before creating an object.
putType($id, $request_data=null)
Update member type.
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.
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.