dolibarr  16.0.5
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 Frédéric France <frederic.france@netlogic.fr>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 use Luracast\Restler\RestException;
22 
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';
27 
34 class Members extends DolibarrApi
35 {
39  public static $FIELDS = array(
40  'morphy',
41  'typeid'
42  );
43 
47  public function __construct()
48  {
49  global $db, $conf;
50  $this->db = $db;
51  }
52 
63  public function get($id)
64  {
65  if (!DolibarrApiAccess::$user->rights->adherent->lire) {
66  throw new RestException(401);
67  }
68 
69  $member = new Adherent($this->db);
70  if ($id == 0) {
71  $result = $member->initAsSpecimen();
72  } else {
73  $result = $member->fetch($id);
74  }
75  if (!$result) {
76  throw new RestException(404, 'member not found');
77  }
78 
79  if (!DolibarrApi::_checkAccessToResource('adherent', $member->id) && $id > 0) {
80  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
81  }
82 
83  return $this->_cleanObjectDatas($member);
84  }
85 
100  public function getByThirdparty($thirdparty)
101  {
102  if (!DolibarrApiAccess::$user->rights->adherent->lire) {
103  throw new RestException(401);
104  }
105 
106  $member = new Adherent($this->db);
107  $result = $member->fetch('', '', $thirdparty);
108  if (!$result) {
109  throw new RestException(404, 'member not found');
110  }
111 
112  if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
113  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
114  }
115 
116  return $this->_cleanObjectDatas($member);
117  }
118 
133  public function getByThirdpartyEmail($email)
134  {
135  if (!DolibarrApiAccess::$user->rights->adherent->lire) {
136  throw new RestException(401);
137  }
138 
139  $thirdparty = new Societe($this->db);
140  $result = $thirdparty->fetch('', '', '', '', '', '', '', '', '', '', $email);
141  if (!$result) {
142  throw new RestException(404, 'thirdparty not found');
143  }
144 
145  $member = new Adherent($this->db);
146  $result = $member->fetch('', '', $thirdparty->id);
147  if (!$result) {
148  throw new RestException(404, 'member not found');
149  }
150 
151  if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
152  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
153  }
154 
155  return $this->_cleanObjectDatas($member);
156  }
157 
172  public function getByThirdpartyBarcode($barcode)
173  {
174  if (!DolibarrApiAccess::$user->rights->adherent->lire) {
175  throw new RestException(401);
176  }
177 
178  $thirdparty = new Societe($this->db);
179  $result = $thirdparty->fetch('', '', '', $barcode);
180  if (!$result) {
181  throw new RestException(404, 'thirdparty not found');
182  }
183 
184  $member = new Adherent($this->db);
185  $result = $member->fetch('', '', $thirdparty->id);
186  if (!$result) {
187  throw new RestException(404, 'member not found');
188  }
189 
190  if (!DolibarrApi::_checkAccessToResource('adherent', $member->id)) {
191  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
192  }
193 
194  return $this->_cleanObjectDatas($member);
195  }
196 
214  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $typeid = '', $category = 0, $sqlfilters = '')
215  {
216  global $db, $conf;
217 
218  $obj_ret = array();
219 
220  if (!DolibarrApiAccess::$user->rights->adherent->lire) {
221  throw new RestException(401);
222  }
223 
224  $sql = "SELECT t.rowid";
225  $sql .= " FROM ".MAIN_DB_PREFIX."adherent as t";
226  if ($category > 0) {
227  $sql .= ", ".MAIN_DB_PREFIX."categorie_member as c";
228  }
229  $sql .= ' WHERE t.entity IN ('.getEntity('adherent').')';
230  if (!empty($typeid)) {
231  $sql .= ' AND t.fk_adherent_type='.((int) $typeid);
232  }
233  // Select members of given category
234  if ($category > 0) {
235  $sql .= " AND c.fk_categorie = ".((int) $category);
236  $sql .= " AND c.fk_member = t.rowid";
237  }
238  // Add sql filters
239  if ($sqlfilters) {
240  $errormessage = '';
241  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
242  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
243  }
244  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
245  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
246  }
247 
248  $sql .= $this->db->order($sortfield, $sortorder);
249  if ($limit) {
250  if ($page < 0) {
251  $page = 0;
252  }
253  $offset = $limit * $page;
254 
255  $sql .= $this->db->plimit($limit + 1, $offset);
256  }
257 
258  $result = $this->db->query($sql);
259  if ($result) {
260  $i = 0;
261  $num = $this->db->num_rows($result);
262  $min = min($num, ($limit <= 0 ? $num : $limit));
263  while ($i < $min) {
264  $obj = $this->db->fetch_object($result);
265  $member = new Adherent($this->db);
266  if ($member->fetch($obj->rowid)) {
267  $obj_ret[] = $this->_cleanObjectDatas($member);
268  }
269  $i++;
270  }
271  } else {
272  throw new RestException(503, 'Error when retrieve member list : '.$this->db->lasterror());
273  }
274  if (!count($obj_ret)) {
275  throw new RestException(404, 'No member found');
276  }
277 
278  return $obj_ret;
279  }
280 
287  public function post($request_data = null)
288  {
289  if (!DolibarrApiAccess::$user->rights->adherent->creer) {
290  throw new RestException(401);
291  }
292  // Check mandatory fields
293  $result = $this->_validate($request_data);
294 
295  $member = new Adherent($this->db);
296  foreach ($request_data as $field => $value) {
297  $member->$field = $value;
298  }
299  if ($member->create(DolibarrApiAccess::$user) < 0) {
300  throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
301  }
302  return $member->id;
303  }
304 
312  public function put($id, $request_data = null)
313  {
314  if (!DolibarrApiAccess::$user->rights->adherent->creer) {
315  throw new RestException(401);
316  }
317 
318  $member = new Adherent($this->db);
319  $result = $member->fetch($id);
320  if (!$result) {
321  throw new RestException(404, 'member not found');
322  }
323 
324  if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
325  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
326  }
327 
328  foreach ($request_data as $field => $value) {
329  if ($field == 'id') {
330  continue;
331  }
332  // Process the status separately because it must be updated using
333  // the validate(), resiliate() and exclude() methods of the class Adherent.
334  if ($field == 'statut') {
335  if ($value == '0') {
336  $result = $member->resiliate(DolibarrApiAccess::$user);
337  if ($result < 0) {
338  throw new RestException(500, 'Error when resiliating member: '.$member->error);
339  }
340  } elseif ($value == '1') {
341  $result = $member->validate(DolibarrApiAccess::$user);
342  if ($result < 0) {
343  throw new RestException(500, 'Error when validating member: '.$member->error);
344  }
345  } elseif ($value == '-2') {
346  $result = $member->exclude(DolibarrApiAccess::$user);
347  if ($result < 0) {
348  throw new RestException(500, 'Error when excluding member: '.$member->error);
349  }
350  }
351  } else {
352  $member->$field = $value;
353  }
354  }
355 
356  // If there is no error, update() returns the number of affected rows
357  // so if the update is a no op, the return value is zero.
358  if ($member->update(DolibarrApiAccess::$user) >= 0) {
359  return $this->get($id);
360  } else {
361  throw new RestException(500, 'Error when updating member: '.$member->error);
362  }
363  }
364 
371  public function delete($id)
372  {
373  if (!DolibarrApiAccess::$user->rights->adherent->supprimer) {
374  throw new RestException(401);
375  }
376  $member = new Adherent($this->db);
377  $result = $member->fetch($id);
378  if (!$result) {
379  throw new RestException(404, 'member not found');
380  }
381 
382  if (!DolibarrApi::_checkAccessToResource('member', $member->id)) {
383  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
384  }
385 
386 
387  $res = $member->delete($member->id, DolibarrApiAccess::$user);
388  if ($res < 0) {
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");
392  }
393 
394  return array(
395  'success' => array(
396  'code' => 200,
397  'message' => 'Member deleted'
398  )
399  );
400  }
401 
410  private function _validate($data)
411  {
412  $member = array();
413  foreach (Members::$FIELDS as $field) {
414  if (!isset($data[$field])) {
415  throw new RestException(400, "$field field missing");
416  }
417  $member[$field] = $data[$field];
418  }
419  return $member;
420  }
421 
422  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
429  protected function _cleanObjectDatas($object)
430  {
431  // phpcs:enable
432  $object = parent::_cleanObjectDatas($object);
433 
434  // Remove the subscriptions because they are handled as a subresource.
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);
441 
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);
447 
448  return $object;
449  }
450 
463  public function getSubscriptions($id)
464  {
465  $obj_ret = array();
466 
467  if (!DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
468  throw new RestException(401);
469  }
470 
471  $member = new Adherent($this->db);
472  $result = $member->fetch($id);
473  if (!$result) {
474  throw new RestException(404, 'member not found');
475  }
476 
477  $obj_ret = array();
478  foreach ($member->subscriptions as $subscription) {
479  $obj_ret[] = $this->_cleanObjectDatas($subscription);
480  }
481  return $obj_ret;
482  }
483 
496  public function createSubscription($id, $start_date, $end_date, $amount, $label = '')
497  {
498  if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
499  throw new RestException(401);
500  }
501 
502  $member = new Adherent($this->db);
503  $result = $member->fetch($id);
504  if (!$result) {
505  throw new RestException(404, 'member not found');
506  }
507 
508  return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
509  }
510 
524  public function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
525  {
526  if (!DolibarrApiAccess::$user->rights->categorie->lire) {
527  throw new RestException(401);
528  }
529 
530  $categories = new Categorie($this->db);
531 
532  $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
533 
534  if (empty($result)) {
535  throw new RestException(404, 'No category found');
536  }
537 
538  if ($result < 0) {
539  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
540  }
541 
542  return $result;
543  }
544 }
Members\getCategories
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a member.
Definition: api_members.class.php:524
Members\put
put($id, $request_data=null)
Update member.
Definition: api_members.class.php:312
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
db
$conf db
API class for accounts.
Definition: inc.php:41
Members
Definition: api_members.class.php:34
Members\createSubscription
createSubscription($id, $start_date, $end_date, $amount, $label='')
Add a subscription for a member.
Definition: api_members.class.php:496
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
Categorie
Class to manage categories.
Definition: categorie.class.php:47
Members\_validate
_validate($data)
Validate fields before creating an object.
Definition: api_members.class.php:410
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Members\getByThirdparty
getByThirdparty($thirdparty)
Get properties of a member object by linked thirdparty.
Definition: api_members.class.php:100
Members\getByThirdpartyBarcode
getByThirdpartyBarcode($barcode)
Get properties of a member object by linked thirdparty barcode.
Definition: api_members.class.php:172
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Members\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $typeid='', $category=0, $sqlfilters='')
List members.
Definition: api_members.class.php:214
Adherent
Class to manage members of a foundation.
Definition: adherent.class.php:46
Members\getSubscriptions
getSubscriptions($id)
List subscriptions of a member.
Definition: api_members.class.php:463
Members\getByThirdpartyEmail
getByThirdpartyEmail($email)
Get properties of a member object by linked thirdparty email.
Definition: api_members.class.php:133
Members\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_members.class.php:429
Members\__construct
__construct()
Constructor.
Definition: api_members.class.php:47
Members\post
post($request_data=null)
Create member object.
Definition: api_members.class.php:287