dolibarr  19.0.0-dev
api_memberstypes.class.php
1 <?php
2 /* Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.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 <https://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  public static $FIELDS = array(
34  'label',
35  );
36 
40  public function __construct()
41  {
42  global $db, $conf;
43  $this->db = $db;
44  }
45 
56  public function get($id)
57  {
58  if (!DolibarrApiAccess::$user->hasRight('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  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
90  {
91  global $db, $conf;
92 
93  $obj_ret = array();
94 
95  if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
96  throw new RestException(401);
97  }
98 
99  $sql = "SELECT t.rowid";
100  $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type AS t LEFT JOIN ".MAIN_DB_PREFIX."adherent_type_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
101  $sql .= ' WHERE t.entity IN ('.getEntity('member_type').')';
102 
103  // Add sql filters
104  if ($sqlfilters) {
105  $errormessage = '';
106  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
107  if ($errormessage) {
108  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
109  }
110  }
111 
112  $sql .= $this->db->order($sortfield, $sortorder);
113  if ($limit) {
114  if ($page < 0) {
115  $page = 0;
116  }
117  $offset = $limit * $page;
118 
119  $sql .= $this->db->plimit($limit + 1, $offset);
120  }
121 
122  $result = $this->db->query($sql);
123  if ($result) {
124  $i = 0;
125  $num = $this->db->num_rows($result);
126  $min = min($num, ($limit <= 0 ? $num : $limit));
127  while ($i < $min) {
128  $obj = $this->db->fetch_object($result);
129  $membertype = new AdherentType($this->db);
130  if ($membertype->fetch($obj->rowid)) {
131  $obj_ret[] = $this->_cleanObjectDatas($membertype);
132  }
133  $i++;
134  }
135  } else {
136  throw new RestException(503, 'Error when retrieve member type list : '.$this->db->lasterror());
137  }
138  if (!count($obj_ret)) {
139  throw new RestException(404, 'No member type found');
140  }
141 
142  return $obj_ret;
143  }
144 
151  public function post($request_data = null)
152  {
153  if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
154  throw new RestException(401);
155  }
156  // Check mandatory fields
157  $result = $this->_validate($request_data);
158 
159  $membertype = new AdherentType($this->db);
160  foreach ($request_data as $field => $value) {
161  $membertype->$field = $value;
162  }
163  if ($membertype->create(DolibarrApiAccess::$user) < 0) {
164  throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
165  }
166  return $membertype->id;
167  }
168 
176  public function put($id, $request_data = null)
177  {
178  if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
179  throw new RestException(401);
180  }
181 
182  $membertype = new AdherentType($this->db);
183  $result = $membertype->fetch($id);
184  if (!$result) {
185  throw new RestException(404, 'member type not found');
186  }
187 
188  if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
189  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
190  }
191 
192  foreach ($request_data as $field => $value) {
193  if ($field == 'id') {
194  continue;
195  }
196  // Process the status separately because it must be updated using
197  // the validate(), resiliate() and exclude() methods of the class AdherentType.
198  $membertype->$field = $value;
199  }
200 
201  // If there is no error, update() returns the number of affected rows
202  // so if the update is a no op, the return value is zero.
203  if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
204  return $this->get($id);
205  } else {
206  throw new RestException(500, 'Error when updating member type: '.$membertype->error);
207  }
208  }
209 
216  public function delete($id)
217  {
218  if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
219  throw new RestException(401);
220  }
221  $membertype = new AdherentType($this->db);
222  $result = $membertype->fetch($id);
223  if (!$result) {
224  throw new RestException(404, 'member type not found');
225  }
226 
227  if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
228  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
229  }
230 
231  $res = $membertype->delete();
232  if ($res < 0) {
233  throw new RestException(500, "Can't delete, error occurs");
234  } elseif ($res == 0) {
235  throw new RestException(409, "Can't delete, that product is probably used");
236  }
237 
238  return array(
239  'success' => array(
240  'code' => 200,
241  'message' => 'Member type deleted'
242  )
243  );
244  }
245 
254  private function _validate($data)
255  {
256  $membertype = array();
257  foreach (MembersTypes::$FIELDS as $field) {
258  if (!isset($data[$field])) {
259  throw new RestException(400, "$field field missing");
260  }
261  $membertype[$field] = $data[$field];
262  }
263  return $membertype;
264  }
265 
266  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
273  protected function _cleanObjectDatas($object)
274  {
275  // phpcs:enable
276  $object = parent::_cleanObjectDatas($object);
277 
278  unset($object->array_options);
279  unset($object->linkedObjectsIds);
280  unset($object->context);
281  unset($object->canvas);
282  unset($object->fk_project);
283  unset($object->contact);
284  unset($object->contact_id);
285  unset($object->thirdparty);
286  unset($object->user);
287  unset($object->origin);
288  unset($object->origin_id);
289  unset($object->ref_ext);
290  unset($object->country);
291  unset($object->country_id);
292  unset($object->country_code);
293  unset($object->barcode_type);
294  unset($object->barcode_type_code);
295  unset($object->barcode_type_label);
296  unset($object->barcode_type_coder);
297  unset($object->mode_reglement_id);
298  unset($object->cond_reglement_id);
299  unset($object->cond_reglement);
300  unset($object->fk_delivery_address);
301  unset($object->shipping_method_id);
302  unset($object->model_pdf);
303  unset($object->fk_account);
304  unset($object->note_public);
305  unset($object->note_private);
306  unset($object->fk_incoterms);
307  unset($object->label_incoterms);
308  unset($object->location_incoterms);
309  unset($object->name);
310  unset($object->lastname);
311  unset($object->firstname);
312  unset($object->civility_id);
313  unset($object->total_ht);
314  unset($object->total_tva);
315  unset($object->total_localtax1);
316  unset($object->total_localtax2);
317  unset($object->total_ttc);
318 
319  return $object;
320  }
321 }
Class to manage members type.
Class for API REST v1.
Definition: api.class.php:31
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:282
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List members types.
__construct()
Constructor.
post($request_data=null)
Create member type object.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before creating an object.
put($id, $request_data=null)
Update member type.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria