18 use Luracast\Restler\RestException;
20require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
21require_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,
'');
127 private function getCommon($id = 0, $track_id =
'', $ref =
'')
129 if (!DolibarrApiAccess::$user->rights->ticket->read) {
130 throw new RestException(403);
134 if (($id < 0) && !$track_id && !$ref) {
135 throw new RestException(401,
'Wrong parameters');
138 $result = $this->ticket->initAsSpecimen();
140 $result = $this->ticket->fetch($id, $ref, $track_id);
143 throw new RestException(404,
'Ticket not found');
147 if ($this->ticket->fk_user_assign > 0) {
148 $userStatic =
new User($this->db);
149 $userStatic->fetch($this->ticket->fk_user_assign);
150 $this->ticket->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
155 $this->ticket->loadCacheMsgsTicket();
156 if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
157 $num = count($this->ticket->cache_msgs_ticket);
160 if ($this->ticket->cache_msgs_ticket[$i][
'fk_user_author'] > 0) {
161 $user_action =
new User($this->db);
162 $user_action->fetch($this->ticket->cache_msgs_ticket[$i][
'fk_user_author']);
167 'id' => $this->ticket->cache_msgs_ticket[$i][
'id'],
168 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i][
'fk_user_author'],
169 'fk_user_action_socid' => $user_action->socid,
170 'fk_user_action_string' =>
dolGetFirstLastname($user_action->firstname, $user_action->lastname),
171 'message' => $this->ticket->cache_msgs_ticket[$i][
'message'],
172 'datec' => $this->ticket->cache_msgs_ticket[$i][
'datec'],
173 'private' => $this->ticket->cache_msgs_ticket[$i][
'private']
177 $this->ticket->messages = $messages;
181 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
202 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
206 if (!DolibarrApiAccess::$user->rights->ticket->read) {
207 throw new RestException(403);
212 if (!$socid && DolibarrApiAccess::$user->socid) {
213 $socid = DolibarrApiAccess::$user->socid;
219 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
220 $search_sale = DolibarrApiAccess::$user->id;
223 $sql =
"SELECT t.rowid";
224 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
225 $sql .=
", sc.fk_soc, sc.fk_user";
227 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket AS t LEFT JOIN ".MAIN_DB_PREFIX.
"ticket_extrafields AS ef ON (ef.fk_object = t.rowid)";
229 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
230 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
233 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
234 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
235 $sql .=
" AND t.fk_soc = sc.fk_soc";
238 $sql .=
" AND t.fk_soc = ".((int) $socid);
240 if ($search_sale > 0) {
241 $sql .=
" AND t.rowid = sc.fk_soc";
245 if ($search_sale > 0) {
246 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
253 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
257 $sql .= $this->db->order($sortfield, $sortorder);
263 $offset = $limit * $page;
265 $sql .= $this->db->plimit($limit, $offset);
268 $result = $this->db->query($sql);
270 $num = $this->db->num_rows($result);
273 $obj = $this->db->fetch_object($result);
274 $ticket_static =
new Ticket($this->db);
275 if ($ticket_static->fetch($obj->rowid)) {
276 if ($ticket_static->fk_user_assign > 0) {
277 $userStatic =
new User($this->db);
278 $userStatic->fetch($ticket_static->fk_user_assign);
279 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
286 throw new RestException(503,
'Error when retrieve ticket list');
298 public function post($request_data =
null)
300 $ticketstatic =
new Ticket($this->db);
301 if (!DolibarrApiAccess::$user->rights->ticket->write) {
302 throw new RestException(401);
305 $result = $this->
_validate($request_data);
307 foreach ($request_data as $field => $value) {
308 if ($field ===
'caller') {
310 $this->ticket->context[
'caller'] = $request_data[
'caller'];
314 $this->ticket->$field = $value;
316 if (empty($this->ticket->ref)) {
317 $this->ticket->ref = $ticketstatic->getDefaultRef();
319 if (empty($this->ticket->track_id)) {
323 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
324 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
327 return $this->ticket->id;
339 $ticketstatic =
new Ticket($this->db);
340 if (!DolibarrApiAccess::$user->rights->ticket->write) {
341 throw new RestException(401);
346 foreach ($request_data as $field => $value) {
347 if ($field ===
'caller') {
349 $this->ticket->context[
'caller'] = $request_data[
'caller'];
353 $this->ticket->$field = $value;
355 $ticketMessageText = $this->ticket->message;
356 $result = $this->ticket->fetch(
'',
'', $this->ticket->track_id);
358 throw new RestException(404,
'Ticket not found');
360 $this->ticket->message = $ticketMessageText;
361 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
362 throw new RestException(500,
'Error when creating ticket');
364 return $this->ticket->id;
375 public function put($id, $request_data =
null)
377 if (!DolibarrApiAccess::$user->rights->ticket->write) {
378 throw new RestException(401);
381 $result = $this->ticket->fetch($id);
383 throw new RestException(404,
'Ticket not found');
387 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
390 foreach ($request_data as $field => $value) {
391 if ($field ===
'caller') {
393 $this->ticket->context[
'caller'] = $request_data[
'caller'];
397 $this->ticket->$field = $value;
400 if ($this->ticket->update(DolibarrApiAccess::$user)) {
401 return $this->
get($id);
414 public function delete($id)
416 if (!DolibarrApiAccess::$user->rights->ticket->delete) {
417 throw new RestException(401);
419 $result = $this->ticket->fetch($id);
421 throw new RestException(404,
'Ticket not found');
425 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
428 if (!$this->ticket->delete($id)) {
429 throw new RestException(500,
'Error when deleting ticket');
435 'message' =>
'Ticket deleted'
451 foreach (Tickets::$FIELDS as $field) {
452 if (!isset($data[$field])) {
453 throw new RestException(400,
"$field field missing");
455 $ticket[$field] = $data[$field];
471 foreach (Tickets::$FIELDS_MESSAGES as $field) {
472 if (!isset($data[$field])) {
473 throw new RestException(400,
"$field field missing");
475 $ticket[$field] = $data[$field];
493 $object = parent::_cleanObjectDatas($object);
502 "table_element_line",
509 "barcode_type_label",
510 "barcode_type_coder",
514 "fk_delivery_address",
515 "shipping_method_id",
528 "location_incoterms",
536 "cache_types_tickets",
537 "cache_category_tickets",
542 foreach ($attr2clean as $toclean) {
543 unset($object->$toclean);
547 if (isset($object->lines) && count($object->lines) > 0) {
548 $nboflines = count($object->lines);
549 for ($i = 0; $i < $nboflines; $i++) {
555 if (isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
556 foreach ($object->linkedObjects as $type_object => $linked_object) {
557 foreach ($linked_object as $object2clean) {
_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.
getCommon($id=0, $track_id='', $ref='')
Get properties of a Ticket object Return an array with ticket informations.
getByRef($ref)
Get properties of a Ticket object from ref.
index($socid=0, $sortfield="t.rowid", $sortorder="ASC", $limit=100, $page=0, $sqlfilters='', $properties='')
List tickets.
__construct()
Constructor.
_cleanObjectDatas($object)
Clean sensible object datas.
postNewMessage($request_data=null)
Create ticket object.
post($request_data=null)
Create ticket object.
put($id, $request_data=null)
Update ticket.
_validateMessage($data)
Validate fields before create or update object message.
getByTrackId($track_id)
Get properties of a Ticket object from track id.
_validate($data)
Validate fields before create or update object.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
Class to generate the form for creating a new ticket.
generate_random_id($car=16)
Generate a random id.