18 use Luracast\Restler\RestException;
20 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
21 require_once DOL_DOCUMENT_ROOT.
'/core/lib/ticket.lib.php';
35 public static $FIELDS = array(
43 public static $FIELDS_MESSAGES = array(
60 $this->ticket =
new Ticket($this->
db);
75 public function get($id)
96 return $this->
getCommon(0, $track_id,
'');
131 private function getCommon($id = 0, $track_id =
'', $ref =
'')
133 if (!DolibarrApiAccess::$user->rights->ticket->read) {
134 throw new RestException(403);
138 if (($id < 0) && !$track_id && !$ref) {
139 throw new RestException(401,
'Wrong parameters');
142 $result = $this->ticket->initAsSpecimen();
144 $result = $this->ticket->fetch($id, $ref, $track_id);
147 throw new RestException(404,
'Ticket not found');
151 if ($this->ticket->fk_user_assign > 0) {
152 $userStatic =
new User($this->
db);
153 $userStatic->fetch($this->ticket->fk_user_assign);
154 $this->ticket->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
159 $this->ticket->loadCacheMsgsTicket();
160 if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
161 $num = count($this->ticket->cache_msgs_ticket);
164 if ($this->ticket->cache_msgs_ticket[$i][
'fk_user_author'] > 0) {
165 $user_action =
new User($this->
db);
166 $user_action->fetch($this->ticket->cache_msgs_ticket[$i][
'fk_user_author']);
171 'id' => $this->ticket->cache_msgs_ticket[$i][
'id'],
172 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
173 'fk_user_action_socid' => $user_action->socid,
174 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
175 'message' => $this->ticket->cache_msgs_ticket[$i][
'message'],
176 'datec' => $this->ticket->cache_msgs_ticket[$i][
'datec'],
177 'private' => $this->ticket->cache_msgs_ticket[$i][
'private']
181 $this->ticket->messages = $messages;
186 $this->ticket->loadCacheLogsTicket();
187 if (is_array($this->ticket->cache_logs_ticket) && count($this->ticket->cache_logs_ticket) > 0) {
188 $num = count($this->ticket->cache_logs_ticket);
191 if ($this->ticket->cache_logs_ticket[$i][
'fk_user_create'] > 0) {
192 $user_action =
new User($this->
db);
193 $user_action->fetch($this->ticket->cache_logs_ticket[$i][
'fk_user_create']);
198 'id' => $this->ticket->cache_logs_ticket[$i][
'id'],
199 'fk_user_author' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
200 'fk_user_action' => $this->ticket->cache_logs_ticket[$i][
'fk_user_create'],
201 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
202 'message' => $this->ticket->cache_logs_ticket[$i][
'message'],
203 'datec' => $this->ticket->cache_logs_ticket[$i][
'datec'],
207 $this->ticket->history = $history;
211 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
231 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'')
235 if (!DolibarrApiAccess::$user->rights->ticket->read) {
236 throw new RestException(403);
241 if (!$socid && DolibarrApiAccess::$user->socid) {
242 $socid = DolibarrApiAccess::$user->socid;
246 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
247 $search_sale = DolibarrApiAccess::$user->id;
250 $sql =
"SELECT t.rowid";
251 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
252 $sql .=
", sc.fk_soc, sc.fk_user";
254 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket as t";
256 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
257 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
260 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
261 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
262 $sql .=
" AND t.fk_soc = sc.fk_soc";
265 $sql .=
" AND t.fk_soc = ".((int) $socid);
267 if ($search_sale > 0) {
268 $sql .=
" AND t.rowid = sc.fk_soc";
272 if ($search_sale > 0) {
273 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
279 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
281 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
282 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
285 $sql .= $this->
db->order($sortfield, $sortorder);
291 $offset = $limit * $page;
293 $sql .= $this->
db->plimit($limit, $offset);
296 $result = $this->
db->query($sql);
298 $num = $this->
db->num_rows($result);
301 $obj = $this->
db->fetch_object($result);
302 $ticket_static =
new Ticket($this->
db);
303 if ($ticket_static->fetch($obj->rowid)) {
304 if ($ticket_static->fk_user_assign > 0) {
305 $userStatic =
new User($this->
db);
306 $userStatic->fetch($ticket_static->fk_user_assign);
307 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
314 throw new RestException(503,
'Error when retrieve ticket list');
316 if (!count($obj_ret)) {
317 throw new RestException(404,
'No ticket found');
328 public function post($request_data = null)
330 $ticketstatic =
new Ticket($this->
db);
331 if (!DolibarrApiAccess::$user->rights->ticket->write) {
332 throw new RestException(401);
335 $result = $this->
_validate($request_data);
337 foreach ($request_data as $field => $value) {
338 $this->ticket->$field = $value;
340 if (empty($this->ticket->ref)) {
341 $this->ticket->ref = $ticketstatic->getDefaultRef();
343 if (empty($this->ticket->track_id)) {
347 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
348 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
351 return $this->ticket->id;
363 $ticketstatic =
new Ticket($this->
db);
364 if (!DolibarrApiAccess::$user->rights->ticket->write) {
365 throw new RestException(401);
370 foreach ($request_data as $field => $value) {
371 $this->ticket->$field = $value;
373 $ticketMessageText = $this->ticket->message;
374 $result = $this->ticket->fetch(
'',
'', $this->ticket->track_id);
376 throw new RestException(404,
'Ticket not found');
378 $this->ticket->message = $ticketMessageText;
379 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
380 throw new RestException(500,
'Error when creating ticket');
382 return $this->ticket->id;
393 public function put($id, $request_data = null)
395 if (!DolibarrApiAccess::$user->rights->ticket->write) {
396 throw new RestException(401);
399 $result = $this->ticket->fetch($id);
401 throw new RestException(404,
'Ticket not found');
405 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
408 foreach ($request_data as $field => $value) {
409 $this->ticket->$field = $value;
412 if ($this->ticket->update($id, DolibarrApiAccess::$user)) {
413 return $this->
get($id);
426 public function delete($id)
428 if (!DolibarrApiAccess::$user->rights->ticket->delete) {
429 throw new RestException(401);
431 $result = $this->ticket->fetch($id);
433 throw new RestException(404,
'Ticket not found');
437 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
440 if (!$this->ticket->delete($id)) {
441 throw new RestException(500,
'Error when deleting ticket');
447 'message' =>
'Ticket deleted'
463 foreach (Tickets::$FIELDS as $field) {
464 if (!isset($data[$field])) {
465 throw new RestException(400,
"$field field missing");
467 $ticket[$field] = $data[$field];
483 foreach (Tickets::$FIELDS_MESSAGES as $field) {
484 if (!isset($data[$field])) {
485 throw new RestException(400,
"$field field missing");
487 $ticket[$field] = $data[$field];
505 $object = parent::_cleanObjectDatas($object);
514 "table_element_line",
521 "barcode_type_label",
522 "barcode_type_coder",
526 "fk_delivery_address",
527 "shipping_method_id",
540 "location_incoterms",
548 "cache_types_tickets",
549 "cache_category_tickets",
554 foreach ($attr2clean as $toclean) {
555 unset($object->$toclean);
559 if (isset($object->lines) && count($object->lines) > 0) {
560 $nboflines = count($object->lines);
561 for ($i = 0; $i < $nboflines; $i++) {
567 if (isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
568 foreach ($object->linkedObjects as $type_object => $linked_object) {
569 foreach ($linked_object as $object2clean) {
getCommon($id=0, $track_id= '', $ref= '')
Get properties of a Ticket object Return an array with ticket informations.
_validateMessage($data)
Validate fields before create or update object message.
index($socid=0, $sortfield="t.rowid", $sortorder="ASC", $limit=100, $page=0, $sqlfilters= '')
List tickets.
getByTrackId($track_id)
Get properties of a Ticket object from track id.
$conf db
API class for accounts.
postNewMessage($request_data=null)
Create ticket object.
Class to manage Dolibarr users.
_checkFilters($sqlfilters, &$error= '')
Return if a $sqlfilters parameter is valid.
_cleanObjectDatas($object)
Clean sensible object datas.
generate_random_id($car=16)
Generate a random id.
__construct()
Constructor.
getByRef($ref)
Get properties of a Ticket object from ref.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check access by user to a given resource.
put($id, $request_data=null)
Update ticket.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create ticket object.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.