18 use Luracast\Restler\RestException;
20 require_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');
88 public function index($sortfield =
"dateadh", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
94 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'lire')) {
95 throw new RestException(401);
98 $sql =
"SELECT rowid";
99 $sql .=
" FROM ".MAIN_DB_PREFIX.
"subscription as t";
100 $sql .=
' WHERE 1 = 1';
105 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
107 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
108 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
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());
136 if (!count($obj_ret)) {
137 throw new RestException(404,
'No Subscription found');
149 public function post($request_data =
null)
151 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
152 throw new RestException(401);
155 $result = $this->
_validate($request_data);
158 foreach ($request_data as $field => $value) {
159 $subscription->$field = $value;
161 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
162 throw new RestException(500,
'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
164 return $subscription->id;
174 public function put($id, $request_data =
null)
176 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
177 throw new RestException(401);
181 $result = $subscription->fetch($id);
183 throw new RestException(404,
'Subscription not found');
186 foreach ($request_data as $field => $value) {
187 if ($field ==
'id') {
190 $subscription->$field = $value;
193 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
194 return $this->
get($id);
196 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
206 public function delete($id)
209 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
210 throw new RestException(401);
213 $result = $subscription->fetch($id);
215 throw new RestException(404,
'Subscription not found');
218 $res = $subscription->delete(DolibarrApiAccess::$user);
220 throw new RestException(500,
"Can't delete, error occurs");
221 } elseif ($res == 0) {
222 throw new RestException(409,
"Can't delete, that product is probably used");
228 'message' =>
'Subscription deleted'
243 $subscription = array();
244 foreach (Subscriptions::$FIELDS as $field) {
245 if (!isset($data[$field])) {
246 throw new RestException(400,
"$field field missing");
248 $subscription[$field] = $data[$field];
250 return $subscription;
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
_cleanObjectDatas($object)
Clean sensible object datas.
Class to manage subscriptions of foundation members.
index($sortfield="dateadh", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List subscriptions.
_validate($data)
Validate fields before creating an object.
post($request_data=null)
Create subscription object.
put($id, $request_data=null)
Update subscription.
__construct()
Constructor.
$conf db
API class for accounts.