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.fk_soc IN (".$this->db->sanitize($socids).
")";
214 if ($search_sale && $search_sale !=
'-1') {
215 if ($search_sale == -2) {
216 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
217 } elseif ($search_sale > 0) {
218 $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).
")";
224 $searchCategoryContactList = $category ? array($category) : array();
227 if (!empty($searchCategoryContactList)) {
228 $searchCategoryContactSqlList = array();
230 foreach ($searchCategoryContactList as $searchCategoryContact) {
231 if (intval($searchCategoryContact) == -2) {
232 $searchCategoryContactSqlList[] =
"NOT EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX.
"categorie_contact as ck WHERE t.rowid = ck.fk_socpeople)";
233 } elseif (intval($searchCategoryContact) > 0) {
235 $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).
")";
249 if (!empty($searchCategoryContactSqlList)) {
250 $sql .=
" AND (".implode(
' AND ', $searchCategoryContactSqlList).
")";
261 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
266 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
268 $sql .= $this->db->order($sortfield, $sortorder);
274 $offset = $limit * $page;
276 $sql .= $this->db->plimit($limit + 1, $offset);
278 $result = $this->db->query($sql);
280 $num = $this->db->num_rows($result);
281 $min = min($num, ($limit <= 0 ? $num : $limit));
284 $obj = $this->db->fetch_object($result);
285 $contact_static =
new Contact($this->db);
286 if ($contact_static->fetch($obj->rowid)) {
287 $contact_static->fetchRoles();
289 $contact_static->load_ref_elements();
292 $contact_static->fetchRoles();
295 $contact_static->getNoEmail();
304 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
308 if ($pagination_data) {
309 $totalsResult = $this->db->query($sqlTotals);
310 $total = $this->db->fetch_object($totalsResult)->total;
315 $obj_ret[
'data'] = $tmp;
316 $obj_ret[
'pagination'] = [
317 'total' => (int) $total,
319 'page_count' => ceil((
int) $total / $limit),
339 public function post($request_data =
null)
341 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
342 throw new RestException(403,
'No permission to create/update contacts');
345 $result = $this->
_validate($request_data);
348 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
349 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
350 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
352 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
354 $request_data[
'country_id'] =
$id;
357 foreach ($request_data as $field => $value) {
358 if ($field ===
'caller') {
360 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
363 if ($field ==
'array_options' && is_array($value)) {
364 foreach ($value as $index => $val) {
365 $this->contact->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->contact);
369 if ($field ==
'socid') {
370 $new_socid = (int) $value;
371 $loopthirdpartytmp =
new Societe($this->db);
372 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
373 if ($new_thirdparty_result < 1) {
374 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
377 throw new RestException(403,
'Access to socid/thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
381 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
383 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
384 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
386 if (
isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
387 $this->contact->setNoEmail($this->contact->no_email);
389 return $this->contact->id;
407 public function put(
$id, $request_data =
null)
409 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
410 throw new RestException(403,
'No permission to create/update contacts');
413 $result = $this->contact->fetch(
$id);
415 throw new RestException(404,
'Contact not found');
419 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
422 foreach ($request_data as $field => $value) {
423 if ($field ==
'id') {
426 if ($field ===
'caller') {
428 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
431 if ($field ==
'array_options' && is_array($value)) {
432 foreach ($value as $index => $val) {
433 $this->contact->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->contact);
437 if ($field ==
'socid') {
438 $new_socid = (int) $value;
439 $loopthirdpartytmp =
new Societe($this->db);
440 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
441 if ($new_thirdparty_result < 1) {
442 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
445 throw new RestException(403,
'Access to socid/thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
449 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
452 if (
isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
453 $this->contact->setNoEmail($this->contact->no_email);
456 if ($this->contact->update(
$id, DolibarrApiAccess::$user, 0,
'update') > 0) {
457 return $this->
get(
$id);
459 throw new RestException(500, $this->contact->error);
473 public function delete(
$id)
475 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'supprimer')) {
476 throw new RestException(403,
'No permission to delete contacts');
478 $result = $this->contact->fetch(
$id);
480 throw new RestException(404,
'Contact not found');
484 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
486 $this->contact->oldcopy = clone $this->contact;
488 if ($this->contact->delete(DolibarrApiAccess::$user) <= 0) {
489 throw new RestException(500,
'Error when delete contact ' . $this->contact->error);
495 'message' =>
'Contact deleted'
520 if (!isset($request_data[
"login"])) {
521 throw new RestException(400,
"login field missing");
523 if (!isset($request_data[
"password"])) {
524 throw new RestException(400,
"password field missing");
527 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
528 throw new RestException(403,
'No permission to read contacts');
530 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
531 throw new RestException(403,
'No permission to create user');
534 $contact =
new Contact($this->db);
535 $contact->fetch(
$id);
536 if ($contact->id <= 0) {
537 throw new RestException(404,
'Contact not found');
541 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
545 $login = $request_data[
"login"];
546 $password = $request_data[
"password"];
547 $useraccount =
new User($this->db);
548 $result = $useraccount->create_from_contact($contact, $login, $password);
550 throw new RestException(500,
"User not created");
553 $useraccount->setPassword($useraccount, $password);
573 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
575 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
576 throw new RestException(403);
581 $result = $categories->getListForItem(
$id,
'contact', $sortfield, $sortorder, $limit, $page);
584 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
607 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
608 throw new RestException(403,
'Insufficient rights');
611 $result = $this->contact->fetch(
$id);
613 throw new RestException(404,
'Contact not found');
616 $result = $category->fetch($category_id);
618 throw new RestException(404,
'category not found');
622 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
625 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
628 $category->add_type($this->contact,
'contact');
649 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
650 throw new RestException(403,
'Insufficient rights');
653 $result = $this->contact->fetch(
$id);
655 throw new RestException(404,
'Contact not found');
658 $result = $category->fetch($category_id);
660 throw new RestException(404,
'category not found');
664 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
667 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
670 $category->del_type($this->contact,
'contact');
692 unset(
$object->total_localtax1);
693 unset(
$object->total_localtax2);
713 foreach (Contacts::$FIELDS as $field) {
714 if (!isset($data[$field])) {
715 throw new RestException(400,
"$field field missing");
717 $contact[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage categories.
_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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
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)
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.