dolibarr 22.0.5
api_subscriptions.class.php
1<?php
2/* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
22
30{
34 public static $FIELDS = array(
35 'fk_adherent',
36 'dateh',
37 'datef',
38 'amount',
39 );
40
44 public $subscription;
45
49 public function __construct()
50 {
51 global $db, $conf;
52 $this->db = $db;
53 $this->subscription = new Subscription($this->db);
54 }
55
67 public function get($id)
68 {
69 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
70 throw new RestException(403);
71 }
72
73 $result = $this->subscription->fetch($id);
74 if (!$result) {
75 throw new RestException(404, 'Subscription not found');
76 }
77
78 $this->subscription->fetchObjectLinked();
79
80 return $this->_cleanObjectDatas($this->subscription);
81 }
82
103 public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
104 {
105 global $conf;
106
107 $obj_ret = array();
108
109 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
110 throw new RestException(403);
111 }
112
113 $sql = "SELECT rowid";
114 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
115 $sql .= ' WHERE 1 = 1';
116 // Add sql filters
117 if ($sqlfilters) {
118 $errormessage = '';
119 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
120 if ($errormessage) {
121 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
122 }
123 }
124
125 //this query will return total orders with the filters given
126 $sqlTotals = str_replace('SELECT rowid', 'SELECT count(rowid) as total', $sql);
127
128 $sql .= $this->db->order($sortfield, $sortorder);
129 if ($limit) {
130 if ($page < 0) {
131 $page = 0;
132 }
133 $offset = $limit * $page;
134
135 $sql .= $this->db->plimit($limit + 1, $offset);
136 }
137
138 $result = $this->db->query($sql);
139 if ($result) {
140 $i = 0;
141 $num = $this->db->num_rows($result);
142 while ($i < min($limit, $num)) {
143 $obj = $this->db->fetch_object($result);
144 $subscription = new Subscription($this->db);
145 if ($subscription->fetch($obj->rowid)) {
146 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($subscription), $properties);
147 }
148 $i++;
149 }
150 } else {
151 throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
152 }
153
154 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
155 if ($pagination_data) {
156 $totalsResult = $this->db->query($sqlTotals);
157 $total = $this->db->fetch_object($totalsResult)->total;
158
159 $tmp = $obj_ret;
160 $obj_ret = [];
161
162 $obj_ret['data'] = $tmp;
163 $obj_ret['pagination'] = [
164 'total' => (int) $total,
165 'page' => $page, //count starts from 0
166 'page_count' => ceil((int) $total / $limit),
167 'limit' => $limit
168 ];
169 }
170
171 return $obj_ret;
172 }
173
185 public function post($request_data = null)
186 {
187 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
188 throw new RestException(403);
189 }
190 // Check mandatory fields
191 $result = $this->_validate($request_data);
192
193 $subscription = new Subscription($this->db);
194 foreach ($request_data as $field => $value) {
195 if ($field === 'caller') {
196 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
197 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
198 continue;
199 }
200
201 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
202 }
203 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
204 throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
205 }
206 return $subscription->id;
207 }
208
222 public function put($id, $request_data = null)
223 {
224 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
225 throw new RestException(403);
226 }
227
228 $subscription = new Subscription($this->db);
229 $result = $subscription->fetch($id);
230 if (!$result) {
231 throw new RestException(404, 'Subscription not found');
232 }
233
234 foreach ($request_data as $field => $value) {
235 if ($field == 'id') {
236 continue;
237 }
238 if ($field === 'caller') {
239 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
240 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
241 continue;
242 }
243
244 if ($field == 'array_options' && is_array($value)) {
245 foreach ($value as $index => $val) {
246 $subscription->array_options[$index] = $this->_checkValForAPI($field, $val, $subscription);
247 }
248 continue;
249 }
250 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
251 }
252
253 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
254 return $this->get($id);
255 } else {
256 throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
257 }
258 }
259
273 public function delete($id)
274 {
275 // The right to delete a subscription comes with the right to create one.
276 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
277 throw new RestException(403);
278 }
279 $subscription = new Subscription($this->db);
280 $result = $subscription->fetch($id);
281 if (!$result) {
282 throw new RestException(404, 'Subscription not found');
283 }
284
285 $res = $subscription->delete(DolibarrApiAccess::$user);
286 if ($res < 0) {
287 throw new RestException(500, "Can't delete, error occurs");
288 } elseif ($res == 0) {
289 throw new RestException(409, "No subscription whas deleted");
290 }
291
292 return array(
293 'success' => array(
294 'code' => 200,
295 'message' => 'Subscription deleted'
296 )
297 );
298 }
299
308 private function _validate($data)
309 {
310 $subscription = array();
311 foreach (Subscriptions::$FIELDS as $field) {
312 if (!isset($data[$field])) {
313 throw new RestException(400, "$field field missing");
314 }
315 $subscription[$field] = $data[$field];
316 }
317 return $subscription;
318 }
319}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
Class for API REST v1.
Definition api.class.php:33
_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.
Definition api.class.php:98
_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.
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...
Definition member.php:79