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.fk_soc IN (".$this->db->sanitize($socids).
")";
213 if ($search_sale && $search_sale !=
'-1') {
214 if ($search_sale == -2) {
215 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
216 } elseif ($search_sale > 0) {
217 $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).
")";
223 $searchCategoryContactList = $category ? array($category) : array();
226 if (!empty($searchCategoryContactList)) {
227 $searchCategoryContactSqlList = array();
229 foreach ($searchCategoryContactList as $searchCategoryContact) {
230 if (intval($searchCategoryContact) == -2) {
231 $searchCategoryContactSqlList[] =
"NOT EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX.
"categorie_contact as ck WHERE t.rowid = ck.fk_socpeople)";
232 } elseif (intval($searchCategoryContact) > 0) {
234 $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).
")";
248 if (!empty($searchCategoryContactSqlList)) {
249 $sql .=
" AND (".implode(
' AND ', $searchCategoryContactSqlList).
")";
260 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
265 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
267 $sql .= $this->db->order($sortfield, $sortorder);
273 $offset = $limit * $page;
275 $sql .= $this->db->plimit($limit + 1, $offset);
277 $result = $this->db->query($sql);
279 $num = $this->db->num_rows($result);
280 $min = min($num, ($limit <= 0 ? $num : $limit));
283 $obj = $this->db->fetch_object($result);
284 $contact_static =
new Contact($this->db);
285 if ($contact_static->fetch($obj->rowid)) {
286 $contact_static->fetchRoles();
288 $contact_static->load_ref_elements();
291 $contact_static->fetchRoles();
293 if (isModEnabled(
'mailing')) {
294 $contact_static->getNoEmail();
303 throw new RestException(503,
'Error when retrieve contacts : '.$sql);
307 if ($pagination_data) {
308 $totalsResult = $this->db->query($sqlTotals);
309 $total = $this->db->fetch_object($totalsResult)->total;
314 $obj_ret[
'data'] = $tmp;
315 $obj_ret[
'pagination'] = [
316 'total' => (int) $total,
318 'page_count' => ceil((
int) $total / $limit),
338 public function post($request_data =
null)
340 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
341 throw new RestException(403,
'No permission to create/update contacts');
344 $result = $this->
_validate($request_data);
346 foreach ($request_data as $field => $value) {
347 if ($field ===
'caller') {
349 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
352 if ($field ==
'array_options' && is_array($value)) {
353 foreach ($value as $index => $val) {
354 $this->contact->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->contact);
359 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
361 if ($this->contact->create(DolibarrApiAccess::$user) < 0) {
362 throw new RestException(500,
"Error creating contact", array_merge(array($this->contact->error), $this->contact->errors));
364 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
365 $this->contact->setNoEmail($this->contact->no_email);
367 return $this->contact->id;
385 public function put(
$id, $request_data =
null)
387 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
388 throw new RestException(403,
'No permission to create/update contacts');
391 $result = $this->contact->fetch(
$id);
393 throw new RestException(404,
'Contact not found');
397 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
400 foreach ($request_data as $field => $value) {
401 if ($field ==
'id') {
404 if ($field ===
'caller') {
406 $this->contact->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
409 if ($field ==
'array_options' && is_array($value)) {
410 foreach ($value as $index => $val) {
411 $this->contact->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->contact);
416 $this->contact->$field = $this->
_checkValForAPI($field, $value, $this->contact);
419 if (isModEnabled(
'mailing') && !empty($this->contact->email) && isset($this->contact->no_email)) {
420 $this->contact->setNoEmail($this->contact->no_email);
423 if ($this->contact->update(
$id, DolibarrApiAccess::$user, 0,
'update') > 0) {
424 return $this->
get(
$id);
426 throw new RestException(500, $this->contact->error);
440 public function delete(
$id)
442 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'supprimer')) {
443 throw new RestException(403,
'No permission to delete contacts');
445 $result = $this->contact->fetch(
$id);
447 throw new RestException(404,
'Contact not found');
451 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
453 $this->contact->oldcopy = clone $this->contact;
455 if ($this->contact->delete(DolibarrApiAccess::$user) <= 0) {
456 throw new RestException(500,
'Error when delete contact ' . $this->contact->error);
462 'message' =>
'Contact deleted'
487 if (!isset($request_data[
"login"])) {
488 throw new RestException(400,
"login field missing");
490 if (!isset($request_data[
"password"])) {
491 throw new RestException(400,
"password field missing");
494 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'lire')) {
495 throw new RestException(403,
'No permission to read contacts');
497 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
498 throw new RestException(403,
'No permission to create user');
501 $contact =
new Contact($this->db);
502 $contact->fetch(
$id);
503 if ($contact->id <= 0) {
504 throw new RestException(404,
'Contact not found');
508 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
512 $login = $request_data[
"login"];
513 $password = $request_data[
"password"];
514 $useraccount =
new User($this->db);
515 $result = $useraccount->create_from_contact($contact, $login, $password);
517 throw new RestException(500,
"User not created");
520 $useraccount->setPassword($useraccount, $password);
540 public function getCategories(
$id, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
542 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
543 throw new RestException(403);
548 $result = $categories->getListForItem(
$id,
'contact', $sortfield, $sortorder, $limit, $page);
551 throw new RestException(503,
'Error when retrieve category list : '.$categories->error);
574 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
575 throw new RestException(403,
'Insufficient rights');
578 $result = $this->contact->fetch(
$id);
580 throw new RestException(404,
'Contact not found');
583 $result = $category->fetch($category_id);
585 throw new RestException(404,
'category not found');
589 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
592 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
595 $category->add_type($this->contact,
'contact');
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->del_type($this->contact,
'contact');
656 unset(
$object->total_localtax1);
657 unset(
$object->total_localtax2);
677 foreach (Contacts::$FIELDS as $field) {
678 if (!isset($data[$field])) {
679 throw new RestException(400,
"$field field missing");
681 $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...