dolibarr  16.0.5
api_donations.class.php
1 <?php
2 /* Copyright (C) 2019 Thibault FOUCART <support@ptibogxiv.net>
3  * Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
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 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
22 
29 class Donations extends DolibarrApi
30 {
31 
35  public static $FIELDS = array(
36  'amount'
37  );
38 
42  public $don;
43 
47  public function __construct()
48  {
49  global $db, $conf;
50  $this->db = $db;
51  $this->don = new Don($this->db);
52  }
53 
64  public function get($id)
65  {
66  if (!DolibarrApiAccess::$user->rights->don->lire) {
67  throw new RestException(401);
68  }
69 
70  $result = $this->don->fetch($id);
71  if (!$result) {
72  throw new RestException(404, 'Donation not found');
73  }
74 
75  if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
76  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
77  }
78 
79  // Add external contacts ids
80  //$this->don->contacts_ids = $this->don->liste_contact(-1,'external',1);
81  //$this->don->fetchObjectLinked();
82  return $this->_cleanObjectDatas($this->don);
83  }
84 
85 
86 
102  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
103  {
104  global $db, $conf;
105 
106  if (!DolibarrApiAccess::$user->rights->don->lire) {
107  throw new RestException(401);
108  }
109 
110  $obj_ret = array();
111 
112  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
113  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
114 
115  $sql = "SELECT t.rowid";
116  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) {
117  $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
118  }
119  $sql .= " FROM ".MAIN_DB_PREFIX."don as t";
120 
121  $sql .= ' WHERE t.entity IN ('.getEntity('don').')';
122  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) {
123  $sql .= " AND t.fk_soc = sc.fk_soc";
124  }
125  if ($thirdparty_ids) {
126  $sql .= " AND t.fk_soc = ".((int) $thirdparty_ids)." ";
127  }
128 
129  // Add sql filters
130  if ($sqlfilters) {
131  $errormessage = '';
132  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
133  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
134  }
135  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
136  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
137  }
138 
139  $sql .= $this->db->order($sortfield, $sortorder);
140  if ($limit) {
141  if ($page < 0) {
142  $page = 0;
143  }
144  $offset = $limit * $page;
145 
146  $sql .= $this->db->plimit($limit + 1, $offset);
147  }
148 
149  dol_syslog("API Rest request");
150  $result = $this->db->query($sql);
151 
152  if ($result) {
153  $num = $this->db->num_rows($result);
154  $min = min($num, ($limit <= 0 ? $num : $limit));
155  $i = 0;
156  while ($i < $min) {
157  $obj = $this->db->fetch_object($result);
158  $don_static = new Don($this->db);
159  if ($don_static->fetch($obj->rowid)) {
160  // Add external contacts ids
161  //$don_static->contacts_ids = $don_static->liste_contact(-1, 'external', 1);
162  $obj_ret[] = $this->_cleanObjectDatas($don_static);
163  }
164  $i++;
165  }
166  } else {
167  throw new RestException(503, 'Error when retrieve donation list : '.$this->db->lasterror());
168  }
169  if (!count($obj_ret)) {
170  throw new RestException(404, 'No donation found');
171  }
172 
173  return $obj_ret;
174  }
175 
182  public function post($request_data = null)
183  {
184  if (!DolibarrApiAccess::$user->rights->don->creer) {
185  throw new RestException(401, "Insuffisant rights");
186  }
187 
188  // Check mandatory fields
189  $result = $this->_validate($request_data);
190 
191  foreach ($request_data as $field => $value) {
192  $this->don->$field = $value;
193  }
194  /*if (isset($request_data["lines"])) {
195  $lines = array();
196  foreach ($request_data["lines"] as $line) {
197  array_push($lines, (object) $line);
198  }
199  $this->don->lines = $lines;
200  }*/
201 
202  if ($this->don->create(DolibarrApiAccess::$user) < 0) {
203  throw new RestException(500, "Error creating donation", array_merge(array($this->don->error), $this->don->errors));
204  }
205 
206  return $this->don->id;
207  }
208 
217  public function put($id, $request_data = null)
218  {
219  if (!DolibarrApiAccess::$user->rights->don->creer) {
220  throw new RestException(401);
221  }
222 
223  $result = $this->don->fetch($id);
224  if (!$result) {
225  throw new RestException(404, 'Donation not found');
226  }
227 
228  if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
229  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
230  }
231  foreach ($request_data as $field => $value) {
232  if ($field == 'id') {
233  continue;
234  }
235  $this->don->$field = $value;
236  }
237 
238  if ($this->don->update(DolibarrApiAccess::$user) > 0) {
239  return $this->get($id);
240  } else {
241  throw new RestException(500, $this->don->error);
242  }
243  }
244 
251  public function delete($id)
252  {
253  if (!DolibarrApiAccess::$user->rights->don->supprimer) {
254  throw new RestException(401);
255  }
256 
257  $result = $this->don->fetch($id);
258  if (!$result) {
259  throw new RestException(404, 'Donation not found');
260  }
261 
262  if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
263  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
264  }
265 
266  if (!$this->don->delete(DolibarrApiAccess::$user)) {
267  throw new RestException(500, 'Error when delete donation : '.$this->don->error);
268  }
269 
270  return array(
271  'success' => array(
272  'code' => 200,
273  'message' => 'Donation deleted'
274  )
275  );
276  }
277 
300  public function validate($id, $idwarehouse = 0, $notrigger = 0)
301  {
302  if (!DolibarrApiAccess::$user->rights->don->creer) {
303  throw new RestException(401);
304  }
305 
306  $result = $this->don->fetch($id);
307  if (!$result) {
308  throw new RestException(404, 'Donation not found');
309  }
310 
311  if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
312  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
313  }
314 
315  $result = $this->don->valid_promesse($id, DolibarrApiAccess::$user->id, $notrigger);
316  if ($result == 0) {
317  throw new RestException(304, 'Error nothing done. May be object is already validated');
318  }
319  if ($result < 0) {
320  throw new RestException(500, 'Error when validating Order: '.$this->don->error);
321  }
322  $result = $this->don->fetch($id);
323  if (!$result) {
324  throw new RestException(404, 'Order not found');
325  }
326 
327  if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
328  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
329  }
330 
331  $this->don->fetchObjectLinked();
332 
333  return $this->_cleanObjectDatas($this->don);
334  }
335 
336  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
343  protected function _cleanObjectDatas($object)
344  {
345  // phpcs:enable
346  $object = parent::_cleanObjectDatas($object);
347 
348  unset($object->note);
349  unset($object->address);
350  unset($object->barcode_type);
351  unset($object->barcode_type_code);
352  unset($object->barcode_type_label);
353  unset($object->barcode_type_coder);
354 
355  return $object;
356  }
357 
365  private function _validate($data)
366  {
367  $don = array();
368  foreach (Donations::$FIELDS as $field) {
369  if (!isset($data[$field])) {
370  throw new RestException(400, $field." field missing");
371  }
372  $don[$field] = $data[$field];
373  }
374  return $don;
375  }
376 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Donations
Definition: api_donations.class.php:29
Donations\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_donations.class.php:365
Donations\put
put($id, $request_data=null)
Update order general fields (won't touch lines of order)
Definition: api_donations.class.php:217
Donations\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List donations.
Definition: api_donations.class.php:102
Don
Class to manage donations.
Definition: don.class.php:38
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Donations\validate
validate($id, $idwarehouse=0, $notrigger=0)
Validate an donation.
Definition: api_donations.class.php:300
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Donations\__construct
__construct()
Constructor.
Definition: api_donations.class.php:47
Donations\post
post($request_data=null)
Create donation object.
Definition: api_donations.class.php:182
Donations\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_donations.class.php:343