18use Luracast\Restler\RestException;
20require_once DOL_DOCUMENT_ROOT.
'/adherents/class/subscription.class.php';
33 public static $FIELDS = array(
59 public function get($id)
61 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
62 throw new RestException(401);
66 $result = $subscription->fetch($id);
68 throw new RestException(404,
'Subscription not found');
89 public function index($sortfield =
"dateadh", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
95 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
96 throw new RestException(401);
99 $sql =
"SELECT rowid";
100 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
101 $sql .=
' WHERE 1 = 1';
107 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
111 $sql .= $this->db->order($sortfield, $sortorder);
116 $offset = $limit * $page;
118 $sql .= $this->db->plimit($limit + 1, $offset);
121 $result = $this->db->query($sql);
124 $num = $this->db->num_rows($result);
125 while ($i < min($limit, $num)) {
126 $obj = $this->db->fetch_object($result);
128 if ($subscription->fetch($obj->rowid)) {
134 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
146 public function post($request_data =
null)
148 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
149 throw new RestException(401);
152 $result = $this->
_validate($request_data);
155 foreach ($request_data as $field => $value) {
156 if ($field ===
'caller') {
158 $subscription->context[
'caller'] = $request_data[
'caller'];
162 $subscription->$field = $value;
164 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
165 throw new RestException(500,
'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
167 return $subscription->id;
177 public function put($id, $request_data =
null)
179 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
180 throw new RestException(401);
184 $result = $subscription->fetch($id);
186 throw new RestException(404,
'Subscription not found');
189 foreach ($request_data as $field => $value) {
190 if ($field ==
'id') {
193 if ($field ===
'caller') {
195 $subscription->context[
'caller'] = $request_data[
'caller'];
199 $subscription->$field = $value;
202 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
203 return $this->
get($id);
205 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
215 public function delete($id)
218 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
219 throw new RestException(401);
222 $result = $subscription->fetch($id);
224 throw new RestException(404,
'Subscription not found');
227 $res = $subscription->delete(DolibarrApiAccess::$user);
229 throw new RestException(500,
"Can't delete, error occurs");
230 } elseif ($res == 0) {
231 throw new RestException(409,
"Can't delete, that product is probably used");
237 'message' =>
'Subscription deleted'
252 $subscription = array();
253 foreach (Subscriptions::$FIELDS as $field) {
254 if (!isset($data[$field])) {
255 throw new RestException(400,
"$field field missing");
257 $subscription[$field] = $data[$field];
259 return $subscription;
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_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