dolibarr 20.0.0
api_subscriptions.class.php
1<?php
2/* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18use Luracast\Restler\RestException;
19
20require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
21
29{
33 public static $FIELDS = array(
34 'fk_adherent',
35 'dateh',
36 'datef',
37 'amount',
38 );
39
43 public function __construct()
44 {
45 global $db, $conf;
46 $this->db = $db;
47 }
48
60 public function get($id)
61 {
62 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
63 throw new RestException(403);
64 }
65
66 $subscription = new Subscription($this->db);
67 $result = $subscription->fetch($id);
68 if (!$result) {
69 throw new RestException(404, 'Subscription not found');
70 }
71
72 return $this->_cleanObjectDatas($subscription);
73 }
74
92 public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
93 {
94 global $conf;
95
96 $obj_ret = array();
97
98 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
99 throw new RestException(403);
100 }
101
102 $sql = "SELECT rowid";
103 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
104 $sql .= ' WHERE 1 = 1';
105 // Add sql filters
106 if ($sqlfilters) {
107 $errormessage = '';
108 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
109 if ($errormessage) {
110 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
111 }
112 }
113
114 $sql .= $this->db->order($sortfield, $sortorder);
115 if ($limit) {
116 if ($page < 0) {
117 $page = 0;
118 }
119 $offset = $limit * $page;
120
121 $sql .= $this->db->plimit($limit + 1, $offset);
122 }
123
124 $result = $this->db->query($sql);
125 if ($result) {
126 $i = 0;
127 $num = $this->db->num_rows($result);
128 while ($i < min($limit, $num)) {
129 $obj = $this->db->fetch_object($result);
130 $subscription = new Subscription($this->db);
131 if ($subscription->fetch($obj->rowid)) {
132 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($subscription), $properties);
133 }
134 $i++;
135 }
136 } else {
137 throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
138 }
139
140 return $obj_ret;
141 }
142
152 public function post($request_data = null)
153 {
154 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
155 throw new RestException(403);
156 }
157 // Check mandatory fields
158 $result = $this->_validate($request_data);
159
160 $subscription = new Subscription($this->db);
161 foreach ($request_data as $field => $value) {
162 if ($field === 'caller') {
163 // 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
164 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
165 continue;
166 }
167
168 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
169 }
170 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
171 throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
172 }
173 return $subscription->id;
174 }
175
187 public function put($id, $request_data = null)
188 {
189 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
190 throw new RestException(403);
191 }
192
193 $subscription = new Subscription($this->db);
194 $result = $subscription->fetch($id);
195 if (!$result) {
196 throw new RestException(404, 'Subscription not found');
197 }
198
199 foreach ($request_data as $field => $value) {
200 if ($field == 'id') {
201 continue;
202 }
203 if ($field === 'caller') {
204 // 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
205 $subscription->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
206 continue;
207 }
208
209 $subscription->$field = $this->_checkValForAPI($field, $value, $subscription);
210 }
211
212 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
213 return $this->get($id);
214 } else {
215 throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
216 }
217 }
218
230 public function delete($id)
231 {
232 // The right to delete a subscription comes with the right to create one.
233 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
234 throw new RestException(403);
235 }
236 $subscription = new Subscription($this->db);
237 $result = $subscription->fetch($id);
238 if (!$result) {
239 throw new RestException(404, 'Subscription not found');
240 }
241
242 $res = $subscription->delete(DolibarrApiAccess::$user);
243 if ($res < 0) {
244 throw new RestException(500, "Can't delete, error occurs");
245 } elseif ($res == 0) {
246 throw new RestException(409, "No subscription whas deleted");
247 }
248
249 return array(
250 'success' => array(
251 'code' => 200,
252 'message' => 'Subscription deleted'
253 )
254 );
255 }
256
265 private function _validate($data)
266 {
267 $subscription = array();
268 foreach (Subscriptions::$FIELDS as $field) {
269 if (!isset($data[$field])) {
270 throw new RestException(400, "$field field missing");
271 }
272 $subscription[$field] = $data[$field];
273 }
274 return $subscription;
275 }
276}
Class for API REST v1.
Definition api.class.php:30
_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:82
_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.
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.