19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
34 public static $FIELDS = array(
67 public function get(
$id)
69 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
70 throw new RestException(403);
73 $result = $this->subscription->fetch(
$id);
75 throw new RestException(404,
'Subscription not found');
80 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
83 $this->subscription->fetchObjectLinked();
108 public function index($sortfield =
"dateadh", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
114 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
115 throw new RestException(403);
118 $sql =
"SELECT rowid";
119 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
120 $sql .=
' WHERE 1 = 1';
126 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
131 $sqlTotals = str_replace(
'SELECT rowid',
'SELECT count(rowid) as total', $sql);
133 $sql .= $this->db->order($sortfield, $sortorder);
138 $offset = $limit * $page;
140 $sql .= $this->db->plimit($limit + 1, $offset);
143 $result = $this->db->query($sql);
146 $num = $this->db->num_rows($result);
147 while ($i < min($limit, $num)) {
148 $obj = $this->db->fetch_object($result);
150 if ($subscription->fetch($obj->rowid)) {
156 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
160 if ($pagination_data) {
161 $totalsResult = $this->db->query($sqlTotals);
162 $total = $this->db->fetch_object($totalsResult)->total;
167 $obj_ret[
'data'] = $tmp;
168 $obj_ret[
'pagination'] = [
169 'total' => (int) $total,
171 'page_count' => ceil((
int) $total / $limit),
190 public function post($request_data =
null)
192 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
193 throw new RestException(403);
196 $result = $this->
_validate($request_data);
200 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
204 foreach ($request_data as $field => $value) {
205 if ($field ===
'caller') {
207 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
211 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
213 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
214 throw new RestException(500,
'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
216 return $subscription->id;
232 public function put(
$id, $request_data =
null)
234 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
235 throw new RestException(403);
239 $result = $subscription->fetch(
$id);
241 throw new RestException(404,
'Subscription not found');
246 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
249 foreach ($request_data as $field => $value) {
250 if ($field ==
'id') {
253 if ($field ===
'caller') {
255 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
259 if ($field ==
'array_options' && is_array($value)) {
260 foreach ($value as $index => $val) {
261 $subscription->array_options[$index] = $this->
_checkValForAPI($field, $val, $subscription);
265 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
268 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
269 return $this->
get(
$id);
271 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
288 public function delete(
$id)
291 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
292 throw new RestException(403);
295 $result = $subscription->fetch(
$id);
297 throw new RestException(404,
'Subscription not found');
302 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
305 $res = $subscription->delete(DolibarrApiAccess::$user);
307 throw new RestException(500,
"Can't delete, error occurs");
308 } elseif ($res == 0) {
309 throw new RestException(409,
"No subscription whas deleted");
315 'message' =>
'Subscription deleted'
330 $subscription = array();
331 foreach (Subscriptions::$FIELDS as $field) {
332 if (!isset($data[$field])) {
333 throw new RestException(400,
"$field field missing");
335 $subscription[$field] = $data[$field];
337 return $subscription;
$id
Support class for third parties, contacts, members, users or resources.
_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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T of Object.
Class to manage subscriptions of foundation members.
_validate($data)
Validate fields before creating an object.
post($request_data=null)
Create subscription object.
index($sortfield="dateadh", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List subscriptions.
put($id, $request_data=null)
Update subscription.
__construct()
Constructor.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...