api_nav.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2023 Deák Ferenc
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. dol_include_once('/nav/class/action.class.php');
  20. /**
  21. * \file nav/class/api_nav.class.php
  22. * \ingroup nav
  23. * \brief File for API management of action.
  24. */
  25. /**
  26. * API class for nav action
  27. *
  28. * @access protected
  29. * @class DolibarrApiAccess {@requires user,external}
  30. */
  31. class NavApi extends DolibarrApi
  32. {
  33. /**
  34. * @var Action $action {@type Action}
  35. */
  36. public $action;
  37. /**
  38. * Constructor
  39. *
  40. * @url GET /
  41. *
  42. */
  43. public function __construct()
  44. {
  45. global $db;
  46. $this->db = $db;
  47. $this->action = new Action($this->db);
  48. }
  49. /**
  50. * ...
  51. *
  52. * Return an array with product information.
  53. *
  54. *
  55. * @param int $limit ...
  56. * @return array|mixed Data without useless information
  57. *
  58. * @url GET syncNAV
  59. *
  60. * @throws RestException 401
  61. * @throws RestException 403
  62. * @throws RestException 404
  63. */
  64. public function syncNAV($limit=100)
  65. {
  66. require_once(DOL_DOCUMENT_ROOT.'/custom/nav/class/nav.class.php');
  67. $nav = new Nav($this->db);
  68. $nav->syncNAV($limit);
  69. return [$nav->output,$nav->error];
  70. }
  71. /**
  72. * Get properties of a action object
  73. *
  74. * Return an array with action informations
  75. *
  76. * @param int $id ID of action
  77. * @return array|mixed data without useless information
  78. *
  79. * @url GET actions/{id}
  80. *
  81. * @throws RestException 401 Not allowed
  82. * @throws RestException 404 Not found
  83. */
  84. public function get($id)
  85. {
  86. if (!DolibarrApiAccess::$user->rights->nav->action->read) {
  87. throw new RestException(401);
  88. }
  89. $result = $this->action->fetch($id);
  90. if (!$result) {
  91. throw new RestException(404, 'Action not found');
  92. }
  93. if (!DolibarrApi::_checkAccessToResource('action', $this->action->id, 'nav_action')) {
  94. throw new RestException(401, 'Access to instance id='.$this->action->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  95. }
  96. return $this->_cleanObjectDatas($this->action);
  97. }
  98. /**
  99. * List actions
  100. *
  101. * Get a list of actions
  102. *
  103. * @param string $sortfield Sort field
  104. * @param string $sortorder Sort order
  105. * @param int $limit Limit for list
  106. * @param int $page Page number
  107. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  108. * @return array Array of order objects
  109. *
  110. * @throws RestException
  111. *
  112. * @url GET /actions/
  113. */
  114. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
  115. {
  116. global $db, $conf;
  117. $obj_ret = array();
  118. $tmpobject = new Action($this->db);
  119. if (!DolibarrApiAccess::$user->rights->nav->action->read) {
  120. throw new RestException(401);
  121. }
  122. $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
  123. $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
  124. // If the internal user must only see his customers, force searching by him
  125. $search_sale = 0;
  126. if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  127. $search_sale = DolibarrApiAccess::$user->id;
  128. }
  129. $sql = "SELECT t.rowid";
  130. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  131. $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)
  132. }
  133. $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
  134. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  135. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  136. }
  137. $sql .= " WHERE 1 = 1";
  138. // Example of use $mode
  139. //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
  140. //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
  141. if ($tmpobject->ismultientitymanaged) {
  142. $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
  143. }
  144. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  145. $sql .= " AND t.fk_soc = sc.fk_soc";
  146. }
  147. if ($restrictonsocid && $socid) {
  148. $sql .= " AND t.fk_soc = ".((int) $socid);
  149. }
  150. if ($restrictonsocid && $search_sale > 0) {
  151. $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  152. }
  153. // Insert sale filter
  154. if ($restrictonsocid && $search_sale > 0) {
  155. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  156. }
  157. if ($sqlfilters) {
  158. $errormessage = '';
  159. if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
  160. throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
  161. }
  162. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
  163. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  164. }
  165. $sql .= $this->db->order($sortfield, $sortorder);
  166. if ($limit) {
  167. if ($page < 0) {
  168. $page = 0;
  169. }
  170. $offset = $limit * $page;
  171. $sql .= $this->db->plimit($limit + 1, $offset);
  172. }
  173. $result = $this->db->query($sql);
  174. $i = 0;
  175. if ($result) {
  176. $num = $this->db->num_rows($result);
  177. while ($i < $num) {
  178. $obj = $this->db->fetch_object($result);
  179. $tmp_object = new Action($this->db);
  180. if ($tmp_object->fetch($obj->rowid)) {
  181. $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
  182. }
  183. $i++;
  184. }
  185. } else {
  186. throw new RestException(503, 'Error when retrieving action list: '.$this->db->lasterror());
  187. }
  188. if (!count($obj_ret)) {
  189. throw new RestException(404, 'No action found');
  190. }
  191. return $obj_ret;
  192. }
  193. /**
  194. * Create action object
  195. *
  196. * @param array $request_data Request datas
  197. * @return int ID of action
  198. *
  199. * @throws RestException
  200. *
  201. * @url POST actions/
  202. */
  203. public function post($request_data = null)
  204. {
  205. if (!DolibarrApiAccess::$user->rights->nav->action->write) {
  206. throw new RestException(401);
  207. }
  208. // Check mandatory fields
  209. $result = $this->_validate($request_data);
  210. foreach ($request_data as $field => $value) {
  211. $this->action->$field = $this->_checkValForAPI($field, $value, $this->action);
  212. }
  213. // Clean data
  214. // $this->action->abc = sanitizeVal($this->action->abc, 'alphanohtml');
  215. if ($this->action->create(DolibarrApiAccess::$user)<0) {
  216. throw new RestException(500, "Error creating Action", array_merge(array($this->action->error), $this->action->errors));
  217. }
  218. return $this->action->id;
  219. }
  220. /**
  221. * Update action
  222. *
  223. * @param int $id Id of action to update
  224. * @param array $request_data Datas
  225. * @return int
  226. *
  227. * @throws RestException
  228. *
  229. * @url PUT actions/{id}
  230. */
  231. public function put($id, $request_data = null)
  232. {
  233. if (!DolibarrApiAccess::$user->rights->nav->action->write) {
  234. throw new RestException(401);
  235. }
  236. $result = $this->action->fetch($id);
  237. if (!$result) {
  238. throw new RestException(404, 'Action not found');
  239. }
  240. if (!DolibarrApi::_checkAccessToResource('action', $this->action->id, 'nav_action')) {
  241. throw new RestException(401, 'Access to instance id='.$this->action->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  242. }
  243. foreach ($request_data as $field => $value) {
  244. if ($field == 'id') {
  245. continue;
  246. }
  247. $this->action->$field = $this->_checkValForAPI($field, $value, $this->action);
  248. }
  249. // Clean data
  250. // $this->action->abc = sanitizeVal($this->action->abc, 'alphanohtml');
  251. if ($this->action->update(DolibarrApiAccess::$user, false) > 0) {
  252. return $this->get($id);
  253. } else {
  254. throw new RestException(500, $this->action->error);
  255. }
  256. }
  257. /**
  258. * Delete action
  259. *
  260. * @param int $id Action ID
  261. * @return array
  262. *
  263. * @throws RestException
  264. *
  265. * @url DELETE actions/{id}
  266. */
  267. public function delete($id)
  268. {
  269. if (!DolibarrApiAccess::$user->rights->nav->action->delete) {
  270. throw new RestException(401);
  271. }
  272. $result = $this->action->fetch($id);
  273. if (!$result) {
  274. throw new RestException(404, 'Action not found');
  275. }
  276. if (!DolibarrApi::_checkAccessToResource('action', $this->action->id, 'nav_action')) {
  277. throw new RestException(401, 'Access to instance id='.$this->action->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
  278. }
  279. if (!$this->action->delete(DolibarrApiAccess::$user)) {
  280. throw new RestException(500, 'Error when deleting Action : '.$this->action->error);
  281. }
  282. return array(
  283. 'success' => array(
  284. 'code' => 200,
  285. 'message' => 'Action deleted'
  286. )
  287. );
  288. }
  289. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  290. /**
  291. * Clean sensible object datas
  292. *
  293. * @param Object $object Object to clean
  294. * @return Object Object with cleaned properties
  295. */
  296. protected function _cleanObjectDatas($object)
  297. {
  298. // phpcs:enable
  299. $object = parent::_cleanObjectDatas($object);
  300. unset($object->rowid);
  301. unset($object->canvas);
  302. /*unset($object->name);
  303. unset($object->lastname);
  304. unset($object->firstname);
  305. unset($object->civility_id);
  306. unset($object->statut);
  307. unset($object->state);
  308. unset($object->state_id);
  309. unset($object->state_code);
  310. unset($object->region);
  311. unset($object->region_code);
  312. unset($object->country);
  313. unset($object->country_id);
  314. unset($object->country_code);
  315. unset($object->barcode_type);
  316. unset($object->barcode_type_code);
  317. unset($object->barcode_type_label);
  318. unset($object->barcode_type_coder);
  319. unset($object->total_ht);
  320. unset($object->total_tva);
  321. unset($object->total_localtax1);
  322. unset($object->total_localtax2);
  323. unset($object->total_ttc);
  324. unset($object->fk_account);
  325. unset($object->comments);
  326. unset($object->note);
  327. unset($object->mode_reglement_id);
  328. unset($object->cond_reglement_id);
  329. unset($object->cond_reglement);
  330. unset($object->shipping_method_id);
  331. unset($object->fk_incoterms);
  332. unset($object->label_incoterms);
  333. unset($object->location_incoterms);
  334. */
  335. // If object has lines, remove $db property
  336. if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
  337. $nboflines = count($object->lines);
  338. for ($i = 0; $i < $nboflines; $i++) {
  339. $this->_cleanObjectDatas($object->lines[$i]);
  340. unset($object->lines[$i]->lines);
  341. unset($object->lines[$i]->note);
  342. }
  343. }
  344. return $object;
  345. }
  346. /**
  347. * Validate fields before create or update object
  348. *
  349. * @param array $data Array of data to validate
  350. * @return array
  351. *
  352. * @throws RestException
  353. */
  354. private function _validate($data)
  355. {
  356. $action = array();
  357. foreach ($this->action->fields as $field => $propfield) {
  358. if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
  359. continue; // Not a mandatory field
  360. }
  361. if (!isset($data[$field])) {
  362. throw new RestException(400, "$field field missing");
  363. }
  364. $action[$field] = $data[$field];
  365. }
  366. return $action;
  367. }
  368. }