2015-05-03 19:12:18 +00:00
< ? php
2019-02-23 18:09:27 +00:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
2025-11-20 00:47:46 +00:00
* Copyright ( C ) 2019 - 2025 Frédéric France < frederic . france @ free . fr >
2024-03-15 12:02:35 +00:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2025-05-07 09:20:14 +00:00
* Copyright ( C ) 2025 William Mead < william @ m34d . com >
2015-05-03 19:12:18 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2015-05-03 19:12:18 +00:00
*/
use Luracast\Restler\RestException ;
2017-10-22 13:01:35 +00:00
//require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
//require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
2025-12-04 15:51:47 +00:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2015-05-03 19:12:18 +00:00
/**
2016-06-13 21:24:54 +00:00
* API class for contacts
2015-05-03 19:12:18 +00:00
*
2025-05-07 09:20:14 +00:00
* @ since 3.8 . 0 Initial implementation
*
2017-06-15 09:17:02 +00:00
* @ access protected
2015-05-03 19:12:18 +00:00
* @ class DolibarrApiAccess { @ requires user , external }
*/
2016-06-13 21:24:54 +00:00
class Contacts extends DolibarrApi
2015-05-03 19:12:18 +00:00
{
/**
*
2024-11-06 22:57:45 +00:00
* @ var string [] $FIELDS Mandatory fields , checked when create and update object
2015-05-03 19:12:18 +00:00
*/
2021-02-26 20:17:52 +00:00
public static $FIELDS = array (
2018-08-15 12:28:34 +00:00
'lastname' ,
2015-05-03 19:12:18 +00:00
);
/**
* @ var Contact $contact { @ type Contact }
*/
public $contact ;
/**
* Constructor
*/
2020-10-31 13:32:18 +00:00
public function __construct ()
2017-06-15 19:51:31 +00:00
{
2015-05-03 19:12:18 +00:00
global $db , $conf ;
$this -> db = $db ;
2017-06-25 15:09:08 +00:00
2017-10-22 12:48:42 +00:00
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2017-06-25 15:09:08 +00:00
2015-05-03 19:12:18 +00:00
$this -> contact = new Contact ( $this -> db );
}
/**
2025-04-29 04:54:56 +00:00
* Get a contact
2015-05-03 19:12:18 +00:00
*
2024-01-12 16:18:52 +00:00
* Return an array with contact information
2015-05-03 19:12:18 +00:00
*
2025-04-29 04:54:56 +00:00
* @ since 3.8 . 0 Initial implementation
*
* @ param int $id ID of contact
* @ param int $includecount Include count of elements the contact is used as a link for
* @ param int $includeroles Includes roles of the contact
* @ return object Cleaned contact object
2017-06-15 09:17:02 +00:00
*
2023-09-26 16:43:25 +00:00
* @ throws RestException
2015-05-03 19:12:18 +00:00
*/
2020-11-23 22:31:46 +00:00
public function get ( $id , $includecount = 0 , $includeroles = 0 )
2017-06-15 19:51:31 +00:00
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'lire' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'No permission to read contacts' );
2015-05-03 19:12:18 +00:00
}
2021-04-08 17:05:28 +00:00
2021-06-05 07:37:38 +00:00
if ( $id === 0 ) {
2019-09-30 20:09:11 +00:00
$result = $this -> contact -> initAsSpecimen ();
2019-09-07 12:48:25 +00:00
} else {
$result = $this -> contact -> fetch ( $id );
}
2019-02-14 15:22:42 +00:00
2021-02-26 20:17:52 +00:00
if ( ! $result ) {
2015-05-03 19:12:18 +00:00
throw new RestException ( 404 , 'Contact not found' );
}
2021-02-26 20:17:52 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id , 'socpeople&societe' )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2015-05-03 19:12:18 +00:00
}
2020-11-23 21:55:36 +00:00
2021-02-26 20:17:52 +00:00
if ( $includecount ) {
2020-10-31 13:32:18 +00:00
$this -> contact -> load_ref_elements ();
2019-02-14 15:22:42 +00:00
}
2020-11-23 22:33:45 +00:00
2021-02-26 20:17:52 +00:00
if ( $includeroles ) {
2020-11-23 22:31:46 +00:00
$this -> contact -> fetchRoles ();
}
2022-08-20 11:16:54 +00:00
2022-08-20 11:16:04 +00:00
if ( isModEnabled ( 'mailing' )) {
$this -> contact -> getNoEmail ();
}
2019-02-14 15:22:42 +00:00
2015-05-03 19:12:18 +00:00
return $this -> _cleanObjectDatas ( $this -> contact );
}
2020-09-18 15:52:48 +00:00
/**
2025-04-29 04:54:56 +00:00
* Get a contact by Email
*
* @ since 13.0 . 0 Initial implementation
2020-09-18 15:52:48 +00:00
*
2025-04-29 04:54:56 +00:00
* @ param string $email Email of contact
* @ param int $includecount Include count of elements the contact is used as a link for
* @ param int $includeroles Includes roles of the contact
* @ return array | mixed Cleaned contact object
2020-09-18 15:52:48 +00:00
*
* @ url GET email / { email }
*
2025-04-29 04:54:56 +00:00
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 User or group not found
2020-09-18 15:52:48 +00:00
*/
2020-11-23 22:31:46 +00:00
public function getByEmail ( $email , $includecount = 0 , $includeroles = 0 )
2020-09-18 15:52:48 +00:00
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'lire' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'No permission to read contacts' );
2020-09-18 15:52:48 +00:00
}
2021-04-08 17:05:28 +00:00
2020-09-18 15:52:48 +00:00
if ( empty ( $email )) {
$result = $this -> contact -> initAsSpecimen ();
} else {
2024-11-06 22:57:45 +00:00
$result = $this -> contact -> fetch ( 0 , null , '' , $email );
2020-09-18 15:52:48 +00:00
}
2021-02-26 20:17:52 +00:00
if ( ! $result ) {
2020-09-18 15:52:48 +00:00
throw new RestException ( 404 , 'Contact not found' );
}
2021-02-26 20:17:52 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id , 'socpeople&societe' )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-09-18 15:52:48 +00:00
}
2020-11-23 21:55:36 +00:00
2021-02-26 20:17:52 +00:00
if ( $includecount ) {
2020-11-23 22:31:46 +00:00
$this -> contact -> load_ref_elements ();
2020-11-23 21:53:56 +00:00
}
2020-09-18 15:52:48 +00:00
2021-02-26 20:17:52 +00:00
if ( $includeroles ) {
2020-11-23 22:31:46 +00:00
$this -> contact -> fetchRoles ();
2020-09-18 15:52:48 +00:00
}
2022-08-20 11:16:54 +00:00
2022-08-20 11:16:04 +00:00
if ( isModEnabled ( 'mailing' )) {
$this -> contact -> getNoEmail ();
}
2020-09-18 15:52:48 +00:00
return $this -> _cleanObjectDatas ( $this -> contact );
}
2015-05-03 19:12:18 +00:00
/**
* List contacts
2017-06-15 09:17:02 +00:00
*
2015-05-03 19:12:18 +00:00
* Get a list of contacts
2017-06-15 09:17:02 +00:00
*
2025-04-29 04:54:56 +00:00
* @ since 3.8 . 0 Initial implementation
*
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
* @ param string $thirdparty_ids Third party ids to filter contacts of ( example '1' or '1,2,3' ) { @ pattern /^ [ 0 - 9 ,] * $ / i }
* @ param int $category Use this param to filter list by category
2026-05-29 10:16:42 +00:00
* @ param string $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " (t.ref:like:'SO-%') and (t.date_creation:>:'20160101') "
2025-04-29 04:54:56 +00:00
* @ param int $includecount Include count of elements the contact is used as a link for
* @ param int $includeroles Includes roles of the contact
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
* @ param bool $pagination_data If this parameter is set to true , the response will include pagination data . Default value is false . Page starts from 0 *
* @ return Contact [] Array of contact objects
*
* @ throws RestException
2020-10-31 13:32:18 +00:00
*/
2024-08-20 08:18:06 +00:00
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 )
2020-10-31 13:32:18 +00:00
{
2015-05-03 19:12:18 +00:00
global $db , $conf ;
$obj_ret = array ();
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'lire' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'No permission to read contacts' );
2017-06-15 09:17:02 +00:00
}
2020-10-31 13:32:18 +00:00
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid
2025-07-01 18:34:09 +00:00
$socids = DolibarrApiAccess :: $user -> socid ? : $thirdparty_ids ;
2015-05-03 19:12:18 +00:00
// If the internal user must only see his customers, force searching by him
2016-12-07 18:02:39 +00:00
$search_sale = 0 ;
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'client' , 'voir' ) && ! $socids ) {
2015-05-03 19:12:18 +00:00
$search_sale = DolibarrApiAccess :: $user -> id ;
2021-02-26 20:17:52 +00:00
}
2015-05-03 19:12:18 +00:00
2016-10-25 16:33:45 +00:00
$sql = " SELECT t.rowid " ;
2020-02-21 16:53:37 +00:00
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as t " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " socpeople_extrafields as te ON te.fk_object = t.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON t.fk_soc = s.rowid " ;
2022-03-18 11:05:59 +00:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'contact' ) . ')' ;
2021-02-26 20:17:52 +00:00
if ( $socids ) {
2021-03-22 10:30:18 +00:00
$sql .= " AND t.fk_soc IN ( " . $this -> db -> sanitize ( $socids ) . " ) " ;
2021-02-26 20:17:52 +00:00
}
2024-01-09 09:44:50 +00:00
// Search on sale representative
if ( $search_sale && $search_sale != '-1' ) {
if ( $search_sale == - 2 ) {
$sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . " societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc) " ;
} elseif ( $search_sale > 0 ) {
$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 ) . " ) " ;
}
2015-05-03 19:12:18 +00:00
}
2020-10-31 13:32:18 +00:00
// Select contacts of given category
if ( $category > 0 ) {
2024-12-17 12:33:29 +00:00
// Search Contact Categories
$searchCategoryContactList = $category ? array ( $category ) : array ();
2024-12-17 16:59:21 +00:00
// $searchCategoryContactOperator = 0;
2024-12-17 12:33:29 +00:00
// Search for tag/category ($searchCategoryContactList is an array of ID)
if ( ! empty ( $searchCategoryContactList )) {
$searchCategoryContactSqlList = array ();
2024-12-17 16:59:21 +00:00
// $listofcategoryid = '';
2024-12-17 12:33:29 +00:00
foreach ( $searchCategoryContactList as $searchCategoryContact ) {
if ( intval ( $searchCategoryContact ) == - 2 ) {
$searchCategoryContactSqlList [] = " NOT EXISTS (SELECT ck.fk_socpeople FROM " . MAIN_DB_PREFIX . " categorie_contact as ck WHERE t.rowid = ck.fk_socpeople) " ;
} elseif ( intval ( $searchCategoryContact ) > 0 ) {
2024-12-17 16:59:21 +00:00
// if ($searchCategoryContactOperator == 0) {
2024-12-17 12:33:29 +00:00
$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 ) . " ) " ;
2024-12-17 16:59:21 +00:00
// } else {
// $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryContact);
// }
2024-12-17 12:33:29 +00:00
}
}
2024-12-17 16:59:21 +00:00
// if ($listofcategoryid) {
// $searchCategoryContactSqlList[] = " EXISTS (SELECT ck.fk_socpeople FROM ".MAIN_DB_PREFIX."categorie_contact as ck WHERE t.rowid = ck.fk_socpeople AND ck.fk_categorie IN (".$this->db->sanitize($listofcategoryid)."))";
// }
// if ($searchCategoryContactOperator == 1) {
// if (!empty($searchCategoryContactSqlList)) {
// $sql .= " AND (".implode(' OR ', $searchCategoryContactSqlList).")";
// }
// } else {
if ( ! empty ( $searchCategoryContactSqlList )) {
$sql .= " AND ( " . implode ( ' AND ' , $searchCategoryContactSqlList ) . " ) " ;
2024-12-17 12:33:29 +00:00
}
2024-12-17 16:59:21 +00:00
// }
2024-12-17 12:33:29 +00:00
}
2020-10-31 13:32:18 +00:00
}
2020-04-15 16:26:07 +00:00
2020-10-31 13:32:18 +00:00
// Add sql filters
2021-02-26 20:17:52 +00:00
if ( $sqlfilters ) {
2021-12-20 19:49:32 +00:00
$errormessage = '' ;
2023-02-25 18:48:33 +00:00
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage );
if ( $errormessage ) {
throw new RestException ( 400 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
2020-10-31 13:32:18 +00:00
}
}
2017-06-15 09:17:02 +00:00
2024-08-20 08:18:06 +00:00
//this query will return total orders with the filters given
$sqlTotals = str_replace ( 'SELECT t.rowid' , 'SELECT count(t.rowid) as total' , $sql );
2020-10-31 13:32:18 +00:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2015-05-03 19:12:18 +00:00
2021-02-26 20:17:52 +00:00
if ( $limit ) {
if ( $page < 0 ) {
2015-05-03 19:12:18 +00:00
$page = 0 ;
}
$offset = $limit * $page ;
2020-09-19 21:30:29 +00:00
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
2015-05-03 19:12:18 +00:00
}
2020-09-19 21:30:29 +00:00
$result = $this -> db -> query ( $sql );
2021-02-26 20:17:52 +00:00
if ( $result ) {
2020-09-19 21:30:29 +00:00
$num = $this -> db -> num_rows ( $result );
2017-05-22 08:45:02 +00:00
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
2020-10-31 13:32:18 +00:00
$i = 0 ;
2021-02-26 20:17:52 +00:00
while ( $i < $min ) {
2020-09-19 21:30:29 +00:00
$obj = $this -> db -> fetch_object ( $result );
$contact_static = new Contact ( $this -> db );
2021-02-26 20:17:52 +00:00
if ( $contact_static -> fetch ( $obj -> rowid )) {
2020-11-22 15:25:38 +00:00
$contact_static -> fetchRoles ();
2021-02-26 20:17:52 +00:00
if ( $includecount ) {
2020-10-31 13:32:18 +00:00
$contact_static -> load_ref_elements ();
2020-11-23 22:33:45 +00:00
}
2021-02-26 20:17:52 +00:00
if ( $includeroles ) {
2020-11-23 22:31:46 +00:00
$contact_static -> fetchRoles ();
2020-10-31 13:32:18 +00:00
}
2022-08-20 11:16:04 +00:00
if ( isModEnabled ( 'mailing' )) {
$contact_static -> getNoEmail ();
}
2020-11-23 22:31:46 +00:00
2023-09-26 16:04:48 +00:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $contact_static ), $properties );
2019-02-14 20:12:00 +00:00
}
2019-03-01 23:14:22 +00:00
2015-05-03 19:12:18 +00:00
$i ++ ;
}
2020-05-21 13:05:19 +00:00
} else {
2020-02-21 16:53:37 +00:00
throw new RestException ( 503 , 'Error when retrieve contacts : ' . $sql );
2015-05-03 19:12:18 +00:00
}
2024-08-20 08:18:06 +00:00
//if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
if ( $pagination_data ) {
$totalsResult = $this -> db -> query ( $sqlTotals );
$total = $this -> db -> fetch_object ( $totalsResult ) -> total ;
$tmp = $obj_ret ;
$obj_ret = [];
$obj_ret [ 'data' ] = $tmp ;
$obj_ret [ 'pagination' ] = [
'total' => ( int ) $total ,
'page' => $page , //count starts from 0
'page_count' => ceil (( int ) $total / $limit ),
'limit' => $limit
];
2015-05-03 19:12:18 +00:00
}
2024-08-20 08:18:06 +00:00
2015-05-03 19:12:18 +00:00
return $obj_ret ;
}
/**
2025-04-29 04:54:56 +00:00
* Create a contact
2015-05-03 19:12:18 +00:00
*
2025-04-29 04:54:56 +00:00
* @ since 3.8 . 0 Initial implementation
*
* @ param array $request_data Request data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
* @ return int ID of contact
2024-03-15 12:02:35 +00:00
*
* @ suppress PhanPluginUnknownArrayMethodParamType Luracast limitation
2015-05-03 19:12:18 +00:00
*/
2020-10-31 13:32:18 +00:00
public function post ( $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'creer' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'No permission to create/update contacts' );
2015-05-03 19:12:18 +00:00
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
2025-11-20 10:03:08 +00:00
// External api user does not know internal country ID
if ( ! isset ( $request_data [ 'country_id' ]) && isset ( $request_data [ 'country_code' ])) {
$field = strlen ( $request_data [ 'country_code' ]) > 2 ? 'code_iso' : 'code' ;
$id = dol_getIdFromCode ( $this -> db , $request_data [ 'country_code' ], " c_country " , $field , " rowid " );
if ( $id < 0 ) {
throw new RestException ( 404 , 'Country code not found in database: ' . $this -> db -> error );
}
$request_data [ 'country_id' ] = $id ;
}
2021-02-26 20:17:52 +00:00
foreach ( $request_data as $field => $value ) {
2023-12-15 11:15:33 +00:00
if ( $field === 'caller' ) {
2024-01-12 16:18:52 +00:00
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2024-04-02 10:28:55 +00:00
$this -> contact -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
if ( $field == 'array_options' && is_array ( $value )) {
2026-05-08 23:16:35 +00:00
$this -> contact -> fetch_optionals (); // To force the load of the extrafields definition by fetch_name_optionals_label()
2023-12-15 11:15:33 +00:00
foreach ( $value as $index => $val ) {
2026-05-08 23:16:35 +00:00
$this -> contact -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $this -> contact );
2023-12-15 11:15:33 +00:00
}
continue ;
}
2025-12-04 15:51:47 +00:00
if ( $field == 'socid' ) {
$new_socid = ( int ) $value ;
$loopthirdpartytmp = new Societe ( $this -> db );
$new_thirdparty_result = $loopthirdpartytmp -> fetch ( $new_socid );
if ( $new_thirdparty_result < 1 ) {
throw new RestException ( 404 , 'Thirdparty with id=' . $new_socid . ' not found or not allowed' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $new_socid )) {
throw new RestException ( 403 , 'Access to socid/thirdparty=' . $new_socid . ' is not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
}
2023-12-15 11:15:33 +00:00
$this -> contact -> $field = $this -> _checkValForAPI ( $field , $value , $this -> contact );
2015-05-03 19:12:18 +00:00
}
2016-12-23 01:08:22 +00:00
if ( $this -> contact -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2020-10-31 13:32:18 +00:00
throw new RestException ( 500 , " Error creating contact " , array_merge ( array ( $this -> contact -> error ), $this -> contact -> errors ));
2016-12-23 01:08:22 +00:00
}
2022-08-20 11:24:38 +00:00
if ( isModEnabled ( 'mailing' ) && ! empty ( $this -> contact -> email ) && isset ( $this -> contact -> no_email )) {
2022-08-20 11:16:04 +00:00
$this -> contact -> setNoEmail ( $this -> contact -> no_email );
}
2016-12-23 01:08:22 +00:00
return $this -> contact -> id ;
2015-05-03 19:12:18 +00:00
}
/**
2025-04-29 04:54:56 +00:00
* Update a contact
2015-05-03 19:12:18 +00:00
*
2025-04-29 04:54:56 +00:00
* @ since 3.8 . 0 Initial implementation
*
* @ param int $id ID of contact to update
* @ param array $request_data Request data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
* @ return Object | false Updated object , false when error updating contact
2024-05-03 16:24:03 +00:00
*
2024-05-03 16:03:14 +00:00
* @ throws RestException 401
* @ throws RestException 404
* @ throws RestException 500
2015-05-03 19:12:18 +00:00
*/
2020-10-31 13:32:18 +00:00
public function put ( $id , $request_data = null )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'creer' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'No permission to create/update contacts' );
2015-05-03 19:12:18 +00:00
}
$result = $this -> contact -> fetch ( $id );
2021-02-26 20:17:52 +00:00
if ( ! $result ) {
2015-05-03 19:12:18 +00:00
throw new RestException ( 404 , 'Contact not found' );
}
2021-02-26 20:17:52 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id , 'socpeople&societe' )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2015-05-03 19:12:18 +00:00
}
2021-02-26 20:17:52 +00:00
foreach ( $request_data as $field => $value ) {
if ( $field == 'id' ) {
continue ;
2023-12-15 11:15:33 +00:00
}
if ( $field === 'caller' ) {
2024-01-12 16:18:52 +00:00
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2024-04-02 10:28:55 +00:00
$this -> contact -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
if ( $field == 'array_options' && is_array ( $value )) {
2023-03-14 22:01:54 +00:00
foreach ( $value as $index => $val ) {
2026-05-08 23:16:35 +00:00
$this -> contact -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $this -> contact );
2023-03-14 22:01:54 +00:00
}
2023-12-15 11:15:33 +00:00
continue ;
2021-02-26 20:17:52 +00:00
}
2025-12-04 15:51:47 +00:00
if ( $field == 'socid' ) {
$new_socid = ( int ) $value ;
$loopthirdpartytmp = new Societe ( $this -> db );
$new_thirdparty_result = $loopthirdpartytmp -> fetch ( $new_socid );
if ( $new_thirdparty_result < 1 ) {
throw new RestException ( 404 , 'Thirdparty with id=' . $new_socid . ' not found or not allowed' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'societe' , $new_socid )) {
throw new RestException ( 403 , 'Access to socid/thirdparty=' . $new_socid . ' is not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
}
2023-12-15 11:15:33 +00:00
$this -> contact -> $field = $this -> _checkValForAPI ( $field , $value , $this -> contact );
2015-05-03 19:12:18 +00:00
}
2022-08-20 11:16:54 +00:00
2022-08-20 11:24:38 +00:00
if ( isModEnabled ( 'mailing' ) && ! empty ( $this -> contact -> email ) && isset ( $this -> contact -> no_email )) {
2022-08-20 11:16:04 +00:00
$this -> contact -> setNoEmail ( $this -> contact -> no_email );
}
2015-05-03 19:12:18 +00:00
2024-05-03 16:03:14 +00:00
if ( $this -> contact -> update ( $id , DolibarrApiAccess :: $user , 0 , 'update' ) > 0 ) {
2015-05-03 19:12:18 +00:00
return $this -> get ( $id );
2024-05-03 16:03:14 +00:00
} else {
throw new RestException ( 500 , $this -> contact -> error );
2021-02-26 20:17:52 +00:00
}
2015-05-03 19:12:18 +00:00
}
/**
2025-04-29 04:54:56 +00:00
* Delete a contact
2015-05-03 19:12:18 +00:00
*
2025-04-29 04:54:56 +00:00
* @ since 3.8 . 0 Initial implementation
*
* @ param int $id Contact ID
* @ return array []
2024-11-06 22:57:45 +00:00
* @ phan - return array < string , array { code : int , message : string } >
* @ phpstan - return array < string , array { code : int , message : string } >
2015-05-03 19:12:18 +00:00
*/
2020-10-31 13:32:18 +00:00
public function delete ( $id )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'supprimer' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'No permission to delete contacts' );
2015-05-03 19:12:18 +00:00
}
$result = $this -> contact -> fetch ( $id );
2021-02-26 20:17:52 +00:00
if ( ! $result ) {
2015-05-03 19:12:18 +00:00
throw new RestException ( 404 , 'Contact not found' );
}
2021-02-26 20:17:52 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id , 'socpeople&societe' )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2015-05-03 19:12:18 +00:00
}
2024-11-06 22:57:45 +00:00
$this -> contact -> oldcopy = clone $this -> contact ; // @phan-suppress-current-line PhanTypeMismatchProperty
2024-10-04 09:49:44 +00:00
2024-10-07 13:17:49 +00:00
if ( $this -> contact -> delete ( DolibarrApiAccess :: $user ) <= 0 ) {
2024-10-04 09:49:44 +00:00
throw new RestException ( 500 , 'Error when delete contact ' . $this -> contact -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Contact deleted'
)
);
2015-05-03 19:12:18 +00:00
}
2016-06-17 12:35:35 +00:00
/**
2025-04-29 04:54:56 +00:00
* Create a user account object from contact ( external user )
*
* @ since 5.0 . 0 Initial implementation
2016-06-17 12:35:35 +00:00
*
2025-04-29 04:54:56 +00:00
* @ param int $id ID of contact
* @ param array $request_data Request data
2024-11-06 22:57:45 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2025-04-29 04:54:56 +00:00
* @ return int ID of user
2016-06-17 12:35:35 +00:00
*
* @ url POST { id } / createUser
2024-03-15 12:02:35 +00:00
* @ suppress PhanPluginUnknownArrayMethodParamType Luracast limitation
2016-06-17 12:35:35 +00:00
*/
2020-10-31 13:32:18 +00:00
public function createUser ( $id , $request_data = null )
{
2023-06-19 18:08:27 +00:00
//if (!DolibarrApiAccess::$user->hasRight('user', 'user', 'creer')) {
2024-02-01 18:16:58 +00:00
//throw new RestException(403);
2020-10-31 13:32:18 +00:00
//}
2021-02-26 20:17:52 +00:00
if ( ! isset ( $request_data [ " login " ])) {
2020-10-31 13:32:18 +00:00
throw new RestException ( 400 , " login field missing " );
2021-02-26 20:17:52 +00:00
}
if ( ! isset ( $request_data [ " password " ])) {
2020-10-31 13:32:18 +00:00
throw new RestException ( 400 , " password field missing " );
2021-02-26 20:17:52 +00:00
}
2020-10-31 13:32:18 +00:00
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'lire' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'No permission to read contacts' );
2020-10-31 13:32:18 +00:00
}
2023-06-19 18:08:27 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'No permission to create user' );
2020-10-31 13:32:18 +00:00
}
$contact = new Contact ( $this -> db );
$contact -> fetch ( $id );
if ( $contact -> id <= 0 ) {
throw new RestException ( 404 , 'Contact not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $contact -> id , 'socpeople&societe' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 13:32:18 +00:00
}
// Check mandatory fields
$login = $request_data [ " login " ];
$password = $request_data [ " password " ];
$useraccount = new User ( $this -> db );
$result = $useraccount -> create_from_contact ( $contact , $login , $password );
if ( $result <= 0 ) {
throw new RestException ( 500 , " User not created " );
}
// password parameter not used in create_from_contact
$useraccount -> setPassword ( $useraccount , $password );
return $result ;
2016-06-17 12:35:35 +00:00
}
2017-06-15 09:17:02 +00:00
2020-10-31 13:32:18 +00:00
/**
2025-04-29 04:54:56 +00:00
* Get categories of a contact
*
* @ since 5.0 . 0 Initial implementation
2020-10-31 13:32:18 +00:00
*
2025-04-29 04:54:56 +00:00
* @ param int $id ID of contact
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
2020-10-31 13:32:18 +00:00
*
2025-04-29 04:54:56 +00:00
* @ return mixed
2020-10-31 13:32:18 +00:00
*
* @ url GET { id } / categories
*/
public function getCategories ( $id , $sortfield = " s.rowid " , $sortorder = 'ASC' , $limit = 0 , $page = 0 )
2017-10-03 11:07:07 +00:00
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'categorie' , 'lire' )) {
2024-02-01 18:16:58 +00:00
throw new RestException ( 403 );
2017-10-03 17:09:46 +00:00
}
2017-10-03 17:01:54 +00:00
$categories = new Categorie ( $this -> db );
2017-10-03 11:07:07 +00:00
2017-10-03 17:01:54 +00:00
$result = $categories -> getListForItem ( $id , 'contact' , $sortfield , $sortorder , $limit , $page );
2017-10-03 11:07:07 +00:00
2017-10-03 17:01:54 +00:00
if ( $result < 0 ) {
throw new RestException ( 503 , 'Error when retrieve category list : ' . $categories -> error );
}
return $result ;
2020-10-31 17:51:30 +00:00
}
/**
* Add a category to a contact
*
2025-04-29 04:54:56 +00:00
* @ since 11.0 . 0 Initial implementation
*
* @ param int $id ID of contact
* @ param int $category_id ID of category
2020-10-31 17:51:30 +00:00
*
2025-04-29 04:54:56 +00:00
* @ return mixed
2020-10-31 17:51:30 +00:00
*
2025-04-29 04:54:56 +00:00
* @ url PUT { id } / categories / { category_id }
2020-10-31 17:51:30 +00:00
*
2025-04-29 04:54:56 +00:00
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 Category or contact not found
2020-10-31 17:51:30 +00:00
*/
public function addCategory ( $id , $category_id )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'creer' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Insufficient rights' );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contact -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contact not found' );
}
$category = new Categorie ( $this -> db );
$result = $category -> fetch ( $category_id );
if ( ! $result ) {
throw new RestException ( 404 , 'category not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'category' , $category -> id )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
$category -> add_type ( $this -> contact , 'contact' );
return $this -> _cleanObjectDatas ( $this -> contact );
}
/**
* Remove the link between a category and a contact
*
2025-04-29 04:54:56 +00:00
* @ since 11.0 . 0 Initial implementation
2020-10-31 17:51:30 +00:00
*
2025-04-29 04:54:56 +00:00
* @ param int $id ID of contact
* @ param int $category_id ID of category
* @ return mixed
2020-10-31 17:51:30 +00:00
*
2025-04-29 04:54:56 +00:00
* @ url DELETE { id } / categories / { category_id }
*
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 Category or contact not found
2020-10-31 17:51:30 +00:00
*/
public function deleteCategory ( $id , $category_id )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'creer' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Insufficient rights' );
2020-10-31 17:51:30 +00:00
}
$result = $this -> contact -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'Contact not found' );
}
$category = new Categorie ( $this -> db );
$result = $category -> fetch ( $category_id );
if ( ! $result ) {
throw new RestException ( 404 , 'category not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'category' , $category -> id )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 17:51:30 +00:00
}
$category -> del_type ( $this -> contact , 'contact' );
return $this -> _cleanObjectDatas ( $this -> contact );
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
2025-04-29 04:54:56 +00:00
* Clean sensible object data
2025-11-20 00:47:46 +00:00
* @ phpstan - template T
2020-10-31 17:51:30 +00:00
*
2025-04-29 04:54:56 +00:00
* @ param Object $object Object to clean
* @ return Object Object with cleaned properties
2025-11-20 00:47:46 +00:00
* @ phpstan - param T $object
* @ phpstan - return T
2020-10-31 17:51:30 +00:00
*/
protected function _cleanObjectDatas ( $object )
{
// phpcs:enable
$object = parent :: _cleanObjectDatas ( $object );
unset ( $object -> total_ht );
unset ( $object -> total_tva );
unset ( $object -> total_localtax1 );
unset ( $object -> total_localtax2 );
unset ( $object -> total_ttc );
unset ( $object -> note );
unset ( $object -> lines );
unset ( $object -> thirdparty );
return $object ;
}
/**
* Validate fields before create or update object
*
2025-04-29 04:54:56 +00:00
* @ param string [] | null $data Data to validate
* @ return string []
* @ throws RestException
2020-10-31 17:51:30 +00:00
*/
private function _validate ( $data )
{
$contact = array ();
foreach ( Contacts :: $FIELDS as $field ) {
if ( ! isset ( $data [ $field ])) {
throw new RestException ( 400 , " $field field missing " );
}
$contact [ $field ] = $data [ $field ];
}
return $contact ;
}
2015-05-03 19:12:18 +00:00
}