api_multicompany.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /* Copyright (C) 2017-2021 Regis Houssin <regis.houssin@inodbox.com>
  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. dol_include_once('/multicompany/class/dao_multicompany.class.php', 'DaoMulticompany');
  19. /**
  20. * API class for multicompany
  21. *
  22. * @access protected
  23. * @class DolibarrApiAccess {@requires user,external}
  24. */
  25. class Multicompany extends DolibarrApi
  26. {
  27. /**
  28. * @var array $FIELDS Mandatory fields, checked when create and update object
  29. */
  30. static $FIELDS = array(
  31. 'label'
  32. );
  33. /**
  34. * Constructor
  35. */
  36. function __construct()
  37. {
  38. global $db, $conf;
  39. $this->db = $db;
  40. }
  41. /**
  42. * Get properties of an entity
  43. *
  44. * Return an array with entity informations
  45. *
  46. * @param int $id ID of entity
  47. * @return array|mixed data without useless information
  48. *
  49. * @throws RestException
  50. */
  51. function get($id)
  52. {
  53. if (! DolibarrApiAccess::$user->rights->multicompany->read) {
  54. throw new RestException(401);
  55. }
  56. // The DaoMulticompany::fetch() method uses the global variable $user.
  57. global $user;
  58. $user = DolibarrApiAccess::$user;
  59. $multicompany = new DaoMulticompany($this->db);
  60. $result = $multicompany->fetch($id);
  61. if (empty($result)) {
  62. throw new RestException(404, 'entity not found');
  63. }
  64. return $this->_cleanObjectDatas($multicompany);
  65. }
  66. /**
  67. * List entities
  68. *
  69. * Get a list of entities
  70. *
  71. * @param string $sortfield Sort field
  72. * @param string $sortorder Sort order
  73. * @param int $limit Limit for list
  74. * @param int $page Page number
  75. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'SO-%') and (t.visible:=:'1')"
  76. * @return array Array of entities objects
  77. *
  78. * @throws RestException
  79. */
  80. function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $sqlfilters = '') {
  81. global $db, $conf;
  82. $obj_ret = array();
  83. if(! DolibarrApiAccess::$user->rights->multicompany->read) {
  84. throw new RestException(401);
  85. }
  86. $sql = "SELECT t.rowid";
  87. $sql.= " FROM ".MAIN_DB_PREFIX."entity as t";
  88. $sql.= ' WHERE t.active = 1';
  89. // Add sql filters
  90. if ($sqlfilters)
  91. {
  92. if (! DolibarrApi::_checkFilters($sqlfilters))
  93. {
  94. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  95. }
  96. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  97. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  98. }
  99. $sql.= $db->order($sortfield, $sortorder);
  100. if ($limit) {
  101. if ($page < 0)
  102. {
  103. $page = 0;
  104. }
  105. $offset = $limit * $page;
  106. $sql.= $db->plimit($limit + 1, $offset);
  107. }
  108. $result = $db->query($sql);
  109. if ($result)
  110. {
  111. // The DaoMulticompany::fetch() method uses the global variable $user.
  112. global $user;
  113. $user = DolibarrApiAccess::$user;
  114. $i=0;
  115. $num = $db->num_rows($result);
  116. $min = min($num, ($limit <= 0 ? $num : $limit));
  117. while ($i < $min)
  118. {
  119. $obj = $db->fetch_object($result);
  120. $multicompany= new DaoMulticompany($this->db);
  121. if ($multicompany->fetch($obj->rowid)) {
  122. $obj_ret[] = $this->_cleanObjectDatas($multicompany);
  123. }
  124. $i++;
  125. }
  126. }
  127. else {
  128. throw new RestException(503, 'Error when retrieve entities list : '.$db->lasterror());
  129. }
  130. if ( ! count($obj_ret)) {
  131. throw new RestException(404, 'No entities found');
  132. }
  133. return $obj_ret;
  134. }
  135. /**
  136. * Create entity object
  137. *
  138. * @param array $request_data Request data
  139. * @return int ID of entity
  140. */
  141. /*function post($request_data = null)
  142. {
  143. if (! DolibarrApiAccess::$user->rights->adherent->configurer) {
  144. throw new RestException(401);
  145. }
  146. // Check mandatory fields
  147. $result = $this->_validate($request_data);
  148. $membertype = new AdherentType($this->db);
  149. foreach($request_data as $field => $value) {
  150. $membertype->$field = $value;
  151. }
  152. if ($membertype->create(DolibarrApiAccess::$user) < 0) {
  153. throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
  154. }
  155. return $membertype->id;
  156. }*/
  157. /**
  158. * Update entity
  159. *
  160. * @param int $id ID of entity to update
  161. * @param array $request_data Datas
  162. * @return int
  163. */
  164. /*function put($id, $request_data = null)
  165. {
  166. if (! DolibarrApiAccess::$user->rights->adherent->configurer) {
  167. throw new RestException(401);
  168. }
  169. $membertype = new AdherentType($this->db);
  170. $result = $membertype->fetch($id);
  171. if (empty($result)) {
  172. throw new RestException(404, 'member type not found');
  173. }
  174. if( ! DolibarrApi::_checkAccessToResource('member',$membertype->id,'adherent_type')) {
  175. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  176. }
  177. foreach($request_data as $field => $value) {
  178. if ($field == 'id') continue;
  179. // Process the status separately because it must be updated using
  180. // the validate() and resiliate() methods of the class AdherentType.
  181. $membertype->$field = $value;
  182. }
  183. // If there is no error, update() returns the number of affected rows
  184. // so if the update is a no op, the return value is zero.
  185. if ($membertype->update(DolibarrApiAccess::$user) >= 0)
  186. return $this->get($id);
  187. return false;
  188. }*/
  189. /**
  190. * Delete entity
  191. *
  192. * @param int $id entity ID
  193. * @return array
  194. */
  195. /*function delete($id)
  196. {
  197. if (! DolibarrApiAccess::$user->rights->adherent->configurer) {
  198. throw new RestException(401);
  199. }
  200. $membertype = new AdherentType($this->db);
  201. $result = $membertype->fetch($id);
  202. if (empty($result)) {
  203. throw new RestException(404, 'member type not found');
  204. }
  205. if ( ! DolibarrApi::_checkAccessToResource('member',$membertype->id,'adherent_type')) {
  206. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  207. }
  208. if (!$membertype->delete($membertype->id)) {
  209. throw new RestException(401,'error when deleting member type');
  210. }
  211. return array(
  212. 'success' => array(
  213. 'code' => 200,
  214. 'message' => 'member type deleted'
  215. )
  216. );
  217. }*/
  218. /**
  219. * Validate fields before creating an object
  220. *
  221. * @param array|null $data Data to validate
  222. * @return array
  223. *
  224. * @throws RestException
  225. */
  226. function _validate($data)
  227. {
  228. $membertype = array();
  229. foreach (MembersTypes::$FIELDS as $field) {
  230. if (!isset($data[$field]))
  231. throw new RestException(400, "$field field missing");
  232. $membertype[$field] = $data[$field];
  233. }
  234. return $membertype;
  235. }
  236. /**
  237. * Clean sensible object datas
  238. *
  239. * @param object $object Object to clean
  240. * @return array Array of cleaned object properties
  241. */
  242. function _cleanObjectDatas($object) {
  243. $object = parent::_cleanObjectDatas($object);
  244. // Remove constants
  245. foreach($object as $key => $value)
  246. {
  247. if (preg_match('/^MAIN_/', $key))
  248. {
  249. unset($object->$key);
  250. }
  251. }
  252. unset($object->language);
  253. unset($object->fk_tables);
  254. unset($object->import_key);
  255. //unset($object->array_options);
  256. unset($object->linkedObjectsIds);
  257. unset($object->context);
  258. unset($object->canvas);
  259. unset($object->fk_project);
  260. unset($object->contact);
  261. unset($object->contact_id);
  262. unset($object->thirdparty);
  263. unset($object->user);
  264. unset($object->origin);
  265. unset($object->origin_id);
  266. unset($object->ref_ext);
  267. unset($object->barcode_type);
  268. unset($object->barcode_type_code);
  269. unset($object->barcode_type_label);
  270. unset($object->barcode_type_coder);
  271. unset($object->mode_reglement_id);
  272. unset($object->cond_reglement_id);
  273. unset($object->cond_reglement);
  274. unset($object->fk_delivery_address);
  275. unset($object->shipping_method_id);
  276. unset($object->modelpdf);
  277. unset($object->fk_account);
  278. unset($object->note_public);
  279. unset($object->note_private);
  280. unset($object->fk_incoterms);
  281. unset($object->libelle_incoterms);
  282. unset($object->location_incoterms);
  283. unset($object->name);
  284. unset($object->lastname);
  285. unset($object->firstname);
  286. unset($object->civility_id);
  287. unset($object->total_ht);
  288. unset($object->total_tva);
  289. unset($object->total_localtax1);
  290. unset($object->total_localtax2);
  291. unset($object->total_ttc);
  292. unset($object->ref);
  293. unset($object->statut);
  294. unset($object->note);
  295. return $object;
  296. }
  297. }