api_mymodule.class.php 12 KB

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