2015-05-03 21:12:18 +02:00
< ? php
2019-02-23 19:09:27 +01:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
2024-12-17 17:59:21 +01:00
* Copyright ( C ) 2019 - 2024 Frédéric France < frederic . france @ free . fr >
2024-03-15 13:02:35 +01:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2015-05-03 21:12:18 +02: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 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2015-05-03 21:12:18 +02:00
*/
use Luracast\Restler\RestException ;
2017-10-22 15:01:35 +02:00
//require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
//require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
2015-05-03 21:12:18 +02:00
/**
2016-06-13 23:24:54 +02:00
* API class for contacts
2015-05-03 21:12:18 +02:00
*
2017-06-15 11:17:02 +02:00
* @ access protected
2015-05-03 21:12:18 +02:00
* @ class DolibarrApiAccess { @ requires user , external }
*/
2016-06-13 23:24:54 +02:00
class Contacts extends DolibarrApi
2015-05-03 21:12:18 +02:00
{
/**
*
2024-11-06 23:57:45 +01:00
* @ var string [] $FIELDS Mandatory fields , checked when create and update object
2015-05-03 21:12:18 +02:00
*/
2021-02-26 21:17:52 +01:00
public static $FIELDS = array (
2018-08-15 14:28:34 +02:00
'lastname' ,
2015-05-03 21:12:18 +02:00
);
/**
* @ var Contact $contact { @ type Contact }
*/
public $contact ;
/**
* Constructor
*/
2020-10-31 14:32:18 +01:00
public function __construct ()
2017-06-15 21:51:31 +02:00
{
2015-05-03 21:12:18 +02:00
global $db , $conf ;
$this -> db = $db ;
2017-06-25 17:09:08 +02:00
2017-10-22 14:48:42 +02:00
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2017-06-25 17:09:08 +02:00
2015-05-03 21:12:18 +02:00
$this -> contact = new Contact ( $this -> db );
}
/**
* Get properties of a contact object
*
2024-01-12 17:18:52 +01:00
* Return an array with contact information
2015-05-03 21:12:18 +02:00
*
2023-09-26 18:43:25 +02:00
* @ param int $id ID of contact
2019-02-14 16:22:42 +01:00
* @ param int $includecount Count and return also number of elements the contact is used as a link for
2020-11-23 23:31:46 +01:00
* @ param int $includeroles Includes roles of the contact
2024-05-03 18:18:39 +02:00
* @ return object data without useless information
2017-06-15 11:17:02 +02:00
*
2023-09-26 18:43:25 +02:00
* @ throws RestException
2015-05-03 21:12:18 +02:00
*/
2020-11-23 23:31:46 +01:00
public function get ( $id , $includecount = 0 , $includeroles = 0 )
2017-06-15 21:51:31 +02:00
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'lire' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'No permission to read contacts' );
2015-05-03 21:12:18 +02:00
}
2021-04-08 19:05:28 +02:00
2021-06-05 09:37:38 +02:00
if ( $id === 0 ) {
2019-09-30 22:09:11 +02:00
$result = $this -> contact -> initAsSpecimen ();
2019-09-07 14:48:25 +02:00
} else {
$result = $this -> contact -> fetch ( $id );
}
2019-02-14 16:22:42 +01:00
2021-02-26 21:17:52 +01:00
if ( ! $result ) {
2015-05-03 21:12:18 +02:00
throw new RestException ( 404 , 'Contact not found' );
}
2021-02-26 21:17:52 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id , 'socpeople&societe' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2015-05-03 21:12:18 +02:00
}
2020-11-23 22:55:36 +01:00
2021-02-26 21:17:52 +01:00
if ( $includecount ) {
2020-10-31 14:32:18 +01:00
$this -> contact -> load_ref_elements ();
2019-02-14 16:22:42 +01:00
}
2020-11-23 23:33:45 +01:00
2021-02-26 21:17:52 +01:00
if ( $includeroles ) {
2020-11-23 23:31:46 +01:00
$this -> contact -> fetchRoles ();
}
2022-08-20 13:16:54 +02:00
2022-08-20 13:16:04 +02:00
if ( isModEnabled ( 'mailing' )) {
$this -> contact -> getNoEmail ();
}
2019-02-14 16:22:42 +01:00
2015-05-03 21:12:18 +02:00
return $this -> _cleanObjectDatas ( $this -> contact );
}
2020-09-18 17:52:48 +02:00
/**
* Get properties of a contact object by Email
*
2023-09-26 18:43:25 +02:00
* @ param string $email Email of contact
2020-09-18 17:52:48 +02:00
* @ param int $includecount Count and return also number of elements the contact is used as a link for
2020-11-23 23:31:46 +01:00
* @ param int $includeroles Includes roles of the contact
2023-09-26 18:43:25 +02:00
* @ return array | mixed data without useless information
2020-09-18 17:52:48 +02:00
*
* @ url GET email / { email }
*
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 User or group not found
*/
2020-11-23 23:31:46 +01:00
public function getByEmail ( $email , $includecount = 0 , $includeroles = 0 )
2020-09-18 17:52:48 +02:00
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'lire' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'No permission to read contacts' );
2020-09-18 17:52:48 +02:00
}
2021-04-08 19:05:28 +02:00
2020-09-18 17:52:48 +02:00
if ( empty ( $email )) {
$result = $this -> contact -> initAsSpecimen ();
} else {
2024-11-06 23:57:45 +01:00
$result = $this -> contact -> fetch ( 0 , null , '' , $email );
2020-09-18 17:52:48 +02:00
}
2021-02-26 21:17:52 +01:00
if ( ! $result ) {
2020-09-18 17:52:48 +02:00
throw new RestException ( 404 , 'Contact not found' );
}
2021-02-26 21:17:52 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id , 'socpeople&societe' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-09-18 17:52:48 +02:00
}
2020-11-23 22:55:36 +01:00
2021-02-26 21:17:52 +01:00
if ( $includecount ) {
2020-11-23 23:31:46 +01:00
$this -> contact -> load_ref_elements ();
2020-11-23 22:53:56 +01:00
}
2020-09-18 17:52:48 +02:00
2021-02-26 21:17:52 +01:00
if ( $includeroles ) {
2020-11-23 23:31:46 +01:00
$this -> contact -> fetchRoles ();
2020-09-18 17:52:48 +02:00
}
2022-08-20 13:16:54 +02:00
2022-08-20 13:16:04 +02:00
if ( isModEnabled ( 'mailing' )) {
$this -> contact -> getNoEmail ();
}
2020-09-18 17:52:48 +02:00
return $this -> _cleanObjectDatas ( $this -> contact );
}
2015-05-03 21:12:18 +02:00
/**
* List contacts
2017-06-15 11:17:02 +02:00
*
2015-05-03 21:12:18 +02:00
* Get a list of contacts
2017-06-15 11:17:02 +02:00
*
2023-09-26 18:43:25 +02:00
* @ 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 Thirdparty 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
2019-02-14 17:23:48 +01:00
* @ param string $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " (t.ref:like:'SO-%') and (t.date_creation:<:'20160101') "
2019-02-15 15:10:10 +01:00
* @ param int $includecount Count and return also number of elements the contact is used as a link for
2023-09-26 18:43:25 +02:00
* @ param int $includeroles Includes roles of the contact
2024-01-12 17:18:52 +01:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
2024-08-20 10:18:06 +02:00
* @ 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 *
2024-03-15 13:02:35 +01:00
* @ return Contact [] Array of contact objects
2020-10-31 14:32:18 +01:00
*
2015-05-06 00:55:42 +02:00
* @ throws RestException
2020-10-31 14:32:18 +01:00
*/
2024-08-20 10:18:06 +02: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 14:32:18 +01:00
{
2015-05-03 21:12:18 +02:00
global $db , $conf ;
$obj_ret = array ();
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'lire' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'No permission to read contacts' );
2017-06-15 11:17:02 +02:00
}
2020-10-31 14:32:18 +01:00
// case of external user, $thirdparty_ids param is ignored and replaced by user's socid
2017-06-15 21:51:31 +02:00
$socids = DolibarrApiAccess :: $user -> socid ? DolibarrApiAccess :: $user -> socid : $thirdparty_ids ;
2015-05-03 21:12:18 +02:00
// If the internal user must only see his customers, force searching by him
2016-12-07 19:02:39 +01:00
$search_sale = 0 ;
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'client' , 'voir' ) && ! $socids ) {
2015-05-03 21:12:18 +02:00
$search_sale = DolibarrApiAccess :: $user -> id ;
2021-02-26 21:17:52 +01:00
}
2015-05-03 21:12:18 +02:00
2016-10-25 18:33:45 +02:00
$sql = " SELECT t.rowid " ;
2020-02-21 17:53:37 +01: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 12:05:59 +01:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'contact' ) . ')' ;
2021-02-26 21:17:52 +01:00
if ( $socids ) {
2021-03-22 11:30:18 +01:00
$sql .= " AND t.fk_soc IN ( " . $this -> db -> sanitize ( $socids ) . " ) " ;
2021-02-26 21:17:52 +01:00
}
2024-01-09 10:44:50 +01: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 21:12:18 +02:00
}
2020-10-31 14:32:18 +01:00
// Select contacts of given category
if ( $category > 0 ) {
2024-12-17 13:33:29 +01:00
// Search Contact Categories
$searchCategoryContactList = $category ? array ( $category ) : array ();
2024-12-17 17:59:21 +01:00
// $searchCategoryContactOperator = 0;
2024-12-17 13:33:29 +01:00
// Search for tag/category ($searchCategoryContactList is an array of ID)
if ( ! empty ( $searchCategoryContactList )) {
$searchCategoryContactSqlList = array ();
2024-12-17 17:59:21 +01:00
// $listofcategoryid = '';
2024-12-17 13:33:29 +01: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 17:59:21 +01:00
// if ($searchCategoryContactOperator == 0) {
2024-12-17 13:33:29 +01: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 17:59:21 +01:00
// } else {
// $listofcategoryid .= ($listofcategoryid ? ', ' : '') .((int) $searchCategoryContact);
// }
2024-12-17 13:33:29 +01:00
}
}
2024-12-17 17:59:21 +01: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 13:33:29 +01:00
}
2024-12-17 17:59:21 +01:00
// }
2024-12-17 13:33:29 +01:00
}
2020-10-31 14:32:18 +01:00
}
2020-04-15 18:26:07 +02:00
2020-10-31 14:32:18 +01:00
// Add sql filters
2021-02-26 21:17:52 +01:00
if ( $sqlfilters ) {
2021-12-20 20:49:32 +01:00
$errormessage = '' ;
2023-02-25 19:48:33 +01:00
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage );
if ( $errormessage ) {
throw new RestException ( 400 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
2020-10-31 14:32:18 +01:00
}
}
2017-06-15 11:17:02 +02:00
2024-08-20 10:18:06 +02: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 14:32:18 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
2015-05-03 21:12:18 +02:00
2021-02-26 21:17:52 +01:00
if ( $limit ) {
if ( $page < 0 ) {
2015-05-03 21:12:18 +02:00
$page = 0 ;
}
$offset = $limit * $page ;
2020-09-19 23:30:29 +02:00
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
2015-05-03 21:12:18 +02:00
}
2020-09-19 23:30:29 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 21:17:52 +01:00
if ( $result ) {
2020-09-19 23:30:29 +02:00
$num = $this -> db -> num_rows ( $result );
2017-05-22 10:45:02 +02:00
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
2020-10-31 14:32:18 +01:00
$i = 0 ;
2021-02-26 21:17:52 +01:00
while ( $i < $min ) {
2020-09-19 23:30:29 +02:00
$obj = $this -> db -> fetch_object ( $result );
$contact_static = new Contact ( $this -> db );
2021-02-26 21:17:52 +01:00
if ( $contact_static -> fetch ( $obj -> rowid )) {
2020-11-22 16:25:38 +01:00
$contact_static -> fetchRoles ();
2021-02-26 21:17:52 +01:00
if ( $includecount ) {
2020-10-31 14:32:18 +01:00
$contact_static -> load_ref_elements ();
2020-11-23 23:33:45 +01:00
}
2021-02-26 21:17:52 +01:00
if ( $includeroles ) {
2020-11-23 23:31:46 +01:00
$contact_static -> fetchRoles ();
2020-10-31 14:32:18 +01:00
}
2022-08-20 13:16:04 +02:00
if ( isModEnabled ( 'mailing' )) {
$contact_static -> getNoEmail ();
}
2020-11-23 23:31:46 +01:00
2023-09-26 18:04:48 +02:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $contact_static ), $properties );
2019-02-14 21:12:00 +01:00
}
2019-03-02 00:14:22 +01:00
2015-05-03 21:12:18 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-02-21 17:53:37 +01:00
throw new RestException ( 503 , 'Error when retrieve contacts : ' . $sql );
2015-05-03 21:12:18 +02:00
}
2024-08-20 10:18:06 +02: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 21:12:18 +02:00
}
2024-08-20 10:18:06 +02:00
2015-05-03 21:12:18 +02:00
return $obj_ret ;
}
/**
* Create contact object
*
2015-05-06 00:55:42 +02:00
* @ param array $request_data Request datas
2024-11-06 23:57:45 +01:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2015-05-06 00:55:42 +02:00
* @ return int ID of contact
2024-03-15 13:02:35 +01:00
*
* @ suppress PhanPluginUnknownArrayMethodParamType Luracast limitation
2015-05-03 21:12:18 +02:00
*/
2020-10-31 14:32:18 +01:00
public function post ( $request_data = null )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'creer' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'No permission to create/update contacts' );
2015-05-03 21:12:18 +02:00
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
2021-02-26 21:17:52 +01:00
foreach ( $request_data as $field => $value ) {
2023-12-15 12:15:33 +01:00
if ( $field === 'caller' ) {
2024-01-12 17:18:52 +01: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 12:28:55 +02:00
$this -> contact -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 12:15:33 +01:00
continue ;
}
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2024-04-02 12:28:55 +02:00
$this -> contact -> array_options [ $index ] = $this -> _checkValForAPI ( 'extrafields' , $val , $this -> contact );
2023-12-15 12:15:33 +01:00
}
continue ;
}
$this -> contact -> $field = $this -> _checkValForAPI ( $field , $value , $this -> contact );
2015-05-03 21:12:18 +02:00
}
2016-12-23 02:08:22 +01:00
if ( $this -> contact -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 500 , " Error creating contact " , array_merge ( array ( $this -> contact -> error ), $this -> contact -> errors ));
2016-12-23 02:08:22 +01:00
}
2022-08-20 13:24:38 +02:00
if ( isModEnabled ( 'mailing' ) && ! empty ( $this -> contact -> email ) && isset ( $this -> contact -> no_email )) {
2022-08-20 13:16:04 +02:00
$this -> contact -> setNoEmail ( $this -> contact -> no_email );
}
2016-12-23 02:08:22 +01:00
return $this -> contact -> id ;
2015-05-03 21:12:18 +02:00
}
/**
* Update contact
*
2024-02-22 01:32:55 +01:00
* @ param int $id Id of contact to update
* @ param array $request_data Datas
2024-11-06 23:57:45 +01:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-03-15 13:02:35 +01:00
* @ return Object | false Updated object , false when issue toupdate
2024-05-03 18:24:03 +02:00
*
2024-05-03 18:03:14 +02:00
* @ throws RestException 401
* @ throws RestException 404
* @ throws RestException 500
2015-05-03 21:12:18 +02:00
*/
2020-10-31 14:32:18 +01:00
public function put ( $id , $request_data = null )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'creer' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'No permission to create/update contacts' );
2015-05-03 21:12:18 +02:00
}
$result = $this -> contact -> fetch ( $id );
2021-02-26 21:17:52 +01:00
if ( ! $result ) {
2015-05-03 21:12:18 +02:00
throw new RestException ( 404 , 'Contact not found' );
}
2021-02-26 21:17:52 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id , 'socpeople&societe' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2015-05-03 21:12:18 +02:00
}
2021-02-26 21:17:52 +01:00
foreach ( $request_data as $field => $value ) {
if ( $field == 'id' ) {
continue ;
2023-12-15 12:15:33 +01:00
}
if ( $field === 'caller' ) {
2024-01-12 17:18:52 +01: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 12:28:55 +02:00
$this -> contact -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 12:15:33 +01:00
continue ;
}
if ( $field == 'array_options' && is_array ( $value )) {
2023-03-14 23:01:54 +01:00
foreach ( $value as $index => $val ) {
2025-01-16 10:37:29 +01:00
$this -> contact -> array_options [ $index ] = $this -> _checkValForAPI ( $field , $val , $this -> contact );
2023-03-14 23:01:54 +01:00
}
2023-12-15 12:15:33 +01:00
continue ;
2021-02-26 21:17:52 +01:00
}
2023-12-15 12:15:33 +01:00
$this -> contact -> $field = $this -> _checkValForAPI ( $field , $value , $this -> contact );
2015-05-03 21:12:18 +02:00
}
2022-08-20 13:16:54 +02:00
2022-08-20 13:24:38 +02:00
if ( isModEnabled ( 'mailing' ) && ! empty ( $this -> contact -> email ) && isset ( $this -> contact -> no_email )) {
2022-08-20 13:16:04 +02:00
$this -> contact -> setNoEmail ( $this -> contact -> no_email );
}
2015-05-03 21:12:18 +02:00
2024-05-03 18:03:14 +02:00
if ( $this -> contact -> update ( $id , DolibarrApiAccess :: $user , 0 , 'update' ) > 0 ) {
2015-05-03 21:12:18 +02:00
return $this -> get ( $id );
2024-05-03 18:03:14 +02:00
} else {
throw new RestException ( 500 , $this -> contact -> error );
2021-02-26 21:17:52 +01:00
}
2015-05-03 21:12:18 +02:00
}
/**
* Delete contact
*
2015-05-06 00:55:42 +02:00
* @ param int $id Contact ID
2024-10-04 11:49:44 +02:00
* @ return array []
2024-11-06 23:57:45 +01:00
* @ phan - return array < string , array { code : int , message : string } >
* @ phpstan - return array < string , array { code : int , message : string } >
2015-05-03 21:12:18 +02:00
*/
2020-10-31 14:32:18 +01:00
public function delete ( $id )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'supprimer' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'No permission to delete contacts' );
2015-05-03 21:12:18 +02:00
}
$result = $this -> contact -> fetch ( $id );
2021-02-26 21:17:52 +01:00
if ( ! $result ) {
2015-05-03 21:12:18 +02:00
throw new RestException ( 404 , 'Contact not found' );
}
2021-02-26 21:17:52 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'contact' , $this -> contact -> id , 'socpeople&societe' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2015-05-03 21:12:18 +02:00
}
2024-11-06 23:57:45 +01:00
$this -> contact -> oldcopy = clone $this -> contact ; // @phan-suppress-current-line PhanTypeMismatchProperty
2024-10-04 11:49:44 +02:00
2024-10-07 15:17:49 +02:00
if ( $this -> contact -> delete ( DolibarrApiAccess :: $user ) <= 0 ) {
2024-10-04 11:49:44 +02:00
throw new RestException ( 500 , 'Error when delete contact ' . $this -> contact -> error );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Contact deleted'
)
);
2015-05-03 21:12:18 +02:00
}
2016-06-17 14:35:35 +02:00
/**
2017-11-10 10:15:50 +01:00
* Create an user account object from contact ( external user )
2016-06-17 14:35:35 +02:00
*
2023-09-26 18:43:25 +02:00
* @ param int $id Id of contact
2016-06-17 14:35:35 +02:00
* @ param array $request_data Request datas
2024-11-06 23:57:45 +01:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2016-06-17 14:35:35 +02:00
* @ return int ID of user
*
* @ url POST { id } / createUser
2024-03-15 13:02:35 +01:00
* @ suppress PhanPluginUnknownArrayMethodParamType Luracast limitation
2016-06-17 14:35:35 +02:00
*/
2020-10-31 14:32:18 +01:00
public function createUser ( $id , $request_data = null )
{
2023-06-19 20:08:27 +02:00
//if (!DolibarrApiAccess::$user->hasRight('user', 'user', 'creer')) {
2024-02-01 19:16:58 +01:00
//throw new RestException(403);
2020-10-31 14:32:18 +01:00
//}
2021-02-26 21:17:52 +01:00
if ( ! isset ( $request_data [ " login " ])) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 400 , " login field missing " );
2021-02-26 21:17:52 +01:00
}
if ( ! isset ( $request_data [ " password " ])) {
2020-10-31 14:32:18 +01:00
throw new RestException ( 400 , " password field missing " );
2021-02-26 21:17:52 +01:00
}
2020-10-31 14:32:18 +01:00
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'lire' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 , 'No permission to read contacts' );
2020-10-31 14:32:18 +01:00
}
2023-06-19 20:08:27 +02:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 , 'No permission to create user' );
2020-10-31 14:32:18 +01: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 13:34:55 +01:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 14:32:18 +01: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 14:35:35 +02:00
}
2017-06-15 11:17:02 +02:00
2020-10-31 14:32:18 +01:00
/**
* Get categories for a contact
*
* @ 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
*
* @ return mixed
*
* @ url GET { id } / categories
*/
public function getCategories ( $id , $sortfield = " s.rowid " , $sortorder = 'ASC' , $limit = 0 , $page = 0 )
2017-10-03 13:07:07 +02:00
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'categorie' , 'lire' )) {
2024-02-01 19:16:58 +01:00
throw new RestException ( 403 );
2017-10-03 19:09:46 +02:00
}
2017-10-03 19:01:54 +02:00
$categories = new Categorie ( $this -> db );
2017-10-03 13:07:07 +02:00
2017-10-03 19:01:54 +02:00
$result = $categories -> getListForItem ( $id , 'contact' , $sortfield , $sortorder , $limit , $page );
2017-10-03 13:07:07 +02:00
2017-10-03 19:01:54 +02:00
if ( $result < 0 ) {
throw new RestException ( 503 , 'Error when retrieve category list : ' . $categories -> error );
}
return $result ;
2020-10-31 18:51:30 +01:00
}
/**
* Add a category to a contact
*
2023-12-02 10:20:38 +01:00
* @ url PUT { id } / categories / { category_id }
2020-10-31 18:51:30 +01:00
*
* @ param int $id Id of contact
* @ param int $category_id Id of category
*
* @ return mixed
*
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 Category or contact not found
*/
public function addCategory ( $id , $category_id )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'creer' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'Insufficient rights' );
2020-10-31 18:51:30 +01: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 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 18:51:30 +01:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'category' , $category -> id )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 18:51:30 +01:00
}
$category -> add_type ( $this -> contact , 'contact' );
return $this -> _cleanObjectDatas ( $this -> contact );
}
/**
* Remove the link between a category and a contact
*
* @ url DELETE { id } / categories / { category_id }
*
* @ param int $id Id of contact
* @ param int $category_id Id of category
* @ return mixed
*
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 Category or contact not found
*/
public function deleteCategory ( $id , $category_id )
{
2024-02-09 15:58:49 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'contact' , 'creer' )) {
2024-04-07 14:59:25 +02:00
throw new RestException ( 403 , 'Insufficient rights' );
2020-10-31 18:51:30 +01: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 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 18:51:30 +01:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'category' , $category -> id )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-10-31 18:51:30 +01:00
}
$category -> del_type ( $this -> contact , 'contact' );
return $this -> _cleanObjectDatas ( $this -> contact );
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
*
* @ param Object $object Object to clean
* @ return Object Object with cleaned properties
*/
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
*
2024-03-15 13:02:35 +01:00
* @ param string [] | null $data Data to validate
* @ return string []
2020-10-31 18:51:30 +01:00
* @ throws RestException
*/
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 21:12:18 +02:00
}