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).
")";
154 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
155 $childids = DolibarrApiAccess::$user->getAllChildIds(1);
156 $sql .=
" AND t.fk_user_action IN (".$this->db->sanitize(implode(
',', $childids)).
")";
159 $sql .=
" AND t.fk_soc = ".((int) $socid);
162 if ($search_sale && $search_sale !=
'-1') {
163 if ($search_sale == -2) {
164 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
165 } elseif ($search_sale > 0) {
166 $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).
"))";
174 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
179 $sqlTotals = str_replace(
'SELECT t.id',
'SELECT count(t.id) as total', $sql);
181 $sql .= $this->db->order($sortfield, $sortorder);
186 $offset = $limit * $page;
188 $sql .= $this->db->plimit($limit + 1, $offset);
191 $result = $this->db->query($sql);
195 $num = $this->db->num_rows($result);
196 $min = min($num, ($limit <= 0 ? $num : $limit));
198 $obj = $this->db->fetch_object($result);
199 $actioncomm_static =
new ActionComm($this->db);
200 if ($actioncomm_static->fetch($obj->id)) {
206 throw new RestException(503,
'Error when retrieve Agenda Event list : '.$this->db->lasterror());
210 if ($pagination_data) {
211 $totalsResult = $this->db->query($sqlTotals);
212 $total = $this->db->fetch_object($totalsResult)->total;
217 $obj_ret[
'data'] = $tmp;
218 $obj_ret[
'pagination'] = [
219 'total' => (int) $total,
221 'page_count' => (
int) ceil((
int) $total / $limit),
241 public function post($request_data =
null)
243 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'create')) {
244 throw new RestException(403,
"Insufficient rights to create your Agenda Event");
246 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'create') && DolibarrApiAccess::$user->
id != $request_data[
'userownerid']) {
247 throw new RestException(403,
"Insufficient rights to create an Agenda Event for owner id ".$request_data[
'userownerid'].
' Your id is '.DolibarrApiAccess::$user->
id);
251 $result = $this->
_validate($request_data);
253 foreach ($request_data as $field => $value) {
254 if ($field ===
'caller') {
256 $this->actioncomm->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
260 $this->actioncomm->$field = $this->
_checkValForAPI($field, $value, $this->actioncomm);
270 if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
271 throw new RestException(500,
"Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
274 return $this->actioncomm->id;
291 public function put(
$id, $request_data =
null)
293 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'create')) {
294 throw new RestException(403,
"Insufficient rights to create your Agenda Event");
296 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'create') && DolibarrApiAccess::$user->
id != $request_data[
'userownerid']) {
297 throw new RestException(403,
"Insufficient rights to create an Agenda Event for owner id ".$request_data[
'userownerid'].
' Your id is '.DolibarrApiAccess::$user->
id);
300 $result = $this->actioncomm->fetch(
$id);
302 $this->actioncomm->fetch_optionals();
303 $this->actioncomm->fetch_userassigned();
304 $this->actioncomm->oldcopy = clone $this->actioncomm;
307 throw new RestException(404,
'actioncomm not found');
311 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
316 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read') && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) {
317 throw new RestException(403,
'Insufficient rights to update event of this owner id. Your id is '.DolibarrApiAccess::$user->
id);
319 foreach ($request_data as $field => $value) {
320 if ($field ==
'id') {
323 if ($field ===
'caller') {
325 $this->actioncomm->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
329 if ($field ==
'array_options' && is_array($value)) {
330 foreach ($value as $index => $val) {
335 $this->actioncomm->$field = $this->
_checkValForAPI($field, $value, $this->actioncomm);
338 if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
339 return $this->
get(
$id);
358 public function delete(
$id)
360 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'delete')) {
361 throw new RestException(403,
"Insufficient rights to delete your Agenda Event");
364 $result = $this->actioncomm->fetch(
$id);
366 $this->actioncomm->fetch_optionals();
367 $this->actioncomm->fetch_userassigned();
368 $this->actioncomm->oldcopy = clone $this->actioncomm;
371 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'delete') && DolibarrApiAccess::$user->
id != $this->actioncomm->userownerid) {
372 throw new RestException(403,
"Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.
' Your id is '.DolibarrApiAccess::$user->id);
376 throw new RestException(404,
'Agenda Event not found');
380 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
383 if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
384 throw new RestException(500,
'Error when delete Agenda Event : '.$this->actioncomm->error);
390 'message' =>
'Agenda Event deleted'
404 if ($data ===
null) {
408 foreach (AgendaEvents::$FIELDS as $field) {
409 if (!isset($data[$field])) {
410 throw new RestException(400,
"$field field missing");
412 $event[$field] = $data[$field];
454 unset(
$object->barcode_type_code);
455 unset(
$object->barcode_type_label);
456 unset(
$object->barcode_type_coder);
457 unset(
$object->mode_reglement_id);
458 unset(
$object->cond_reglement_id);
459 unset(
$object->cond_reglement);
460 unset(
$object->fk_delivery_address);
461 unset(
$object->shipping_method_id);
465 unset(
$object->total_localtax1);
466 unset(
$object->total_localtax2);
469 unset(
$object->label_incoterms);
470 unset(
$object->location_incoterms);
477 unset(
$object->demand_reason_id);
478 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.
_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.
_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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
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.