dolibarr  7.0.0-beta
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 <http://www.gnu.org/licenses/>.
16  */
17 
18 use Luracast\Restler\RestException;
19 
20 require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
21 
29 {
33  static $FIELDS = array(
34  'fk_adherent',
35  'dateh',
36  'datef',
37  'amount'
38  );
39 
43  function __construct()
44  {
45  global $db, $conf;
46  $this->db = $db;
47  }
48 
59  function get($id)
60  {
61  if(! DolibarrApiAccess::$user->rights->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 
88  function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '') {
89  global $db, $conf;
90 
91  $obj_ret = array();
92 
93  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
94  throw new RestException(401);
95  }
96 
97  $sql = "SELECT rowid";
98  $sql.= " FROM ".MAIN_DB_PREFIX."subscription as t";
99  $sql.= ' WHERE 1 = 1';
100  // Add sql filters
101  if ($sqlfilters)
102  {
103  if (! DolibarrApi::_checkFilters($sqlfilters))
104  {
105  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
106  }
107  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
108  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
109  }
110 
111  $sql.= $db->order($sortfield, $sortorder);
112  if ($limit) {
113  if ($page < 0)
114  {
115  $page = 0;
116  }
117  $offset = $limit * $page;
118 
119  $sql.= $db->plimit($limit + 1, $offset);
120  }
121 
122  $result = $db->query($sql);
123  if ($result)
124  {
125  $i=0;
126  $num = $db->num_rows($result);
127  while ($i < min($limit, $num))
128  {
129  $obj = $db->fetch_object($result);
130  $subscription = new Subscription($this->db);
131  if($subscription->fetch($obj->rowid)) {
132  $obj_ret[] = $this->_cleanObjectDatas($subscription);
133  }
134  $i++;
135  }
136  }
137  else {
138  throw new RestException(503, 'Error when retrieve subscription list : '.$db->lasterror());
139  }
140  if( ! count($obj_ret)) {
141  throw new RestException(404, 'No Subscription found');
142  }
143 
144  return $obj_ret;
145  }
146 
153  function post($request_data = null)
154  {
155  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
156  throw new RestException(401);
157  }
158  // Check mandatory fields
159  $result = $this->_validate($request_data);
160 
161  $subscription = new Subscription($this->db);
162  foreach($request_data as $field => $value) {
163  $subscription->$field = $value;
164  }
165  if ($subscription->create(DolibarrApiAccess::$user) < 0) {
166  throw new RestException(500, 'Error when creating subscription', array_merge(array($subscription->error), $subscription->errors));
167  }
168  return $subscription->id;
169  }
170 
178  function put($id, $request_data = null)
179  {
180  if(! DolibarrApiAccess::$user->rights->adherent->creer) {
181  throw new RestException(401);
182  }
183 
184  $subscription = new Subscription($this->db);
185  $result = $subscription->fetch($id);
186  if( ! $result ) {
187  throw new RestException(404, 'Subscription not found');
188  }
189 
190  foreach($request_data as $field => $value) {
191  if ($field == 'id') continue;
192  $subscription->$field = $value;
193  }
194 
195  if ($subscription->update(DolibarrApiAccess::$user) > 0)
196  {
197  return $this->get($id);
198  }
199  else
200  {
201  throw new RestException(500, $subscription->error);
202  }
203  }
204 
211  function delete($id)
212  {
213  // The right to delete a subscription comes with the right to create one.
214  if(! DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
215  throw new RestException(401);
216  }
217  $subscription = new Subscription($this->db);
218  $result = $subscription->fetch($id);
219  if( ! $result ) {
220  throw new RestException(404, 'Subscription not found');
221  }
222 
223  if (! $subscription->delete(DolibarrApiAccess::$user)) {
224  throw new RestException(401,'error when deleting subscription');
225  }
226 
227  return array(
228  'success' => array(
229  'code' => 200,
230  'message' => 'subscription deleted'
231  )
232  );
233  }
234 
243  function _validate($data)
244  {
245  $subscription = array();
246  foreach (Subscriptions::$FIELDS as $field) {
247  if (!isset($data[$field]))
248  throw new RestException(400, "$field field missing");
249  $subscription[$field] = $data[$field];
250  }
251  return $subscription;
252  }
253 }
index($sortfield="dateadh", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List subscriptions.
_cleanObjectDatas($object)
Executed method when API is called without parameter.
Definition: api.class.php:95
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
Class for API REST v1.
Definition: api.class.php:29
post($request_data=null)
Create subscription object.
Class to manage subscriptions of foundation members.
_validate($data)
Validate fields before creating an object.
put($id, $request_data=null)
Update subscription.
__construct()
Constructor.