20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
36 public static $FIELDS = array(
65 public function get(
$id)
67 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read')) {
68 throw new RestException(403,
"Insufficient rights to read an event");
71 $result = $this->actioncomm->initAsSpecimen();
73 $result = $this->actioncomm->fetch(
$id);
75 $this->actioncomm->fetch_optionals();
76 $this->actioncomm->fetchObjectLinked();
80 throw new RestException(404,
'Agenda Events not found');
83 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read') && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) {
84 throw new RestException(403,
'Insufficient rights to read event of this owner id. Your id is '.DolibarrApiAccess::$user->
id);
88 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
108 public function index($sortfield =
"t.id", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
114 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read')) {
115 throw new RestException(403,
"Insufficient rights to read events");
119 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
123 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
124 $search_sale = DolibarrApiAccess::$user->id;
126 if (!isModEnabled(
'societe')) {
130 $sql =
"SELECT t.id";
131 $sql .=
" FROM ".MAIN_DB_PREFIX.
"actioncomm AS t";
132 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"actioncomm_extrafields AS ef ON (ef.fk_object = t.id)";
133 $sql .=
' WHERE t.entity IN ('.getEntity(
'agenda').
')';
135 $sql .=
" AND t.fk_user_action IN (".$this->db->sanitize($user_ids).
")";
138 $sql .=
" AND t.fk_soc = ".((int) $socid);
141 if ($search_sale && $search_sale !=
'-1') {
142 if ($search_sale == -2) {
143 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
144 } elseif ($search_sale > 0) {
145 $sql .=
" AND 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).
")";
153 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
158 $sqlTotals = str_replace(
'SELECT t.id',
'SELECT count(t.id) as total', $sql);
160 $sql .= $this->db->order($sortfield, $sortorder);
165 $offset = $limit * $page;
167 $sql .= $this->db->plimit($limit + 1, $offset);
170 $result = $this->db->query($sql);
174 $num = $this->db->num_rows($result);
175 $min = min($num, ($limit <= 0 ? $num : $limit));
177 $obj = $this->db->fetch_object($result);
178 $actioncomm_static =
new ActionComm($this->db);
179 if ($actioncomm_static->fetch($obj->id)) {
185 throw new RestException(503,
'Error when retrieve Agenda Event list : '.$this->db->lasterror());
189 if ($pagination_data) {
190 $totalsResult = $this->db->query($sqlTotals);
191 $total = $this->db->fetch_object($totalsResult)->total;
196 $obj_ret[
'data'] = $tmp;
197 $obj_ret[
'pagination'] = [
198 'total' => (int) $total,
200 'page_count' => ceil((
int) $total / $limit),
214 public function post($request_data =
null)
216 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'create')) {
217 throw new RestException(403,
"Insufficient rights to create your Agenda Event");
219 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'create') && DolibarrApiAccess::$user->
id != $request_data[
'userownerid']) {
220 throw new RestException(403,
"Insufficient rights to create an Agenda Event for owner id ".$request_data[
'userownerid'].
' Your id is '.DolibarrApiAccess::$user->
id);
224 $result = $this->
_validate($request_data);
226 foreach ($request_data as $field => $value) {
227 if ($field ===
'caller') {
229 $this->actioncomm->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
233 $this->actioncomm->$field = $this->
_checkValForAPI($field, $value, $this->actioncomm);
243 if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
244 throw new RestException(500,
"Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
247 return $this->actioncomm->id;
258 public function put(
$id, $request_data =
null)
260 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'create')) {
261 throw new RestException(403,
"Insufficient rights to create your Agenda Event");
263 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'create') && DolibarrApiAccess::$user->
id != $request_data[
'userownerid']) {
264 throw new RestException(403,
"Insufficient rights to create an Agenda Event for owner id ".$request_data[
'userownerid'].
' Your id is '.DolibarrApiAccess::$user->
id);
267 $result = $this->actioncomm->fetch(
$id);
269 $this->actioncomm->fetch_optionals();
270 $this->actioncomm->fetch_userassigned();
271 $this->actioncomm->oldcopy = clone $this->actioncomm;
274 throw new RestException(404,
'actioncomm not found');
278 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
280 foreach ($request_data as $field => $value) {
281 if ($field ==
'id') {
284 if ($field ===
'caller') {
286 $this->actioncomm->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
290 $this->actioncomm->$field = $this->
_checkValForAPI($field, $value, $this->actioncomm);
293 if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
294 return $this->
get(
$id);
307 public function delete(
$id)
309 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'delete')) {
310 throw new RestException(403,
"Insufficient rights to delete your Agenda Event");
313 $result = $this->actioncomm->fetch(
$id);
315 $this->actioncomm->fetch_optionals();
316 $this->actioncomm->fetch_userassigned();
317 $this->actioncomm->oldcopy = clone $this->actioncomm;
320 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'delete') && DolibarrApiAccess::$user->
id != $this->actioncomm->userownerid) {
321 throw new RestException(403,
"Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.
' Your id is '.DolibarrApiAccess::$user->id);
325 throw new RestException(404,
'Agenda Event not found');
329 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
332 if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
333 throw new RestException(500,
'Error when delete Agenda Event : '.$this->actioncomm->error);
339 'message' =>
'Agenda Event deleted'
354 foreach (AgendaEvents::$FIELDS as $field) {
355 if (!isset($data[$field])) {
356 throw new RestException(400,
"$field field missing");
358 $event[$field] = $data[$field];
397 unset(
$object->barcode_type_code);
398 unset(
$object->barcode_type_label);
399 unset(
$object->barcode_type_coder);
400 unset(
$object->mode_reglement_id);
401 unset(
$object->cond_reglement_id);
402 unset(
$object->cond_reglement);
403 unset(
$object->fk_delivery_address);
404 unset(
$object->shipping_method_id);
408 unset(
$object->total_localtax1);
409 unset(
$object->total_localtax2);
412 unset(
$object->label_incoterms);
413 unset(
$object->location_incoterms);
420 unset(
$object->demand_reason_id);
421 unset(
$object->transport_mode_id);
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage agenda events (actions)
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
put($id, $request_data=null)
Update Agenda Event general fields.
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 Agenda Event object.
_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.