21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
39 public static $FIELDS = array(
72 public function get(
$id)
74 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read')) {
75 throw new RestException(403,
"Insufficient rights to read an event");
78 $result = $this->actioncomm->initAsSpecimen();
80 $result = $this->actioncomm->fetch(
$id);
82 $this->actioncomm->fetch_optionals();
83 $this->actioncomm->fetchObjectLinked();
87 throw new RestException(404,
'Agenda Events not found');
90 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read') && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) {
91 throw new RestException(403,
'Insufficient rights to read event of this owner id. Your id is '.DolibarrApiAccess::$user->
id);
95 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
122 public function index($sortfield =
"t.id", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
128 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'read')) {
129 throw new RestException(403,
"Insufficient rights to read events");
133 $socid = DolibarrApiAccess::$user->socid ?: 0;
137 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
138 $search_sale = DolibarrApiAccess::$user->id;
140 if (!isModEnabled(
'societe')) {
144 $sql =
"SELECT t.id";
145 $sql .=
" FROM ".MAIN_DB_PREFIX.
"actioncomm AS t";
146 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"actioncomm_extrafields AS ef ON (ef.fk_object = t.id)";
147 $sql .=
' WHERE t.entity IN ('.getEntity(
'agenda').
')';
149 $sql .=
" AND t.fk_user_action IN (".$this->db->sanitize($user_ids).
")";
152 $sql .=
" AND t.fk_soc = ".((int) $socid);
155 if ($search_sale && $search_sale !=
'-1') {
156 if ($search_sale == -2) {
157 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
158 } elseif ($search_sale > 0) {
159 $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).
")";
167 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
172 $sqlTotals = str_replace(
'SELECT t.id',
'SELECT count(t.id) as total', $sql);
174 $sql .= $this->db->order($sortfield, $sortorder);
179 $offset = $limit * $page;
181 $sql .= $this->db->plimit($limit + 1, $offset);
184 $result = $this->db->query($sql);
188 $num = $this->db->num_rows($result);
189 $min = min($num, ($limit <= 0 ? $num : $limit));
191 $obj = $this->db->fetch_object($result);
192 $actioncomm_static =
new ActionComm($this->db);
193 if ($actioncomm_static->fetch($obj->id)) {
199 throw new RestException(503,
'Error when retrieve Agenda Event list : '.$this->db->lasterror());
203 if ($pagination_data) {
204 $totalsResult = $this->db->query($sqlTotals);
205 $total = $this->db->fetch_object($totalsResult)->total;
210 $obj_ret[
'data'] = $tmp;
211 $obj_ret[
'pagination'] = [
212 'total' => (int) $total,
214 'page_count' => (
int) ceil((
int) $total / $limit),
234 public function post($request_data =
null)
236 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'create')) {
237 throw new RestException(403,
"Insufficient rights to create your Agenda Event");
239 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'create') && DolibarrApiAccess::$user->
id != $request_data[
'userownerid']) {
240 throw new RestException(403,
"Insufficient rights to create an Agenda Event for owner id ".$request_data[
'userownerid'].
' Your id is '.DolibarrApiAccess::$user->
id);
244 $result = $this->
_validate($request_data);
246 foreach ($request_data as $field => $value) {
247 if ($field ===
'caller') {
249 $this->actioncomm->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
253 $this->actioncomm->$field = $this->
_checkValForAPI($field, $value, $this->actioncomm);
263 if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
264 throw new RestException(500,
"Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
267 return $this->actioncomm->id;
284 public function put(
$id, $request_data =
null)
286 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'create')) {
287 throw new RestException(403,
"Insufficient rights to create your Agenda Event");
289 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'create') && DolibarrApiAccess::$user->
id != $request_data[
'userownerid']) {
290 throw new RestException(403,
"Insufficient rights to create an Agenda Event for owner id ".$request_data[
'userownerid'].
' Your id is '.DolibarrApiAccess::$user->
id);
293 $result = $this->actioncomm->fetch(
$id);
295 $this->actioncomm->fetch_optionals();
296 $this->actioncomm->fetch_userassigned();
297 $this->actioncomm->oldcopy = clone $this->actioncomm;
300 throw new RestException(404,
'actioncomm not found');
304 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
306 foreach ($request_data as $field => $value) {
307 if ($field ==
'id') {
310 if ($field ===
'caller') {
312 $this->actioncomm->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
316 if ($field ==
'array_options' && is_array($value)) {
317 foreach ($value as $index => $val) {
318 $this->actioncomm->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->actioncomm);
322 $this->actioncomm->$field = $this->
_checkValForAPI($field, $value, $this->actioncomm);
325 if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
326 return $this->
get(
$id);
345 public function delete(
$id)
347 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'myactions',
'delete')) {
348 throw new RestException(403,
"Insufficient rights to delete your Agenda Event");
351 $result = $this->actioncomm->fetch(
$id);
353 $this->actioncomm->fetch_optionals();
354 $this->actioncomm->fetch_userassigned();
355 $this->actioncomm->oldcopy = clone $this->actioncomm;
358 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'delete') && DolibarrApiAccess::$user->
id != $this->actioncomm->userownerid) {
359 throw new RestException(403,
"Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.
' Your id is '.DolibarrApiAccess::$user->id);
363 throw new RestException(404,
'Agenda Event not found');
367 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
370 if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
371 throw new RestException(500,
'Error when delete Agenda Event : '.$this->actioncomm->error);
377 'message' =>
'Agenda Event deleted'
391 if ($data ===
null) {
395 foreach (AgendaEvents::$FIELDS as $field) {
396 if (!isset($data[$field])) {
397 throw new RestException(400,
"$field field missing");
399 $event[$field] = $data[$field];
438 unset(
$object->barcode_type_code);
439 unset(
$object->barcode_type_label);
440 unset(
$object->barcode_type_coder);
441 unset(
$object->mode_reglement_id);
442 unset(
$object->cond_reglement_id);
443 unset(
$object->cond_reglement);
444 unset(
$object->fk_delivery_address);
445 unset(
$object->shipping_method_id);
449 unset(
$object->total_localtax1);
450 unset(
$object->total_localtax2);
453 unset(
$object->label_incoterms);
454 unset(
$object->location_incoterms);
461 unset(
$object->demand_reason_id);
462 unset(
$object->transport_mode_id);
$id
Support class for third parties, contacts, members, users or resources.
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 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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...