20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/resource/class/dolresource.class.php';
45 public static $FIELDS = array(
78 public function get(
$id)
80 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'read')) {
81 throw new RestException(403);
84 $result = $this->resource->fetch(
$id);
86 throw new RestException(404,
'Resource not found');
90 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
115 public function index($sortfield =
"t.ref", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
117 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'read')) {
118 throw new RestException(403);
123 $sql =
"SELECT t.rowid";
124 $sql .=
" FROM ".$this->db->prefix().
"resource as t";
125 $sql .=
" WHERE t.entity IN (".getEntity(
'resource').
")";
132 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
136 $sql .= $this->db->order($sortfield, $sortorder);
141 $offset = $limit * $page;
143 $sql .= $this->db->plimit($limit + 1, $offset);
146 $result = $this->db->query($sql);
148 throw new RestException(503,
'Error when retrieving resource list: '.$this->db->lasterror());
151 $num = $this->db->num_rows($result);
152 $min = min($num, ($limit <= 0 ? $num : $limit));
155 $obj = $this->db->fetch_object($result);
157 if ($resource_static->fetch($obj->rowid) > 0) {
178 public function post($request_data =
null)
180 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'write')) {
181 throw new RestException(403);
187 foreach ($request_data as $field => $value) {
188 if ($field ===
'caller') {
190 $this->resource->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
194 $this->resource->$field = $this->
_checkValForAPI($field, $value, $this->resource);
199 $result = $this->resource->create(DolibarrApiAccess::$user);
200 if ($result <= 0 || $result != $this->resource->id) {
201 throw new RestException(500,
"Error creating resource", array_merge(array($this->resource->error), $this->resource->errors));
204 return $this->resource->id;
220 public function put(
$id, $request_data =
null)
222 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'write')) {
223 throw new RestException(403);
226 $result = $this->resource->fetch(
$id);
228 throw new RestException(404,
'Resource not found');
232 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
235 $this->resource->oldcopy =
dol_clone($this->resource, 2);
237 foreach ($request_data as $field => $value) {
238 if ($field ==
'id') {
241 if ($field ===
'caller') {
243 $this->resource->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
247 if ($field ==
'array_options' && is_array($value)) {
248 foreach ($value as $index => $val) {
249 $this->resource->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->resource);
254 $this->resource->$field = $this->
_checkValForAPI($field, $value, $this->resource);
257 if ($this->resource->update(DolibarrApiAccess::$user) <= 0) {
258 throw new RestException(500, $this->resource->errorsToString());
261 return $this->
get(
$id);
276 public function delete(
$id)
278 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'delete')) {
279 throw new RestException(403);
282 $result = $this->resource->fetch(
$id);
284 throw new RestException(404,
'Resource not found');
288 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
291 if ($this->resource->delete(DolibarrApiAccess::$user) <= 0) {
292 throw new RestException(500,
'Error when deleting resource: '.$this->resource->errorsToString());
298 'message' =>
'Resource deleted'
323 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'read')) {
324 throw new RestException(403);
330 foreach ($this->resource->getElementResources($element->element, $element->id) as $link) {
359 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'write')) {
360 throw new RestException(403);
363 if ($request_data ===
null) {
364 $request_data = array();
366 if (!isset($request_data[
'resource_id'])) {
367 throw new RestException(400,
"resource_id field missing");
372 $resource_id = (int) $request_data[
'resource_id'];
373 if ($this->resource->fetch($resource_id) <= 0) {
374 throw new RestException(404,
'Resource not found');
377 $busy = empty($request_data[
'busy']) ? 0 : 1;
378 $mandatory = empty($request_data[
'mandatory']) ? 0 : 1;
382 foreach ($this->resource->getElementResources($element->element, $element->id) as $existing) {
383 if ((
int) $existing[
'resource_id'] == $resource_id && $existing[
'resource_type'] == $this->resource->element) {
384 throw new RestException(409,
'Resource already linked to this element');
391 $conflicts = $this->_getBookingConflicts($element, $resource_id);
392 if (!empty($conflicts)) {
393 throw new RestException(409,
'Resource already used on this period by: '.$this->
_describeConflicts($conflicts));
397 if ($element->add_element_resource($resource_id, $this->resource->element, $busy, $mandatory) <= 0) {
398 throw new RestException(500,
'Error when linking resource: '.$element->errorsToString());
401 foreach ($this->resource->getElementResources($element->element, $element->id) as $link) {
402 if ((
int) $link[
'resource_id'] == $resource_id && $link[
'resource_type'] == $this->resource->element) {
407 throw new RestException(500,
'Link created but not found back');
428 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'delete')) {
429 throw new RestException(403);
437 foreach ($this->resource->getElementResources($element->element, $element->id) as $link) {
438 if ((
int) $link[
'rowid'] == (
int)
$id) {
444 throw new RestException(404,
'Link not found for this element');
447 if ($element->delete_resource(
$id, $element->element) <= 0) {
448 throw new RestException(500,
'Error when unlinking resource: '.$element->errorsToString());
454 'message' =>
'Resource unlinked'
481 if (!DolibarrApiAccess::$user->hasRight(
'resource',
'read')) {
482 throw new RestException(403);
485 if ($this->resource->fetch(
$id) <= 0) {
486 throw new RestException(404,
'Resource not found');
492 throw new RestException(400,
'date_end is before date_start');
495 $conflicts = $this->resource->getBookingConflicts($this->resource->id, $this->resource->element, $start, $end);
496 if (!is_array($conflicts)) {
497 throw new RestException(500,
'Error when searching the conflicts: '.$this->resource->errorsToString());
515 unset(
$object->cache_code_type_resource);
545 $element_type = (string) $element_type;
546 if ($element_type ===
'' || !preg_match(
'/^[a-z0-9_]+$/i', $element_type)) {
547 throw new RestException(400,
'Bad value for parameter element_type');
549 if ((
int) $element_id <= 0) {
550 throw new RestException(400,
'Bad value for parameter element_id');
553 $element = fetchObjectByElement((
int) $element_id, $element_type);
554 if (!is_object($element) || empty($element->id)) {
555 throw new RestException(404,
'Element '.$element_type.
' not found');
561 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
577 if ($resource_static->fetch((
int) $link[
'resource_id']) > 0) {
582 'id' => (
int) $link[
'rowid'],
583 'resource_id' => (
int) $link[
'resource_id'],
584 'resource_type' => $link[
'resource_type'],
585 'busy' => (
int) $link[
'busy'],
586 'mandatory' => (
int) $link[
'mandatory'],
587 'resource' => $resource
600 private function _getBookingConflicts($element, $resource_id)
607 if ($element->element ==
'action') {
609 '@phan-var-force ActionComm $element';
610 $date_start = empty($element->datep) ? 0 : $element->datep;
611 $date_end = empty($element->datef) ?
$date_start : $element->datef;
612 if (
$date_start && !empty($element->fulldayevent)) {
615 $date_end =
dol_mktime(23, 59, 59, $parts[
'mon'], $parts[
'mday'], $parts[
'year']);
623 $conflicts = $this->resource->getBookingConflicts($resource_id, $this->resource->element,
$date_start, $date_end, $element->element, $element->id);
624 if (!is_array($conflicts)) {
625 throw new RestException(500,
'Error when searching the conflicts: '.$this->resource->errorsToString());
640 foreach ($conflicts as $conflict) {
641 $out[] = $conflict[
'element_type'].
' '.$conflict[
'element_id'].(empty($conflict[
'ref']) ?
'' :
' ('.$conflict[
'ref'].
')');
644 return implode(
', ', $out);
658 if (is_numeric($value)) {
663 if (empty($timestamp)) {
664 throw new RestException(400,
'Bad value for parameter '.$name);
667 return (
int) $timestamp;
680 if ($data ===
null) {
684 foreach (Dolresources::$FIELDS as $field) {
685 if (!isset($data[$field])) {
686 throw new RestException(400,
"$field field missing");
688 $resource[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
_describeConflicts($conflicts)
Describe conflicting bookings for an error message.
_cleanObjectDatas($object)
Clean sensible object datas.
post($request_data=null)
Create resource object.
getBookingConflicts($id, $date_start, $date_end)
Get the bookings that conflict with a period for a resource.
_toTimestamp($value, $name)
Convert a date given to the API into a timestamp.
__construct()
Constructor.
_fetchElement($element_type, $element_id)
Load the element carrying the resources and check the access to it.
deleteElementResources($element_type, $element_id, $id)
Unlink a resource from an element.
_validate($data)
Validate fields before create or update object.
_formatLink($link)
Format a link of llx_element_resources for the answer.
put($id, $request_data=null)
Update resource.
getElementResources($element_type, $element_id)
Get the resources linked to an element.
index($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List resources.
postElementResources($element_type, $element_id, $request_data=null)
Link a resource to an element.
dol_stringtotime($string, $gm=1, $processnotimeasnoon=0)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
$date_start
Variables from include:
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.