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 $this->category->$field = $value;
241 }
242
243 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
244 return $this->get($id);
245 } else {
246 throw new RestException(500, $this->category->error);
247 }
248 }
249
256 public function delete($id)
257 {
258 if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
259 throw new RestException(401);
260 }
261 $result = $this->category->fetch($id);
262 if (!$result) {
263 throw new RestException(404, 'category not found');
264 }
265
266 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
267 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
268 }
269
270 if (!$this->category->delete(DolibarrApiAccess::$user)) {
271 throw new RestException(401, 'error when delete category');
272 }
273
274 return array(
275 'success' => array(
276 'code' => 200,
277 'message' => 'Category deleted'
278 )
279 );
280 }
281
299 public function getListForObject($id, $type, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
300 {
301 if (!in_array($type, [
302 Categorie::TYPE_PRODUCT,
303 Categorie::TYPE_CONTACT,
304 Categorie::TYPE_CUSTOMER,
305 Categorie::TYPE_SUPPLIER,
306 Categorie::TYPE_MEMBER,
307 Categorie::TYPE_PROJECT,
308 Categorie::TYPE_KNOWLEDGEMANAGEMENT
309 ])) {
310 throw new RestException(401);
311 }
312
313 if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
314 throw new RestException(401);
315 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
316 throw new RestException(401);
317 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight('societe', 'lire')) {
318 throw new RestException(401);
319 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
320 throw new RestException(401);
321 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
322 throw new RestException(401);
323 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
324 throw new RestException(401);
325 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight('knowledgemanagement', 'knowledgerecord', 'read')) {
326 throw new RestException(401);
327 }
328
329 $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
330
331 if (!is_array($categories)) {
332 if ($categories == 0) {
333 throw new RestException(404, 'No category found for this object');
334 }
335 throw new RestException(600, 'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
336 }
337 return $categories;
338 }
339
352 public function linkObjectById($id, $type, $object_id)
353 {
354 if (empty($type) || empty($object_id)) {
355 throw new RestException(401);
356 }
357
358 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
359 throw new RestException(401);
360 }
361
362 $result = $this->category->fetch($id);
363 if (!$result) {
364 throw new RestException(404, 'category not found');
365 }
366
367 if ($type === Categorie::TYPE_PRODUCT) {
368 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
369 throw new RestException(401);
370 }
371 $object = new Product($this->db);
372 } elseif ($type === Categorie::TYPE_CUSTOMER) {
373 if (!DolibarrApiAccess::$user->rights->societe->creer) {
374 throw new RestException(401);
375 }
376 $object = new Societe($this->db);
377 } elseif ($type === Categorie::TYPE_SUPPLIER) {
378 if (!DolibarrApiAccess::$user->rights->societe->creer) {
379 throw new RestException(401);
380 }
381 $object = new Societe($this->db);
382 } elseif ($type === Categorie::TYPE_CONTACT) {
383 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
384 throw new RestException(401);
385 }
386 $object = new Contact($this->db);
387 } elseif ($type === Categorie::TYPE_MEMBER) {
388 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
389 throw new RestException(401);
390 }
391 $object = new Adherent($this->db);
392 } else {
393 throw new RestException(401, "this type is not recognized yet.");
394 }
395
396 if (!empty($object)) {
397 $result = $object->fetch($object_id);
398 if ($result > 0) {
399 $result = $this->category->add_type($object, $type);
400 if ($result < 0) {
401 if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
402 throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
403 }
404 }
405 } else {
406 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
407 }
408
409 return array(
410 'success' => array(
411 'code' => 200,
412 'message' => 'Objects succefully linked to the category'
413 )
414 );
415 }
416
417 throw new RestException(401);
418 }
419
432 public function linkObjectByRef($id, $type, $object_ref)
433 {
434 if (empty($type) || empty($object_ref)) {
435 throw new RestException(401);
436 }
437
438 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
439 throw new RestException(401);
440 }
441
442 $result = $this->category->fetch($id);
443 if (!$result) {
444 throw new RestException(404, 'category not found');
445 }
446
447 if ($type === Categorie::TYPE_PRODUCT) {
448 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
449 throw new RestException(401);
450 }
451 $object = new Product($this->db);
452 } elseif ($type === Categorie::TYPE_CUSTOMER) {
453 if (!DolibarrApiAccess::$user->rights->societe->creer) {
454 throw new RestException(401);
455 }
456 $object = new Societe($this->db);
457 } elseif ($type === Categorie::TYPE_SUPPLIER) {
458 if (!DolibarrApiAccess::$user->rights->societe->creer) {
459 throw new RestException(401);
460 }
461 $object = new Societe($this->db);
462 } elseif ($type === Categorie::TYPE_CONTACT) {
463 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
464 throw new RestException(401);
465 }
466 $object = new Contact($this->db);
467 } elseif ($type === Categorie::TYPE_MEMBER) {
468 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
469 throw new RestException(401);
470 }
471 $object = new Adherent($this->db);
472 } else {
473 throw new RestException(401, "this type is not recognized yet.");
474 }
475
476 if (!empty($object)) {
477 $result = $object->fetch('', $object_ref);
478 if ($result > 0) {
479 $result = $this->category->add_type($object, $type);
480 if ($result < 0) {
481 if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
482 throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
483 }
484 }
485 } else {
486 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
487 }
488
489 return array(
490 'success' => array(
491 'code' => 200,
492 'message' => 'Objects succefully linked to the category'
493 )
494 );
495 }
496
497 throw new RestException(401);
498 }
499
512 public function unlinkObjectById($id, $type, $object_id)
513 {
514 if (empty($type) || empty($object_id)) {
515 throw new RestException(401);
516 }
517
518 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
519 throw new RestException(401);
520 }
521
522 $result = $this->category->fetch($id);
523 if (!$result) {
524 throw new RestException(404, 'category not found');
525 }
526
527 if ($type === Categorie::TYPE_PRODUCT) {
528 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
529 throw new RestException(401);
530 }
531 $object = new Product($this->db);
532 } elseif ($type === Categorie::TYPE_CUSTOMER) {
533 if (!DolibarrApiAccess::$user->rights->societe->creer) {
534 throw new RestException(401);
535 }
536 $object = new Societe($this->db);
537 } elseif ($type === Categorie::TYPE_SUPPLIER) {
538 if (!DolibarrApiAccess::$user->rights->societe->creer) {
539 throw new RestException(401);
540 }
541 $object = new Societe($this->db);
542 } elseif ($type === Categorie::TYPE_CONTACT) {
543 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
544 throw new RestException(401);
545 }
546 $object = new Contact($this->db);
547 } elseif ($type === Categorie::TYPE_MEMBER) {
548 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
549 throw new RestException(401);
550 }
551 $object = new Adherent($this->db);
552 } else {
553 throw new RestException(401, "this type is not recognized yet.");
554 }
555
556 if (!empty($object)) {
557 $result = $object->fetch((int) $object_id);
558 if ($result > 0) {
559 $result = $this->category->del_type($object, $type);
560 if ($result < 0) {
561 throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
562 }
563 } else {
564 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
565 }
566
567 return array(
568 'success' => array(
569 'code' => 200,
570 'message' => 'Objects succefully unlinked from the category'
571 )
572 );
573 }
574
575 throw new RestException(401);
576 }
577
590 public function unlinkObjectByRef($id, $type, $object_ref)
591 {
592 if (empty($type) || empty($object_ref)) {
593 throw new RestException(401);
594 }
595
596 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
597 throw new RestException(401);
598 }
599
600 $result = $this->category->fetch($id);
601 if (!$result) {
602 throw new RestException(404, 'category not found');
603 }
604
605 if ($type === Categorie::TYPE_PRODUCT) {
606 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
607 throw new RestException(401);
608 }
609 $object = new Product($this->db);
610 } elseif ($type === Categorie::TYPE_CUSTOMER) {
611 if (!DolibarrApiAccess::$user->rights->societe->creer) {
612 throw new RestException(401);
613 }
614 $object = new Societe($this->db);
615 } elseif ($type === Categorie::TYPE_SUPPLIER) {
616 if (!DolibarrApiAccess::$user->rights->societe->creer) {
617 throw new RestException(401);
618 }
619 $object = new Societe($this->db);
620 } elseif ($type === Categorie::TYPE_CONTACT) {
621 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
622 throw new RestException(401);
623 }
624 $object = new Contact($this->db);
625 } elseif ($type === Categorie::TYPE_MEMBER) {
626 if (!DolibarrApiAccess::$user->hasRight('adherent', 'creer')) {
627 throw new RestException(401);
628 }
629 $object = new Adherent($this->db);
630 } else {
631 throw new RestException(401, "this type is not recognized yet.");
632 }
633
634 if (!empty($object)) {
635 $result = $object->fetch('', (string) $object_ref);
636 if ($result > 0) {
637 $result = $this->category->del_type($object, $type);
638 if ($result < 0) {
639 throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
640 }
641 } else {
642 throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors));
643 }
644
645 return array(
646 'success' => array(
647 'code' => 200,
648 'message' => 'Objects succefully unlinked from the category'
649 )
650 );
651 }
652
653 throw new RestException(401);
654 }
655
656
657 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
664 protected function _cleanObjectDatas($object)
665 {
666 // phpcs:enable
667 $object = parent::_cleanObjectDatas($object);
668
669 // Remove fields not relevent to categories
670 unset($object->MAP_CAT_FK);
671 unset($object->MAP_CAT_TABLE);
672 unset($object->MAP_OBJ_CLASS);
673 unset($object->MAP_OBJ_TABLE);
674 unset($object->country);
675 unset($object->country_id);
676 unset($object->country_code);
677 unset($object->total_ht);
678 unset($object->total_ht);
679 unset($object->total_localtax1);
680 unset($object->total_localtax2);
681 unset($object->total_ttc);
682 unset($object->total_tva);
683 unset($object->lines);
684 unset($object->civility_id);
685 unset($object->name);
686 unset($object->lastname);
687 unset($object->firstname);
688 unset($object->shipping_method_id);
689 unset($object->fk_delivery_address);
690 unset($object->cond_reglement);
691 unset($object->cond_reglement_id);
692 unset($object->mode_reglement_id);
693 unset($object->barcode_type_coder);
694 unset($object->barcode_type_label);
695 unset($object->barcode_type_code);
696 unset($object->barcode_type);
697 unset($object->canvas);
698 unset($object->cats);
699 unset($object->motherof);
700 unset($object->context);
701 unset($object->socid);
702 unset($object->thirdparty);
703 unset($object->contact);
704 unset($object->contact_id);
705 unset($object->user);
706 unset($object->fk_account);
707 unset($object->fk_project);
708 unset($object->note);
709 unset($object->statut);
710
711 return $object;
712 }
713
722 private function _validate($data)
723 {
724 $category = array();
725 foreach (Categories::$FIELDS as $field) {
726 if (!isset($data[$field])) {
727 throw new RestException(400, "$field field missing");
728 }
729 $category[$field] = $data[$field];
730 }
731 return $category;
732 }
733
745 public function getObjects($id, $type, $onlyids = 0)
746 {
747 dol_syslog("getObjects($id, $type, $onlyids)", LOG_DEBUG);
748
749 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
750 throw new RestException(401);
751 }
752
753 if (empty($type)) {
754 throw new RestException(500, 'The "type" parameter is required.');
755 }
756
757 $result = $this->category->fetch($id);
758 if (!$result) {
759 throw new RestException(404, 'category not found');
760 }
761
762 if (!DolibarrApi::_checkAccessToResource('categorie', $this->category->id)) {
763 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
764 }
765
766 $result = $this->category->getObjectsInCateg($type, $onlyids);
767
768 if ($result < 0) {
769 throw new RestException(503, 'Error when retrieving objects list : '.$this->category->error);
770 }
771
772 $objects = $result;
773 $cleaned_objects = array();
774 $objects_api = null;
775 if ($type == 'member') {
776 $objects_api = new Members();
777 } elseif ($type == 'customer' || $type == 'supplier') {
778 $objects_api = new Thirdparties();
779 } elseif ($type == 'product') {
780 $objects_api = new Products();
781 } elseif ($type == 'contact') {
782 $objects_api = new Contacts();
783 } elseif ($type == 'project') {
784 $objects_api = new Projects();
785 }
786 if (is_object($objects_api)) {
787 foreach ($objects as $obj) {
788 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
789 }
790 }
791
792 return $cleaned_objects;
793 }
794}
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.
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.