api_tickets.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 Object Object with cleaned properties
  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. return $this->getCommon(0, '', $ref);
  106. }
  107. /**
  108. * Get properties of a Ticket object
  109. * Return an array with ticket informations
  110. *
  111. * @param int $id ID of ticket
  112. * @param string $track_id Tracking ID of ticket
  113. * @param string $ref Reference for ticket
  114. * @return array|mixed Data without useless information
  115. */
  116. private function getCommon($id = 0, $track_id = '', $ref = '')
  117. {
  118. if (!DolibarrApiAccess::$user->rights->ticket->read) {
  119. throw new RestException(403);
  120. }
  121. // Check parameters
  122. if (($id < 0) && !$track_id && !$ref) {
  123. throw new RestException(401, 'Wrong parameters');
  124. }
  125. if ($id == 0) {
  126. $result = $this->ticket->initAsSpecimen();
  127. } else {
  128. $result = $this->ticket->fetch($id, $ref, $track_id);
  129. }
  130. if (!$result) {
  131. throw new RestException(404, 'Ticket not found');
  132. }
  133. // String for user assigned
  134. if ($this->ticket->fk_user_assign > 0) {
  135. $userStatic = new User($this->db);
  136. $userStatic->fetch($this->ticket->fk_user_assign);
  137. $this->ticket->fk_user_assign_string = $userStatic->firstname.' '.$userStatic->lastname;
  138. }
  139. // Messages of ticket
  140. $messages = array();
  141. $this->ticket->loadCacheMsgsTicket();
  142. if (is_array($this->ticket->cache_msgs_ticket) && count($this->ticket->cache_msgs_ticket) > 0) {
  143. $num = count($this->ticket->cache_msgs_ticket);
  144. $i = 0;
  145. while ($i < $num) {
  146. if ($this->ticket->cache_msgs_ticket[$i]['fk_user_author'] > 0) {
  147. $user_action = new User($this->db);
  148. $user_action->fetch($this->ticket->cache_msgs_ticket[$i]['fk_user_author']);
  149. }
  150. // Now define messages
  151. $messages[] = array(
  152. 'id' => $this->ticket->cache_msgs_ticket[$i]['id'],
  153. 'fk_user_action' => $this->ticket->cache_msgs_ticket[$i]['fk_user_author'],
  154. 'fk_user_action_socid' => $user_action->socid,
  155. 'fk_user_action_string' => dolGetFirstLastname($user_action->firstname, $user_action->lastname),
  156. 'message' => $this->ticket->cache_msgs_ticket[$i]['message'],
  157. 'datec' => $this->ticket->cache_msgs_ticket[$i]['datec'],
  158. 'private' => $this->ticket->cache_msgs_ticket[$i]['private']
  159. );
  160. $i++;
  161. }
  162. $this->ticket->messages = $messages;
  163. }
  164. if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
  165. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  166. }
  167. return $this->_cleanObjectDatas($this->ticket);
  168. }
  169. /**
  170. * List tickets
  171. *
  172. * Get a list of tickets
  173. *
  174. * @param int $socid Filter list with thirdparty ID
  175. * @param string $sortfield Sort field
  176. * @param string $sortorder Sort order
  177. * @param int $limit Limit for list
  178. * @param int $page Page number
  179. * @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)"
  180. *
  181. * @return array Array of ticket objects
  182. *
  183. */
  184. public function index($socid = 0, $sortfield = "t.rowid", $sortorder = "ASC", $limit = 100, $page = 0, $sqlfilters = '')
  185. {
  186. global $db, $conf;
  187. if (!DolibarrApiAccess::$user->rights->ticket->read) {
  188. throw new RestException(403);
  189. }
  190. $obj_ret = array();
  191. if (!$socid && DolibarrApiAccess::$user->socid) {
  192. $socid = DolibarrApiAccess::$user->socid;
  193. }
  194. $search_sale = null;
  195. // If the internal user must only see his customers, force searching by him
  196. $search_sale = 0;
  197. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  198. $search_sale = DolibarrApiAccess::$user->id;
  199. }
  200. $sql = "SELECT t.rowid";
  201. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  202. $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)
  203. }
  204. $sql .= " FROM ".MAIN_DB_PREFIX."ticket AS t LEFT JOIN ".MAIN_DB_PREFIX."ticket_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
  205. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  206. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  207. }
  208. $sql .= ' WHERE t.entity IN ('.getEntity('ticket', 1).')';
  209. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  210. $sql .= " AND t.fk_soc = sc.fk_soc";
  211. }
  212. if ($socid > 0) {
  213. $sql .= " AND t.fk_soc = ".((int) $socid);
  214. }
  215. if ($search_sale > 0) {
  216. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  217. }
  218. // Insert sale filter
  219. if ($search_sale > 0) {
  220. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  221. }
  222. // Add sql filters
  223. if ($sqlfilters) {
  224. $errormessage = '';
  225. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  226. if ($errormessage) {
  227. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  228. }
  229. }
  230. $sql .= $this->db->order($sortfield, $sortorder);
  231. if ($limit) {
  232. if ($page < 0) {
  233. $page = 0;
  234. }
  235. $offset = $limit * $page;
  236. $sql .= $this->db->plimit($limit, $offset);
  237. }
  238. $result = $this->db->query($sql);
  239. if ($result) {
  240. $num = $this->db->num_rows($result);
  241. $i = 0;
  242. while ($i < $num) {
  243. $obj = $this->db->fetch_object($result);
  244. $ticket_static = new Ticket($this->db);
  245. if ($ticket_static->fetch($obj->rowid)) {
  246. if ($ticket_static->fk_user_assign > 0) {
  247. $userStatic = new User($this->db);
  248. $userStatic->fetch($ticket_static->fk_user_assign);
  249. $ticket_static->fk_user_assign_string = $userStatic->firstname.' '.$userStatic->lastname;
  250. }
  251. $obj_ret[] = $this->_cleanObjectDatas($ticket_static);
  252. }
  253. $i++;
  254. }
  255. } else {
  256. throw new RestException(503, 'Error when retrieve ticket list');
  257. }
  258. if (!count($obj_ret)) {
  259. throw new RestException(404, 'No ticket found');
  260. }
  261. return $obj_ret;
  262. }
  263. /**
  264. * Create ticket object
  265. *
  266. * @param array $request_data Request datas
  267. * @return int ID of ticket
  268. */
  269. public function post($request_data = null)
  270. {
  271. $ticketstatic = new Ticket($this->db);
  272. if (!DolibarrApiAccess::$user->rights->ticket->write) {
  273. throw new RestException(401);
  274. }
  275. // Check mandatory fields
  276. $result = $this->_validate($request_data);
  277. foreach ($request_data as $field => $value) {
  278. $this->ticket->$field = $value;
  279. }
  280. if (empty($this->ticket->ref)) {
  281. $this->ticket->ref = $ticketstatic->getDefaultRef();
  282. }
  283. if (empty($this->ticket->track_id)) {
  284. $this->ticket->track_id = generate_random_id(16);
  285. }
  286. if ($this->ticket->create(DolibarrApiAccess::$user) < 0) {
  287. throw new RestException(500, "Error creating ticket", array_merge(array($this->ticket->error), $this->ticket->errors));
  288. }
  289. return $this->ticket->id;
  290. }
  291. /**
  292. * Create ticket object
  293. *
  294. * @param array $request_data Request datas
  295. * @return int ID of ticket
  296. *
  297. */
  298. public function postNewMessage($request_data = null)
  299. {
  300. $ticketstatic = new Ticket($this->db);
  301. if (!DolibarrApiAccess::$user->rights->ticket->write) {
  302. throw new RestException(401);
  303. }
  304. // Check mandatory fields
  305. $result = $this->_validateMessage($request_data);
  306. foreach ($request_data as $field => $value) {
  307. $this->ticket->$field = $value;
  308. }
  309. $ticketMessageText = $this->ticket->message;
  310. $result = $this->ticket->fetch('', '', $this->ticket->track_id);
  311. if (!$result) {
  312. throw new RestException(404, 'Ticket not found');
  313. }
  314. $this->ticket->message = $ticketMessageText;
  315. if (!$this->ticket->createTicketMessage(DolibarrApiAccess::$user)) {
  316. throw new RestException(500, 'Error when creating ticket');
  317. }
  318. return $this->ticket->id;
  319. }
  320. /**
  321. * Update ticket
  322. *
  323. * @param int $id Id of ticket to update
  324. * @param array $request_data Datas
  325. * @return int
  326. *
  327. */
  328. public function put($id, $request_data = null)
  329. {
  330. if (!DolibarrApiAccess::$user->rights->ticket->write) {
  331. throw new RestException(401);
  332. }
  333. $result = $this->ticket->fetch($id);
  334. if (!$result) {
  335. throw new RestException(404, 'Ticket not found');
  336. }
  337. if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
  338. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  339. }
  340. foreach ($request_data as $field => $value) {
  341. $this->ticket->$field = $value;
  342. }
  343. if ($this->ticket->update($id, DolibarrApiAccess::$user)) {
  344. return $this->get($id);
  345. }
  346. return false;
  347. }
  348. /**
  349. * Delete ticket
  350. *
  351. * @param int $id Ticket ID
  352. * @return array
  353. *
  354. */
  355. public function delete($id)
  356. {
  357. if (!DolibarrApiAccess::$user->rights->ticket->delete) {
  358. throw new RestException(401);
  359. }
  360. $result = $this->ticket->fetch($id);
  361. if (!$result) {
  362. throw new RestException(404, 'Ticket not found');
  363. }
  364. if (!DolibarrApi::_checkAccessToResource('ticket', $this->ticket->id)) {
  365. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  366. }
  367. if (!$this->ticket->delete($id)) {
  368. throw new RestException(500, 'Error when deleting ticket');
  369. }
  370. return array(
  371. 'success' => array(
  372. 'code' => 200,
  373. 'message' => 'Ticket deleted'
  374. )
  375. );
  376. }
  377. /**
  378. * Validate fields before create or update object
  379. *
  380. * @param array $data Data to validate
  381. * @return array
  382. *
  383. * @throws RestException
  384. */
  385. private function _validate($data)
  386. {
  387. $ticket = array();
  388. foreach (Tickets::$FIELDS as $field) {
  389. if (!isset($data[$field])) {
  390. throw new RestException(400, "$field field missing");
  391. }
  392. $ticket[$field] = $data[$field];
  393. }
  394. return $ticket;
  395. }
  396. /**
  397. * Validate fields before create or update object message
  398. *
  399. * @param array $data Data to validate
  400. * @return array
  401. *
  402. * @throws RestException
  403. */
  404. private function _validateMessage($data)
  405. {
  406. $ticket = array();
  407. foreach (Tickets::$FIELDS_MESSAGES as $field) {
  408. if (!isset($data[$field])) {
  409. throw new RestException(400, "$field field missing");
  410. }
  411. $ticket[$field] = $data[$field];
  412. }
  413. return $ticket;
  414. }
  415. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  416. /**
  417. * Clean sensible object datas
  418. *
  419. * @param Object $object Object to clean
  420. * @return Object Object with cleaned properties
  421. *
  422. * @todo use an array for properties to clean
  423. *
  424. */
  425. protected function _cleanObjectDatas($object)
  426. {
  427. // phpcs:enable
  428. $object = parent::_cleanObjectDatas($object);
  429. // Other attributes to clean
  430. $attr2clean = array(
  431. "contact",
  432. "contact_id",
  433. "ref_previous",
  434. "ref_next",
  435. "ref_ext",
  436. "table_element_line",
  437. "statut",
  438. "country",
  439. "country_id",
  440. "country_code",
  441. "barcode_type",
  442. "barcode_type_code",
  443. "barcode_type_label",
  444. "barcode_type_coder",
  445. "mode_reglement_id",
  446. "cond_reglement_id",
  447. "cond_reglement",
  448. "fk_delivery_address",
  449. "shipping_method_id",
  450. "modelpdf",
  451. "fk_account",
  452. "note_public",
  453. "note_private",
  454. "note",
  455. "total_ht",
  456. "total_tva",
  457. "total_localtax1",
  458. "total_localtax2",
  459. "total_ttc",
  460. "fk_incoterms",
  461. "label_incoterms",
  462. "location_incoterms",
  463. "name",
  464. "lastname",
  465. "firstname",
  466. "civility_id",
  467. "canvas",
  468. "cache_msgs_ticket",
  469. "cache_logs_ticket",
  470. "cache_types_tickets",
  471. "cache_category_tickets",
  472. "regeximgext",
  473. "statuts_short",
  474. "statuts"
  475. );
  476. foreach ($attr2clean as $toclean) {
  477. unset($object->$toclean);
  478. }
  479. // If object has lines, remove $db property
  480. if (isset($object->lines) && count($object->lines) > 0) {
  481. $nboflines = count($object->lines);
  482. for ($i = 0; $i < $nboflines; $i++) {
  483. $this->_cleanObjectDatas($object->lines[$i]);
  484. }
  485. }
  486. // If object has linked objects, remove $db property
  487. if (isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
  488. foreach ($object->linkedObjects as $type_object => $linked_object) {
  489. foreach ($linked_object as $object2clean) {
  490. $this->_cleanObjectDatas($object2clean);
  491. }
  492. }
  493. }
  494. return $object;
  495. }
  496. }