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.fk_soc IN (".$this->db->sanitize($socids).
")";
215 if ($search_sale && $search_sale !=
'-1') {
216 if ($search_sale == -2) {
217 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
218 } elseif ($search_sale > 0) {
219 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
225 $searchCategoryContactList = $category ? array($category) : array();
228 if (!empty($searchCategoryContactList)) {
229 $searchCategoryContactSqlList = array();
231 foreach ($searchCategoryContactList as $searchCategoryContact) {
232 if (intval($searchCategoryContact) == -2) {
233 $searchCategoryContactSqlList[] =
"NOT EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX.
"categorie_contact as ck WHERE t.rowid = ck.fk_socpeople)";
234 } elseif (intval($searchCategoryContact) > 0) {
236 $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).
")";
250 if (!empty($searchCategoryContactSqlList)) {
251 $sql .=
" AND (".implode(
' AND ', $searchCategoryContactSqlList).
")";
262 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
267 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
269 $sql .= $this->db->order($sortfield, $sortorder);
275 $offset = $limit * $page;
277 $sql .= $this->db->plimit($limit + 1, $offset);
279 $result = $this->db->query($sql);
281 $num = $this->db->num_rows($result);
282 $min = min($num, ($limit <= 0 ? $num : $limit));
285 $obj = $this->db->fetch_object($result);
286 $contact_static =
new Contact($this->db);
287 if ($contact_static->fetch($obj->rowid)) {
288 $contact_static->fetchRoles();
290 $contact_static->load_ref_elements();
293 $contact_static->fetchRoles();
296 $contact_static->getNoEmail();
305 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
309 if ($pagination_data) {
310 $totalsResult = $this->db->query($sqlTotals);
311 $total = $this->db->fetch_object($totalsResult)->total;
316 $obj_ret[
'data'] = $tmp;
317 $obj_ret[
'pagination'] = [
318 'total' => (int) $total,
320 'page_count' => ceil((
int) $total / $limit),
340 public function post($request_data =
null)
342 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
343 throw new RestException(403,
'No permission to create/update contacts');
346 $result = $this->
_validate($request_data);
349 if (!isset($request_data[
'country_id']) && isset($request_data[
'country_code'])) {
350 $field = strlen($request_data[
'country_code']) > 2 ?
'code_iso' :
'code';
351 $id =
dol_getIdFromCode($this->db, $request_data[
'country_code'],
"c_country", $field,
"rowid");
353 throw new RestException(404,
'Country code not found in database: ' . $this->db->error);
355 $request_data[
'country_id'] =
$id;
358 foreach ($request_data as $field => $value) {
359 if ($field ===
'caller') {
361 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
364 if ($field ==
'array_options' && is_array($value)) {
365 $this->contact->fetch_optionals();
367 foreach ($value as $index => $val) {
372 if ($field ==
'socid') {
373 $new_socid = (int) $value;
374 $loopthirdpartytmp =
new Societe($this->db);
375 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
376 if ($new_thirdparty_result < 1) {
377 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
380 throw new RestException(403,
'Access to socid/thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
384 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
386 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
387 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
389 if (
isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
390 $this->contact->setNoEmail($this->contact->no_email);
392 return $this->contact->id;
410 public function put(
$id, $request_data =
null)
412 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
413 throw new RestException(403,
'No permission to create/update contacts');
416 $result = $this->contact->fetch(
$id);
418 throw new RestException(404,
'Contact not found');
422 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
425 foreach ($request_data as $field => $value) {
426 if ($field ==
'id') {
429 if ($field ===
'caller') {
431 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
434 if ($field ==
'array_options' && is_array($value)) {
435 foreach ($value as $index => $val) {
440 if ($field ==
'socid') {
441 $new_socid = (int) $value;
442 $loopthirdpartytmp =
new Societe($this->db);
443 $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid);
444 if ($new_thirdparty_result < 1) {
445 throw new RestException(404,
'Thirdparty with id='.$new_socid.
' not found or not allowed');
448 throw new RestException(403,
'Access to socid/thirdparty='.$new_socid.
' is not allowed for login '.DolibarrApiAccess::$user->login);
452 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
455 if (
isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
456 $this->contact->setNoEmail($this->contact->no_email);
459 if ($this->contact->update(
$id, DolibarrApiAccess::$user, 0,
'update') > 0) {
460 return $this->
get(
$id);
462 throw new RestException(500, $this->contact->error);
476 public function delete(
$id)
478 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'supprimer')) {
479 throw new RestException(403,
'No permission to delete contacts');
481 $result = $this->contact->fetch(
$id);
483 throw new RestException(404,
'Contact not found');
487 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
489 $this->contact->oldcopy = clone $this->contact;
491 if ($this->contact->delete(DolibarrApiAccess::$user) <= 0) {
492 throw new RestException(500,
'Error when delete contact ' . $this->contact->error);
498 'message' =>
'Contact deleted'
523 if (!isset($request_data[
"login"])) {
524 throw new RestException(400,
"login field missing");
526 if (!isset($request_data[
"password"])) {
527 throw new RestException(400,
"password field missing");
530 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
531 throw new RestException(403,
'No permission to read contacts');
533 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
534 throw new RestException(403,
'No permission to create user');
537 $contact =
new Contact($this->db);
538 $contact->fetch(
$id);
539 if ($contact->id <= 0) {
540 throw new RestException(404,
'Contact not found');
544 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
548 $login = $request_data[
"login"];
549 $password = $request_data[
"password"];
550 $useraccount =
new User($this->db);
551 $result = $useraccount->create_from_contact($contact, $login, $password);
553 throw new RestException(500,
"User not created");
556 $useraccount->setPassword($useraccount, $password);
576 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
578 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
579 throw new RestException(403);
584 $result = $categories->getListForItem(
$id,
'contact', $sortfield, $sortorder, $limit, $page);
587 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
610 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
611 throw new RestException(403,
'Insufficient rights');
614 $result = $this->contact->fetch(
$id);
616 throw new RestException(404,
'Contact not found');
619 $result = $category->fetch($category_id);
621 throw new RestException(404,
'category not found');
625 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
628 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
631 $category->add_type($this->contact,
'contact');
652 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
653 throw new RestException(403,
'Insufficient rights');
656 $result = $this->contact->fetch(
$id);
658 throw new RestException(404,
'Contact not found');
661 $result = $category->fetch($category_id);
663 throw new RestException(404,
'category not found');
667 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
670 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
673 $category->del_type($this->contact,
'contact');
695 unset(
$object->total_localtax1);
696 unset(
$object->total_localtax2);
716 foreach (Contacts::$FIELDS as $field) {
717 if (!isset($data[$field])) {
718 throw new RestException(400,
"$field field missing");
720 $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.