21use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
43 public static $FIELDS = array(
60 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
61 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
63 $this->contact =
new Contact($this->db);
80 public function get(
$id, $includecount = 0, $includeroles = 0)
82 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
83 throw new RestException(403,
'No permission to read contacts');
87 $result = $this->contact->initAsSpecimen();
89 $result = $this->contact->fetch(
$id);
93 throw new RestException(404,
'Contact not found');
97 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
101 $this->contact->load_ref_elements();
105 $this->contact->fetchRoles();
109 $this->contact->getNoEmail();
130 public function getByEmail($email, $includecount = 0, $includeroles = 0)
132 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
133 throw new RestException(403,
'No permission to read contacts');
137 $result = $this->contact->initAsSpecimen();
139 $result = $this->contact->fetch(0,
null,
'', $email);
143 throw new RestException(404,
'Contact not found');
147 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
151 $this->contact->load_ref_elements();
155 $this->contact->fetchRoles();
159 $this->contact->getNoEmail();
187 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $category = 0, $sqlfilters =
'', $includecount = 0, $includeroles = 0, $properties =
'', $pagination_data =
false)
193 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
194 throw new RestException(403,
'No permission to read contacts');
198 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
202 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
203 $search_sale = DolibarrApiAccess::$user->id;
206 $sql =
"SELECT t.rowid";
207 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
208 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object = t.rowid";
209 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON t.fk_soc = s.rowid";
210 $sql .=
' WHERE t.entity IN ('.getEntity(
'contact').
')';
212 $sql .=
" AND (t.priv = 0 OR t.fk_user_creat = ".((int) DolibarrApiAccess::$user->
id).
")";
214 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
217 if ($search_sale && $search_sale !=
'-1') {
218 if ($search_sale == -2) {
219 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
220 } elseif ($search_sale > 0) {
221 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
227 $searchCategoryContactList = $category ? array($category) : array();
230 if (!empty($searchCategoryContactList)) {
231 $searchCategoryContactSqlList = array();
233 foreach ($searchCategoryContactList as $searchCategoryContact) {
234 if (intval($searchCategoryContact) == -2) {
235 $searchCategoryContactSqlList[] =
"NOT EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX.
"categorie_contact as ck WHERE t.rowid = ck.fk_socpeople)";
236 } elseif (intval($searchCategoryContact) > 0) {
238 $searchCategoryContactSqlList[] =
" EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX.
"categorie_contact as ck WHERE t.rowid = ck.fk_socpeople AND ck.fk_categorie = ".((int) $searchCategoryContact).
")";
252 if (!empty($searchCategoryContactSqlList)) {
253 $sql .=
" AND (".implode(
' AND ', $searchCategoryContactSqlList).
")";
264 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
269 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
271 $sql .= $this->db->order($sortfield, $sortorder);
277 $offset = $limit * $page;
279 $sql .= $this->db->plimit($limit + 1, $offset);
281 $result = $this->db->query($sql);
283 $num = $this->db->num_rows($result);
284 $min = min($num, ($limit <= 0 ? $num : $limit));
287 $obj = $this->db->fetch_object($result);
288 $contact_static =
new Contact($this->db);
289 if ($contact_static->fetch($obj->rowid)) {
290 $contact_static->fetchRoles();
292 $contact_static->load_ref_elements();
295 $contact_static->fetchRoles();
298 $contact_static->getNoEmail();
307 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
311 if ($pagination_data) {
312 $totalsResult = $this->db->query($sqlTotals);
313 $total = $this->db->fetch_object($totalsResult)->total;
318 $obj_ret[
'data'] = $tmp;
319 $obj_ret[
'pagination'] = [
320 'total' => (int) $total,
322 'page_count' => ceil((
int) $total / $limit),
342 public function post($request_data =
null)
344 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
345 throw new RestException(403,
'No permission to create/update contacts');
348 $result = $this->
_validate($request_data);
351 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
352 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
353 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
355 throw new RestException(404,
'Country code not found in database: ' . $this->db->lasterror());
357 $request_data[
'country_id'] =
$id;
360 foreach ($request_data as $field => $value) {
361 if ($field ===
'caller') {
363 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
366 if ($field ==
'array_options' && is_array($value)) {
367 $this->contact->fetch_optionals();
369 foreach ($value as $index => $val) {
374 if ($field ==
'socid') {
375 $new_socid = (int) $value;
376 $loopthirdpartytmp =
new Societe($this->db);
377 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
378 if ($new_thirdparty_result < 1) {
379 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
382 throw new RestException(403,
'Access to socid/thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
386 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
388 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
389 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
391 if (
isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
392 $this->contact->setNoEmail($this->contact->no_email);
394 return $this->contact->id;
412 public function put(
$id, $request_data =
null)
414 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
415 throw new RestException(403,
'No permission to create/update contacts');
418 $result = $this->contact->fetch(
$id);
420 throw new RestException(404,
'Contact not found');
424 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
427 foreach ($request_data as $field => $value) {
428 if ($field ==
'id') {
431 if ($field ===
'caller') {
433 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
436 if ($field ==
'array_options' && is_array($value)) {
437 foreach ($value as $index => $val) {
442 if ($field ==
'socid') {
443 $new_socid = (int) $value;
444 $loopthirdpartytmp =
new Societe($this->db);
445 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
446 if ($new_thirdparty_result < 1) {
447 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
450 throw new RestException(403,
'Access to socid/thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
454 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
457 if (
isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
458 $this->contact->setNoEmail($this->contact->no_email);
461 if ($this->contact->update(
$id, DolibarrApiAccess::$user, 0,
'update') > 0) {
462 return $this->
get(
$id);
464 throw new RestException(500, $this->contact->errorsToString());
478 public function delete(
$id)
480 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'supprimer')) {
481 throw new RestException(403,
'No permission to delete contacts');
483 $result = $this->contact->fetch(
$id);
485 throw new RestException(404,
'Contact not found');
489 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
491 $this->contact->oldcopy = clone $this->contact;
493 if ($this->contact->delete(DolibarrApiAccess::$user) <= 0) {
494 throw new RestException(500,
'Error when delete contact ' . $this->contact->errorsToString());
500 'message' =>
'Contact deleted'
525 if (!isset($request_data[
"login"])) {
526 throw new RestException(400,
"login field missing");
528 if (!isset($request_data[
"password"])) {
529 throw new RestException(400,
"password field missing");
532 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
533 throw new RestException(403,
'No permission to read contacts');
535 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
536 throw new RestException(403,
'No permission to create user');
539 $contact =
new Contact($this->db);
540 $contact->fetch(
$id);
541 if ($contact->id <= 0) {
542 throw new RestException(404,
'Contact not found');
546 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
550 $login = $request_data[
"login"];
551 $password = $request_data[
"password"];
552 $useraccount =
new User($this->db);
553 $result = $useraccount->create_from_contact($contact, $login, $password);
555 throw new RestException(500,
"User not created");
558 $useraccount->setPassword($useraccount, $password);
578 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
580 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
581 throw new RestException(403);
585 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
590 $result = $categories->getListForItem(
$id,
'contact', $sortfield, $sortorder, $limit, $page);
593 throw new RestException(503,
'Error when retrieve category list : '.$categories->errorsToString());
616 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
617 throw new RestException(403,
'Insufficient rights');
620 $result = $this->contact->fetch(
$id);
622 throw new RestException(404,
'Contact not found');
625 $result = $category->fetch($category_id);
627 throw new RestException(404,
'category not found');
631 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
634 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
637 $category->add_type($this->contact,
'contact');
658 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
659 throw new RestException(403,
'Insufficient rights');
662 $result = $this->contact->fetch(
$id);
664 throw new RestException(404,
'Contact not found');
667 $result = $category->fetch($category_id);
669 throw new RestException(404,
'category not found');
673 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
676 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
679 $category->del_type($this->contact,
'contact');
701 unset(
$object->total_localtax1);
702 unset(
$object->total_localtax2);
722 foreach (Contacts::$FIELDS as $field) {
723 if (!isset($data[$field])) {
724 throw new RestException(400,
"$field field missing");
726 $contact[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage categories.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
isModEnabled($module)
Is Dolibarr module enabled.