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)
109 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
110 throw new RestException(403);
113 $sql =
"SELECT rowid";
114 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
115 $sql .=
' WHERE 1 = 1';
121 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
126 $sqlTotals = str_replace(
'SELECT rowid',
'SELECT count(rowid) as total', $sql);
128 $sql .= $this->db->order($sortfield, $sortorder);
133 $offset = $limit * $page;
135 $sql .= $this->db->plimit($limit + 1, $offset);
138 $result = $this->db->query($sql);
141 $num = $this->db->num_rows($result);
142 $min = min($num, ($limit <= 0 ? $num : $limit));
144 $obj = $this->db->fetch_object($result);
146 if ($subscription->fetch($obj->rowid)) {
152 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
156 if ($pagination_data) {
157 $totalsResult = $this->db->query($sqlTotals);
158 $total = $this->db->fetch_object($totalsResult)->total;
163 $obj_ret[
'data'] = $tmp;
164 $obj_ret[
'pagination'] = [
165 'total' => (int) $total,
167 'page_count' => ceil((
int) $total / $limit),
186 public function post($request_data =
null)
188 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
189 throw new RestException(403);
192 $result = $this->
_validate($request_data);
195 foreach ($request_data as $field => $value) {
196 if ($field ===
'caller') {
198 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
202 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
204 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
205 throw new RestException(500,
'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
207 return $subscription->id;
223 public function put(
$id, $request_data =
null)
225 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
226 throw new RestException(403);
230 $result = $subscription->fetch(
$id);
232 throw new RestException(404,
'Subscription not found');
235 foreach ($request_data as $field => $value) {
236 if ($field ==
'id') {
239 if ($field ===
'caller') {
241 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
245 if ($field ==
'array_options' && is_array($value)) {
246 foreach ($value as $index => $val) {
247 $subscription->array_options[$index] = $this->
_checkValForAPI($field, $val, $subscription);
251 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
254 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
255 return $this->
get(
$id);
257 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
274 public function delete(
$id)
277 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
278 throw new RestException(403);
281 $result = $subscription->fetch(
$id);
283 throw new RestException(404,
'Subscription not found');
286 $res = $subscription->delete(DolibarrApiAccess::$user);
288 throw new RestException(500,
"Can't delete, error occurs");
289 } elseif ($res == 0) {
290 throw new RestException(409,
"No subscription whas deleted");
296 'message' =>
'Subscription deleted'
311 $subscription = array();
312 foreach (Subscriptions::$FIELDS as $field) {
313 if (!isset($data[$field])) {
314 throw new RestException(400,
"$field field missing");
316 $subscription[$field] = $data[$field];
318 return $subscription;
$id
Support class for third parties, contacts, members, users or resources.
_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)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.