dolibarr 24.0.0-beta
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 $obj_ret = array();
106
107 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
108 throw new RestException(403);
109 }
110
111 $sql = "SELECT rowid";
112 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
113 $sql .= ' WHERE 1 = 1';
114 // Add sql filters
115 if ($sqlfilters) {
116 $errormessage = '';
117 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
118 if ($errormessage) {
119 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
120 }
121 }
122
123 //this query will return total orders with the filters given
124 $sqlTotals = str_replace('SELECT rowid', 'SELECT count(rowid) as total', $sql);
125
126 $sql .= $this->db->order($sortfield, $sortorder);
127 if ($limit) {
128 if ($page < 0) {
129 $page = 0;
130 }
131 $offset = $limit * $page;
132
133 $sql .= $this->db->plimit($limit + 1, $offset);
134 }
135
136 $result = $this->db->query($sql);
137 if ($result) {
138 $i = 0;
139 $num = $this->db->num_rows($result);
140 $min = min($num, ($limit <= 0 ? $num : $limit));
141 while ($i < $min) {
142 $obj = $this->db->fetch_object($result);
143 $subscription = new Subscription($this->db);
144 if ($subscription->fetch($obj->rowid)) {
145 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($subscription), $properties);
146 }
147 $i++;
148 }
149 } else {
150 throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
151 }
152
153 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
154 if ($pagination_data) {
155 $totalsResult = $this->db->query($sqlTotals);
156 $total = $this->db->fetch_object($totalsResult)->total;
157
158 $tmp = $obj_ret;
159 $obj_ret = [];
160
161 $obj_ret['data'] = $tmp;
162 $obj_ret['pagination'] = [
163 'total' => (int) $total,
164 'page' => $page, //count starts from 0
165 'page_count' => ceil((int) $total / $limit),
166 'limit' => $limit
167 ];
168 }
169
170 return $obj_ret;
171 }
172
184 public function post($request_data = null)
185 {
186 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
187 throw new RestException(403);
188 }
189 // Check mandatory fields
190 $result = $this->_validate($request_data);
191
192 $subscription = new Subscription($this->db);
193 foreach ($request_data as $field => $value) {
194 if ($field === 'caller') {
195 // 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
196 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
197 continue;
198 }
199
200 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
201 }
202 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
203 throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
204 }
205 return $subscription->id;
206 }
207
221 public function put($id, $request_data = null)
222 {
223 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
224 throw new RestException(403);
225 }
226
227 $subscription = new Subscription($this->db);
228 $result = $subscription->fetch($id);
229 if (!$result) {
230 throw new RestException(404, 'Subscription not found');
231 }
232
233 foreach ($request_data as $field => $value) {
234 if ($field == 'id') {
235 continue;
236 }
237 if ($field === 'caller') {
238 // 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
239 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
240 continue;
241 }
242
243 if ($field == 'array_options' && is_array($value)) {
244 foreach ($value as $index => $val) {
245 $subscription->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $subscription);
246 }
247 continue;
248 }
249 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
250 }
251
252 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
253 return $this->get($id);
254 } else {
255 throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
256 }
257 }
258
272 public function delete($id)
273 {
274 // The right to delete a subscription comes with the right to create one.
275 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
276 throw new RestException(403);
277 }
278 $subscription = new Subscription($this->db);
279 $result = $subscription->fetch($id);
280 if (!$result) {
281 throw new RestException(404, 'Subscription not found');
282 }
283
284 $res = $subscription->delete(DolibarrApiAccess::$user);
285 if ($res < 0) {
286 throw new RestException(500, "Can't delete, error occurs");
287 } elseif ($res == 0) {
288 throw new RestException(409, "No subscription whas deleted");
289 }
290
291 return array(
292 'success' => array(
293 'code' => 200,
294 'message' => 'Subscription deleted'
295 )
296 );
297 }
298
307 private function _validate($data)
308 {
309 $subscription = array();
310 foreach (Subscriptions::$FIELDS as $field) {
311 if (!isset($data[$field])) {
312 throw new RestException(400, "$field field missing");
313 }
314 $subscription[$field] = $data[$field];
315 }
316 return $subscription;
317 }
318}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Class for API REST v1.
Definition api.class.php:35
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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 sensitive object data @phpstan-template T.
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.