api_memberstypes.class.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /* Copyright (C) 2017 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 <https://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent_type.class.php';
  19. /**
  20. * API class for members types
  21. *
  22. * @access protected
  23. * @class DolibarrApiAccess {@requires user,external}
  24. */
  25. class MembersTypes extends DolibarrApi
  26. {
  27. /**
  28. * @var array $FIELDS Mandatory fields, checked when create and update object
  29. */
  30. public static $FIELDS = array(
  31. 'label',
  32. );
  33. /**
  34. * Constructor
  35. */
  36. public function __construct()
  37. {
  38. global $db, $conf;
  39. $this->db = $db;
  40. }
  41. /**
  42. * Get properties of a member type object
  43. *
  44. * Return an array with member type informations
  45. *
  46. * @param int $id ID of member type
  47. * @return Object Object with cleaned properties
  48. *
  49. * @throws RestException
  50. */
  51. public function get($id)
  52. {
  53. if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
  54. throw new RestException(401);
  55. }
  56. $membertype = new AdherentType($this->db);
  57. $result = $membertype->fetch($id);
  58. if (!$result) {
  59. throw new RestException(404, 'member type not found');
  60. }
  61. if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
  62. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  63. }
  64. return $this->_cleanObjectDatas($membertype);
  65. }
  66. /**
  67. * List members types
  68. *
  69. * Get a list of members types
  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.libelle:like:'SO-%') and (t.subscription:=:'1')"
  76. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  77. * @return array Array of member type objects
  78. *
  79. * @throws RestException
  80. */
  81. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
  82. {
  83. global $db, $conf;
  84. $obj_ret = array();
  85. if (!DolibarrApiAccess::$user->hasRight('adherent', 'lire')) {
  86. throw new RestException(401);
  87. }
  88. $sql = "SELECT t.rowid";
  89. $sql .= " FROM ".MAIN_DB_PREFIX."adherent_type AS t LEFT JOIN ".MAIN_DB_PREFIX."adherent_type_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
  90. $sql .= ' WHERE t.entity IN ('.getEntity('member_type').')';
  91. // Add sql filters
  92. if ($sqlfilters) {
  93. $errormessage = '';
  94. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  95. if ($errormessage) {
  96. throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
  97. }
  98. }
  99. $sql .= $this->db->order($sortfield, $sortorder);
  100. if ($limit) {
  101. if ($page < 0) {
  102. $page = 0;
  103. }
  104. $offset = $limit * $page;
  105. $sql .= $this->db->plimit($limit + 1, $offset);
  106. }
  107. $result = $this->db->query($sql);
  108. if ($result) {
  109. $i = 0;
  110. $num = $this->db->num_rows($result);
  111. $min = min($num, ($limit <= 0 ? $num : $limit));
  112. while ($i < $min) {
  113. $obj = $this->db->fetch_object($result);
  114. $membertype = new AdherentType($this->db);
  115. if ($membertype->fetch($obj->rowid)) {
  116. $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($membertype), $properties);
  117. }
  118. $i++;
  119. }
  120. } else {
  121. throw new RestException(503, 'Error when retrieve member type list : '.$this->db->lasterror());
  122. }
  123. return $obj_ret;
  124. }
  125. /**
  126. * Create member type object
  127. *
  128. * @param array $request_data Request data
  129. * @return int ID of member type
  130. */
  131. public function post($request_data = null)
  132. {
  133. if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
  134. throw new RestException(401);
  135. }
  136. // Check mandatory fields
  137. $result = $this->_validate($request_data);
  138. $membertype = new AdherentType($this->db);
  139. foreach ($request_data as $field => $value) {
  140. if ($field === 'caller') {
  141. // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
  142. $membertype->context['caller'] = $request_data['caller'];
  143. continue;
  144. }
  145. $membertype->$field = $value;
  146. }
  147. if ($membertype->create(DolibarrApiAccess::$user) < 0) {
  148. throw new RestException(500, 'Error creating member type', array_merge(array($membertype->error), $membertype->errors));
  149. }
  150. return $membertype->id;
  151. }
  152. /**
  153. * Update member type
  154. *
  155. * @param int $id ID of member type to update
  156. * @param array $request_data Datas
  157. * @return int
  158. */
  159. public function put($id, $request_data = null)
  160. {
  161. if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
  162. throw new RestException(401);
  163. }
  164. $membertype = new AdherentType($this->db);
  165. $result = $membertype->fetch($id);
  166. if (!$result) {
  167. throw new RestException(404, 'member type not found');
  168. }
  169. if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
  170. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  171. }
  172. foreach ($request_data as $field => $value) {
  173. if ($field == 'id') {
  174. continue;
  175. }
  176. if ($field === 'caller') {
  177. // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
  178. $membertype->context['caller'] = $request_data['caller'];
  179. continue;
  180. }
  181. // Process the status separately because it must be updated using
  182. // the validate(), resiliate() and exclude() methods of the class AdherentType.
  183. $membertype->$field = $value;
  184. }
  185. // If there is no error, update() returns the number of affected rows
  186. // so if the update is a no op, the return value is zero.
  187. if ($membertype->update(DolibarrApiAccess::$user) >= 0) {
  188. return $this->get($id);
  189. } else {
  190. throw new RestException(500, 'Error when updating member type: '.$membertype->error);
  191. }
  192. }
  193. /**
  194. * Delete member type
  195. *
  196. * @param int $id member type ID
  197. * @return array
  198. */
  199. public function delete($id)
  200. {
  201. if (!DolibarrApiAccess::$user->hasRight('adherent', 'configurer')) {
  202. throw new RestException(401);
  203. }
  204. $membertype = new AdherentType($this->db);
  205. $result = $membertype->fetch($id);
  206. if (!$result) {
  207. throw new RestException(404, 'member type not found');
  208. }
  209. if (!DolibarrApi::_checkAccessToResource('member', $membertype->id, 'adherent_type')) {
  210. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  211. }
  212. $res = $membertype->delete(DolibarrApiAccess::$user);
  213. if ($res < 0) {
  214. throw new RestException(500, "Can't delete, error occurs");
  215. } elseif ($res == 0) {
  216. throw new RestException(409, "Can't delete, that product is probably used");
  217. }
  218. return array(
  219. 'success' => array(
  220. 'code' => 200,
  221. 'message' => 'Member type deleted'
  222. )
  223. );
  224. }
  225. /**
  226. * Validate fields before creating an object
  227. *
  228. * @param array|null $data Data to validate
  229. * @return array
  230. *
  231. * @throws RestException
  232. */
  233. private function _validate($data)
  234. {
  235. $membertype = array();
  236. foreach (MembersTypes::$FIELDS as $field) {
  237. if (!isset($data[$field])) {
  238. throw new RestException(400, "$field field missing");
  239. }
  240. $membertype[$field] = $data[$field];
  241. }
  242. return $membertype;
  243. }
  244. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  245. /**
  246. * Clean sensible object datas
  247. *
  248. * @param Object $object Object to clean
  249. * @return Object Object with cleaned properties
  250. */
  251. protected function _cleanObjectDatas($object)
  252. {
  253. // phpcs:enable
  254. $object = parent::_cleanObjectDatas($object);
  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->country);
  268. unset($object->country_id);
  269. unset($object->country_code);
  270. unset($object->barcode_type);
  271. unset($object->barcode_type_code);
  272. unset($object->barcode_type_label);
  273. unset($object->barcode_type_coder);
  274. unset($object->mode_reglement_id);
  275. unset($object->cond_reglement_id);
  276. unset($object->cond_reglement);
  277. unset($object->fk_delivery_address);
  278. unset($object->shipping_method_id);
  279. unset($object->model_pdf);
  280. unset($object->fk_account);
  281. unset($object->note_public);
  282. unset($object->note_private);
  283. unset($object->fk_incoterms);
  284. unset($object->label_incoterms);
  285. unset($object->location_incoterms);
  286. unset($object->name);
  287. unset($object->lastname);
  288. unset($object->firstname);
  289. unset($object->civility_id);
  290. unset($object->total_ht);
  291. unset($object->total_tva);
  292. unset($object->total_localtax1);
  293. unset($object->total_localtax2);
  294. unset($object->total_ttc);
  295. return $object;
  296. }
  297. }