21 use Luracast\Restler\RestException;
23 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
24 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent.class.php';
25 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
26 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
39 public static $FIELDS = array(
63 public function get($id)
65 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
66 throw new RestException(401);
71 $result = $member->initAsSpecimen();
73 $result = $member->fetch($id);
76 throw new RestException(404,
'member not found');
80 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
102 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
103 throw new RestException(401);
107 $result = $member->fetch(
'',
'', $thirdparty);
109 throw new RestException(404,
'member not found');
113 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
135 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
136 throw new RestException(401);
140 $result = $thirdparty->fetch(
'',
'',
'',
'',
'',
'',
'',
'',
'',
'', $email);
142 throw new RestException(404,
'thirdparty not found');
146 $result = $member->fetch(
'',
'', $thirdparty->id);
148 throw new RestException(404,
'member not found');
152 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
174 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
175 throw new RestException(401);
179 $result = $thirdparty->fetch(
'',
'',
'', $barcode);
181 throw new RestException(404,
'thirdparty not found');
185 $result = $member->fetch(
'',
'', $thirdparty->id);
187 throw new RestException(404,
'member not found');
191 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
214 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $typeid =
'', $category = 0, $sqlfilters =
'')
220 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
221 throw new RestException(401);
224 $sql =
"SELECT t.rowid";
225 $sql .=
" FROM ".MAIN_DB_PREFIX.
"adherent as t";
227 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_member as c";
229 $sql .=
' WHERE t.entity IN ('.getEntity(
'adherent').
')';
230 if (!empty($typeid)) {
231 $sql .=
' AND t.fk_adherent_type='.((int) $typeid);
235 $sql .=
" AND c.fk_categorie = ".((int) $category);
236 $sql .=
" AND c.fk_member = t.rowid";
242 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
244 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
245 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
248 $sql .= $this->
db->order($sortfield, $sortorder);
253 $offset = $limit * $page;
255 $sql .= $this->
db->plimit($limit + 1, $offset);
258 $result = $this->
db->query($sql);
261 $num = $this->
db->num_rows($result);
262 $min = min($num, ($limit <= 0 ? $num : $limit));
264 $obj = $this->
db->fetch_object($result);
266 if ($member->fetch($obj->rowid)) {
272 throw new RestException(503,
'Error when retrieve member list : '.$this->
db->lasterror());
274 if (!count($obj_ret)) {
275 throw new RestException(404,
'No member found');
287 public function post($request_data =
null)
289 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
290 throw new RestException(401);
293 $result = $this->
_validate($request_data);
296 foreach ($request_data as $field => $value) {
297 $member->$field = $value;
299 if ($member->create(DolibarrApiAccess::$user) < 0) {
300 throw new RestException(500,
'Error creating member', array_merge(array($member->error), $member->errors));
312 public function put($id, $request_data =
null)
314 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
315 throw new RestException(401);
319 $result = $member->fetch($id);
321 throw new RestException(404,
'member not found');
325 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
328 foreach ($request_data as $field => $value) {
329 if ($field ==
'id') {
334 if ($field ==
'statut') {
336 $result = $member->resiliate(DolibarrApiAccess::$user);
338 throw new RestException(500,
'Error when resiliating member: '.$member->error);
340 } elseif ($value ==
'1') {
341 $result = $member->validate(DolibarrApiAccess::$user);
343 throw new RestException(500,
'Error when validating member: '.$member->error);
345 } elseif ($value ==
'-2') {
346 $result = $member->exclude(DolibarrApiAccess::$user);
348 throw new RestException(500,
'Error when excluding member: '.$member->error);
352 $member->$field = $value;
358 if ($member->update(DolibarrApiAccess::$user) >= 0) {
359 return $this->
get($id);
361 throw new RestException(500,
'Error when updating member: '.$member->error);
371 public function delete($id)
373 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'supprimer')) {
374 throw new RestException(401);
377 $result = $member->fetch($id);
379 throw new RestException(404,
'member not found');
383 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
387 $res = $member->delete($member->id, DolibarrApiAccess::$user);
389 throw new RestException(500,
"Can't delete, error occurs");
390 } elseif ($res == 0) {
391 throw new RestException(409,
"Can't delete, that product is probably used");
397 'message' =>
'Member deleted'
413 foreach (Members::$FIELDS as $field) {
414 if (!isset($data[$field])) {
415 throw new RestException(400,
"$field field missing");
417 $member[$field] = $data[$field];
432 $object = parent::_cleanObjectDatas($object);
435 unset($object->subscriptions);
436 unset($object->fk_incoterms);
437 unset($object->label_incoterms);
438 unset($object->location_incoterms);
439 unset($object->fk_delivery_address);
440 unset($object->shipping_method_id);
442 unset($object->total_ht);
443 unset($object->total_ttc);
444 unset($object->total_tva);
445 unset($object->total_localtax1);
446 unset($object->total_localtax2);
467 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
468 throw new RestException(401);
472 $result = $member->fetch($id);
474 throw new RestException(404,
'member not found');
478 foreach ($member->subscriptions as $subscription) {
498 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
499 throw new RestException(401);
503 $result = $member->fetch($id);
505 throw new RestException(404,
'member not found');
508 return $member->subscription($start_date, $amount, 0,
'', $label,
'',
'',
'', $end_date);
524 public function getCategories($id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
526 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
527 throw new RestException(401);
532 $result = $categories->getListForItem($id,
'member', $sortfield, $sortorder, $limit, $page);
534 if (empty($result)) {
535 throw new RestException(404,
'No category found');
539 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
Class to manage members of a foundation.
Class to manage categories.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
put($id, $request_data=null)
Update member.
createSubscription($id, $start_date, $end_date, $amount, $label='')
Add a subscription for a member.
_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.
__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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $typeid='', $category=0, $sqlfilters='')
List members.
_cleanObjectDatas($object)
Clean sensible object datas.
getSubscriptions($id)
List subscriptions of a member.
post($request_data=null)
Create member object.
Class to manage third parties objects (customers, suppliers, prospects...)
$conf db
API class for accounts.