dolibarr 19.0.3
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
59 public function get($id)
60 {
61 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
62 throw new RestException(401);
63 }
64
65 $subscription = new Subscription($this->db);
66 $result = $subscription->fetch($id);
67 if (!$result) {
68 throw new RestException(404, 'Subscription not found');
69 }
70
71 return $this->_cleanObjectDatas($subscription);
72 }
73
89 public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
90 {
91 global $conf;
92
93 $obj_ret = array();
94
95 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'lire')) {
96 throw new RestException(401);
97 }
98
99 $sql = "SELECT rowid";
100 $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
101 $sql .= ' WHERE 1 = 1';
102 // Add sql filters
103 if ($sqlfilters) {
104 $errormessage = '';
105 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
106 if ($errormessage) {
107 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
108 }
109 }
110
111 $sql .= $this->db->order($sortfield, $sortorder);
112 if ($limit) {
113 if ($page < 0) {
114 $page = 0;
115 }
116 $offset = $limit * $page;
117
118 $sql .= $this->db->plimit($limit + 1, $offset);
119 }
120
121 $result = $this->db->query($sql);
122 if ($result) {
123 $i = 0;
124 $num = $this->db->num_rows($result);
125 while ($i < min($limit, $num)) {
126 $obj = $this->db->fetch_object($result);
127 $subscription = new Subscription($this->db);
128 if ($subscription->fetch($obj->rowid)) {
129 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($subscription), $properties);
130 }
131 $i++;
132 }
133 } else {
134 throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
135 }
136
137 return $obj_ret;
138 }
139
146 public function post($request_data = null)
147 {
148 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
149 throw new RestException(401);
150 }
151 // Check mandatory fields
152 $result = $this->_validate($request_data);
153
154 $subscription = new Subscription($this->db);
155 foreach ($request_data as $field => $value) {
156 if ($field === 'caller') {
157 // 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 whith the caller
158 $subscription->context['caller'] = $request_data['caller'];
159 continue;
160 }
161
162 $subscription->$field = $value;
163 }
164 if ($subscription->create(DolibarrApiAccess::$user) < 0) {
165 throw new RestException(500, 'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
166 }
167 return $subscription->id;
168 }
169
177 public function put($id, $request_data = null)
178 {
179 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
180 throw new RestException(401);
181 }
182
183 $subscription = new Subscription($this->db);
184 $result = $subscription->fetch($id);
185 if (!$result) {
186 throw new RestException(404, 'Subscription not found');
187 }
188
189 foreach ($request_data as $field => $value) {
190 if ($field == 'id') {
191 continue;
192 }
193 if ($field === 'caller') {
194 // 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 whith the caller
195 $subscription->context['caller'] = $request_data['caller'];
196 continue;
197 }
198
199 $subscription->$field = $value;
200 }
201
202 if ($subscription->update(DolibarrApiAccess::$user) > 0) {
203 return $this->get($id);
204 } else {
205 throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
206 }
207 }
208
215 public function delete($id)
216 {
217 // The right to delete a subscription comes with the right to create one.
218 if (!DolibarrApiAccess::$user->hasRight('adherent', 'cotisation', 'creer')) {
219 throw new RestException(401);
220 }
221 $subscription = new Subscription($this->db);
222 $result = $subscription->fetch($id);
223 if (!$result) {
224 throw new RestException(404, 'Subscription not found');
225 }
226
227 $res = $subscription->delete(DolibarrApiAccess::$user);
228 if ($res < 0) {
229 throw new RestException(500, "Can't delete, error occurs");
230 } elseif ($res == 0) {
231 throw new RestException(409, "Can't delete, that product is probably used");
232 }
233
234 return array(
235 'success' => array(
236 'code' => 200,
237 'message' => 'Subscription deleted'
238 )
239 );
240 }
241
250 private function _validate($data)
251 {
252 $subscription = array();
253 foreach (Subscriptions::$FIELDS as $field) {
254 if (!isset($data[$field])) {
255 throw new RestException(400, "$field field missing");
256 }
257 $subscription[$field] = $data[$field];
258 }
259 return $subscription;
260 }
261}
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_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