18use Luracast\Restler\RestException;
20require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
33 public static $FIELDS = array(
60 public function get($id)
62 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
63 throw new RestException(403);
67 $result = $subscription->fetch($id);
69 throw new RestException(404,
'Subscription not found');
92 public function index($sortfield =
"dateadh", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
98 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
99 throw new RestException(403);
102 $sql =
"SELECT rowid";
103 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
104 $sql .=
' WHERE 1 = 1';
110 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
114 $sql .= $this->db->order($sortfield, $sortorder);
119 $offset = $limit * $page;
121 $sql .= $this->db->plimit($limit + 1, $offset);
124 $result = $this->db->query($sql);
127 $num = $this->db->num_rows($result);
128 while ($i < min($limit, $num)) {
129 $obj = $this->db->fetch_object($result);
131 if ($subscription->fetch($obj->rowid)) {
137 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
152 public function post($request_data =
null)
154 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
155 throw new RestException(403);
158 $result = $this->
_validate($request_data);
161 foreach ($request_data as $field => $value) {
162 if ($field ===
'caller') {
164 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
168 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
170 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
171 throw new RestException(500,
'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
173 return $subscription->id;
187 public function put($id, $request_data =
null)
189 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
190 throw new RestException(403);
194 $result = $subscription->fetch($id);
196 throw new RestException(404,
'Subscription not found');
199 foreach ($request_data as $field => $value) {
200 if ($field ==
'id') {
203 if ($field ===
'caller') {
205 $subscription->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
209 $subscription->$field = $this->
_checkValForAPI($field, $value, $subscription);
212 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
213 return $this->
get($id);
215 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
230 public function delete($id)
233 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
234 throw new RestException(403);
237 $result = $subscription->fetch($id);
239 throw new RestException(404,
'Subscription not found');
242 $res = $subscription->delete(DolibarrApiAccess::$user);
244 throw new RestException(500,
"Can't delete, error occurs");
245 } elseif ($res == 0) {
246 throw new RestException(409,
"No subscription whas deleted");
252 'message' =>
'Subscription deleted'
267 $subscription = array();
268 foreach (Subscriptions::$FIELDS as $field) {
269 if (!isset($data[$field])) {
270 throw new RestException(400,
"$field field missing");
272 $subscription[$field] = $data[$field];
274 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 sensible object datas.
Class to manage subscriptions of foundation members.
_validate($data)
Validate fields before creating an object.
index($sortfield="dateadh", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List subscriptions.
post($request_data=null)
Create subscription object.
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.