api_mymodule.class.php 11 KB

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