dolibarr 24.0.3
api_tickets.class.php
1<?php
2/* Copyright (C) 2016 Jean-François Ferry <hello@librethic.io>
3 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
23require_once DOL_DOCUMENT_ROOT.'/core/lib/ticket.lib.php';
24
25
32class Tickets extends DolibarrApi
33{
37 public static $FIELDS = array(
38 'subject',
39 'message'
40 );
41
45 public static $FIELDS_MESSAGES = array(
46 'track_id',
47 'message'
48 );
49
53 public $ticket;
54
58 public function __construct()
59 {
60 global $db;
61 $this->db = $db;
62 $this->ticket = new Ticket($this->db);
63 }
64
78 public function get($id, $contact_list = 1)
79 {
80 return $this->getCommon($id, '', '', $contact_list);
81 }
82
98 public function getByTrackId($track_id, $contact_list = 1)
99 {
100 return $this->getCommon(0, $track_id, '', $contact_list);
101 }
102
118 public function getByRef($ref, $contact_list = 1)
119 {
120 return $this->getCommon(0, '', $ref, $contact_list);
121 }
122
133 private function getCommon($id = 0, $track_id = '', $ref = '', $contact_list = 1)
134 {
135 global $conf;
136
137 if (!DolibarrApiAccess::$user->hasRight('ticket', 'read')) {
138 throw new RestException(403);
139 }
140
141 // Check parameters
142 if (($id < 0) && !$track_id && !$ref) {
143 throw new RestException(400, 'Wrong parameters');
144 }
145 if (empty($id) && empty($ref) && empty($track_id)) {
146 $result = $this->ticket->initAsSpecimen();
147 } else {
148 $result = $this->ticket->fetch($id, $ref, $track_id);
149 }
150 if (!$result) {
151 throw new RestException(404, 'Ticket not found');
152 }
153
154 // Messages of ticket
155 $messages = array();
156
157 $this->ticket->loadCacheMsgsTicket();
158
159 if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
160 $num = count($this->ticket->cache_msgs_ticket);
161 $i = 0;
162 while ($i < $num) {
163 $iduseraction = $this->ticket->cache_msgs_ticket[$i]['fk_user_action'];
164 if ($iduseraction > 0) {
165 if (empty($conf->cache['user'][$iduseraction])) {
166 $user_action = new User($this->db);
167 $user_action->fetch($iduseraction);
168
169 $conf->cache['user'][$iduseraction] = $user_action;
170 } else {
171 $user_action = $conf->cache['user'][$iduseraction];
172 }
173 } else {
174 $user_action = null;
175 }
176
177 // Now define messages
178 $messages[] = array(
179 'id' => $this->ticket->cache_msgs_ticket[$i]['id'],
180 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i]['fk_user_action'], // Id of user owning the event
181 'fk_user_action_socid' => $user_action === null ? '' : $user_action->socid,
182 'fk_user_action_string' => $user_action === null ? '' : dolGetFirstLastname($user_action->firstname, $user_action->lastname),
183 'datec' => $this->ticket->cache_msgs_ticket[$i]['datec'],
184 'datep' => $this->ticket->cache_msgs_ticket[$i]['datep'],
185 'subject' => $this->ticket->cache_msgs_ticket[$i]['subject'],
186 'message' => $this->ticket->cache_msgs_ticket[$i]['message'],
187 'private' => $this->ticket->cache_msgs_ticket[$i]['private']
188 );
189 $i++;
190 }
191 $this->ticket->messages = $messages;
192 }
193
194 if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
195 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
196 }
197
198 if ($contact_list > -1) {
199 // Add external contacts ids
200 $tmparray = $this->ticket->liste_contact(-1, 'external', $contact_list);
201 if (is_array($tmparray)) {
202 $this->ticket->contacts_ids = $tmparray;
203 }
204 $tmparray = $this->ticket->liste_contact(-1, 'internal', $contact_list);
205 if (is_array($tmparray)) {
206 $this->ticket->contacts_ids_internal = $tmparray;
207 }
208 }
209
210 return $this->_cleanObjectDatas($this->ticket);
211 }
212
232 public function index($socid = 0, $sortfield = "t.rowid", $sortorder = "ASC", $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $loadcontacts = 0, $pagination_data = false)
233 {
234 if (!DolibarrApiAccess::$user->hasRight('ticket', 'read')) {
235 throw new RestException(403);
236 }
237
238 $obj_ret = array();
239
240 $socid = DolibarrApiAccess::$user->socid ?: $socid;
241
242 $search_sale = null;
243 // If the internal user must only see his customers, force searching by him
244 $search_sale = 0;
245 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socid) {
246 $search_sale = DolibarrApiAccess::$user->id;
247 }
248
249 $sql = "SELECT t.rowid";
250 $sql .= " FROM ".MAIN_DB_PREFIX."ticket AS t";
251 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe AS s ON (s.rowid = t.fk_soc)";
252 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."ticket_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
253 $sql .= ' WHERE t.entity IN ('.getEntity('ticket', 1).')';
254 if ($socid > 0) {
255 $sql .= " AND t.fk_soc = ".((int) $socid);
256 }
257 // Search on sale representative
258 if ($search_sale && $search_sale != '-1') {
259 if ($search_sale == -2) {
260 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
261 } elseif ($search_sale > 0) {
262 $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).")";
263 }
264 }
265 // Add sql filters
266 if ($sqlfilters) {
267 $errormessage = '';
268 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
269 if ($errormessage) {
270 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
271 }
272 }
273
274 //this query will return total orders with the filters given
275 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
276
277 $sql .= $this->db->order($sortfield, $sortorder);
278
279 if ($limit) {
280 if ($page < 0) {
281 $page = 0;
282 }
283 $offset = $limit * $page;
284
285 $sql .= $this->db->plimit($limit, $offset);
286 }
287
288 $result = $this->db->query($sql);
289 if ($result) {
290 $i = 0;
291 $num = $this->db->num_rows($result);
292 $min = min($num, ($limit <= 0 ? $num : $limit));
293 while ($i < $min) {
294 $obj = $this->db->fetch_object($result);
295 $ticket_static = new Ticket($this->db);
296 if ($ticket_static->fetch($obj->rowid)) {
297 if ($ticket_static->fk_user_assign > 0) {
298 $userStatic = new User($this->db);
299 $userStatic->fetch($ticket_static->fk_user_assign);
300 //$ticket_static->fk_user_assign_string = dolGetFirstLastname($userStatic->firstname, $userStatic->lastname);
301 }
302
303 if ($loadcontacts) {
304 // Add external contacts ids
305 $tmparray = $ticket_static->liste_contact(-1, 'external', 1);
306 if (is_array($tmparray)) {
307 $ticket_static->contacts_ids = $tmparray;
308 }
309 $tmparray = $ticket_static->liste_contact(-1, 'internal', 1);
310 if (is_array($tmparray)) {
311 $ticket_static->contacts_ids_internal = $tmparray;
312 }
313 }
314
315 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($ticket_static), $properties);
316 }
317 $i++;
318 }
319 } else {
320 throw new RestException(503, 'Error when retrieve ticket list');
321 }
322
323 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
324 if ($pagination_data) {
325 $totalsResult = $this->db->query($sqlTotals);
326 $total = $this->db->fetch_object($totalsResult)->total;
327
328 $tmp = $obj_ret;
329 $obj_ret = [];
330
331 $obj_ret['data'] = $tmp;
332 $obj_ret['pagination'] = [
333 'total' => (int) $total,
334 'page' => $page, //count starts from 0
335 'page_count' => ceil((int) $total / $limit),
336 'limit' => $limit
337 ];
338 }
339
340 return $obj_ret;
341 }
342
351 public function post($request_data = null)
352 {
353 $ticketstatic = new Ticket($this->db);
354 if (!DolibarrApiAccess::$user->hasRight('ticket', 'write')) {
355 throw new RestException(403);
356 }
357
358 // Check mandatory fields
359 $this->_validate($request_data);
360
361 // Check thirdparty validity
362 $socid = (int) ($request_data['socid'] ?? 0);
363 if ($socid > 0) {
364 $thirdpartytmp = new Societe($this->db);
365 $thirdparty_result = $thirdpartytmp->fetch($socid);
366 if ($thirdparty_result < 1) {
367 throw new RestException(404, 'Thirdparty with id='.$socid.' not found or not allowed');
368 }
369 if (!DolibarrApi::_checkAccessToResource('societe', $thirdpartytmp->id)) {
370 throw new RestException(404, 'Thirdparty with id='.$thirdpartytmp->id.' not found or not allowed');
371 }
372 }
373
374 foreach ($request_data as $field => $value) {
375 if ($field === 'caller') {
376 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
377 $this->ticket->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
378 continue;
379 }
380
381 $this->ticket->$field = $this->_checkValForAPI($field, $value, $this->ticket);
382 }
383 if (empty($this->ticket->ref)) {
384 $this->ticket->ref = $ticketstatic->getDefaultRef();
385 }
386 if (empty($this->ticket->track_id)) {
387 $this->ticket->track_id = generate_random_id(16);
388 }
389
390 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
391 throw new RestException(500, "Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
392 }
393
394 return $this->ticket->id;
395 }
396
407 public function postNewMessage($request_data = null)
408 {
409 if (!DolibarrApiAccess::$user->hasRight('ticket', 'write')) {
410 throw new RestException(403);
411 }
412
413 // Check mandatory fields
414 $result = $this->_validateMessage($request_data);
415
416 $return_action_id = false;
417 if (isset($request_data['return_action_id'])) {
418 $return_action_id = !empty($request_data['return_action_id']);
419 unset($request_data['return_action_id']);
420 }
421
422 $attachments = array();
423 if (isset($request_data['attachments']) && is_array($request_data['attachments'])) {
424 $attachments = $request_data['attachments'];
425 unset($request_data['attachments']);
426 }
427
428 foreach ($request_data as $field => $value) {
429 if ($field === 'caller') {
430 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
431 $this->ticket->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
432 continue;
433 }
434
435 $this->ticket->$field = $this->_checkValForAPI($field, $value, $this->ticket);
436 }
437 $ticketMessageText = $this->ticket->message;
438 $result = $this->ticket->fetch(0, '', $this->ticket->track_id);
439 // fetch() returns -1 on an error and 0 when nothing matches, and !(-1) is false, so an error went past
440 // this 404 and was reported later as a 400 or a 500 instead of a missing ticket.
441 if ($result <= 0) {
442 throw new RestException(404, 'Ticket not found');
443 }
444
445 if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
446 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
447 }
448
449 $this->ticket->message = $ticketMessageText;
450
451 $filename_list = array();
452 $mimetype_list = array();
453 $mimefilename_list = array();
454 if (!empty($attachments)) {
455 global $conf;
456 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
457
458 $destdir = $conf->ticket->dir_output.'/'.$this->ticket->ref;
459 if (!dol_is_dir($destdir) && dol_mkdir($destdir) < 0) {
460 throw new RestException(500, 'Error while trying to create directory '.$destdir);
461 }
462
463 foreach ($attachments as $attachment) {
464 if (!is_array($attachment) || empty($attachment['filename'])) {
465 continue;
466 }
467
468 $filename = dol_sanitizeFileName($attachment['filename']);
469 if (empty($filename)) {
470 continue;
471 }
472
473 $filecontent = isset($attachment['filecontent']) ? $attachment['filecontent'] : '';
474 $fileencoding = isset($attachment['fileencoding']) ? $attachment['fileencoding'] : '';
475 $content = ($fileencoding === 'base64') ? base64_decode($filecontent) : $filecontent;
476 if ($content === false) {
477 throw new RestException(400, 'Failed to decode attachment '.$filename);
478 }
479
480 $destfile = $destdir.'/'.$filename;
481 if (is_file($destfile)) {
482 $pathinfo = pathinfo($filename);
483 $suffix = ' - '.dol_print_date(dol_now(), 'dayhourlog');
484 $extension = !empty($pathinfo['extension']) ? '.'.$pathinfo['extension'] : '';
485 $basename = !empty($pathinfo['filename']) ? $pathinfo['filename'] : preg_replace('/\.[^.]+$/', '', $filename);
486 $destfile = $destdir.'/'.$basename.$suffix.$extension;
487 }
488
489 $destfiletmp = DOL_DATA_ROOT.'/admin/temp/'.basename($destfile);
490 if (!dol_is_dir(dirname($destfiletmp))) {
491 dol_mkdir(dirname($destfiletmp));
492 }
493
494 $fhandle = @fopen($destfiletmp, 'w');
495 if (!$fhandle) {
496 throw new RestException(500, "Failed to open file '".$destfiletmp."' for write");
497 }
498 $nbofbyteswrote = fwrite($fhandle, $content);
499 fclose($fhandle);
500 if ($nbofbyteswrote === false) {
501 throw new RestException(500, "Failed to write file '".$destfiletmp."'");
502 }
503
504 $moreinfo = array(
505 'description' => 'File uploaded using ticket API',
506 'src_object_type' => $this->ticket->element,
507 'src_object_id' => $this->ticket->id,
508 'gen_or_uploaded' => 'uploaded'
509 );
510 $resultmove = dol_move($destfiletmp, $destfile, '0', 1, 0, 1, $moreinfo);
511 if (!$resultmove) {
512 throw new RestException(500, "Failed to move file into '".$destfile."'");
513 }
514
515 $filename_list[] = $destfile;
516 $mimefilename_list[] = basename($destfile);
517 $mimetype_list[] = !empty($attachment['mimetype']) ? $attachment['mimetype'] : '';
518 }
519 }
520
521 $actionid = $this->ticket->createTicketMessage(DolibarrApiAccess::$user, 0, $filename_list, $mimetype_list, $mimefilename_list);
522 if ($actionid <= 0) {
523 throw new RestException(500, 'Error when creating ticket');
524 }
525 if ($return_action_id) {
526 return array('ticket_id' => $this->ticket->id, 'action_id' => $actionid);
527 }
528 return $this->ticket->id;
529 }
530
540 public function put($id, $request_data = null)
541 {
542 if (!DolibarrApiAccess::$user->hasRight('ticket', 'write')) {
543 throw new RestException(403);
544 }
545
546 $result = $this->ticket->fetch($id);
547 if (!$result) {
548 throw new RestException(404, 'Ticket not found');
549 }
550
551 if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
552 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
553 }
554
555 // Check thirdparty validity
556 $socid = (int) ($request_data['socid'] ?? 0);
557 if ($socid > 0) {
558 $thirdpartytmp = new Societe($this->db);
559 $thirdparty_result = $thirdpartytmp->fetch($socid);
560 if ($thirdparty_result < 1) {
561 throw new RestException(404, 'Thirdparty with id='.$socid.' not found or not allowed');
562 }
563 if (!DolibarrApi::_checkAccessToResource('societe', $thirdpartytmp->id)) {
564 throw new RestException(404, 'Thirdparty with id='.$thirdpartytmp->id.' not found or not allowed');
565 }
566 }
567
568 foreach ($request_data as $field => $value) {
569 if ($field === 'caller') {
570 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
571 $this->ticket->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
572 continue;
573 }
574
575 if ($field == 'id') {
576 continue;
577 }
578 if ($field == 'array_options' && is_array($value)) {
579 foreach ($value as $index => $val) {
580 $this->ticket->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->ticket);
581 }
582 continue;
583 }
584
585 $this->ticket->$field = $this->_checkValForAPI($field, $value, $this->ticket);
586 }
587
588 if ($this->ticket->update(DolibarrApiAccess::$user) > 0) {
589 return $this->get($id);
590 } else {
591 throw new RestException(500, $this->ticket->error);
592 }
593 }
594
603 public function delete($id)
604 {
605 if (!DolibarrApiAccess::$user->hasRight('ticket', 'delete')) {
606 throw new RestException(403);
607 }
608 $result = $this->ticket->fetch($id);
609 if (!$result) {
610 throw new RestException(404, 'Ticket not found');
611 }
612
613 if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
614 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
615 }
616
617 if (!$this->ticket->delete(DolibarrApiAccess::$user)) {
618 throw new RestException(500, 'Error when deleting ticket');
619 }
620
621 return array(
622 'success' => array(
623 'code' => 200,
624 'message' => 'Ticket deleted'
625 )
626 );
627 }
628
637 private function _validate($data)
638 {
639 if ($data === null) {
640 $data = array();
641 }
642 $ticket = array();
643 foreach (Tickets::$FIELDS as $field) {
644 if (!isset($data[$field])) {
645 throw new RestException(400, "$field field missing");
646 }
647 $ticket[$field] = $data[$field];
648 }
649 return $ticket;
650 }
651
660 private function _validateMessage($data)
661 {
662 if ($data === null) {
663 $data = array();
664 }
665 $ticket = array();
666 foreach (Tickets::$FIELDS_MESSAGES as $field) {
667 if (!isset($data[$field])) {
668 throw new RestException(400, "$field field missing");
669 }
670 $ticket[$field] = $data[$field];
671 }
672 return $ticket;
673 }
674
675 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
687 protected function _cleanObjectDatas($object)
688 {
689 // phpcs:enable
690 $object = parent::_cleanObjectDatas($object);
691
692 // Other attributes to clean
693 $attr2clean = array(
694 "contact",
695 "contact_id",
696 "ref_previous",
697 "ref_next",
698 "ref_ext",
699 "table_element_line",
700 "statut",
701 "country",
702 "country_id",
703 "country_code",
704 "barcode_type",
705 "barcode_type_code",
706 "barcode_type_label",
707 "barcode_type_coder",
708 "mode_reglement_id",
709 "cond_reglement_id",
710 "cond_reglement",
711 "fk_delivery_address",
712 "shipping_method_id",
713 "modelpdf",
714 "fk_account",
715 "note_public",
716 "note_private",
717 "note",
718 "total_ht",
719 "total_tva",
720 "total_localtax1",
721 "total_localtax2",
722 "total_ttc",
723 "fk_incoterms",
724 "label_incoterms",
725 "location_incoterms",
726 "name",
727 "lastname",
728 "firstname",
729 "civility_id",
730 "canvas",
731 "cache_msgs_ticket",
732 "cache_logs_ticket",
733 "cache_types_tickets",
734 "regeximgext",
735 "labelStatus",
736 "labelStatusShort",
737 "multicurrency_code",
738 "multicurrency_tx",
739 "multicurrency_total_ht",
740 "multicurrency_total_ttc",
741 "multicurrency_total_tva",
742 "multicurrency_total_localtax1",
743 "multicurrency_total_localtax2"
744 );
745 foreach ($attr2clean as $toclean) {
746 unset($object->$toclean);
747 }
748
749 // If object has lines, remove $db property
750 if (isset($object->lines) && count($object->lines) > 0) {
751 $nboflines = count($object->lines);
752 for ($i = 0; $i < $nboflines; $i++) {
753 $this->_cleanObjectDatas($object->lines[$i]);
754 }
755 }
756
757 // If object has linked objects, remove $db property
758 if (isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
759 foreach ($object->linkedObjects as $type_object => $linked_object) {
760 foreach ($linked_object as $object2clean) {
761 $this->_cleanObjectDatas($object2clean);
762 }
763 }
764 }
765 return $object;
766 }
767
788 public function postContact(int $id, int $contactid, string $type, string $source = "external", int $notrigger = 0): array
789 {
790 // Check permissions
791 if (!DolibarrApiAccess::$user->hasRight('ticket', 'write')) {
792 throw new RestException(403);
793 }
794
795 // test source
796 if (empty($source)) {
797 throw new RestException(400, 'Source can not be empty');
798 }
799 // test type
800 if (empty($type)) {
801 throw new RestException(400, 'type can not be empty');
802 }
803
804 // Check type/source contact exists
805 $sqlCheckTypeSource = "SELECT tc.rowid";
806 $sqlCheckTypeSource .= " FROM ".$this->db->prefix()."c_type_contact as tc";
807 $sqlCheckTypeSource .= " WHERE tc.element LIKE 'ticket'";
808 $sqlCheckTypeSource .= " AND tc.source = '".$this->db->escape($source)."'";
809 $sqlCheckTypeSource .= " AND tc.code = '".$this->db->escape($type)."'";
810 $sqlCheckTypeSource .= " AND tc.active = 1";
811 $result = $this->db->query($sqlCheckTypeSource);
812
813 if ($result && $this->db->num_rows($result) == 0) {
814 throw new RestException(400, 'Contact type not found');
815 }
816
817 // Check contact exists
818 if ($source == "external") {
819 // Check external contact exists
820 $sqlCheckExternalContact = "SELECT 1 as exist";
821 $sqlCheckExternalContact .= " FROM ".MAIN_DB_PREFIX."socpeople";
822 $sqlCheckExternalContact .= " WHERE rowid = " . intval($contactid);
823 $result = $this->db->query($sqlCheckExternalContact);
824
825 if ($result && $this->db->num_rows($result) == 0) {
826 throw new RestException(404, 'External contact not found');
827 }
828 } else {
829 // Check internal contact exists
830 $sqlCheckInternalContact = "SELECT 1 as exist";
831 $sqlCheckInternalContact .= " FROM ".MAIN_DB_PREFIX."user";
832 $sqlCheckInternalContact .= " WHERE rowid = " . intval($contactid);
833 $result = $this->db->query($sqlCheckInternalContact);
834
835 if ($result && $this->db->num_rows($result) == 0) {
836 throw new RestException(404, 'Internal contact not found');
837 }
838 }
839
840 // tests done, let's get it
841 $result = $this->ticket->fetch($id);
842 if (!$result) {
843 throw new RestException(404, 'Ticket not found');
844 }
845 if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
846 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
847 }
848
849 $result = $this->ticket->add_contact($contactid, $type, $source, $notrigger);
850
851 if ($result == 0) {
852 throw new RestException(400, 'Already exists: Contact='.$contactid.' is already linked to the ticket='.$id.' as source='.$source.' and type='.$type);
853 } elseif ($result == -1) {
854 throw new RestException(400, 'Wrong contact='.$contactid);
855 } elseif ($result == -2) {
856 throw new RestException(400, 'Wrong type='.$type);
857 } elseif ($result == -3) {
858 throw new RestException(400, 'Not allowed contacts');
859 } elseif ($result == -4) {
860 throw new RestException(400, 'ErrorCommercialNotAllowedForThirdparty');
861 } elseif ($result == -5) {
862 throw new RestException(400, 'Trigger failed');
863 } elseif ($result == -6) {
864 throw new RestException(400, 'DB_ERROR_RECORD_ALREADY_EXISTS');
865 } elseif ($result == -7) {
866 throw new RestException(400, 'Some other error');
867 } elseif ($result <= -8) {
868 throw new RestException(400, 'Unknown error occurred');
869 }
870
871 return array(
872 'success' => array(
873 'code' => 200,
874 'message' => 'Contact='.$contactid.' linked to the ticket='.$id.' as '.$source.' '.$type
875 )
876 );
877 }
878
898 public function deleteContact(int $id, int $contactid, string $type, string $source = "external"): array
899 {
900 // Check permissions
901 if (!DolibarrApiAccess::$user->hasRight('ticket', 'write')) {
902 throw new RestException(403);
903 }
904
905 // test source
906 if (empty($source)) {
907 throw new RestException(400, 'Source can not be empty');
908 }
909 // test type
910 if (empty($type)) {
911 throw new RestException(400, 'type can not be empty');
912 }
913
914 $result = $this->ticket->fetch($id);
915 if (!$result) {
916 throw new RestException(404, 'Ticket not found');
917 }
918
919 if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
920 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
921 }
922
923 $contacts = $this->ticket->liste_contact(-1, $source);
924 foreach ($contacts as $contact) {
925 if ($contact['id'] == $contactid && $contact['code'] == $type) {
926 $result = $this->ticket->delete_contact($contact['rowid']);
927
928 if (!$result) {
929 throw new RestException(500, 'Error when deleting the contact '.$contact['rowid']);
930 }
931 }
932 }
933
934 return array(
935 'success' => array(
936 'code' => 200,
937 'message' => 'Contact unlinked from ticket'
938 )
939 );
940 }
941}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_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.
Class to manage third parties objects (customers, suppliers, prospects...)
getByTrackId($track_id, $contact_list=1)
Get properties of a Ticket object from track id.
deleteContact(int $id, int $contactid, string $type, string $source="external")
Unlink a contact type of given ticket.
getCommon($id=0, $track_id='', $ref='', $contact_list=1)
Get properties of a Ticket object Return an array with ticket information.
__construct()
Constructor.
index($socid=0, $sortfield="t.rowid", $sortorder="ASC", $limit=100, $page=0, $sqlfilters='', $properties='', $loadcontacts=0, $pagination_data=false)
List tickets.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
postNewMessage($request_data=null)
Add a new message to an existing ticket identified by property ->track_id into request.
post($request_data=null)
Create ticket object.
put($id, $request_data=null)
Update ticket.
_validateMessage($data)
Validate fields before create or update object message.
postContact(int $id, int $contactid, string $type, string $source="external", int $notrigger=0)
Add a contact type of given ticket.
getByRef($ref, $contact_list=1)
Get properties of a Ticket object from ref.
_validate($data)
Validate fields before create or update object.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array(), $entity=null)
Move a file into another name.
dol_is_dir($folder)
Test if filename is a directory.
dol_now($mode='gmt')
Return date for now.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
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.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Class to generate the form for creating a new ticket.
generate_random_id($car=16)
Generate a random id.