19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
34 public static $FIELDS = array(
61 public function get(
$id)
63 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
64 throw new RestException(403);
68 $result = $subscription->fetch(
$id);
70 throw new RestException(404,
'Subscription not found');
96 public function index($sortfield =
"dateadh", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
102 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
103 throw new RestException(403);
106 $sql =
"SELECT rowid";
107 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
108 $sql .=
' WHERE 1 = 1';
114 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
119 $sqlTotals = str_replace(
'SELECT rowid',
'SELECT count(rowid) as total', $sql);
121 $sql .= $this->db->order($sortfield, $sortorder);
126 $offset = $limit * $page;
128 $sql .= $this->db->plimit($limit + 1, $offset);
131 $result = $this->db->query($sql);
134 $num = $this->db->num_rows($result);
135 while ($i < min($limit, $num)) {
136 $obj = $this->db->fetch_object($result);
138 if ($subscription->fetch($obj->rowid)) {
144 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
148 if ($pagination_data) {
149 $totalsResult = $this->db->query($sqlTotals);
150 $total = $this->db->fetch_object($totalsResult)->total;
155 $obj_ret[
'data'] = $tmp;
156 $obj_ret[
'pagination'] = [
157 'total' => (int) $total,
159 'page_count' => ceil((
int) $total / $limit),
178 public function post($request_data =
null)
180 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
181 throw new RestException(403);
184 $result = $this->
_validate($request_data);
187 foreach ($request_data as $field => $value) {
188 if ($field ===
'caller') {
190 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
194 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
196 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
197 throw new RestException(500,
'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
199 return $subscription->id;
215 public function put(
$id, $request_data =
null)
217 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
218 throw new RestException(403);
222 $result = $subscription->fetch(
$id);
224 throw new RestException(404,
'Subscription not found');
227 foreach ($request_data as $field => $value) {
228 if ($field ==
'id') {
231 if ($field ===
'caller') {
233 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
237 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
240 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
241 return $this->
get(
$id);
243 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
260 public function delete(
$id)
263 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
264 throw new RestException(403);
267 $result = $subscription->fetch(
$id);
269 throw new RestException(404,
'Subscription not found');
272 $res = $subscription->delete(DolibarrApiAccess::$user);
274 throw new RestException(500,
"Can't delete, error occurs");
275 } elseif ($res == 0) {
276 throw new RestException(409,
"No subscription whas deleted");
282 'message' =>
'Subscription deleted'
297 $subscription = array();
298 foreach (Subscriptions::$FIELDS as $field) {
299 if (!isset($data[$field])) {
300 throw new RestException(400,
"$field field missing");
302 $subscription[$field] = $data[$field];
304 return $subscription;
_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 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...