21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/don/class/don.class.php';
36 public static $FIELDS = array(
52 $this->don =
new Don($this->db);
65 public function get(
$id)
67 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
68 throw new RestException(403);
71 $result = $this->don->fetch(
$id);
73 throw new RestException(404,
'Donation not found');
77 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
105 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
107 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
108 throw new RestException(403);
114 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
116 $sql =
"SELECT t.rowid";
117 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
118 $sql .=
", sc.fk_soc, sc.fk_user";
120 $sql .=
" FROM ".MAIN_DB_PREFIX.
"don AS t LEFT JOIN ".MAIN_DB_PREFIX.
"don_extrafields AS ef ON (ef.fk_object = t.rowid)";
122 $sql .=
' WHERE t.entity IN ('.getEntity(
'don').
')';
123 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
124 $sql .=
" AND t.fk_soc = sc.fk_soc";
126 if ($thirdparty_ids) {
127 $sql .=
" AND t.fk_soc = ".((int) $thirdparty_ids).
" ";
133 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
135 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
140 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
142 $sql .= $this->db->order($sortfield, $sortorder);
147 $offset = $limit * $page;
149 $sql .= $this->db->plimit($limit + 1, $offset);
153 $result = $this->db->query($sql);
156 $num = $this->db->num_rows($result);
157 $min = min($num, ($limit <= 0 ? $num : $limit));
160 $obj = $this->db->fetch_object($result);
161 $don_static =
new Don($this->db);
162 if ($don_static->fetch($obj->rowid)) {
170 throw new RestException(503,
'Error when retrieve donation list : '.$this->db->lasterror());
174 if ($pagination_data) {
175 $totalsResult = $this->db->query($sqlTotals);
176 $total = $this->db->fetch_object($totalsResult)->total;
181 $obj_ret[
'data'] = $tmp;
182 $obj_ret[
'pagination'] = [
183 'total' => (int) $total,
185 'page_count' => ceil((
int) $total / $limit),
201 public function post($request_data =
null)
203 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
204 throw new RestException(403,
"Insufficiant rights");
208 $result = $this->
_validate($request_data);
210 foreach ($request_data as $field => $value) {
211 if ($field ===
'caller') {
213 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
217 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
227 if ($this->don->create(DolibarrApiAccess::$user) < 0) {
228 throw new RestException(500,
"Error creating donation", array_merge(array($this->don->error), $this->don->errors));
231 return $this->don->id;
243 public function put(
$id, $request_data =
null)
245 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
246 throw new RestException(403);
249 $result = $this->don->fetch(
$id);
251 throw new RestException(404,
'Donation not found');
255 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
257 foreach ($request_data as $field => $value) {
258 if ($field ==
'id') {
261 if ($field ===
'caller') {
263 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
267 if ($field ==
'array_options' && is_array($value)) {
268 foreach ($value as $index => $val) {
274 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
277 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
278 return $this->
get(
$id);
280 throw new RestException(500, $this->don->error);
292 public function delete(
$id)
294 if (!DolibarrApiAccess::$user->hasRight(
'don',
'supprimer')) {
295 throw new RestException(403);
298 $result = $this->don->fetch(
$id);
300 throw new RestException(404,
'Donation not found');
304 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
307 if (!$this->don->delete(DolibarrApiAccess::$user)) {
308 throw new RestException(500,
'Error when delete donation : '.$this->don->error);
314 'message' =>
'Donation deleted'
343 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
344 throw new RestException(403);
347 $result = $this->don->fetch(
$id);
349 throw new RestException(404,
'Donation not found');
353 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
357 $result = $this->don->valid_promesse(
$id, DolibarrApiAccess::$user->
id, $notrigger);
359 throw new RestException(304,
'Error nothing done. May be object is already validated');
362 throw new RestException(500,
'Error when validating Order: '.$this->don->error);
364 $result = $this->don->fetch(
$id);
366 throw new RestException(404,
'Order not found');
374 $this->don->fetchObjectLinked();
397 unset(
$object->barcode_type_code);
398 unset(
$object->barcode_type_label);
399 unset(
$object->barcode_type_coder);
413 if ($data ===
null) {
417 foreach (Donations::$FIELDS as $field) {
418 if (!isset($data[$field])) {
419 throw new RestException(400, $field.
" field missing");
421 $don[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage donations.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an donation.
post($request_data=null)
Create donation object.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
put($id, $request_data=null)
Update order general fields (won't touch lines of order)
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List donations.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.