18 use Luracast\Restler\RestException;
20 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/adherent_type.class.php';
33 public static $FIELDS = array(
56 public function get($id)
58 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
59 throw new RestException(401);
63 $result = $membertype->fetch($id);
65 throw new RestException(404,
'member type not found');
69 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
89 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
95 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
96 throw new RestException(401);
99 $sql =
"SELECT t.rowid";
100 $sql .=
" FROM ".MAIN_DB_PREFIX.
"adherent_type as t";
101 $sql .=
' WHERE t.entity IN ('.getEntity(
'member_type').
')';
107 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
109 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
110 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
113 $sql .= $this->
db->order($sortfield, $sortorder);
118 $offset = $limit * $page;
120 $sql .= $this->
db->plimit($limit + 1, $offset);
123 $result = $this->
db->query($sql);
126 $num = $this->
db->num_rows($result);
127 $min = min($num, ($limit <= 0 ? $num : $limit));
129 $obj = $this->
db->fetch_object($result);
131 if ($membertype->fetch($obj->rowid)) {
137 throw new RestException(503,
'Error when retrieve member type list : '.$this->
db->lasterror());
139 if (!count($obj_ret)) {
140 throw new RestException(404,
'No member type found');
152 public function post($request_data =
null)
154 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
155 throw new RestException(401);
158 $result = $this->
_validate($request_data);
161 foreach ($request_data as $field => $value) {
162 $membertype->$field = $value;
164 if ($membertype->create(DolibarrApiAccess::$user) < 0) {
165 throw new RestException(500,
'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
167 return $membertype->id;
177 public function put($id, $request_data =
null)
179 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
180 throw new RestException(401);
184 $result = $membertype->fetch($id);
186 throw new RestException(404,
'member type not found');
190 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
193 foreach ($request_data as $field => $value) {
194 if ($field ==
'id') {
199 $membertype->$field = $value;
204 if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
205 return $this->
get($id);
207 throw new RestException(500,
'Error when updating member type: '.$membertype->error);
217 public function delete($id)
219 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'configurer')) {
220 throw new RestException(401);
223 $result = $membertype->fetch($id);
225 throw new RestException(404,
'member type not found');
229 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
232 $res = $membertype->delete();
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");
242 'message' =>
'Member type deleted'
257 $membertype = array();
258 foreach (MembersTypes::$FIELDS as $field) {
259 if (!isset($data[$field])) {
260 throw new RestException(400,
"$field field missing");
262 $membertype[$field] = $data[$field];
277 $object = parent::_cleanObjectDatas($object);
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);
Class to manage members type.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
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.
$conf db
API class for accounts.