18 use Luracast\Restler\RestException;
20 require
'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,
'');
132 private function getCommon($id = 0, $track_id =
'', $ref =
'')
134 if (!DolibarrApiAccess::$user->rights->ticket->read) {
135 throw new RestException(403);
139 if (($id < 0) && !$track_id && !$ref) {
140 throw new RestException(401,
'Wrong parameters');
143 $result = $this->ticket->initAsSpecimen();
145 $result = $this->ticket->fetch($id, $ref, $track_id);
148 throw new RestException(404,
'Ticket not found');
152 if ($this->ticket->fk_user_assign > 0) {
153 $userStatic =
new User($this->
db);
154 $userStatic->fetch($this->ticket->fk_user_assign);
155 $this->ticket->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
160 $this->ticket->loadCacheMsgsTicket();
161 if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
162 $num = count($this->ticket->cache_msgs_ticket);
165 if ($this->ticket->cache_msgs_ticket[$i][
'fk_user_author'] > 0) {
166 $user_action =
new User($this->
db);
167 $user_action->fetch($this->ticket->cache_msgs_ticket[$i][
'fk_user_author']);
172 'id' => $this->ticket->cache_msgs_ticket[$i][
'id'],
173 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
174 'fk_user_action_socid' => $user_action->socid,
175 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
176 'message' => $this->ticket->cache_msgs_ticket[$i][
'message'],
177 'datec' => $this->ticket->cache_msgs_ticket[$i][
'datec'],
178 'private' => $this->ticket->cache_msgs_ticket[$i][
'private']
182 $this->ticket->messages = $messages;
187 $this->ticket->loadCacheLogsTicket();
188 if (is_array($this->ticket->cache_logs_ticket) && count($this->ticket->cache_logs_ticket) > 0) {
189 $num = count($this->ticket->cache_logs_ticket);
192 if ($this->ticket->cache_logs_ticket[$i][
'fk_user_create'] > 0) {
193 $user_action =
new User($this->
db);
194 $user_action->fetch($this->ticket->cache_logs_ticket[$i][
'fk_user_create']);
199 'id' => $this->ticket->cache_logs_ticket[$i][
'id'],
200 'fk_user_author' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
201 'fk_user_action' => $this->ticket->cache_logs_ticket[$i][
'fk_user_create'],
202 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
203 'message' => $this->ticket->cache_logs_ticket[$i][
'message'],
204 'datec' => $this->ticket->cache_logs_ticket[$i][
'datec'],
208 $this->ticket->history = $history;
212 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
232 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'')
238 if (!$socid && DolibarrApiAccess::$user->socid) {
239 $socid = DolibarrApiAccess::$user->socid;
243 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
244 $search_sale = DolibarrApiAccess::$user->id;
247 $sql =
"SELECT t.rowid";
248 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
249 $sql .=
", sc.fk_soc, sc.fk_user";
251 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket as t";
253 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
254 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
257 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
258 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
259 $sql .=
" AND t.fk_soc = sc.fk_soc";
262 $sql .=
" AND t.fk_soc = ".$socid;
264 if ($search_sale > 0) {
265 $sql .=
" AND t.rowid = sc.fk_soc";
269 if ($search_sale > 0) {
270 $sql .=
" AND sc.fk_user = ".$search_sale;
275 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
277 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
278 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
281 $sql .= $this->
db->order($sortfield, $sortorder);
287 $offset = $limit * $page;
289 $sql .= $this->
db->plimit($limit, $offset);
292 $result = $this->
db->query($sql);
294 $num = $this->
db->num_rows($result);
297 $obj = $this->
db->fetch_object($result);
298 $ticket_static =
new Ticket($this->
db);
299 if ($ticket_static->fetch($obj->rowid)) {
300 if ($ticket_static->fk_user_assign > 0) {
301 $userStatic =
new User($this->
db);
302 $userStatic->fetch($ticket_static->fk_user_assign);
303 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
310 throw new RestException(503,
'Error when retrieve ticket list');
312 if (!count($obj_ret)) {
313 throw new RestException(404,
'No ticket found');
324 public function post($request_data = null)
326 $ticketstatic =
new Ticket($this->
db);
327 if (!DolibarrApiAccess::$user->rights->ticket->write) {
328 throw new RestException(401);
331 $result = $this->
_validate($request_data);
333 foreach ($request_data as $field => $value) {
334 $this->ticket->$field = $value;
336 if (empty($this->ticket->ref)) {
337 $this->ticket->ref = $ticketstatic->getDefaultRef();
339 if (empty($this->ticket->track_id)) {
343 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
344 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
347 return $this->ticket->id;
359 $ticketstatic =
new Ticket($this->
db);
360 if (!DolibarrApiAccess::$user->rights->ticket->write) {
361 throw new RestException(401);
366 foreach ($request_data as $field => $value) {
367 $this->ticket->$field = $value;
369 $ticketMessageText = $this->ticket->message;
370 $result = $this->ticket->fetch(
'',
'', $this->ticket->track_id);
372 throw new RestException(404,
'Ticket not found');
374 $this->ticket->message = $ticketMessageText;
375 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
376 throw new RestException(500);
378 return $this->ticket->id;
389 public function put($id, $request_data = null)
391 if (!DolibarrApiAccess::$user->rights->ticket->write) {
392 throw new RestException(401);
395 $result = $this->ticket->fetch($id);
397 throw new RestException(404,
'Ticket not found');
401 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
404 foreach ($request_data as $field => $value) {
405 $this->ticket->$field = $value;
408 if ($this->ticket->update($id, DolibarrApiAccess::$user)) {
409 return $this->
get($id);
422 public function delete($id)
424 if (!DolibarrApiAccess::$user->rights->ticket->delete) {
425 throw new RestException(401);
427 $result = $this->ticket->fetch($id);
429 throw new RestException(404,
'Ticket not found');
433 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
436 if (!$this->ticket->delete($id)) {
437 throw new RestException(500);
443 'message' =>
'Ticket deleted'
459 foreach (Tickets::$FIELDS as $field) {
460 if (!isset($data[$field])) {
461 throw new RestException(400,
"$field field missing");
463 $ticket[$field] = $data[$field];
479 foreach (Tickets::$FIELDS_MESSAGES as $field) {
480 if (!isset($data[$field])) {
481 throw new RestException(400,
"$field field missing");
483 $ticket[$field] = $data[$field];
501 $object = parent::_cleanObjectDatas($object);
510 "table_element_line",
517 "barcode_type_label",
518 "barcode_type_coder",
522 "fk_delivery_address",
523 "shipping_method_id",
536 "location_incoterms",
544 "cache_types_tickets",
545 "cache_category_tickets",
550 foreach ($attr2clean as $toclean) {
551 unset($object->$toclean);
555 if (isset($object->lines) && count($object->lines) > 0) {
556 $nboflines = count($object->lines);
557 for ($i = 0; $i < $nboflines; $i++) {
563 if (isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
564 foreach ($object->linkedObjects as $type_object => $linked_object) {
565 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.
postNewMessage($request_data=null)
Create ticket object.
Class to manage Dolibarr users.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
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 user access to a 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.