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 t.rowid";
112 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
115 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"adherent as d ON d.rowid = t.fk_adherent";
116 $sql .=
' WHERE d.entity IN ('.getEntity(
'adherent').
')';
122 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
127 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
129 $sql .= $this->db->order($sortfield, $sortorder);
134 $offset = $limit * $page;
136 $sql .= $this->db->plimit($limit + 1, $offset);
139 $result = $this->db->query($sql);
142 $num = $this->db->num_rows($result);
143 $min = min($num, ($limit <= 0 ? $num : $limit));
145 $obj = $this->db->fetch_object($result);
147 if ($subscription->fetch($obj->rowid)) {
153 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
157 if ($pagination_data) {
158 $totalsResult = $this->db->query($sqlTotals);
159 $total = $this->db->fetch_object($totalsResult)->total;
164 $obj_ret[
'data'] = $tmp;
165 $obj_ret[
'pagination'] = [
166 'total' => (int) $total,
168 'page_count' => ceil((
int) $total / $limit),
187 public function post($request_data =
null)
189 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
190 throw new RestException(403);
193 $result = $this->
_validate($request_data);
196 foreach ($request_data as $field => $value) {
197 if ($field ===
'caller') {
199 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
203 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
205 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
206 throw new RestException(500,
'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
208 return $subscription->id;
224 public function put(
$id, $request_data =
null)
226 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
227 throw new RestException(403);
231 $result = $subscription->fetch(
$id);
233 throw new RestException(404,
'Subscription not found');
236 foreach ($request_data as $field => $value) {
237 if ($field ==
'id') {
240 if ($field ===
'caller') {
242 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
246 if ($field ==
'array_options' && is_array($value)) {
247 foreach ($value as $index => $val) {
252 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
255 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
256 return $this->
get(
$id);
258 throw new RestException(500,
'Error when updating contribution: '.$subscription->errorsToString());
275 public function delete(
$id)
278 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
279 throw new RestException(403);
282 $result = $subscription->fetch(
$id);
284 throw new RestException(404,
'Subscription not found');
287 $res = $subscription->delete(DolibarrApiAccess::$user);
289 throw new RestException(500,
"Can't delete, error occurs");
290 } elseif ($res == 0) {
291 throw new RestException(409,
"No subscription whas deleted");
297 'message' =>
'Subscription deleted'
312 $subscription = array();
313 foreach (Subscriptions::$FIELDS as $field) {
314 if (!isset($data[$field])) {
315 throw new RestException(400,
"$field field missing");
317 $subscription[$field] = $data[$field];
319 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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.