22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
40 public static $FIELDS = array(
73 public function get(
$id)
75 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read')) {
76 throw new RestException(403,
"Insufficient rights to read an event");
79 $result = $this->actioncomm->initAsSpecimen();
81 $result = $this->actioncomm->fetch(
$id);
83 $this->actioncomm->fetch_optionals();
84 $this->actioncomm->fetchObjectLinked();
88 throw new RestException(404,
'Agenda Events not found');
91 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read') && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) {
92 throw new RestException(403,
'Insufficient rights to read event of this owner id. Your id is '.DolibarrApiAccess::$user->
id);
96 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
123 public function index($sortfield =
"t.id", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
129 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read')) {
130 throw new RestException(403,
"Insufficient rights to read events");
134 $socid = DolibarrApiAccess::$user->socid ?: 0;
138 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
139 $search_sale = DolibarrApiAccess::$user->id;
145 $sql =
"SELECT t.id";
146 $sql .=
" FROM ".MAIN_DB_PREFIX.
"actioncomm AS t";
147 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"actioncomm_extrafields AS ef ON (ef.fk_object = t.id)";
148 $sql .=
' WHERE t.entity IN ('.getEntity(
'agenda').
')';
150 $sql .=
" AND t.fk_user_action IN (".$this->db->sanitize($user_ids).
")";
153 $sql .=
" AND t.fk_soc = ".((int) $socid);
156 if ($search_sale && $search_sale !=
'-1') {
157 if ($search_sale == -2) {
158 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
159 } elseif ($search_sale > 0) {
160 $sql .=
" AND (t.fk_soc IS NULL OR EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
"))";
168 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
173 $sqlTotals = str_replace(
'SELECT t.id',
'SELECT count(t.id) as total', $sql);
175 $sql .= $this->db->order($sortfield, $sortorder);
180 $offset = $limit * $page;
182 $sql .= $this->db->plimit($limit + 1, $offset);
185 $result = $this->db->query($sql);
189 $num = $this->db->num_rows($result);
190 $min = min($num, ($limit <= 0 ? $num : $limit));
192 $obj = $this->db->fetch_object($result);
193 $actioncomm_static =
new ActionComm($this->db);
194 if ($actioncomm_static->fetch($obj->id)) {
200 throw new RestException(503,
'Error when retrieve Agenda Event list : '.$this->db->lasterror());
204 if ($pagination_data) {
205 $totalsResult = $this->db->query($sqlTotals);
206 $total = $this->db->fetch_object($totalsResult)->total;
211 $obj_ret[
'data'] = $tmp;
212 $obj_ret[
'pagination'] = [
213 'total' => (int) $total,
215 'page_count' => (
int) ceil((
int) $total / $limit),
235 public function post($request_data =
null)
237 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'create')) {
238 throw new RestException(403,
"Insufficient rights to create your Agenda Event");
240 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'create') && DolibarrApiAccess::$user->
id != $request_data[
'userownerid']) {
241 throw new RestException(403,
"Insufficient rights to create an Agenda Event for owner id ".$request_data[
'userownerid'].
' Your id is '.DolibarrApiAccess::$user->
id);
245 $result = $this->
_validate($request_data);
247 foreach ($request_data as $field => $value) {
248 if ($field ===
'caller') {
250 $this->actioncomm->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
254 $this->actioncomm->$field = $this->
_checkValForAPI($field, $value, $this->actioncomm);
264 if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
265 throw new RestException(500,
"Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
268 return $this->actioncomm->id;
285 public function put(
$id, $request_data =
null)
287 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'create')) {
288 throw new RestException(403,
"Insufficient rights to create your Agenda Event");
290 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'create') && DolibarrApiAccess::$user->
id != $request_data[
'userownerid']) {
291 throw new RestException(403,
"Insufficient rights to create an Agenda Event for owner id ".$request_data[
'userownerid'].
' Your id is '.DolibarrApiAccess::$user->
id);
294 $result = $this->actioncomm->fetch(
$id);
296 $this->actioncomm->fetch_optionals();
297 $this->actioncomm->fetch_userassigned();
298 $this->actioncomm->oldcopy = clone $this->actioncomm;
301 throw new RestException(404,
'actioncomm not found');
305 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
307 foreach ($request_data as $field => $value) {
308 if ($field ==
'id') {
311 if ($field ===
'caller') {
313 $this->actioncomm->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
317 if ($field ==
'array_options' && is_array($value)) {
318 foreach ($value as $index => $val) {
319 $this->actioncomm->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->actioncomm);
323 $this->actioncomm->$field = $this->
_checkValForAPI($field, $value, $this->actioncomm);
326 if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
327 return $this->
get(
$id);
346 public function delete(
$id)
348 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'delete')) {
349 throw new RestException(403,
"Insufficient rights to delete your Agenda Event");
352 $result = $this->actioncomm->fetch(
$id);
354 $this->actioncomm->fetch_optionals();
355 $this->actioncomm->fetch_userassigned();
356 $this->actioncomm->oldcopy = clone $this->actioncomm;
359 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'delete') && DolibarrApiAccess::$user->
id != $this->actioncomm->userownerid) {
360 throw new RestException(403,
"Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.
' Your id is '.DolibarrApiAccess::$user->id);
364 throw new RestException(404,
'Agenda Event not found');
368 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
371 if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
372 throw new RestException(500,
'Error when delete Agenda Event : '.$this->actioncomm->error);
378 'message' =>
'Agenda Event deleted'
392 if ($data ===
null) {
396 foreach (AgendaEvents::$FIELDS as $field) {
397 if (!isset($data[$field])) {
398 throw new RestException(400,
"$field field missing");
400 $event[$field] = $data[$field];
442 unset(
$object->barcode_type_code);
443 unset(
$object->barcode_type_label);
444 unset(
$object->barcode_type_coder);
445 unset(
$object->mode_reglement_id);
446 unset(
$object->cond_reglement_id);
447 unset(
$object->cond_reglement);
448 unset(
$object->fk_delivery_address);
449 unset(
$object->shipping_method_id);
453 unset(
$object->total_localtax1);
454 unset(
$object->total_localtax2);
457 unset(
$object->label_incoterms);
458 unset(
$object->location_incoterms);
465 unset(
$object->demand_reason_id);
466 unset(
$object->transport_mode_id);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage agenda events (actions)
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
put($id, $request_data=null)
Update an agenda event.
index($sortfield="t.id", $sortorder='ASC', $limit=100, $page=0, $user_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List agenda events.
post($request_data=null)
Create an agenda event.
_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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.