dolibarr 19.0.3
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
18use Luracast\Restler\RestException;
19
20require_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
90 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
91 {
92 global $db, $conf;
93
94 $obj_ret = array();
95
96 if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
97 throw new RestException(401);
98 }
99
100 $sql = "SELECT t.rowid";
101 $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
102 $sql .= ' WHERE t.entity IN ('.getEntity('member_type').')';
103
104 // Add sql filters
105 if ($sqlfilters) {
106 $errormessage = '';
107 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
108 if ($errormessage) {
109 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
110 }
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->_filterObjectProperties($this->_cleanObjectDatas($membertype), $properties);
133 }
134 $i++;
135 }
136 } else {
137 throw new RestException(503, 'Error when retrieve member type list : '.$this->db->lasterror());
138 }
139
140 return $obj_ret;
141 }
142
149 public function post($request_data = null)
150 {
151 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
152 throw new RestException(401);
153 }
154 // Check mandatory fields
155 $result = $this->_validate($request_data);
156
157 $membertype = new AdherentType($this->db);
158 foreach ($request_data as $field => $value) {
159 if ($field === 'caller') {
160 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
161 $membertype->context['caller'] = $request_data['caller'];
162 continue;
163 }
164
165 $membertype->$field = $value;
166 }
167 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
168 throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
169 }
170 return $membertype->id;
171 }
172
180 public function put($id, $request_data = null)
181 {
182 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
183 throw new RestException(401);
184 }
185
186 $membertype = new AdherentType($this->db);
187 $result = $membertype->fetch($id);
188 if (!$result) {
189 throw new RestException(404, 'member type not found');
190 }
191
192 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
193 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
194 }
195
196 foreach ($request_data as $field => $value) {
197 if ($field == 'id') {
198 continue;
199 }
200 if ($field === 'caller') {
201 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
202 $membertype->context['caller'] = $request_data['caller'];
203 continue;
204 }
205
206 // Process the status separately because it must be updated using
207 // the validate(), resiliate() and exclude() methods of the class AdherentType.
208 $membertype->$field = $value;
209 }
210
211 // If there is no error, update() returns the number of affected rows
212 // so if the update is a no op, the return value is zero.
213 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
214 return $this->get($id);
215 } else {
216 throw new RestException(500, 'Error when updating member type: '.$membertype->error);
217 }
218 }
219
226 public function delete($id)
227 {
228 if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
229 throw new RestException(401);
230 }
231 $membertype = new AdherentType($this->db);
232 $result = $membertype->fetch($id);
233 if (!$result) {
234 throw new RestException(404, 'member type not found');
235 }
236
237 if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
238 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
239 }
240
241 $res = $membertype->delete(DolibarrApiAccess::$user);
242 if ($res < 0) {
243 throw new RestException(500, "Can't delete, error occurs");
244 } elseif ($res == 0) {
245 throw new RestException(409, "Can't delete, that product is probably used");
246 }
247
248 return array(
249 'success' => array(
250 'code' => 200,
251 'message' => 'Member type deleted'
252 )
253 );
254 }
255
264 private function _validate($data)
265 {
266 $membertype = array();
267 foreach (MembersTypes::$FIELDS as $field) {
268 if (!isset($data[$field])) {
269 throw new RestException(400, "$field field missing");
270 }
271 $membertype[$field] = $data[$field];
272 }
273 return $membertype;
274 }
275
276 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
283 protected function _cleanObjectDatas($object)
284 {
285 // phpcs:enable
286 $object = parent::_cleanObjectDatas($object);
287
288 unset($object->array_options);
289 unset($object->linkedObjectsIds);
290 unset($object->context);
291 unset($object->canvas);
292 unset($object->fk_project);
293 unset($object->contact);
294 unset($object->contact_id);
295 unset($object->thirdparty);
296 unset($object->user);
297 unset($object->origin);
298 unset($object->origin_id);
299 unset($object->ref_ext);
300 unset($object->country);
301 unset($object->country_id);
302 unset($object->country_code);
303 unset($object->barcode_type);
304 unset($object->barcode_type_code);
305 unset($object->barcode_type_label);
306 unset($object->barcode_type_coder);
307 unset($object->mode_reglement_id);
308 unset($object->cond_reglement_id);
309 unset($object->cond_reglement);
310 unset($object->fk_delivery_address);
311 unset($object->shipping_method_id);
312 unset($object->model_pdf);
313 unset($object->fk_account);
314 unset($object->note_public);
315 unset($object->note_private);
316 unset($object->fk_incoterms);
317 unset($object->label_incoterms);
318 unset($object->location_incoterms);
319 unset($object->name);
320 unset($object->lastname);
321 unset($object->firstname);
322 unset($object->civility_id);
323 unset($object->total_ht);
324 unset($object->total_tva);
325 unset($object->total_localtax1);
326 unset($object->total_localtax2);
327 unset($object->total_ttc);
328
329 return $object;
330 }
331}
Class to manage members type.
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
__construct()
Constructor.
post($request_data=null)
Create member type object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List members types.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before creating an object.
put($id, $request_data=null)
Update member type.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria