21use Luracast\Restler\RestException;
41 public static $FIELDS = array(
58 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
59 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
61 $this->contact =
new Contact($this->db);
78 public function get(
$id, $includecount = 0, $includeroles = 0)
80 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
81 throw new RestException(403,
'No permission to read contacts');
85 $result = $this->contact->initAsSpecimen();
87 $result = $this->contact->fetch(
$id);
91 throw new RestException(404,
'Contact not found');
95 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
99 $this->contact->load_ref_elements();
103 $this->contact->fetchRoles();
106 if (isModEnabled(
'mailing')) {
107 $this->contact->getNoEmail();
128 public function getByEmail($email, $includecount = 0, $includeroles = 0)
130 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
131 throw new RestException(403,
'No permission to read contacts');
135 $result = $this->contact->initAsSpecimen();
137 $result = $this->contact->fetch(0,
null,
'', $email);
141 throw new RestException(404,
'Contact not found');
145 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
149 $this->contact->load_ref_elements();
153 $this->contact->fetchRoles();
156 if (isModEnabled(
'mailing')) {
157 $this->contact->getNoEmail();
185 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)
191 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
192 throw new RestException(403,
'No permission to read contacts');
196 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
200 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
201 $search_sale = DolibarrApiAccess::$user->id;
204 $sql =
"SELECT t.rowid";
205 $sql .=
" FROM ".MAIN_DB_PREFIX.
"socpeople as t";
206 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople_extrafields as te ON te.fk_object = t.rowid";
207 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON t.fk_soc = s.rowid";
208 $sql .=
' WHERE t.entity IN ('.getEntity(
'contact').
')';
210 $sql .=
" AND (t.priv = 0 OR t.fk_user_creat = ".((int) DolibarrApiAccess::$user->
id).
")";
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 NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
218 } elseif ($search_sale > 0) {
219 $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).
")";
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();
295 if (isModEnabled(
'mailing')) {
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);
348 foreach ($request_data as $field => $value) {
349 if ($field ===
'caller') {
351 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
354 if ($field ==
'array_options' && is_array($value)) {
355 foreach ($value as $index => $val) {
356 $this->contact->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->contact);
361 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
363 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
364 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
366 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
367 $this->contact->setNoEmail($this->contact->no_email);
369 return $this->contact->id;
387 public function put(
$id, $request_data =
null)
389 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
390 throw new RestException(403,
'No permission to create/update contacts');
393 $result = $this->contact->fetch(
$id);
395 throw new RestException(404,
'Contact not found');
399 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
402 foreach ($request_data as $field => $value) {
403 if ($field ==
'id') {
406 if ($field ===
'caller') {
408 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
411 if ($field ==
'array_options' && is_array($value)) {
412 foreach ($value as $index => $val) {
413 $this->contact->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->contact);
418 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
421 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
422 $this->contact->setNoEmail($this->contact->no_email);
425 if ($this->contact->update(
$id, DolibarrApiAccess::$user, 0,
'update') > 0) {
426 return $this->
get(
$id);
428 throw new RestException(500, $this->contact->error);
442 public function delete(
$id)
444 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'supprimer')) {
445 throw new RestException(403,
'No permission to delete contacts');
447 $result = $this->contact->fetch(
$id);
449 throw new RestException(404,
'Contact not found');
453 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
455 $this->contact->oldcopy = clone $this->contact;
457 if ($this->contact->delete(DolibarrApiAccess::$user) <= 0) {
458 throw new RestException(500,
'Error when delete contact ' . $this->contact->error);
464 'message' =>
'Contact deleted'
489 if (!isset($request_data[
"login"])) {
490 throw new RestException(400,
"login field missing");
492 if (!isset($request_data[
"password"])) {
493 throw new RestException(400,
"password field missing");
496 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
497 throw new RestException(403,
'No permission to read contacts');
499 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
500 throw new RestException(403,
'No permission to create user');
503 $contact =
new Contact($this->db);
504 $contact->fetch(
$id);
505 if ($contact->id <= 0) {
506 throw new RestException(404,
'Contact not found');
510 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
514 $login = $request_data[
"login"];
515 $password = $request_data[
"password"];
516 $useraccount =
new User($this->db);
517 $result = $useraccount->create_from_contact($contact, $login, $password);
519 throw new RestException(500,
"User not created");
522 $useraccount->setPassword($useraccount, $password);
542 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
544 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
545 throw new RestException(403);
549 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
554 $result = $categories->getListForItem(
$id,
'contact', $sortfield, $sortorder, $limit, $page);
557 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
580 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
581 throw new RestException(403,
'Insufficient rights');
584 $result = $this->contact->fetch(
$id);
586 throw new RestException(404,
'Contact not found');
589 $result = $category->fetch($category_id);
591 throw new RestException(404,
'category not found');
595 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
598 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
601 $category->add_type($this->contact,
'contact');
622 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
623 throw new RestException(403,
'Insufficient rights');
626 $result = $this->contact->fetch(
$id);
628 throw new RestException(404,
'Contact not found');
631 $result = $category->fetch($category_id);
633 throw new RestException(404,
'category not found');
637 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
640 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
643 $category->del_type($this->contact,
'contact');
662 unset(
$object->total_localtax1);
663 unset(
$object->total_localtax2);
683 foreach (Contacts::$FIELDS as $field) {
684 if (!isset($data[$field])) {
685 throw new RestException(400,
"$field field missing");
687 $contact[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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 Dolibarr users.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...