api_tickets.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /* Copyright (C) 2016 Jean-François Ferry <hello@librethic.io>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
  19. require_once DOL_DOCUMENT_ROOT.'/core/lib/ticket.lib.php';
  20. /**
  21. * API class for ticket object
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class Tickets extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. public static $FIELDS = array(
  32. 'subject',
  33. 'message'
  34. );
  35. /**
  36. * @var array $FIELDS_MESSAGES Mandatory fields, checked when create and update object
  37. */
  38. public static $FIELDS_MESSAGES = array(
  39. 'track_id',
  40. 'message'
  41. );
  42. /**
  43. * @var Ticket $ticket {@type Ticket}
  44. */
  45. public $ticket;
  46. /**
  47. * Constructor
  48. */
  49. public function __construct()
  50. {
  51. global $db;
  52. $this->db = $db;
  53. $this->ticket = new Ticket($this->db);
  54. }
  55. /**
  56. * Get properties of a Ticket object.
  57. *
  58. * Return an array with ticket informations
  59. *
  60. * @param int $id ID of ticket
  61. * @return array|mixed Data without useless information
  62. *
  63. * @throws RestException 401
  64. * @throws RestException 403
  65. * @throws RestException 404
  66. */
  67. public function get($id)
  68. {
  69. return $this->getCommon($id, '', '');
  70. }
  71. /**
  72. * Get properties of a Ticket object from track id
  73. *
  74. * Return an array with ticket informations
  75. *
  76. * @param string $track_id Tracking ID of ticket
  77. * @return array|mixed Data without useless information
  78. *
  79. * @url GET track_id/{track_id}
  80. *
  81. * @throws RestException 401
  82. * @throws RestException 403
  83. * @throws RestException 404
  84. */
  85. public function getByTrackId($track_id)
  86. {
  87. return $this->getCommon(0, $track_id, '');
  88. }
  89. /**
  90. * Get properties of a Ticket object from ref
  91. *
  92. * Return an array with ticket informations
  93. *
  94. * @param string $ref Reference for ticket
  95. * @return array|mixed Data without useless information
  96. *
  97. * @url GET ref/{ref}
  98. *
  99. * @throws RestException 401
  100. * @throws RestException 403
  101. * @throws RestException 404
  102. */
  103. public function getByRef($ref)
  104. {
  105. try {
  106. return $this->getCommon(0, '', $ref);
  107. } catch (Exception $e) {
  108. throw $e;
  109. }
  110. }
  111. /**
  112. * Get properties of a Ticket object
  113. * Return an array with ticket informations
  114. *
  115. * @param int $id ID of ticket
  116. * @param string $track_id Tracking ID of ticket
  117. * @param string $ref Reference for ticket
  118. * @return array|mixed Data without useless information
  119. */
  120. private function getCommon($id = 0, $track_id = '', $ref = '')
  121. {
  122. if (!DolibarrApiAccess::$user->rights->ticket->read) {
  123. throw new RestException(403);
  124. }
  125. // Check parameters
  126. if (($id < 0) && !$track_id && !$ref) {
  127. throw new RestException(401, 'Wrong parameters');
  128. }
  129. if ($id == 0) {
  130. $result = $this->ticket->initAsSpecimen();
  131. } else {
  132. $result = $this->ticket->fetch($id, $ref, $track_id);
  133. }
  134. if (!$result) {
  135. throw new RestException(404, 'Ticket not found');
  136. }
  137. // String for user assigned
  138. if ($this->ticket->fk_user_assign > 0) {
  139. $userStatic = new User($this->db);
  140. $userStatic->fetch($this->ticket->fk_user_assign);
  141. $this->ticket->fk_user_assign_string = $userStatic->firstname.' '.$userStatic->lastname;
  142. }
  143. // Messages of ticket
  144. $messages = array();
  145. $this->ticket->loadCacheMsgsTicket();
  146. if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
  147. $num = count($this->ticket->cache_msgs_ticket);
  148. $i = 0;
  149. while ($i < $num) {
  150. if ($this->ticket->cache_msgs_ticket[$i]['fk_user_author'] > 0) {
  151. $user_action = new User($this->db);
  152. $user_action->fetch($this->ticket->cache_msgs_ticket[$i]['fk_user_author']);
  153. }
  154. // Now define messages
  155. $messages[] = array(
  156. 'id' => $this->ticket->cache_msgs_ticket[$i]['id'],
  157. 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i]['fk_user_author'],
  158. 'fk_user_action_socid' => $user_action->socid,
  159. 'fk_user_action_string' => dolGetFirstLastname($user_action->firstname, $user_action->lastname),
  160. 'message' => $this->ticket->cache_msgs_ticket[$i]['message'],
  161. 'datec' => $this->ticket->cache_msgs_ticket[$i]['datec'],
  162. 'private' => $this->ticket->cache_msgs_ticket[$i]['private']
  163. );
  164. $i++;
  165. }
  166. $this->ticket->messages = $messages;
  167. }
  168. if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
  169. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  170. }
  171. return $this->_cleanObjectDatas($this->ticket);
  172. }
  173. /**
  174. * List tickets
  175. *
  176. * Get a list of tickets
  177. *
  178. * @param int $socid Filter list with thirdparty ID
  179. * @param string $sortfield Sort field
  180. * @param string $sortorder Sort order
  181. * @param int $limit Limit for list
  182. * @param int $page Page number
  183. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101') and (t.fk_statut:=:1)"
  184. *
  185. * @return array Array of ticket objects
  186. *
  187. */
  188. public function index($socid = 0, $sortfield = "t.rowid", $sortorder = "ASC", $limit = 100, $page = 0, $sqlfilters = '')
  189. {
  190. global $db, $conf;
  191. if (!DolibarrApiAccess::$user->rights->ticket->read) {
  192. throw new RestException(403);
  193. }
  194. $obj_ret = array();
  195. if (!$socid && DolibarrApiAccess::$user->socid) {
  196. $socid = DolibarrApiAccess::$user->socid;
  197. }
  198. $search_sale = null;
  199. // If the internal user must only see his customers, force searching by him
  200. $search_sale = 0;
  201. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  202. $search_sale = DolibarrApiAccess::$user->id;
  203. }
  204. $sql = "SELECT t.rowid";
  205. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  206. $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  207. }
  208. $sql .= " FROM ".MAIN_DB_PREFIX."ticket as t";
  209. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  210. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  211. }
  212. $sql .= ' WHERE t.entity IN ('.getEntity('ticket', 1).')';
  213. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  214. $sql .= " AND t.fk_soc = sc.fk_soc";
  215. }
  216. if ($socid > 0) {
  217. $sql .= " AND t.fk_soc = ".((int) $socid);
  218. }
  219. if ($search_sale > 0) {
  220. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  221. }
  222. // Insert sale filter
  223. if ($search_sale > 0) {
  224. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  225. }
  226. // Add sql filters
  227. if ($sqlfilters) {
  228. $errormessage = '';
  229. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  230. if ($errormessage) {
  231. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  232. }
  233. }
  234. $sql .= $this->db->order($sortfield, $sortorder);
  235. if ($limit) {
  236. if ($page < 0) {
  237. $page = 0;
  238. }
  239. $offset = $limit * $page;
  240. $sql .= $this->db->plimit($limit, $offset);
  241. }
  242. $result = $this->db->query($sql);
  243. if ($result) {
  244. $num = $this->db->num_rows($result);
  245. $i = 0;
  246. while ($i < $num) {
  247. $obj = $this->db->fetch_object($result);
  248. $ticket_static = new Ticket($this->db);
  249. if ($ticket_static->fetch($obj->rowid)) {
  250. if ($ticket_static->fk_user_assign > 0) {
  251. $userStatic = new User($this->db);
  252. $userStatic->fetch($ticket_static->fk_user_assign);
  253. $ticket_static->fk_user_assign_string = $userStatic->firstname.' '.$userStatic->lastname;
  254. }
  255. $obj_ret[] = $this->_cleanObjectDatas($ticket_static);
  256. }
  257. $i++;
  258. }
  259. } else {
  260. throw new RestException(503, 'Error when retrieve ticket list');
  261. }
  262. if (!count($obj_ret)) {
  263. throw new RestException(404, 'No ticket found');
  264. }
  265. return $obj_ret;
  266. }
  267. /**
  268. * Create ticket object
  269. *
  270. * @param array $request_data Request datas
  271. * @return int ID of ticket
  272. */
  273. public function post($request_data = null)
  274. {
  275. $ticketstatic = new Ticket($this->db);
  276. if (!DolibarrApiAccess::$user->rights->ticket->write) {
  277. throw new RestException(401);
  278. }
  279. // Check mandatory fields
  280. $result = $this->_validate($request_data);
  281. foreach ($request_data as $field => $value) {
  282. $this->ticket->$field = $value;
  283. }
  284. if (empty($this->ticket->ref)) {
  285. $this->ticket->ref = $ticketstatic->getDefaultRef();
  286. }
  287. if (empty($this->ticket->track_id)) {
  288. $this->ticket->track_id = generate_random_id(16);
  289. }
  290. if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
  291. throw new RestException(500, "Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
  292. }
  293. return $this->ticket->id;
  294. }
  295. /**
  296. * Create ticket object
  297. *
  298. * @param array $request_data Request datas
  299. * @return int ID of ticket
  300. *
  301. */
  302. public function postNewMessage($request_data = null)
  303. {
  304. $ticketstatic = new Ticket($this->db);
  305. if (!DolibarrApiAccess::$user->rights->ticket->write) {
  306. throw new RestException(401);
  307. }
  308. // Check mandatory fields
  309. $result = $this->_validateMessage($request_data);
  310. foreach ($request_data as $field => $value) {
  311. $this->ticket->$field = $value;
  312. }
  313. $ticketMessageText = $this->ticket->message;
  314. $result = $this->ticket->fetch('', '', $this->ticket->track_id);
  315. if (!$result) {
  316. throw new RestException(404, 'Ticket not found');
  317. }
  318. $this->ticket->message = $ticketMessageText;
  319. if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
  320. throw new RestException(500, 'Error when creating ticket');
  321. }
  322. return $this->ticket->id;
  323. }
  324. /**
  325. * Update ticket
  326. *
  327. * @param int $id Id of ticket to update
  328. * @param array $request_data Datas
  329. * @return int
  330. *
  331. */
  332. public function put($id, $request_data = null)
  333. {
  334. if (!DolibarrApiAccess::$user->rights->ticket->write) {
  335. throw new RestException(401);
  336. }
  337. $result = $this->ticket->fetch($id);
  338. if (!$result) {
  339. throw new RestException(404, 'Ticket not found');
  340. }
  341. if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
  342. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  343. }
  344. foreach ($request_data as $field => $value) {
  345. $this->ticket->$field = $value;
  346. }
  347. if ($this->ticket->update($id, DolibarrApiAccess::$user)) {
  348. return $this->get($id);
  349. }
  350. return false;
  351. }
  352. /**
  353. * Delete ticket
  354. *
  355. * @param int $id Ticket ID
  356. * @return array
  357. *
  358. */
  359. public function delete($id)
  360. {
  361. if (!DolibarrApiAccess::$user->rights->ticket->delete) {
  362. throw new RestException(401);
  363. }
  364. $result = $this->ticket->fetch($id);
  365. if (!$result) {
  366. throw new RestException(404, 'Ticket not found');
  367. }
  368. if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
  369. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  370. }
  371. if (!$this->ticket->delete($id)) {
  372. throw new RestException(500, 'Error when deleting ticket');
  373. }
  374. return array(
  375. 'success' => array(
  376. 'code' => 200,
  377. 'message' => 'Ticket deleted'
  378. )
  379. );
  380. }
  381. /**
  382. * Validate fields before create or update object
  383. *
  384. * @param array $data Data to validate
  385. * @return array
  386. *
  387. * @throws RestException
  388. */
  389. private function _validate($data)
  390. {
  391. $ticket = array();
  392. foreach (Tickets::$FIELDS as $field) {
  393. if (!isset($data[$field])) {
  394. throw new RestException(400, "$field field missing");
  395. }
  396. $ticket[$field] = $data[$field];
  397. }
  398. return $ticket;
  399. }
  400. /**
  401. * Validate fields before create or update object message
  402. *
  403. * @param array $data Data to validate
  404. * @return array
  405. *
  406. * @throws RestException
  407. */
  408. private function _validateMessage($data)
  409. {
  410. $ticket = array();
  411. foreach (Tickets::$FIELDS_MESSAGES as $field) {
  412. if (!isset($data[$field])) {
  413. throw new RestException(400, "$field field missing");
  414. }
  415. $ticket[$field] = $data[$field];
  416. }
  417. return $ticket;
  418. }
  419. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  420. /**
  421. * Clean sensible object datas
  422. *
  423. * @param Object $object Object to clean
  424. * @return Object Object with cleaned properties
  425. *
  426. * @todo use an array for properties to clean
  427. *
  428. */
  429. protected function _cleanObjectDatas($object)
  430. {
  431. // phpcs:enable
  432. $object = parent::_cleanObjectDatas($object);
  433. // Other attributes to clean
  434. $attr2clean = array(
  435. "contact",
  436. "contact_id",
  437. "ref_previous",
  438. "ref_next",
  439. "ref_ext",
  440. "table_element_line",
  441. "statut",
  442. "country",
  443. "country_id",
  444. "country_code",
  445. "barcode_type",
  446. "barcode_type_code",
  447. "barcode_type_label",
  448. "barcode_type_coder",
  449. "mode_reglement_id",
  450. "cond_reglement_id",
  451. "cond_reglement",
  452. "fk_delivery_address",
  453. "shipping_method_id",
  454. "modelpdf",
  455. "fk_account",
  456. "note_public",
  457. "note_private",
  458. "note",
  459. "total_ht",
  460. "total_tva",
  461. "total_localtax1",
  462. "total_localtax2",
  463. "total_ttc",
  464. "fk_incoterms",
  465. "label_incoterms",
  466. "location_incoterms",
  467. "name",
  468. "lastname",
  469. "firstname",
  470. "civility_id",
  471. "canvas",
  472. "cache_msgs_ticket",
  473. "cache_logs_ticket",
  474. "cache_types_tickets",
  475. "cache_category_tickets",
  476. "regeximgext",
  477. "statuts_short",
  478. "statuts"
  479. );
  480. foreach ($attr2clean as $toclean) {
  481. unset($object->$toclean);
  482. }
  483. // If object has lines, remove $db property
  484. if (isset($object->lines) && count($object->lines) > 0) {
  485. $nboflines = count($object->lines);
  486. for ($i = 0; $i < $nboflines; $i++) {
  487. $this->_cleanObjectDatas($object->lines[$i]);
  488. }
  489. }
  490. // If object has linked objects, remove $db property
  491. if (isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
  492. foreach ($object->linkedObjects as $type_object => $linked_object) {
  493. foreach ($linked_object as $object2clean) {
  494. $this->_cleanObjectDatas($object2clean);
  495. }
  496. }
  497. }
  498. return $object;
  499. }
  500. }