dolibarr 18.0.6
api_categories.class.php
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 <https://www.gnu.org/licenses/>.
16 */
17
18use Luracast\Restler\RestException;
19
20require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
21require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
22
23
24require_once DOL_DOCUMENT_ROOT.'/adherents/class/api_members.class.php';
25require_once DOL_DOCUMENT_ROOT.'/product/class/api_products.class.php';
26require_once DOL_DOCUMENT_ROOT.'/societe/class/api_contacts.class.php';
27require_once DOL_DOCUMENT_ROOT.'/societe/class/api_thirdparties.class.php';
28require_once DOL_DOCUMENT_ROOT.'/projet/class/api_projects.class.php';
29
37{
41 static $FIELDS = array(
42 'label',
43 'type'
44 );
45
46 static $TYPES = array(
47 0 => 'product',
48 1 => 'supplier',
49 2 => 'customer',
50 3 => 'member',
51 4 => 'contact',
52 5 => 'account',
53 6 => 'project',
54 7 => 'user',
55 8 => 'bank_line',
56 9 => 'warehouse',
57 10 => 'actioncomm',
58 11 => 'website_page',
59 12 => 'ticket',
60 13 => 'knowledgemanagement'
61 );
62
66 public $category;
67
71 public function __construct()
72 {
73 global $db, $conf;
74 $this->db = $db;
75 $this->category = new Categorie($this->db);
76 }
77
89 public function get($id, $include_childs = false)
90 {
91 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
92 throw new RestException(401);
93 }
94
95 $result = $this->category->fetch($id);
96 if (!$result) {
97 throw new RestException(404, 'category not found');
98 }
99
100 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
101 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
102 }
103
104 if ($include_childs) {
105 $cats = $this->category->get_filles();
106 if (!is_array($cats)) {
107 throw new RestException(500, 'Error when fetching child categories', array_merge(array($this->category->error), $this->category->errors));
108 }
109 $this->category->childs = array();
110 foreach ($cats as $cat) {
111 $this->category->childs[] = $this->_cleanObjectDatas($cat);
112 }
113 }
114
115 return $this->_cleanObjectDatas($this->category);
116 }
117
133 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $sqlfilters = '')
134 {
135 global $db, $conf;
136
137 $obj_ret = array();
138
139 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
140 throw new RestException(401);
141 }
142
143 $sql = "SELECT t.rowid";
144 $sql .= " FROM ".MAIN_DB_PREFIX."categorie AS t LEFT JOIN ".MAIN_DB_PREFIX."categories_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
145 $sql .= ' WHERE t.entity IN ('.getEntity('category').')';
146 if (!empty($type)) {
147 $sql .= ' AND t.type='.array_search($type, Categories::$TYPES);
148 }
149 // Add sql filters
150 if ($sqlfilters) {
151 $errormessage = '';
152 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
153 if ($errormessage) {
154 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
155 }
156 }
157
158 $sql .= $this->db->order($sortfield, $sortorder);
159 if ($limit) {
160 if ($page < 0) {
161 $page = 0;
162 }
163 $offset = $limit * $page;
164
165 $sql .= $this->db->plimit($limit + 1, $offset);
166 }
167
168 $result = $this->db->query($sql);
169 if ($result) {
170 $i = 0;
171 $num = $this->db->num_rows($result);
172 $min = min($num, ($limit <= 0 ? $num : $limit));
173 while ($i < $min) {
174 $obj = $this->db->fetch_object($result);
175 $category_static = new Categorie($this->db);
176 if ($category_static->fetch($obj->rowid)) {
177 $obj_ret[] = $this->_cleanObjectDatas($category_static);
178 }
179 $i++;
180 }
181 } else {
182 throw new RestException(503, 'Error when retrieve category list : '.$this->db->lasterror());
183 }
184 if (!count($obj_ret)) {
185 throw new RestException(404, 'No category found');
186 }
187 return $obj_ret;
188 }
189
196 public function post($request_data = null)
197 {
198 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
199 throw new RestException(401);
200 }
201
202 // Check mandatory fields
203 $result = $this->_validate($request_data);
204
205 foreach ($request_data as $field => $value) {
206 $this->category->$field = $value;
207 }
208 if ($this->category->create(DolibarrApiAccess::$user) < 0) {
209 throw new RestException(500, 'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
210 }
211 return $this->category->id;
212 }
213
221 public function put($id, $request_data = null)
222 {
223 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
224 throw new RestException(401);
225 }
226
227 $result = $this->category->fetch($id);
228 if (!$result) {
229 throw new RestException(404, 'category not found');
230 }
231
232 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
233 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
234 }
235
236 foreach ($request_data as $field => $value) {
237 if ($field == 'id') {
238 continue;
239 }
240 if ($field == 'array_options' && is_array($value)) {
241 foreach ($value as $index => $val) {
242 $this->category->array_options[$index] = $this->_checkValForAPI($field, $val, $this->category);
243 }
244 continue;
245 }
246 $this->category->$field = $value;
247 }
248
249 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
250 return $this->get($id);
251 } else {
252 throw new RestException(500, $this->category->error);
253 }
254 }
255
262 public function delete($id)
263 {
264 if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
265 throw new RestException(401);
266 }
267 $result = $this->category->fetch($id);
268 if (!$result) {
269 throw new RestException(404, 'category not found');
270 }
271
272 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
273 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
274 }
275
276 if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
277 throw new RestException(500, 'Error when delete category : ' . $this->category->error);
278 }
279
280 return array(
281 'success' => array(
282 'code' => 200,
283 'message' => 'Category deleted'
284 )
285 );
286 }
287
305 public function getListForObject($id, $type, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
306 {
307 if (!in_array($type, [
308 Categorie::TYPE_PRODUCT,
309 Categorie::TYPE_CONTACT,
310 Categorie::TYPE_CUSTOMER,
311 Categorie::TYPE_SUPPLIER,
312 Categorie::TYPE_MEMBER,
313 Categorie::TYPE_PROJECT,
314 Categorie::TYPE_KNOWLEDGEMANAGEMENT
315 ])) {
316 throw new RestException(401);
317 }
318
319 if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
320 throw new RestException(401);
321 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
322 throw new RestException(401);
323 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
324 throw new RestException(401);
325 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
326 throw new RestException(401);
327 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
328 throw new RestException(401);
329 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
330 throw new RestException(401);
331 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'read')) {
332 throw new RestException(401);
333 }
334
335 $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
336
337 if (!is_array($categories)) {
338 if ($categories == 0) {
339 throw new RestException(404, 'No category found for this object');
340 }
341 throw new RestException(600, 'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
342 }
343 return $categories;
344 }
345
358 public function linkObjectById($id, $type, $object_id)
359 {
360 if (empty($type) || empty($object_id)) {
361 throw new RestException(401);
362 }
363
364 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
365 throw new RestException(401);
366 }
367
368 $result = $this->category->fetch($id);
369 if (!$result) {
370 throw new RestException(404, 'category not found');
371 }
372
373 if ($type === Categorie::TYPE_PRODUCT) {
374 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
375 throw new RestException(401);
376 }
377 $object = new Product($this->db);
378 } elseif ($type === Categorie::TYPE_CUSTOMER) {
379 if (!DolibarrApiAccess::$user->rights->societe->creer) {
380 throw new RestException(401);
381 }
382 $object = new Societe($this->db);
383 } elseif ($type === Categorie::TYPE_SUPPLIER) {
384 if (!DolibarrApiAccess::$user->rights->societe->creer) {
385 throw new RestException(401);
386 }
387 $object = new Societe($this->db);
388 } elseif ($type === Categorie::TYPE_CONTACT) {
389 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
390 throw new RestException(401);
391 }
392 $object = new Contact($this->db);
393 } elseif ($type === Categorie::TYPE_MEMBER) {
394 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
395 throw new RestException(401);
396 }
397 $object = new Adherent($this->db);
398 } else {
399 throw new RestException(401, "this type is not recognized yet.");
400 }
401
402 if (!empty($object)) {
403 $result = $object->fetch($object_id);
404 if ($result > 0) {
405 $result = $this->category->add_type($object, $type);
406 if ($result < 0) {
407 if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
408 throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
409 }
410 }
411 } else {
412 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
413 }
414
415 return array(
416 'success' => array(
417 'code' => 200,
418 'message' => 'Objects succefully linked to the category'
419 )
420 );
421 }
422
423 throw new RestException(401);
424 }
425
438 public function linkObjectByRef($id, $type, $object_ref)
439 {
440 if (empty($type) || empty($object_ref)) {
441 throw new RestException(401);
442 }
443
444 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
445 throw new RestException(401);
446 }
447
448 $result = $this->category->fetch($id);
449 if (!$result) {
450 throw new RestException(404, 'category not found');
451 }
452
453 if ($type === Categorie::TYPE_PRODUCT) {
454 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
455 throw new RestException(401);
456 }
457 $object = new Product($this->db);
458 } elseif ($type === Categorie::TYPE_CUSTOMER) {
459 if (!DolibarrApiAccess::$user->rights->societe->creer) {
460 throw new RestException(401);
461 }
462 $object = new Societe($this->db);
463 } elseif ($type === Categorie::TYPE_SUPPLIER) {
464 if (!DolibarrApiAccess::$user->rights->societe->creer) {
465 throw new RestException(401);
466 }
467 $object = new Societe($this->db);
468 } elseif ($type === Categorie::TYPE_CONTACT) {
469 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
470 throw new RestException(401);
471 }
472 $object = new Contact($this->db);
473 } elseif ($type === Categorie::TYPE_MEMBER) {
474 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
475 throw new RestException(401);
476 }
477 $object = new Adherent($this->db);
478 } else {
479 throw new RestException(401, "this type is not recognized yet.");
480 }
481
482 if (!empty($object)) {
483 $result = $object->fetch('', $object_ref);
484 if ($result > 0) {
485 $result = $this->category->add_type($object, $type);
486 if ($result < 0) {
487 if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
488 throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
489 }
490 }
491 } else {
492 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
493 }
494
495 return array(
496 'success' => array(
497 'code' => 200,
498 'message' => 'Objects succefully linked to the category'
499 )
500 );
501 }
502
503 throw new RestException(401);
504 }
505
518 public function unlinkObjectById($id, $type, $object_id)
519 {
520 if (empty($type) || empty($object_id)) {
521 throw new RestException(401);
522 }
523
524 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
525 throw new RestException(401);
526 }
527
528 $result = $this->category->fetch($id);
529 if (!$result) {
530 throw new RestException(404, 'category not found');
531 }
532
533 if ($type === Categorie::TYPE_PRODUCT) {
534 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
535 throw new RestException(401);
536 }
537 $object = new Product($this->db);
538 } elseif ($type === Categorie::TYPE_CUSTOMER) {
539 if (!DolibarrApiAccess::$user->rights->societe->creer) {
540 throw new RestException(401);
541 }
542 $object = new Societe($this->db);
543 } elseif ($type === Categorie::TYPE_SUPPLIER) {
544 if (!DolibarrApiAccess::$user->rights->societe->creer) {
545 throw new RestException(401);
546 }
547 $object = new Societe($this->db);
548 } elseif ($type === Categorie::TYPE_CONTACT) {
549 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
550 throw new RestException(401);
551 }
552 $object = new Contact($this->db);
553 } elseif ($type === Categorie::TYPE_MEMBER) {
554 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
555 throw new RestException(401);
556 }
557 $object = new Adherent($this->db);
558 } else {
559 throw new RestException(401, "this type is not recognized yet.");
560 }
561
562 if (!empty($object)) {
563 $result = $object->fetch((int) $object_id);
564 if ($result > 0) {
565 $result = $this->category->del_type($object, $type);
566 if ($result < 0) {
567 throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
568 }
569 } else {
570 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
571 }
572
573 return array(
574 'success' => array(
575 'code' => 200,
576 'message' => 'Objects succefully unlinked from the category'
577 )
578 );
579 }
580
581 throw new RestException(401);
582 }
583
596 public function unlinkObjectByRef($id, $type, $object_ref)
597 {
598 if (empty($type) || empty($object_ref)) {
599 throw new RestException(401);
600 }
601
602 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
603 throw new RestException(401);
604 }
605
606 $result = $this->category->fetch($id);
607 if (!$result) {
608 throw new RestException(404, 'category not found');
609 }
610
611 if ($type === Categorie::TYPE_PRODUCT) {
612 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
613 throw new RestException(401);
614 }
615 $object = new Product($this->db);
616 } elseif ($type === Categorie::TYPE_CUSTOMER) {
617 if (!DolibarrApiAccess::$user->rights->societe->creer) {
618 throw new RestException(401);
619 }
620 $object = new Societe($this->db);
621 } elseif ($type === Categorie::TYPE_SUPPLIER) {
622 if (!DolibarrApiAccess::$user->rights->societe->creer) {
623 throw new RestException(401);
624 }
625 $object = new Societe($this->db);
626 } elseif ($type === Categorie::TYPE_CONTACT) {
627 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
628 throw new RestException(401);
629 }
630 $object = new Contact($this->db);
631 } elseif ($type === Categorie::TYPE_MEMBER) {
632 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
633 throw new RestException(401);
634 }
635 $object = new Adherent($this->db);
636 } else {
637 throw new RestException(401, "this type is not recognized yet.");
638 }
639
640 if (!empty($object)) {
641 $result = $object->fetch('', (string) $object_ref);
642 if ($result > 0) {
643 $result = $this->category->del_type($object, $type);
644 if ($result < 0) {
645 throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
646 }
647 } else {
648 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
649 }
650
651 return array(
652 'success' => array(
653 'code' => 200,
654 'message' => 'Objects succefully unlinked from the category'
655 )
656 );
657 }
658
659 throw new RestException(401);
660 }
661
662
663 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
670 protected function _cleanObjectDatas($object)
671 {
672 // phpcs:enable
673 $object = parent::_cleanObjectDatas($object);
674
675 // Remove fields not relevent to categories
676 unset($object->MAP_CAT_FK);
677 unset($object->MAP_CAT_TABLE);
678 unset($object->MAP_OBJ_CLASS);
679 unset($object->MAP_OBJ_TABLE);
680 unset($object->country);
681 unset($object->country_id);
682 unset($object->country_code);
683 unset($object->total_ht);
684 unset($object->total_ht);
685 unset($object->total_localtax1);
686 unset($object->total_localtax2);
687 unset($object->total_ttc);
688 unset($object->total_tva);
689 unset($object->lines);
690 unset($object->civility_id);
691 unset($object->name);
692 unset($object->lastname);
693 unset($object->firstname);
694 unset($object->shipping_method_id);
695 unset($object->fk_delivery_address);
696 unset($object->cond_reglement);
697 unset($object->cond_reglement_id);
698 unset($object->mode_reglement_id);
699 unset($object->barcode_type_coder);
700 unset($object->barcode_type_label);
701 unset($object->barcode_type_code);
702 unset($object->barcode_type);
703 unset($object->canvas);
704 unset($object->cats);
705 unset($object->motherof);
706 unset($object->context);
707 unset($object->socid);
708 unset($object->thirdparty);
709 unset($object->contact);
710 unset($object->contact_id);
711 unset($object->user);
712 unset($object->fk_account);
713 unset($object->fk_project);
714 unset($object->note);
715 unset($object->statut);
716
717 return $object;
718 }
719
728 private function _validate($data)
729 {
730 $category = array();
731 foreach (Categories::$FIELDS as $field) {
732 if (!isset($data[$field])) {
733 throw new RestException(400, "$field field missing");
734 }
735 $category[$field] = $data[$field];
736 }
737 return $category;
738 }
739
751 public function getObjects($id, $type, $onlyids = 0)
752 {
753 dol_syslog("getObjects($id, $type, $onlyids)", LOG_DEBUG);
754
755 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
756 throw new RestException(401);
757 }
758
759 if (empty($type)) {
760 throw new RestException(500, 'The "type" parameter is required.');
761 }
762
763 $result = $this->category->fetch($id);
764 if (!$result) {
765 throw new RestException(404, 'category not found');
766 }
767
768 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
769 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
770 }
771
772 $result = $this->category->getObjectsInCateg($type, $onlyids);
773
774 if ($result < 0) {
775 throw new RestException(503, 'Error when retrieving objects list : '.$this->category->error);
776 }
777
778 $objects = $result;
779 $cleaned_objects = array();
780 $objects_api = null;
781 if ($type == 'member') {
782 $objects_api = new Members();
783 } elseif ($type == 'customer' || $type == 'supplier') {
784 $objects_api = new Thirdparties();
785 } elseif ($type == 'product') {
786 $objects_api = new Products();
787 } elseif ($type == 'contact') {
788 $objects_api = new Contacts();
789 } elseif ($type == 'project') {
790 $objects_api = new Projects();
791 }
792 if (is_object($objects_api)) {
793 foreach ($objects as $obj) {
794 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
795 }
796 }
797
798 return $cleaned_objects;
799 }
800}
Class to manage members of a foundation.
Class to manage categories.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $type='', $sqlfilters='')
List categories.
__construct()
Constructor.
put($id, $request_data=null)
Update category.
unlinkObjectById($id, $type, $object_id)
Unlink an object from a category by id.
unlinkObjectByRef($id, $type, $object_ref)
Unlink an object from a category by ref.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create category object.
_cleanObjectDatas($object)
Clean sensible object datas.
getObjects($id, $type, $onlyids=0)
Get the list of objects in a category.
getListForObject($id, $type, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
List categories of an object.
linkObjectById($id, $type, $object_id)
Link an object to a category by id.
linkObjectByRef($id, $type, $object_ref)
Link an object to a category by ref.
Class to manage contact/addresses.
Class for API REST v1.
Definition api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:86
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.