dolibarr  7.0.0-beta
api_members.class.php
1 <?php
2 /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
3  * Copyright (C) 2017 Regis Houssin <regis.houssin@capnetworks.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 
19 use Luracast\Restler\RestException;
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  global $db, $conf;
96 
97  $obj_ret = array();
98 
99  if(! DolibarrApiAccess::$user->rights->adherent->lire) {
100  throw new RestException(401);
101  }
102 
103  $sql = "SELECT t.rowid";
104  $sql.= " FROM ".MAIN_DB_PREFIX."adherent as t";
105  $sql.= ' WHERE t.entity IN ('.getEntity('adherent').')';
106  if (!empty($typeid))
107  {
108  $sql.= ' AND t.fk_adherent_type='.$typeid;
109  }
110  // Add sql filters
111  if ($sqlfilters)
112  {
113  if (! DolibarrApi::_checkFilters($sqlfilters))
114  {
115  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
116  }
117  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
118  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
119  }
120 
121  $sql.= $db->order($sortfield, $sortorder);
122  if ($limit) {
123  if ($page < 0)
124  {
125  $page = 0;
126  }
127  $offset = $limit * $page;
128 
129  $sql.= $db->plimit($limit + 1, $offset);
130  }
131 
132  $result = $db->query($sql);
133  if ($result)
134  {
135  $i=0;
136  $num = $db->num_rows($result);
137  $min = min($num, ($limit <= 0 ? $num : $limit));
138  while ($i < $min)
139  {
140  $obj = $db->fetch_object($result);
141  $member = new Adherent($this->db);
142  if($member->fetch($obj->rowid)) {
143  $obj_ret[] = $this->_cleanObjectDatas($member);
144  }
145  $i++;
146  }
147  }
148  else {
149  throw new RestException(503, 'Error when retrieve member list : '.$db->lasterror());
150  }
151  if( ! count($obj_ret)) {
152  throw new RestException(404, 'No member found');
153  }
154 
155  return $obj_ret;
156  }
157 
164  function post($request_data = null)
165  {
166  if(! DolibarrApiAccess::$user->rights->adherent->creer) {
167  throw new RestException(401);
168  }
169  // Check mandatory fields
170  $result = $this->_validate($request_data);
171 
172  $member = new Adherent($this->db);
173  foreach($request_data as $field => $value) {
174  $member->$field = $value;
175  }
176  if ($member->create(DolibarrApiAccess::$user) < 0) {
177  throw new RestException(500, 'Error creating member', array_merge(array($member->error), $member->errors));
178  }
179  return $member->id;
180  }
181 
189  function put($id, $request_data = null)
190  {
191  if(! DolibarrApiAccess::$user->rights->adherent->creer) {
192  throw new RestException(401);
193  }
194 
195  $member = new Adherent($this->db);
196  $result = $member->fetch($id);
197  if( ! $result ) {
198  throw new RestException(404, 'member not found');
199  }
200 
201  if( ! DolibarrApi::_checkAccessToResource('member',$member->id)) {
202  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
203  }
204 
205  foreach($request_data as $field => $value) {
206  if ($field == 'id') continue;
207  // Process the status separately because it must be updated using
208  // the validate() and resiliate() methods of the class Adherent.
209  if ($field == 'statut') {
210  if ($value == '0') {
211  $result = $member->resiliate(DolibarrApiAccess::$user);
212  if ($result < 0) {
213  throw new RestException(500, 'Error when resiliating member: '.$member->error);
214  }
215  } else if ($value == '1') {
216  $result = $member->validate(DolibarrApiAccess::$user);
217  if ($result < 0) {
218  throw new RestException(500, 'Error when validating member: '.$member->error);
219  }
220  }
221  } else {
222  $member->$field = $value;
223  }
224  }
225 
226  // If there is no error, update() returns the number of affected rows
227  // so if the update is a no op, the return value is zero.
228  if ($member->update(DolibarrApiAccess::$user) >= 0)
229  {
230  return $this->get($id);
231  }
232  else
233  {
234  throw new RestException(500, $member->error);
235  }
236  }
237 
244  function delete($id)
245  {
246  if(! DolibarrApiAccess::$user->rights->adherent->supprimer) {
247  throw new RestException(401);
248  }
249  $member = new Adherent($this->db);
250  $result = $member->fetch($id);
251  if( ! $result ) {
252  throw new RestException(404, 'member not found');
253  }
254 
255  if( ! DolibarrApi::_checkAccessToResource('member',$member->id)) {
256  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
257  }
258 
259  if (! $member->delete($member->id, DolibarrApiAccess::$user)) {
260  throw new RestException(401,'error when deleting member');
261  }
262 
263  return array(
264  'success' => array(
265  'code' => 200,
266  'message' => 'member deleted'
267  )
268  );
269  }
270 
279  function _validate($data)
280  {
281  $member = array();
282  foreach (Members::$FIELDS as $field) {
283  if (!isset($data[$field]))
284  throw new RestException(400, "$field field missing");
285  $member[$field] = $data[$field];
286  }
287  return $member;
288  }
289 
296  function _cleanObjectDatas($object) {
297 
298  $object = parent::_cleanObjectDatas($object);
299 
300  // Remove the subscriptions because they are handled as a subresource.
301  unset($object->subscriptions);
302  unset($object->fk_incoterms);
303  unset($object->libelle_incoterms);
304  unset($object->location_incoterms);
305  unset($object->fk_delivery_address);
306  unset($object->shipping_method_id);
307 
308  unset($object->total_ht);
309  unset($object->total_ttc);
310  unset($object->total_tva);
311  unset($object->total_localtax1);
312  unset($object->total_localtax2);
313 
314  return $object;
315  }
316 
329  function getSubscriptions($id)
330  {
331  $obj_ret = array();
332 
333  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
334  throw new RestException(401);
335  }
336 
337  $member = new Adherent($this->db);
338  $result = $member->fetch($id);
339  if( ! $result ) {
340  throw new RestException(404, 'member not found');
341  }
342 
343  $obj_ret = array();
344  foreach ($member->subscriptions as $subscription) {
345  $obj_ret[] = $this->_cleanObjectDatas($subscription);
346  }
347  return $obj_ret;
348  }
349 
362  function createSubscription($id, $start_date, $end_date, $amount, $label='')
363  {
364  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
365  throw new RestException(401);
366  }
367 
368  $member = new Adherent($this->db);
369  $result = $member->fetch($id);
370  if( ! $result ) {
371  throw new RestException(404, 'member not found');
372  }
373 
374  return $member->subscription($start_date, $amount, 0, '', $label, '', '', '', $end_date);
375  }
376 
390  function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
391  {
392  if (! DolibarrApiAccess::$user->rights->categorie->lire) {
393  throw new RestException(401);
394  }
395 
396  $categories = new Categorie($this->db);
397 
398  $result = $categories->getListForItem($id, 'member', $sortfield, $sortorder, $limit, $page);
399 
400  if (empty($result)) {
401  throw new RestException(404, 'No category found');
402  }
403 
404  if ($result < 0) {
405  throw new RestException(503, 'Error when retrieve category list : '.$categories->error);
406  }
407 
408  return $result;
409  }
410 
411 }
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:246
Class for API REST v1.
Definition: api.class.php:29
Class to manage categories.
getSubscriptions($id)
List subscriptions of a member.
getCategories($id, $sortfield="s.rowid", $sortorder= 'ASC', $limit=0, $page=0)
Get categories for a member.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $typeid= '', $sqlfilters= '')
List members.
Class to manage members of a foundation.
__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:221
_cleanObjectDatas($object)
Clean sensible object datas.