myobject_api_class.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  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. /**
  19. * \file htdocs/modulebuilder/template/class/myobject_api_class.class.php
  20. * \ingroup mymodule
  21. * \brief File for API management of myobject.
  22. */
  23. /**
  24. * API class for mymodule myobject
  25. *
  26. * @smart-auto-routing false
  27. * @access protected
  28. * @class DolibarrApiAccess {@requires user,external}
  29. */
  30. class MyObjectApi extends DolibarrApi
  31. {
  32. /**
  33. * @var array $FIELDS Mandatory fields, checked when create and update object
  34. */
  35. static $FIELDS = array(
  36. 'name'
  37. );
  38. /**
  39. * @var MyObject $myobject {@type MyObject}
  40. */
  41. public $myobject;
  42. /**
  43. * Constructor
  44. *
  45. * @url GET myobject/
  46. *
  47. */
  48. function __construct()
  49. {
  50. global $db, $conf;
  51. $this->db = $db;
  52. $this->myobject = new MyObject($this->db);
  53. }
  54. /**
  55. * Get properties of a myobject object
  56. *
  57. * Return an array with myobject informations
  58. *
  59. * @param int $id ID of myobject
  60. * @return array|mixed data without useless information
  61. *
  62. * @url GET myobject/{id}
  63. * @throws RestException
  64. */
  65. function get($id)
  66. {
  67. if(! DolibarrApiAccess::$user->rights->myobject->read) {
  68. throw new RestException(401);
  69. }
  70. $result = $this->myobject->fetch($id);
  71. if( ! $result ) {
  72. throw new RestException(404, 'MyObject not found');
  73. }
  74. if( ! DolibarrApi::_checkAccessToResource('myobject',$this->myobject->id)) {
  75. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  76. }
  77. return $this->_cleanObjectDatas($this->myobject);
  78. }
  79. /**
  80. * List myobjects
  81. *
  82. * Get a list of myobjects
  83. *
  84. * @param int $mode Use this param to filter list
  85. * @param string $sortfield Sort field
  86. * @param string $sortorder Sort order
  87. * @param int $limit Limit for list
  88. * @param int $page Page number
  89. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101') or (t.import_key:=:'20160101')"
  90. * @return array Array of myobject objects
  91. *
  92. * @url GET /myobjects/
  93. */
  94. function index($mode, $sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $sqlfilters = '') {
  95. global $db, $conf;
  96. $obj_ret = array();
  97. $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : '';
  98. // If the internal user must only see his customers, force searching by him
  99. if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
  100. $sql = "SELECT s.rowid";
  101. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $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)
  102. $sql.= " FROM ".MAIN_DB_PREFIX."myobject as s";
  103. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  104. $sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st";
  105. $sql.= " WHERE s.fk_stcomm = st.id";
  106. // Example of use $mode
  107. //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
  108. //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
  109. $sql.= ' AND s.entity IN ('.getEntity('myobject', 1).')';
  110. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND s.fk_soc = sc.fk_soc";
  111. if ($socid) $sql.= " AND s.fk_soc = ".$socid;
  112. if ($search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
  113. // Insert sale filter
  114. if ($search_sale > 0)
  115. {
  116. $sql .= " AND sc.fk_user = ".$search_sale;
  117. }
  118. if ($sqlfilters)
  119. {
  120. if (! DolibarrApi::_checkFilters($sqlfilters))
  121. {
  122. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  123. }
  124. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  125. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  126. }
  127. $sql.= $db->order($sortfield, $sortorder);
  128. if ($limit) {
  129. if ($page < 0)
  130. {
  131. $page = 0;
  132. }
  133. $offset = $limit * $page;
  134. $sql.= $db->plimit($limit + 1, $offset);
  135. }
  136. $result = $db->query($sql);
  137. if ($result)
  138. {
  139. $num = $db->num_rows($result);
  140. while ($i < $num)
  141. {
  142. $obj = $db->fetch_object($result);
  143. $myobject_static = new MyObject($db);
  144. if($myobject_static->fetch($obj->rowid)) {
  145. $obj_ret[] = parent::_cleanObjectDatas($myobject_static);
  146. }
  147. $i++;
  148. }
  149. }
  150. else {
  151. throw new RestException(503, 'Error when retrieve myobject list');
  152. }
  153. if( ! count($obj_ret)) {
  154. throw new RestException(404, 'No myobject found');
  155. }
  156. return $obj_ret;
  157. }
  158. /**
  159. * Create myobject object
  160. *
  161. * @param array $request_data Request datas
  162. * @return int ID of myobject
  163. *
  164. * @url POST myobject/
  165. */
  166. function post($request_data = NULL)
  167. {
  168. if(! DolibarrApiAccess::$user->rights->myobject->create) {
  169. throw new RestException(401);
  170. }
  171. // Check mandatory fields
  172. $result = $this->_validate($request_data);
  173. foreach($request_data as $field => $value) {
  174. $this->myobject->$field = $value;
  175. }
  176. if( ! $this->myobject->create(DolibarrApiAccess::$user)) {
  177. throw new RestException(500);
  178. }
  179. return $this->myobject->id;
  180. }
  181. /**
  182. * Update myobject
  183. *
  184. * @param int $id Id of myobject to update
  185. * @param array $request_data Datas
  186. * @return int
  187. *
  188. * @url PUT myobject/{id}
  189. */
  190. function put($id, $request_data = NULL)
  191. {
  192. if(! DolibarrApiAccess::$user->rights->myobject->create) {
  193. throw new RestException(401);
  194. }
  195. $result = $this->myobject->fetch($id);
  196. if( ! $result ) {
  197. throw new RestException(404, 'MyObject not found');
  198. }
  199. if( ! DolibarrApi::_checkAccessToResource('myobject',$this->myobject->id)) {
  200. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  201. }
  202. foreach($request_data as $field => $value) {
  203. $this->myobject->$field = $value;
  204. }
  205. if($this->myobject->update($id, DolibarrApiAccess::$user))
  206. return $this->get ($id);
  207. return false;
  208. }
  209. /**
  210. * Delete myobject
  211. *
  212. * @param int $id MyObject ID
  213. * @return array
  214. *
  215. * @url DELETE myobject/{id}
  216. */
  217. function delete($id)
  218. {
  219. if(! DolibarrApiAccess::$user->rights->myobject->supprimer) {
  220. throw new RestException(401);
  221. }
  222. $result = $this->myobject->fetch($id);
  223. if( ! $result ) {
  224. throw new RestException(404, 'MyObject not found');
  225. }
  226. if( ! DolibarrApi::_checkAccessToResource('myobject',$this->myobject->id)) {
  227. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  228. }
  229. if( !$this->myobject->delete($id))
  230. {
  231. throw new RestException(500);
  232. }
  233. return array(
  234. 'success' => array(
  235. 'code' => 200,
  236. 'message' => 'MyObject deleted'
  237. )
  238. );
  239. }
  240. /**
  241. * Validate fields before create or update object
  242. *
  243. * @param array $data Data to validate
  244. * @return array
  245. *
  246. * @throws RestException
  247. */
  248. function _validate($data)
  249. {
  250. $myobject = array();
  251. foreach (MyObjectApi::$FIELDS as $field) {
  252. if (!isset($data[$field]))
  253. throw new RestException(400, "$field field missing");
  254. $myobject[$field] = $data[$field];
  255. }
  256. return $myobject;
  257. }
  258. }