dolibarr  9.0.0
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
20 
21 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
22 require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
23 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
24 
31 class Members extends DolibarrApi
32 {
36  static $FIELDS = array(
37  'morphy',
38  'typeid'
39  );
40 
44  function __construct()
45  {
46  global $db, $conf;
47  $this->db = $db;
48  }
49 
60  function get($id)
61  {
62  if(! DolibarrApiAccess::$user->rights->adherent->lire) {
63  throw new RestException(401);
64  }
65 
66  $member = new Adherent($this->db);
67  $result = $member->fetch($id);
68  if( ! $result ) {
69  throw new RestException(404, 'member not found');
70  }
71 
72  if( ! DolibarrApi::_checkAccessToResource('adherent',$member->id)) {
73  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
74  }
75 
76  return $this->_cleanObjectDatas($member);
77  }
78 
94  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $typeid = '', $sqlfilters = '')
95  {
96  global $db, $conf;
97 
98  $obj_ret = array();
99 
100  if(! DolibarrApiAccess::$user->rights->adherent->lire) {
101  throw new RestException(401);
102  }
103 
104  $sql = "SELECT t.rowid";
105  $sql.= " FROM ".MAIN_DB_PREFIX."adherent as t";
106  $sql.= ' WHERE t.entity IN ('.getEntity('adherent').')';
107  if (!empty($typeid))
108  {
109  $sql.= ' AND t.fk_adherent_type='.$typeid;
110  }
111  // Add sql filters
112  if ($sqlfilters)
113  {
114  if (! DolibarrApi::_checkFilters($sqlfilters))
115  {
116  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
117  }
118  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
119  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
120  }
121 
122  $sql.= $db->order($sortfield, $sortorder);
123  if ($limit) {
124  if ($page < 0)
125  {
126  $page = 0;
127  }
128  $offset = $limit * $page;
129 
130  $sql.= $db->plimit($limit + 1, $offset);
131  }
132 
133  $result = $db->query($sql);
134  if ($result)
135  {
136  $i=0;
137  $num = $db->num_rows($result);
138  $min = min($num, ($limit <= 0 ? $num : $limit));
139  while ($i < $min)
140  {
141  $obj = $db->fetch_object($result);
142  $member = new Adherent($this->db);
143  if($member->fetch($obj->rowid)) {
144  $obj_ret[] = $this->_cleanObjectDatas($member);
145  }
146  $i++;
147  }
148  }
149  else {
150  throw new RestException(503, 'Error when retrieve member list : '.$db->lasterror());
151  }
152  if( ! count($obj_ret)) {
153  throw new RestException(404, 'No member found');
154  }
155 
156  return $obj_ret;
157  }
158 
165  function post($request_data = null)
166  {
167  if(! DolibarrApiAccess::$user->rights->adherent->creer) {
168  throw new RestException(401);
169  }
170  // Check mandatory fields
171  $result = $this->_validate($request_data);
172 
173  $member = new Adherent($this->db);
174  foreach($request_data as $field => $value) {
175  $member->$field = $value;
176  }
177  if ($member->create(DolibarrApiAccess::$user) < 0) {
178  throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
179  }
180  return $member->id;
181  }
182 
190  function put($id, $request_data = null)
191  {
192  if(! DolibarrApiAccess::$user->rights->adherent->creer) {
193  throw new RestException(401);
194  }
195 
196  $member = new Adherent($this->db);
197  $result = $member->fetch($id);
198  if( ! $result ) {
199  throw new RestException(404, 'member not found');
200  }
201 
202  if( ! DolibarrApi::_checkAccessToResource('member',$member->id)) {
203  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
204  }
205 
206  foreach($request_data as $field => $value) {
207  if ($field == 'id') continue;
208  // Process the status separately because it must be updated using
209  // the validate() and resiliate() methods of the class Adherent.
210  if ($field == 'statut') {
211  if ($value == '0') {
212  $result = $member->resiliate(DolibarrApiAccess::$user);
213  if ($result < 0) {
214  throw new RestException(500, 'Error when resiliating member: '.$member->error);
215  }
216  } else if ($value == '1') {
217  $result = $member->validate(DolibarrApiAccess::$user);
218  if ($result < 0) {
219  throw new RestException(500, 'Error when validating member: '.$member->error);
220  }
221  }
222  } else {
223  $member->$field = $value;
224  }
225  }
226 
227  // If there is no error, update() returns the number of affected rows
228  // so if the update is a no op, the return value is zero.
229  if ($member->update(DolibarrApiAccess::$user) >= 0)
230  {
231  return $this->get($id);
232  }
233  else
234  {
235  throw new RestException(500, $member->error);
236  }
237  }
238 
245  function delete($id)
246  {
247  if(! DolibarrApiAccess::$user->rights->adherent->supprimer) {
248  throw new RestException(401);
249  }
250  $member = new Adherent($this->db);
251  $result = $member->fetch($id);
252  if( ! $result ) {
253  throw new RestException(404, 'member not found');
254  }
255 
256  if( ! DolibarrApi::_checkAccessToResource('member',$member->id)) {
257  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
258  }
259 
260  if (! $member->delete($member->id, DolibarrApiAccess::$user)) {
261  throw new RestException(401,'error when deleting member');
262  }
263 
264  return array(
265  'success' => array(
266  'code' => 200,
267  'message' => 'member deleted'
268  )
269  );
270  }
271 
280  function _validate($data)
281  {
282  $member = array();
283  foreach (Members::$FIELDS as $field) {
284  if (!isset($data[$field]))
285  throw new RestException(400, "$field field missing");
286  $member[$field] = $data[$field];
287  }
288  return $member;
289  }
290 
297  function _cleanObjectDatas($object)
298  {
299 
300  $object = parent::_cleanObjectDatas($object);
301 
302  // Remove the subscriptions because they are handled as a subresource.
303  unset($object->subscriptions);
304  unset($object->fk_incoterms);
305  unset($object->libelle_incoterms);
306  unset($object->location_incoterms);
307  unset($object->fk_delivery_address);
308  unset($object->shipping_method_id);
309 
310  unset($object->total_ht);
311  unset($object->total_ttc);
312  unset($object->total_tva);
313  unset($object->total_localtax1);
314  unset($object->total_localtax2);
315 
316  return $object;
317  }
318 
331  function getSubscriptions($id)
332  {
333  $obj_ret = array();
334 
335  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
336  throw new RestException(401);
337  }
338 
339  $member = new Adherent($this->db);
340  $result = $member->fetch($id);
341  if( ! $result ) {
342  throw new RestException(404, 'member not found');
343  }
344 
345  $obj_ret = array();
346  foreach ($member->subscriptions as $subscription) {
347  $obj_ret[] = $this->_cleanObjectDatas($subscription);
348  }
349  return $obj_ret;
350  }
351 
364  function createSubscription($id, $start_date, $end_date, $amount, $label='')
365  {
366  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
367  throw new RestException(401);
368  }
369 
370  $member = new Adherent($this->db);
371  $result = $member->fetch($id);
372  if( ! $result ) {
373  throw new RestException(404, 'member not found');
374  }
375 
376  return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
377  }
378 
392  function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
393  {
394  if (! DolibarrApiAccess::$user->rights->categorie->lire) {
395  throw new RestException(401);
396  }
397 
398  $categories = new Categorie($this->db);
399 
400  $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
401 
402  if (empty($result)) {
403  throw new RestException(404, 'No category found');
404  }
405 
406  if ($result < 0) {
407  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
408  }
409 
410  return $result;
411  }
412 }
post($request_data=null)
Create member object.
put($id, $request_data=null)
Update member.
_validate($data)
Validate fields before creating an object.
createSubscription($id, $start_date, $end_date, $amount, $label='')
Add a subscription for a member.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
Class for API REST v1.
Definition: api.class.php:29
Class to manage categories.
getSubscriptions($id)
List subscriptions of a member.
Class to manage members of a foundation.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $typeid='', $sqlfilters='')
List members.
__construct()
Constructor.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check user access to a resource.
Definition: api.class.php:225
getCategories($id, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Get categories for a member.
_cleanObjectDatas($object)
Clean sensible object datas.