api_products.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  19. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  20. /**
  21. * API class for products
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class Products extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. static $FIELDS = array(
  32. 'ref',
  33. 'label'
  34. );
  35. /**
  36. * @var Product $product {@type Product}
  37. */
  38. public $product;
  39. /**
  40. * Constructor
  41. */
  42. function __construct()
  43. {
  44. global $db, $conf;
  45. $this->db = $db;
  46. $this->product = new Product($this->db);
  47. }
  48. /**
  49. * Get properties of a product object
  50. *
  51. * Return an array with product informations
  52. *
  53. * @param int $id ID of product
  54. * @return array|mixed data without useless information
  55. *
  56. * @throws RestException
  57. * TODO implement getting a product by ref or by $ref_ext
  58. */
  59. function get($id)
  60. {
  61. if(! DolibarrApiAccess::$user->rights->produit->lire) {
  62. throw new RestException(401);
  63. }
  64. $result = $this->product->fetch($id);
  65. if( ! $result ) {
  66. throw new RestException(404, 'Product not found');
  67. }
  68. if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) {
  69. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  70. }
  71. $this->product->load_stock();
  72. return $this->_cleanObjectDatas($this->product);
  73. }
  74. /**
  75. * List products
  76. *
  77. * Get a list of products
  78. *
  79. * @param string $sortfield Sort field
  80. * @param string $sortorder Sort order
  81. * @param int $limit Limit for list
  82. * @param int $page Page number
  83. * @param int $mode Use this param to filter list (0 for all, 1 for only product, 2 for only service)
  84. * @param int $category Use this param to filter list by category
  85. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.tobuy:=:0) and (t.tosell:=:1)"
  86. * @return array Array of product objects
  87. */
  88. function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 0, $page = 0, $mode=0, $category=0, $sqlfilters = '') {
  89. global $db, $conf;
  90. $obj_ret = array();
  91. $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : '';
  92. $sql = "SELECT t.rowid, t.ref, t.ref_ext";
  93. $sql.= " FROM ".MAIN_DB_PREFIX."product as t";
  94. if ($category > 0)
  95. {
  96. $sql.= ", ".MAIN_DB_PREFIX."categorie_product as c";
  97. }
  98. $sql.= ' WHERE t.entity IN ('.getEntity('product', 1).')';
  99. // Select products of given category
  100. if ($category > 0)
  101. {
  102. $sql.= " AND c.fk_categorie = ".$db->escape($category);
  103. $sql.= " AND c.fk_product = t.rowid ";
  104. }
  105. // Show products
  106. if ($mode == 1) $sql.= " AND t.fk_product_type = 0";
  107. // Show services
  108. if ($mode == 2) $sql.= " AND t.fk_product_type = 1";
  109. // Add sql filters
  110. if ($sqlfilters)
  111. {
  112. if (! DolibarrApi::_checkFilters($sqlfilters))
  113. {
  114. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  115. }
  116. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  117. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  118. }
  119. $sql.= $db->order($sortfield, $sortorder);
  120. if ($limit) {
  121. if ($page < 0)
  122. {
  123. $page = 0;
  124. }
  125. $offset = $limit * $page;
  126. $sql.= $db->plimit($limit + 1, $offset);
  127. }
  128. $result = $db->query($sql);
  129. if ($result)
  130. {
  131. $num = $db->num_rows($result);
  132. $min = min($num, ($limit <= 0 ? $num : $limit));
  133. while ($i < $min)
  134. {
  135. $obj = $db->fetch_object($result);
  136. $product_static = new Product($db);
  137. if($product_static->fetch($obj->rowid)) {
  138. $obj_ret[] = $this->_cleanObjectDatas($product_static);
  139. }
  140. $i++;
  141. }
  142. }
  143. else {
  144. throw new RestException(503, 'Error when retrieve product list : '.$db->lasterror());
  145. }
  146. if( ! count($obj_ret)) {
  147. throw new RestException(404, 'No product found');
  148. }
  149. return $obj_ret;
  150. }
  151. /**
  152. * Create product object
  153. *
  154. * @param array $request_data Request data
  155. * @return int ID of product
  156. */
  157. function post($request_data = NULL)
  158. {
  159. if(! DolibarrApiAccess::$user->rights->produit->creer) {
  160. throw new RestException(401);
  161. }
  162. // Check mandatory fields
  163. $result = $this->_validate($request_data);
  164. foreach($request_data as $field => $value) {
  165. $this->product->$field = $value;
  166. }
  167. if ($this->product->create(DolibarrApiAccess::$user) < 0) {
  168. throw new RestException(500, "Error creating product", array_merge(array($this->product->error), $this->product->errors));
  169. }
  170. return $this->product->id;
  171. }
  172. /**
  173. * Update product
  174. *
  175. * @param int $id Id of product to update
  176. * @param array $request_data Datas
  177. * @return int
  178. */
  179. function put($id, $request_data = NULL)
  180. {
  181. if(! DolibarrApiAccess::$user->rights->produit->creer) {
  182. throw new RestException(401);
  183. }
  184. $result = $this->product->fetch($id);
  185. if( ! $result ) {
  186. throw new RestException(404, 'Product not found');
  187. }
  188. if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) {
  189. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  190. }
  191. foreach($request_data as $field => $value) {
  192. if ($field == 'id') continue;
  193. $this->product->$field = $value;
  194. }
  195. if($this->product->update($id, DolibarrApiAccess::$user,1,'update'))
  196. return $this->get ($id);
  197. return false;
  198. }
  199. /**
  200. * Delete product
  201. *
  202. * @param int $id Product ID
  203. * @return array
  204. */
  205. function delete($id)
  206. {
  207. if(! DolibarrApiAccess::$user->rights->produit->supprimer) {
  208. throw new RestException(401);
  209. }
  210. $result = $this->product->fetch($id);
  211. if( ! $result ) {
  212. throw new RestException(404, 'Product not found');
  213. }
  214. if( ! DolibarrApi::_checkAccessToResource('product',$this->product->id)) {
  215. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  216. }
  217. // The Product::delete() method uses the global variable $user.
  218. global $user;
  219. $user = DolibarrApiAccess::$user;
  220. return $this->product->delete(DolibarrApiAccess::$user);
  221. }
  222. /**
  223. * Get categories for a product
  224. *
  225. * @param int $id ID of product
  226. * @param string $sortfield Sort field
  227. * @param string $sortorder Sort order
  228. * @param int $limit Limit for list
  229. * @param int $page Page number
  230. *
  231. * @return mixed
  232. *
  233. * @url GET {id}/categories
  234. */
  235. function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
  236. $categories = new Categories();
  237. return $categories->getListForItem($sortfield, $sortorder, $limit, $page, 'product', $id);
  238. }
  239. /**
  240. * Clean sensible object datas
  241. *
  242. * @param object $object Object to clean
  243. * @return array Array of cleaned object properties
  244. */
  245. function _cleanObjectDatas($object) {
  246. $object = parent::_cleanObjectDatas($object);
  247. unset($object->regeximgext);
  248. return $object;
  249. }
  250. /**
  251. * Validate fields before create or update object
  252. *
  253. * @param array $data Datas to validate
  254. * @return array
  255. * @throws RestException
  256. */
  257. function _validate($data)
  258. {
  259. $product = array();
  260. foreach (Products::$FIELDS as $field) {
  261. if (!isset($data[$field]))
  262. throw new RestException(400, "$field field missing");
  263. $product[$field] = $data[$field];
  264. }
  265. return $product;
  266. }
  267. }