api_ticketsups.class.php 18 KB

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