dolibarr 19.0.3
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 public static $FIELDS = array(
42 'label',
43 'type'
44 );
45
46 public 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
134 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $sqlfilters = '', $properties = '')
135 {
136 global $db, $conf;
137
138 $obj_ret = array();
139
140 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
141 throw new RestException(401);
142 }
143
144 $sql = "SELECT t.rowid";
145 $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
146 $sql .= ' WHERE t.entity IN ('.getEntity('category').')';
147 if (!empty($type)) {
148 $sql .= ' AND t.type='.array_search($type, Categories::$TYPES);
149 }
150 // Add sql filters
151 if ($sqlfilters) {
152 $errormessage = '';
153 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
154 if ($errormessage) {
155 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
156 }
157 }
158
159 $sql .= $this->db->order($sortfield, $sortorder);
160 if ($limit) {
161 if ($page < 0) {
162 $page = 0;
163 }
164 $offset = $limit * $page;
165
166 $sql .= $this->db->plimit($limit + 1, $offset);
167 }
168
169 $result = $this->db->query($sql);
170 if ($result) {
171 $i = 0;
172 $num = $this->db->num_rows($result);
173 $min = min($num, ($limit <= 0 ? $num : $limit));
174 while ($i < $min) {
175 $obj = $this->db->fetch_object($result);
176 $category_static = new Categorie($this->db);
177 if ($category_static->fetch($obj->rowid)) {
178 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($category_static), $properties);
179 }
180 $i++;
181 }
182 } else {
183 throw new RestException(503, 'Error when retrieve category list : '.$this->db->lasterror());
184 }
185
186 return $obj_ret;
187 }
188
195 public function post($request_data = null)
196 {
197 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
198 throw new RestException(401);
199 }
200
201 // Check mandatory fields
202 $result = $this->_validate($request_data);
203
204 foreach ($request_data as $field => $value) {
205 if ($field === 'caller') {
206 // 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
207 $this->category->context['caller'] = $request_data['caller'];
208 continue;
209 }
210
211 $this->category->$field = $value;
212 }
213 if ($this->category->create(DolibarrApiAccess::$user) < 0) {
214 throw new RestException(500, 'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
215 }
216 return $this->category->id;
217 }
218
226 public function put($id, $request_data = null)
227 {
228 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
229 throw new RestException(401);
230 }
231
232 $result = $this->category->fetch($id);
233 if (!$result) {
234 throw new RestException(404, 'category not found');
235 }
236
237 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
238 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
239 }
240
241 foreach ($request_data as $field => $value) {
242 if ($field == 'id') {
243 continue;
244 }
245 if ($field === 'caller') {
246 // 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
247 $this->category->context['caller'] = $request_data['caller'];
248 continue;
249 }
250
251 $this->category->$field = $value;
252 }
253
254 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
255 return $this->get($id);
256 } else {
257 throw new RestException(500, $this->category->error);
258 }
259 }
260
267 public function delete($id)
268 {
269 if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
270 throw new RestException(401);
271 }
272 $result = $this->category->fetch($id);
273 if (!$result) {
274 throw new RestException(404, 'category not found');
275 }
276
277 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
278 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
279 }
280
281 if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
282 throw new RestException(500, 'Error when delete category : ' . $this->category->error);
283 }
284
285 return array(
286 'success' => array(
287 'code' => 200,
288 'message' => 'Category deleted'
289 )
290 );
291 }
292
310 public function getListForObject($id, $type, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
311 {
312 if (!in_array($type, [
313 Categorie::TYPE_PRODUCT,
314 Categorie::TYPE_CONTACT,
315 Categorie::TYPE_CUSTOMER,
316 Categorie::TYPE_SUPPLIER,
317 Categorie::TYPE_MEMBER,
318 Categorie::TYPE_PROJECT,
319 Categorie::TYPE_KNOWLEDGEMANAGEMENT
320 ])) {
321 throw new RestException(401);
322 }
323
324 if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
325 throw new RestException(401);
326 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
327 throw new RestException(401);
328 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
329 throw new RestException(401);
330 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
331 throw new RestException(401);
332 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
333 throw new RestException(401);
334 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
335 throw new RestException(401);
336 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'read')) {
337 throw new RestException(401);
338 }
339
340 $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
341
342 if (!is_array($categories)) {
343 throw new RestException(600, 'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
344 }
345 return $categories;
346 }
347
360 public function linkObjectById($id, $type, $object_id)
361 {
362 if (empty($type) || empty($object_id)) {
363 throw new RestException(401);
364 }
365
366 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
367 throw new RestException(401);
368 }
369
370 $result = $this->category->fetch($id);
371 if (!$result) {
372 throw new RestException(404, 'category not found');
373 }
374
375 if ($type === Categorie::TYPE_PRODUCT) {
376 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
377 throw new RestException(401);
378 }
379 $object = new Product($this->db);
380 } elseif ($type === Categorie::TYPE_CUSTOMER) {
381 if (!DolibarrApiAccess::$user->rights->societe->creer) {
382 throw new RestException(401);
383 }
384 $object = new Societe($this->db);
385 } elseif ($type === Categorie::TYPE_SUPPLIER) {
386 if (!DolibarrApiAccess::$user->rights->societe->creer) {
387 throw new RestException(401);
388 }
389 $object = new Societe($this->db);
390 } elseif ($type === Categorie::TYPE_CONTACT) {
391 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
392 throw new RestException(401);
393 }
394 $object = new Contact($this->db);
395 } elseif ($type === Categorie::TYPE_MEMBER) {
396 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
397 throw new RestException(401);
398 }
399 $object = new Adherent($this->db);
400 } else {
401 throw new RestException(401, "this type is not recognized yet.");
402 }
403
404 if (!empty($object)) {
405 $result = $object->fetch($object_id);
406 if ($result > 0) {
407 $result = $this->category->add_type($object, $type);
408 if ($result < 0) {
409 if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
410 throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
411 }
412 }
413 } else {
414 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
415 }
416
417 return array(
418 'success' => array(
419 'code' => 200,
420 'message' => 'Objects succefully linked to the category'
421 )
422 );
423 }
424
425 throw new RestException(401);
426 }
427
440 public function linkObjectByRef($id, $type, $object_ref)
441 {
442 if (empty($type) || empty($object_ref)) {
443 throw new RestException(401);
444 }
445
446 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
447 throw new RestException(401);
448 }
449
450 $result = $this->category->fetch($id);
451 if (!$result) {
452 throw new RestException(404, 'category not found');
453 }
454
455 if ($type === Categorie::TYPE_PRODUCT) {
456 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
457 throw new RestException(401);
458 }
459 $object = new Product($this->db);
460 } elseif ($type === Categorie::TYPE_CUSTOMER) {
461 if (!DolibarrApiAccess::$user->rights->societe->creer) {
462 throw new RestException(401);
463 }
464 $object = new Societe($this->db);
465 } elseif ($type === Categorie::TYPE_SUPPLIER) {
466 if (!DolibarrApiAccess::$user->rights->societe->creer) {
467 throw new RestException(401);
468 }
469 $object = new Societe($this->db);
470 } elseif ($type === Categorie::TYPE_CONTACT) {
471 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
472 throw new RestException(401);
473 }
474 $object = new Contact($this->db);
475 } elseif ($type === Categorie::TYPE_MEMBER) {
476 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
477 throw new RestException(401);
478 }
479 $object = new Adherent($this->db);
480 } else {
481 throw new RestException(401, "this type is not recognized yet.");
482 }
483
484 if (!empty($object)) {
485 $result = $object->fetch('', $object_ref);
486 if ($result > 0) {
487 $result = $this->category->add_type($object, $type);
488 if ($result < 0) {
489 if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
490 throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
491 }
492 }
493 } else {
494 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
495 }
496
497 return array(
498 'success' => array(
499 'code' => 200,
500 'message' => 'Objects succefully linked to the category'
501 )
502 );
503 }
504
505 throw new RestException(401);
506 }
507
520 public function unlinkObjectById($id, $type, $object_id)
521 {
522 if (empty($type) || empty($object_id)) {
523 throw new RestException(401);
524 }
525
526 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
527 throw new RestException(401);
528 }
529
530 $result = $this->category->fetch($id);
531 if (!$result) {
532 throw new RestException(404, 'category not found');
533 }
534
535 if ($type === Categorie::TYPE_PRODUCT) {
536 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
537 throw new RestException(401);
538 }
539 $object = new Product($this->db);
540 } elseif ($type === Categorie::TYPE_CUSTOMER) {
541 if (!DolibarrApiAccess::$user->rights->societe->creer) {
542 throw new RestException(401);
543 }
544 $object = new Societe($this->db);
545 } elseif ($type === Categorie::TYPE_SUPPLIER) {
546 if (!DolibarrApiAccess::$user->rights->societe->creer) {
547 throw new RestException(401);
548 }
549 $object = new Societe($this->db);
550 } elseif ($type === Categorie::TYPE_CONTACT) {
551 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
552 throw new RestException(401);
553 }
554 $object = new Contact($this->db);
555 } elseif ($type === Categorie::TYPE_MEMBER) {
556 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
557 throw new RestException(401);
558 }
559 $object = new Adherent($this->db);
560 } else {
561 throw new RestException(401, "this type is not recognized yet.");
562 }
563
564 if (!empty($object)) {
565 $result = $object->fetch((int) $object_id);
566 if ($result > 0) {
567 $result = $this->category->del_type($object, $type);
568 if ($result < 0) {
569 throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
570 }
571 } else {
572 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
573 }
574
575 return array(
576 'success' => array(
577 'code' => 200,
578 'message' => 'Objects succefully unlinked from the category'
579 )
580 );
581 }
582
583 throw new RestException(401);
584 }
585
598 public function unlinkObjectByRef($id, $type, $object_ref)
599 {
600 if (empty($type) || empty($object_ref)) {
601 throw new RestException(401);
602 }
603
604 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
605 throw new RestException(401);
606 }
607
608 $result = $this->category->fetch($id);
609 if (!$result) {
610 throw new RestException(404, 'category not found');
611 }
612
613 if ($type === Categorie::TYPE_PRODUCT) {
614 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
615 throw new RestException(401);
616 }
617 $object = new Product($this->db);
618 } elseif ($type === Categorie::TYPE_CUSTOMER) {
619 if (!DolibarrApiAccess::$user->rights->societe->creer) {
620 throw new RestException(401);
621 }
622 $object = new Societe($this->db);
623 } elseif ($type === Categorie::TYPE_SUPPLIER) {
624 if (!DolibarrApiAccess::$user->rights->societe->creer) {
625 throw new RestException(401);
626 }
627 $object = new Societe($this->db);
628 } elseif ($type === Categorie::TYPE_CONTACT) {
629 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
630 throw new RestException(401);
631 }
632 $object = new Contact($this->db);
633 } elseif ($type === Categorie::TYPE_MEMBER) {
634 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
635 throw new RestException(401);
636 }
637 $object = new Adherent($this->db);
638 } else {
639 throw new RestException(401, "this type is not recognized yet.");
640 }
641
642 if (!empty($object)) {
643 $result = $object->fetch('', (string) $object_ref);
644 if ($result > 0) {
645 $result = $this->category->del_type($object, $type);
646 if ($result < 0) {
647 throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
648 }
649 } else {
650 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
651 }
652
653 return array(
654 'success' => array(
655 'code' => 200,
656 'message' => 'Objects succefully unlinked from the category'
657 )
658 );
659 }
660
661 throw new RestException(401);
662 }
663
664
665 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
672 protected function _cleanObjectDatas($object)
673 {
674 // phpcs:enable
675 $object = parent::_cleanObjectDatas($object);
676
677 // Remove fields not relevent to categories
678 unset($object->MAP_CAT_FK);
679 unset($object->MAP_CAT_TABLE);
680 unset($object->MAP_OBJ_CLASS);
681 unset($object->MAP_OBJ_TABLE);
682 unset($object->country);
683 unset($object->country_id);
684 unset($object->country_code);
685 unset($object->total_ht);
686 unset($object->total_ht);
687 unset($object->total_localtax1);
688 unset($object->total_localtax2);
689 unset($object->total_ttc);
690 unset($object->total_tva);
691 unset($object->lines);
692 unset($object->civility_id);
693 unset($object->name);
694 unset($object->lastname);
695 unset($object->firstname);
696 unset($object->shipping_method_id);
697 unset($object->fk_delivery_address);
698 unset($object->cond_reglement);
699 unset($object->cond_reglement_id);
700 unset($object->mode_reglement_id);
701 unset($object->barcode_type_coder);
702 unset($object->barcode_type_label);
703 unset($object->barcode_type_code);
704 unset($object->barcode_type);
705 unset($object->canvas);
706 unset($object->cats);
707 unset($object->motherof);
708 unset($object->context);
709 unset($object->socid);
710 unset($object->thirdparty);
711 unset($object->contact);
712 unset($object->contact_id);
713 unset($object->user);
714 unset($object->fk_account);
715 unset($object->fk_project);
716 unset($object->note);
717 unset($object->statut);
718
719 return $object;
720 }
721
730 private function _validate($data)
731 {
732 $category = array();
733 foreach (Categories::$FIELDS as $field) {
734 if (!isset($data[$field])) {
735 throw new RestException(400, "$field field missing");
736 }
737 $category[$field] = $data[$field];
738 }
739 return $category;
740 }
741
753 public function getObjects($id, $type, $onlyids = 0)
754 {
755 dol_syslog("getObjects($id, $type, $onlyids)", LOG_DEBUG);
756
757 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
758 throw new RestException(401);
759 }
760
761 if (empty($type)) {
762 throw new RestException(500, 'The "type" parameter is required.');
763 }
764
765 $result = $this->category->fetch($id);
766 if (!$result) {
767 throw new RestException(404, 'category not found');
768 }
769
770 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
771 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
772 }
773
774 $result = $this->category->getObjectsInCateg($type, $onlyids);
775
776 if ($result < 0) {
777 throw new RestException(503, 'Error when retrieving objects list : '.$this->category->error);
778 }
779
780 $objects = $result;
781 $cleaned_objects = array();
782 $objects_api = null;
783 if ($type == 'member') {
784 $objects_api = new Members();
785 } elseif ($type == 'customer' || $type == 'supplier') {
786 $objects_api = new Thirdparties();
787 } elseif ($type == 'product') {
788 $objects_api = new Products();
789 } elseif ($type == 'contact') {
790 $objects_api = new Contacts();
791 } elseif ($type == 'project') {
792 $objects_api = new Projects();
793 }
794 if (is_object($objects_api)) {
795 foreach ($objects as $obj) {
796 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
797 }
798 }
799
800 return $cleaned_objects;
801 }
802}
Class to manage members of a foundation.
Class to manage 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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $type='', $sqlfilters='', $properties='')
List categories.
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
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.
Contact()
Old copy.
Definition index.php:572