21use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
42 public static $FIELDS = array(
59 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
60 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
62 $this->contact =
new Contact($this->db);
79 public function get(
$id, $includecount = 0, $includeroles = 0)
81 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
82 throw new RestException(403,
'No permission to read contacts');
86 $result = $this->contact->initAsSpecimen();
88 $result = $this->contact->fetch(
$id);
92 throw new RestException(404,
'Contact not found');
96 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
100 $this->contact->load_ref_elements();
104 $this->contact->fetchRoles();
108 $this->contact->getNoEmail();
129 public function getByEmail($email, $includecount = 0, $includeroles = 0)
131 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
132 throw new RestException(403,
'No permission to read contacts');
136 $result = $this->contact->initAsSpecimen();
138 $result = $this->contact->fetch(0,
null,
'', $email);
142 throw new RestException(404,
'Contact not found');
146 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
150 $this->contact->load_ref_elements();
154 $this->contact->fetchRoles();
158 $this->contact->getNoEmail();
186 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)
192 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
193 throw new RestException(403,
'No permission to read contacts');
197 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
201 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
202 $search_sale = DolibarrApiAccess::$user->id;
205 $sql =
"SELECT t.rowid";
206 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
207 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object = t.rowid";
208 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON t.fk_soc = s.rowid";
209 $sql .=
' WHERE t.entity IN ('.getEntity(
'contact').
')';
211 $sql .=
" AND (t.priv = 0 OR t.fk_user_creat = ".((int) DolibarrApiAccess::$user->
id).
")";
213 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
216 if ($search_sale && $search_sale !=
'-1') {
217 if ($search_sale == -2) {
218 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
219 } elseif ($search_sale > 0) {
220 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
226 $searchCategoryContactList = $category ? array($category) : array();
229 if (!empty($searchCategoryContactList)) {
230 $searchCategoryContactSqlList = array();
232 foreach ($searchCategoryContactList as $searchCategoryContact) {
233 if (intval($searchCategoryContact) == -2) {
234 $searchCategoryContactSqlList[] =
"NOT EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX.
"categorie_contact as ck WHERE t.rowid = ck.fk_socpeople)";
235 } elseif (intval($searchCategoryContact) > 0) {
237 $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).
")";
251 if (!empty($searchCategoryContactSqlList)) {
252 $sql .=
" AND (".implode(
' AND ', $searchCategoryContactSqlList).
")";
263 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
268 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
270 $sql .= $this->db->order($sortfield, $sortorder);
276 $offset = $limit * $page;
278 $sql .= $this->db->plimit($limit + 1, $offset);
280 $result = $this->db->query($sql);
282 $num = $this->db->num_rows($result);
283 $min = min($num, ($limit <= 0 ? $num : $limit));
286 $obj = $this->db->fetch_object($result);
287 $contact_static =
new Contact($this->db);
288 if ($contact_static->fetch($obj->rowid)) {
289 $contact_static->fetchRoles();
291 $contact_static->load_ref_elements();
294 $contact_static->fetchRoles();
297 $contact_static->getNoEmail();
306 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
310 if ($pagination_data) {
311 $totalsResult = $this->db->query($sqlTotals);
312 $total = $this->db->fetch_object($totalsResult)->total;
317 $obj_ret[
'data'] = $tmp;
318 $obj_ret[
'pagination'] = [
319 'total' => (int) $total,
321 'page_count' => ceil((
int) $total / $limit),
341 public function post($request_data =
null)
343 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
344 throw new RestException(403,
'No permission to create/update contacts');
347 $result = $this->
_validate($request_data);
350 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
351 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
352 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
354 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
356 $request_data[
'country_id'] =
$id;
359 foreach ($request_data as $field => $value) {
360 if ($field ===
'caller') {
362 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
365 if ($field ==
'array_options' && is_array($value)) {
366 $this->contact->fetch_optionals();
368 foreach ($value as $index => $val) {
373 if ($field ==
'socid') {
374 $new_socid = (int) $value;
375 $loopthirdpartytmp =
new Societe($this->db);
376 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
377 if ($new_thirdparty_result < 1) {
378 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
381 throw new RestException(403,
'Access to socid/thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
385 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
387 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
388 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
390 if (
isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
391 $this->contact->setNoEmail($this->contact->no_email);
393 return $this->contact->id;
411 public function put(
$id, $request_data =
null)
413 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
414 throw new RestException(403,
'No permission to create/update contacts');
417 $result = $this->contact->fetch(
$id);
419 throw new RestException(404,
'Contact not found');
423 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
426 foreach ($request_data as $field => $value) {
427 if ($field ==
'id') {
430 if ($field ===
'caller') {
432 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
435 if ($field ==
'array_options' && is_array($value)) {
436 foreach ($value as $index => $val) {
441 if ($field ==
'socid') {
442 $new_socid = (int) $value;
443 $loopthirdpartytmp =
new Societe($this->db);
444 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
445 if ($new_thirdparty_result < 1) {
446 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
449 throw new RestException(403,
'Access to socid/thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
453 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
456 if (
isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
457 $this->contact->setNoEmail($this->contact->no_email);
460 if ($this->contact->update(
$id, DolibarrApiAccess::$user, 0,
'update') > 0) {
461 return $this->
get(
$id);
463 throw new RestException(500, $this->contact->error);
477 public function delete(
$id)
479 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'supprimer')) {
480 throw new RestException(403,
'No permission to delete contacts');
482 $result = $this->contact->fetch(
$id);
484 throw new RestException(404,
'Contact not found');
488 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
490 $this->contact->oldcopy = clone $this->contact;
492 if ($this->contact->delete(DolibarrApiAccess::$user) <= 0) {
493 throw new RestException(500,
'Error when delete contact ' . $this->contact->error);
499 'message' =>
'Contact deleted'
524 if (!isset($request_data[
"login"])) {
525 throw new RestException(400,
"login field missing");
527 if (!isset($request_data[
"password"])) {
528 throw new RestException(400,
"password field missing");
531 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
532 throw new RestException(403,
'No permission to read contacts');
534 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
535 throw new RestException(403,
'No permission to create user');
538 $contact =
new Contact($this->db);
539 $contact->fetch(
$id);
540 if ($contact->id <= 0) {
541 throw new RestException(404,
'Contact not found');
545 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
549 $login = $request_data[
"login"];
550 $password = $request_data[
"password"];
551 $useraccount =
new User($this->db);
552 $result = $useraccount->create_from_contact($contact, $login, $password);
554 throw new RestException(500,
"User not created");
557 $useraccount->setPassword($useraccount, $password);
577 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
579 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
580 throw new RestException(403);
584 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
589 $result = $categories->getListForItem(
$id,
'contact', $sortfield, $sortorder, $limit, $page);
592 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
615 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
616 throw new RestException(403,
'Insufficient rights');
619 $result = $this->contact->fetch(
$id);
621 throw new RestException(404,
'Contact not found');
624 $result = $category->fetch($category_id);
626 throw new RestException(404,
'category not found');
630 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
633 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
636 $category->add_type($this->contact,
'contact');
657 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
658 throw new RestException(403,
'Insufficient rights');
661 $result = $this->contact->fetch(
$id);
663 throw new RestException(404,
'Contact not found');
666 $result = $category->fetch($category_id);
668 throw new RestException(404,
'category not found');
672 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
675 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
678 $category->del_type($this->contact,
'contact');
700 unset(
$object->total_localtax1);
701 unset(
$object->total_localtax2);
721 foreach (Contacts::$FIELDS as $field) {
722 if (!isset($data[$field])) {
723 throw new RestException(400,
"$field field missing");
725 $contact[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
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.