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');
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';
106 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
110 $sql .= $this->db->order($sortfield, $sortorder);
115 $offset = $limit * $page;
117 $sql .= $this->db->plimit($limit + 1, $offset);
120 $result = $this->db->query($sql);
123 $num = $this->db->num_rows($result);
124 while ($i < min($limit, $num)) {
125 $obj = $this->db->fetch_object($result);
127 if ($subscription->fetch($obj->rowid)) {
133 throw new RestException(503,
'Error when retrieve subscription list : '.$this->db->lasterror());
135 if (!count($obj_ret)) {
136 throw new RestException(404,
'No Subscription found');
148 public function post($request_data =
null)
150 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
151 throw new RestException(401);
154 $result = $this->
_validate($request_data);
157 foreach ($request_data as $field => $value) {
158 $subscription->$field = $value;
160 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
161 throw new RestException(500,
'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
163 return $subscription->id;
173 public function put($id, $request_data =
null)
175 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
176 throw new RestException(401);
180 $result = $subscription->fetch($id);
182 throw new RestException(404,
'Subscription not found');
185 foreach ($request_data as $field => $value) {
186 if ($field ==
'id') {
189 if ($field ==
'array_options' && is_array($value)) {
190 foreach ($value as $index => $val) {
191 $subscription->array_options[$index] = $this->
_checkValForAPI($field, $val, $subscription);
195 $subscription->$field = $value;
198 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
199 return $this->
get($id);
201 throw new RestException(500,
'Error when updating contribution: '.$subscription->error);
211 public function delete($id)
214 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'cotisation',
'creer')) {
215 throw new RestException(401);
218 $result = $subscription->fetch($id);
220 throw new RestException(404,
'Subscription not found');
223 $res = $subscription->delete(DolibarrApiAccess::$user);
225 throw new RestException(500,
"Can't delete, error occurs");
226 } elseif ($res == 0) {
227 throw new RestException(409,
"Can't delete, that product is probably used");
233 'message' =>
'Subscription deleted'
248 $subscription = array();
249 foreach (Subscriptions::$FIELDS as $field) {
250 if (!isset($data[$field])) {
251 throw new RestException(400,
"$field field missing");
253 $subscription[$field] = $data[$field];
255 return $subscription;
_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.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria