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