dolibarr  7.0.0-beta
api_memberstypes.class.php
1 <?php
2 /* Copyright (C) 2017 Regis Houssin <regis.houssin@capnetworks.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 use Luracast\Restler\RestException;
19 
20 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent_type.class.php';
21 
29 {
33  static $FIELDS = array(
34  'label'
35  );
36 
40  function __construct()
41  {
42  global $db, $conf;
43  $this->db = $db;
44  }
45 
56  function get($id)
57  {
58  if(! DolibarrApiAccess::$user->rights->adherent->lire) {
59  throw new RestException(401);
60  }
61 
62  $membertype = new AdherentType($this->db);
63  $result = $membertype->fetch($id);
64  if( ! $result ) {
65  throw new RestException(404, 'member type not found');
66  }
67 
68  if( ! DolibarrApi::_checkAccessToResource('member',$membertype->id,'adherent_type')) {
69  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
70  }
71 
72  return $this->_cleanObjectDatas($membertype);
73  }
74 
89  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '') {
90  global $db, $conf;
91 
92  $obj_ret = array();
93 
94  if(! DolibarrApiAccess::$user->rights->adherent->lire) {
95  throw new RestException(401);
96  }
97 
98  $sql = "SELECT t.rowid";
99  $sql.= " FROM ".MAIN_DB_PREFIX."adherent_type as t";
100  $sql.= ' WHERE t.entity IN ('.getEntity('adherent').')';
101 
102  // Add sql filters
103  if ($sqlfilters)
104  {
105  if (! DolibarrApi::_checkFilters($sqlfilters))
106  {
107  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
108  }
109  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
110  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
111  }
112 
113  $sql.= $db->order($sortfield, $sortorder);
114  if ($limit) {
115  if ($page < 0)
116  {
117  $page = 0;
118  }
119  $offset = $limit * $page;
120 
121  $sql.= $db->plimit($limit + 1, $offset);
122  }
123 
124  $result = $db->query($sql);
125  if ($result)
126  {
127  $i=0;
128  $num = $db->num_rows($result);
129  $min = min($num, ($limit <= 0 ? $num : $limit));
130  while ($i < $min)
131  {
132  $obj = $db->fetch_object($result);
133  $membertype = new AdherentType($this->db);
134  if ($membertype->fetch($obj->rowid)) {
135  $obj_ret[] = $this->_cleanObjectDatas($membertype);
136  }
137  $i++;
138  }
139  }
140  else {
141  throw new RestException(503, 'Error when retrieve member type list : '.$db->lasterror());
142  }
143  if ( ! count($obj_ret)) {
144  throw new RestException(404, 'No member type found');
145  }
146 
147  return $obj_ret;
148  }
149 
156  function post($request_data = null)
157  {
158  if (! DolibarrApiAccess::$user->rights->adherent->configurer) {
159  throw new RestException(401);
160  }
161  // Check mandatory fields
162  $result = $this->_validate($request_data);
163 
164  $membertype = new AdherentType($this->db);
165  foreach($request_data as $field => $value) {
166  $membertype->$field = $value;
167  }
168  if ($membertype->create(DolibarrApiAccess::$user) < 0) {
169  throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
170  }
171  return $membertype->id;
172  }
173 
181  function put($id, $request_data = null)
182  {
183  if (! DolibarrApiAccess::$user->rights->adherent->configurer) {
184  throw new RestException(401);
185  }
186 
187  $membertype = new AdherentType($this->db);
188  $result = $membertype->fetch($id);
189  if( ! $result ) {
190  throw new RestException(404, 'member type not found');
191  }
192 
193  if( ! DolibarrApi::_checkAccessToResource('member',$membertype->id,'adherent_type')) {
194  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
195  }
196 
197  foreach($request_data as $field => $value) {
198  if ($field == 'id') continue;
199  // Process the status separately because it must be updated using
200  // the validate() and resiliate() methods of the class AdherentType.
201  $membertype->$field = $value;
202  }
203 
204  // If there is no error, update() returns the number of affected rows
205  // so if the update is a no op, the return value is zero.
206  if ($membertype->update(DolibarrApiAccess::$user) >= 0)
207  {
208  return $this->get($id);
209  }
210  else
211  {
212  throw new RestException(500, $membertype->error);
213  }
214  }
215 
222  function delete($id)
223  {
224  if (! DolibarrApiAccess::$user->rights->adherent->configurer) {
225  throw new RestException(401);
226  }
227  $membertype = new AdherentType($this->db);
228  $result = $membertype->fetch($id);
229  if( ! $result ) {
230  throw new RestException(404, 'member type not found');
231  }
232 
233  if ( ! DolibarrApi::_checkAccessToResource('member',$membertype->id,'adherent_type')) {
234  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
235  }
236 
237  if (! $membertype->delete($membertype->id)) {
238  throw new RestException(401,'error when deleting member type');
239  }
240 
241  return array(
242  'success' => array(
243  'code' => 200,
244  'message' => 'member type deleted'
245  )
246  );
247  }
248 
257  function _validate($data)
258  {
259  $membertype = array();
260  foreach (MembersTypes::$FIELDS as $field) {
261  if (!isset($data[$field]))
262  throw new RestException(400, "$field field missing");
263  $membertype[$field] = $data[$field];
264  }
265  return $membertype;
266  }
267 
274  function _cleanObjectDatas($object) {
275 
276  $object = parent::_cleanObjectDatas($object);
277 
278  unset($object->cotisation);
279 
280  unset($object->array_options);
281  unset($object->linkedObjectsIds);
282  unset($object->context);
283  unset($object->canvas);
284  unset($object->fk_project);
285  unset($object->contact);
286  unset($object->contact_id);
287  unset($object->thirdparty);
288  unset($object->user);
289  unset($object->origin);
290  unset($object->origin_id);
291  unset($object->ref_ext);
292  unset($object->country);
293  unset($object->country_id);
294  unset($object->country_code);
295  unset($object->barcode_type);
296  unset($object->barcode_type_code);
297  unset($object->barcode_type_label);
298  unset($object->barcode_type_coder);
299  unset($object->mode_reglement_id);
300  unset($object->cond_reglement_id);
301  unset($object->cond_reglement);
302  unset($object->fk_delivery_address);
303  unset($object->shipping_method_id);
304  unset($object->modelpdf);
305  unset($object->fk_account);
306  unset($object->note_public);
307  unset($object->note_private);
308  unset($object->fk_incoterms);
309  unset($object->libelle_incoterms);
310  unset($object->location_incoterms);
311  unset($object->name);
312  unset($object->lastname);
313  unset($object->firstname);
314  unset($object->civility_id);
315  unset($object->total_ht);
316  unset($object->total_tva);
317  unset($object->total_localtax1);
318  unset($object->total_localtax2);
319  unset($object->total_ttc);
320 
321  return $object;
322  }
323 
324 }
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before creating an object.
__construct()
Constructor.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
Class for API REST v1.
Definition: api.class.php:29
post($request_data=null)
Create member type object.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List members types.
Class to manage members type.
put($id, $request_data=null)
Update member type.
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