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;
185 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
205 public function index($socid = 0, $sortfield =
"t.rowid", $sortorder =
"ASC", $limit = 100, $page = 0, $sqlfilters =
'')
209 if (!DolibarrApiAccess::$user->rights->ticket->read) {
210 throw new RestException(403);
215 if (!$socid && DolibarrApiAccess::$user->socid) {
216 $socid = DolibarrApiAccess::$user->socid;
222 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
223 $search_sale = DolibarrApiAccess::$user->id;
226 $sql =
"SELECT t.rowid";
227 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
228 $sql .=
", sc.fk_soc, sc.fk_user";
230 $sql .=
" FROM ".MAIN_DB_PREFIX.
"ticket as t";
232 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
233 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
236 $sql .=
' WHERE t.entity IN ('.getEntity(
'ticket', 1).
')';
237 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
238 $sql .=
" AND t.fk_soc = sc.fk_soc";
241 $sql .=
" AND t.fk_soc = ".((int) $socid);
243 if ($search_sale > 0) {
244 $sql .=
" AND t.rowid = sc.fk_soc";
248 if ($search_sale > 0) {
249 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
256 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
260 $sql .= $this->
db->order($sortfield, $sortorder);
266 $offset = $limit * $page;
268 $sql .= $this->
db->plimit($limit, $offset);
271 $result = $this->
db->query($sql);
273 $num = $this->
db->num_rows($result);
276 $obj = $this->
db->fetch_object($result);
277 $ticket_static =
new Ticket($this->
db);
278 if ($ticket_static->fetch($obj->rowid)) {
279 if ($ticket_static->fk_user_assign > 0) {
280 $userStatic =
new User($this->
db);
281 $userStatic->fetch($ticket_static->fk_user_assign);
282 $ticket_static->fk_user_assign_string = $userStatic->firstname.
' '.$userStatic->lastname;
289 throw new RestException(503,
'Error when retrieve ticket list');
291 if (!count($obj_ret)) {
292 throw new RestException(404,
'No ticket found');
303 public function post($request_data =
null)
305 $ticketstatic =
new Ticket($this->
db);
306 if (!DolibarrApiAccess::$user->rights->ticket->write) {
307 throw new RestException(401);
310 $result = $this->
_validate($request_data);
312 foreach ($request_data as $field => $value) {
313 $this->ticket->$field = $value;
315 if (empty($this->ticket->ref)) {
316 $this->ticket->ref = $ticketstatic->getDefaultRef();
318 if (empty($this->ticket->track_id)) {
322 if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
323 throw new RestException(500,
"Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
326 return $this->ticket->id;
338 $ticketstatic =
new Ticket($this->
db);
339 if (!DolibarrApiAccess::$user->rights->ticket->write) {
340 throw new RestException(401);
345 foreach ($request_data as $field => $value) {
346 $this->ticket->$field = $value;
348 $ticketMessageText = $this->ticket->message;
349 $result = $this->ticket->fetch(
'',
'', $this->ticket->track_id);
351 throw new RestException(404,
'Ticket not found');
353 $this->ticket->message = $ticketMessageText;
354 if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
355 throw new RestException(500,
'Error when creating ticket');
357 return $this->ticket->id;
368 public function put($id, $request_data =
null)
370 if (!DolibarrApiAccess::$user->rights->ticket->write) {
371 throw new RestException(401);
374 $result = $this->ticket->fetch($id);
376 throw new RestException(404,
'Ticket not found');
380 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
383 foreach ($request_data as $field => $value) {
384 $this->ticket->$field = $value;
387 if ($this->ticket->update($id, DolibarrApiAccess::$user)) {
388 return $this->
get($id);
401 public function delete($id)
403 if (!DolibarrApiAccess::$user->rights->ticket->delete) {
404 throw new RestException(401);
406 $result = $this->ticket->fetch($id);
408 throw new RestException(404,
'Ticket not found');
412 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
415 if (!$this->ticket->delete($id)) {
416 throw new RestException(500,
'Error when deleting ticket');
422 'message' =>
'Ticket deleted'
438 foreach (Tickets::$FIELDS as $field) {
439 if (!isset($data[$field])) {
440 throw new RestException(400,
"$field field missing");
442 $ticket[$field] = $data[$field];
458 foreach (Tickets::$FIELDS_MESSAGES as $field) {
459 if (!isset($data[$field])) {
460 throw new RestException(400,
"$field field missing");
462 $ticket[$field] = $data[$field];
480 $object = parent::_cleanObjectDatas($object);
489 "table_element_line",
496 "barcode_type_label",
497 "barcode_type_coder",
501 "fk_delivery_address",
502 "shipping_method_id",
515 "location_incoterms",
523 "cache_types_tickets",
524 "cache_category_tickets",
529 foreach ($attr2clean as $toclean) {
530 unset($object->$toclean);
534 if (isset($object->lines) && count($object->lines) > 0) {
535 $nboflines = count($object->lines);
536 for ($i = 0; $i < $nboflines; $i++) {
542 if (isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
543 foreach ($object->linkedObjects as $type_object => $linked_object) {
544 foreach ($linked_object as $object2clean) {
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.
__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.
index($socid=0, $sortfield="t.rowid", $sortorder="ASC", $limit=100, $page=0, $sqlfilters='')
List tickets.
_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, &$error='')
forgeSQLFromUniversalSearchCriteria
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
Class to generate the form for creating a new ticket.
$conf db
API class for accounts.
generate_random_id($car=16)
Generate a random id.