dolibarr  13.0.2
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  static $FIELDS = array(
36  'socid'
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  $obj_ret = array();
107 
108  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
109  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
110 
111  $sql = "SELECT t.rowid";
112  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) $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)
113  $sql .= " FROM ".MAIN_DB_PREFIX."don as t";
114 
115  $sql .= ' WHERE t.entity IN ('.getEntity('don').')';
116  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) $sql .= " AND t.fk_soc = sc.fk_soc";
117  if ($thirdparty_ids) $sql .= " AND t.fk_soc = ".$thirdparty_ids." ";
118 
119  // Add sql filters
120  if ($sqlfilters)
121  {
122  if (!DolibarrApi::_checkFilters($sqlfilters))
123  {
124  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
125  }
126  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
127  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
128  }
129 
130  $sql .= $this->db->order($sortfield, $sortorder);
131  if ($limit) {
132  if ($page < 0)
133  {
134  $page = 0;
135  }
136  $offset = $limit * $page;
137 
138  $sql .= $this->db->plimit($limit + 1, $offset);
139  }
140 
141  dol_syslog("API Rest request");
142  $result = $this->db->query($sql);
143 
144  if ($result)
145  {
146  $num = $this->db->num_rows($result);
147  $min = min($num, ($limit <= 0 ? $num : $limit));
148  $i = 0;
149  while ($i < $min)
150  {
151  $obj = $this->db->fetch_object($result);
152  $don_static = new Don($this->db);
153  if ($don_static->fetch($obj->rowid)) {
154  // Add external contacts ids
155  //$don_static->contacts_ids = $don_static->liste_contact(-1, 'external', 1);
156  $obj_ret[] = $this->_cleanObjectDatas($don_static);
157  }
158  $i++;
159  }
160  } else {
161  throw new RestException(503, 'Error when retrieve donation list : '.$this->db->lasterror());
162  }
163  if (!count($obj_ret)) {
164  throw new RestException(404, 'No donation found');
165  }
166 
167  return $obj_ret;
168  }
169 
176  public function post($request_data = null)
177  {
178  if (!DolibarrApiAccess::$user->rights->don->creer) {
179  throw new RestException(401, "Insuffisant rights");
180  }
181  // Check mandatory fields
182  $result = $this->_validate($request_data);
183 
184  foreach ($request_data as $field => $value) {
185  $this->don->$field = $value;
186  }
187  /*if (isset($request_data["lines"])) {
188  $lines = array();
189  foreach ($request_data["lines"] as $line) {
190  array_push($lines, (object) $line);
191  }
192  $this->don->lines = $lines;
193  }*/
194 
195  if ($this->don->create(DolibarrApiAccess::$user) < 0) {
196  throw new RestException(500, "Error creating order", array_merge(array($this->don->error), $this->don->errors));
197  }
198 
199  return $this->don->id;
200  }
201 
210  public function put($id, $request_data = null)
211  {
212  if (!DolibarrApiAccess::$user->rights->don->creer) {
213  throw new RestException(401);
214  }
215 
216  $result = $this->don->fetch($id);
217  if (!$result) {
218  throw new RestException(404, 'Donation not found');
219  }
220 
221  if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
222  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
223  }
224  foreach ($request_data as $field => $value) {
225  if ($field == 'id') continue;
226  $this->don->$field = $value;
227  }
228 
229  if ($this->don->update(DolibarrApiAccess::$user) > 0)
230  {
231  return $this->get($id);
232  } else {
233  throw new RestException(500, $this->don->error);
234  }
235  }
236 
243  public function delete($id)
244  {
245  if (!DolibarrApiAccess::$user->rights->don->supprimer) {
246  throw new RestException(401);
247  }
248  $result = $this->don->fetch($id);
249  if (!$result) {
250  throw new RestException(404, 'Donation not found');
251  }
252 
253  if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
254  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
255  }
256 
257  if (!$this->don->delete(DolibarrApiAccess::$user)) {
258  throw new RestException(500, 'Error when delete donation : '.$this->don->error);
259  }
260 
261  return array(
262  'success' => array(
263  'code' => 200,
264  'message' => 'Donation deleted'
265  )
266  );
267  }
268 
291  public function validate($id, $idwarehouse = 0, $notrigger = 0)
292  {
293  if (!DolibarrApiAccess::$user->rights->don->creer) {
294  throw new RestException(401);
295  }
296  $result = $this->don->fetch($id);
297  if (!$result) {
298  throw new RestException(404, 'Donation not found');
299  }
300 
301  if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
302  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
303  }
304 
305  $result = $this->don->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
306  if ($result == 0) {
307  throw new RestException(304, 'Error nothing done. May be object is already validated');
308  }
309  if ($result < 0) {
310  throw new RestException(500, 'Error when validating Order: '.$this->don->error);
311  }
312  $result = $this->don->fetch($id);
313  if (!$result) {
314  throw new RestException(404, 'Order not found');
315  }
316 
317  if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
318  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
319  }
320 
321  $this->don->fetchObjectLinked();
322 
323  return $this->_cleanObjectDatas($this->don);
324  }
325 
326  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
333  protected function _cleanObjectDatas($object)
334  {
335  // phpcs:enable
336  $object = parent::_cleanObjectDatas($object);
337 
338  unset($object->note);
339  unset($object->address);
340  unset($object->barcode_type);
341  unset($object->barcode_type_code);
342  unset($object->barcode_type_label);
343  unset($object->barcode_type_coder);
344 
345  return $object;
346  }
347 
355  private function _validate($data)
356  {
357  $don = array();
358  foreach (Orders::$FIELDS as $field) {
359  if (!isset($data[$field]))
360  throw new RestException(400, $field." field missing");
361  $don[$field] = $data[$field];
362  }
363  return $don;
364  }
365 }
put($id, $request_data=null)
Update order general fields (won&#39;t touch lines of order)
validate($id, $idwarehouse=0, $notrigger=0)
Validate an donation.
_validate($data)
Validate fields before create or update object.
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
_cleanObjectDatas($object)
Clean sensible object datas.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
post($request_data=null)
Create donation object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
Definition: api.class.php:252
__construct()
Constructor.
Class to manage donations.
Definition: don.class.php:37
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $sqlfilters= '')
List donations.