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');
78 $this->subscription->fetchObjectLinked();
103 public function index($sortfield =
"dateadh", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
107 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
108 throw new RestException(403);
111 $sql =
"SELECT rowid";
112 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
113 $sql .=
' WHERE 1 = 1';
117 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
119 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
124 $sqlTotals = str_replace(
'SELECT rowid',
'SELECT count(rowid) as total', $sql);
126 $sql .= $this->db->order($sortfield, $sortorder);
131 $offset = $limit * $page;
133 $sql .= $this->db->plimit($limit + 1, $offset);
136 $result = $this->db->query($sql);
139 $num = $this->db->num_rows($result);
140 $min = min($num, ($limit <= 0 ? $num : $limit));
142 $obj = $this->db->fetch_object($result);
144 if ($subscription->fetch($obj->rowid)) {
150 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
154 if ($pagination_data) {
155 $totalsResult = $this->db->query($sqlTotals);
156 $total = $this->db->fetch_object($totalsResult)->total;
161 $obj_ret[
'data'] = $tmp;
162 $obj_ret[
'pagination'] = [
163 'total' => (int) $total,
165 'page_count' => ceil((
int) $total / $limit),
184 public function post($request_data =
null)
186 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
187 throw new RestException(403);
190 $result = $this->
_validate($request_data);
193 foreach ($request_data as $field => $value) {
194 if ($field ===
'caller') {
196 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
200 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
202 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
203 throw new RestException(500,
'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
205 return $subscription->id;
221 public function put(
$id, $request_data =
null)
223 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
224 throw new RestException(403);
228 $result = $subscription->fetch(
$id);
230 throw new RestException(404,
'Subscription not found');
233 foreach ($request_data as $field => $value) {
234 if ($field ==
'id') {
237 if ($field ===
'caller') {
239 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
243 if ($field ==
'array_options' && is_array($value)) {
244 foreach ($value as $index => $val) {
249 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
252 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
253 return $this->
get(
$id);
255 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
272 public function delete(
$id)
275 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
276 throw new RestException(403);
279 $result = $subscription->fetch(
$id);
281 throw new RestException(404,
'Subscription not found');
284 $res = $subscription->delete(DolibarrApiAccess::$user);
286 throw new RestException(500,
"Can't delete, error occurs");
287 } elseif ($res == 0) {
288 throw new RestException(409,
"No subscription whas deleted");
294 'message' =>
'Subscription deleted'
309 $subscription = array();
310 foreach (Subscriptions::$FIELDS as $field) {
311 if (!isset($data[$field])) {
312 throw new RestException(400,
"$field field missing");
314 $subscription[$field] = $data[$field];
316 return $subscription;
$id
Support class for third parties, contacts, members, users or resources.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.