dolibarr  16.0.5
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 
18 use Luracast\Restler\RestException;
19 
20 require_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->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  public function index($sortfield = "dateadh", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
89  {
90  global $conf;
91 
92  $obj_ret = array();
93 
94  if (!DolibarrApiAccess::$user->rights->adherent->cotisation->lire) {
95  throw new RestException(401);
96  }
97 
98  $sql = "SELECT rowid";
99  $sql .= " FROM ".MAIN_DB_PREFIX."subscription as t";
100  $sql .= ' WHERE 1 = 1';
101  // Add sql filters
102  if ($sqlfilters) {
103  $errormessage = '';
104  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
105  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
106  }
107  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
108  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
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->_cleanObjectDatas($subscription);
130  }
131  $i++;
132  }
133  } else {
134  throw new RestException(503, 'Error when retrieve subscription list : '.$this->db->lasterror());
135  }
136  if (!count($obj_ret)) {
137  throw new RestException(404, 'No Subscription found');
138  }
139 
140  return $obj_ret;
141  }
142 
149  public function post($request_data = null)
150  {
151  if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
152  throw new RestException(401);
153  }
154  // Check mandatory fields
155  $result = $this->_validate($request_data);
156 
157  $subscription = new Subscription($this->db);
158  foreach ($request_data as $field => $value) {
159  $subscription->$field = $value;
160  }
161  if ($subscription->create(DolibarrApiAccess::$user) < 0) {
162  throw new RestException(500, 'Error when creating contribution', array_merge(array($subscription->error), $subscription->errors));
163  }
164  return $subscription->id;
165  }
166 
174  public function put($id, $request_data = null)
175  {
176  if (!DolibarrApiAccess::$user->rights->adherent->creer) {
177  throw new RestException(401);
178  }
179 
180  $subscription = new Subscription($this->db);
181  $result = $subscription->fetch($id);
182  if (!$result) {
183  throw new RestException(404, 'Subscription not found');
184  }
185 
186  foreach ($request_data as $field => $value) {
187  if ($field == 'id') {
188  continue;
189  }
190  $subscription->$field = $value;
191  }
192 
193  if ($subscription->update(DolibarrApiAccess::$user) > 0) {
194  return $this->get($id);
195  } else {
196  throw new RestException(500, 'Error when updating contribution: '.$subscription->error);
197  }
198  }
199 
206  public function delete($id)
207  {
208  // The right to delete a subscription comes with the right to create one.
209  if (!DolibarrApiAccess::$user->rights->adherent->cotisation->creer) {
210  throw new RestException(401);
211  }
212  $subscription = new Subscription($this->db);
213  $result = $subscription->fetch($id);
214  if (!$result) {
215  throw new RestException(404, 'Subscription not found');
216  }
217 
218  $res = $subscription->delete(DolibarrApiAccess::$user);
219  if ($res < 0) {
220  throw new RestException(500, "Can't delete, error occurs");
221  } elseif ($res == 0) {
222  throw new RestException(409, "Can't delete, that product is probably used");
223  }
224 
225  return array(
226  'success' => array(
227  'code' => 200,
228  'message' => 'Subscription deleted'
229  )
230  );
231  }
232 
241  private function _validate($data)
242  {
243  $subscription = array();
244  foreach (Subscriptions::$FIELDS as $field) {
245  if (!isset($data[$field])) {
246  throw new RestException(400, "$field field missing");
247  }
248  $subscription[$field] = $data[$field];
249  }
250  return $subscription;
251  }
252 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Subscriptions\__construct
__construct()
Constructor.
Definition: api_subscriptions.class.php:43
Subscriptions
Definition: api_subscriptions.class.php:28
Subscriptions\index
index($sortfield="dateadh", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List subscriptions.
Definition: api_subscriptions.class.php:88
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
DolibarrApi\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api.class.php:104
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Subscriptions\put
put($id, $request_data=null)
Update subscription.
Definition: api_subscriptions.class.php:174
Subscriptions\post
post($request_data=null)
Create subscription object.
Definition: api_subscriptions.class.php:149
Subscription
Class to manage subscriptions of foundation members.
Definition: subscription.class.php:33
Subscriptions\_validate
_validate($data)
Validate fields before creating an object.
Definition: api_subscriptions.class.php:241