2016-07-07 16:13:45 +02:00
< ? php
2024-11-10 11:16:23 +01:00
/* Copyright ( C ) 2016 Xebax Christy < xebax @ wanadoo . fr >
* Copyright ( C ) 2017 Regis Houssin < regis . houssin @ inodbox . com >
* Copyright ( C ) 2020 Thibault FOUCART < support @ ptibogxiv . net >
* Copyright ( C ) 2020 - 2024 Frédéric France < frederic . france @ free . fr >
2025-01-20 15:55:19 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2016-07-07 16:13:45 +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 />.
2016-07-07 16:13:45 +02:00
*/
use Luracast\Restler\RestException ;
2020-08-29 12:38:22 +02:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2016-07-07 16:13:45 +02:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php' ;
2016-09-26 01:15:17 +02:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/subscription.class.php' ;
2017-10-03 19:01:54 +02:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2024-03-08 11:00:19 +01:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent_type.class.php' ;
2016-07-07 16:13:45 +02:00
2024-03-08 16:32:44 +01:00
2016-07-07 16:13:45 +02:00
/**
* API class for members
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
*/
class Members extends DolibarrApi
{
2020-10-31 18:51:30 +01:00
/**
2025-01-20 15:55:19 +01:00
* @ var string [] Mandatory fields , checked when create and update object
2020-10-31 18:51:30 +01:00
*/
2021-03-01 00:19:52 +01:00
public static $FIELDS = array (
2020-10-31 18:51:30 +01:00
'morphy' ,
'typeid'
);
/**
* Constructor
*/
public function __construct ()
{
2024-01-12 22:09:51 +01:00
global $db ;
2020-10-31 18:51:30 +01:00
$this -> db = $db ;
}
/**
* Get properties of a member object
*
2024-01-12 20:58:09 +01:00
* Return an array with member information
2020-10-31 18:51:30 +01:00
*
2023-09-26 18:43:25 +02:00
* @ param int $id ID of member
* @ return Object Object with cleaned properties
2020-10-31 18:51:30 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Member not found
2020-10-31 18:51:30 +01:00
*/
public function get ( $id )
{
2022-12-21 08:41:19 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
2016-07-07 16:13:45 +02:00
2020-10-30 07:57:53 +01:00
$member = new Adherent ( $this -> db );
if ( $id == 0 ) {
$result = $member -> initAsSpecimen ();
} else {
$result = $member -> fetch ( $id );
}
2020-10-31 18:51:30 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
2020-10-31 20:33:59 +01:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> id ) && $id > 0 ) {
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
}
return $this -> _cleanObjectDatas ( $member );
}
/**
* Get properties of a member object by linked thirdparty
*
2024-01-12 20:58:09 +01:00
* Return an array with member information
2020-10-31 18:51:30 +01:00
*
2023-09-26 18:43:25 +02:00
* @ param int $thirdparty ID of third party
2020-10-31 18:51:30 +01:00
*
2023-09-26 18:43:25 +02:00
* @ return Object Data without useless information
2020-10-31 18:51:30 +01:00
*
* @ url GET thirdparty / { thirdparty }
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Member not found
2020-10-31 18:51:30 +01:00
*/
public function getByThirdparty ( $thirdparty )
{
2022-12-21 08:41:19 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
$member = new Adherent ( $this -> db );
2024-07-31 18:54:52 +02:00
$result = $member -> fetch ( 0 , '' , $thirdparty );
2020-10-31 18:51:30 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> 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
}
return $this -> _cleanObjectDatas ( $member );
}
2024-10-07 14:20:27 +02:00
/**
* Get properties of a member object by linked thirdparty account
*
* @ param string $site Site key
* @ param string $key_account Key of account
*
* @ return array | mixed
* @ throws RestException 401 Unauthorized : User does not have permission to read thirdparties
* @ throws RestException 404 Not Found : Specified thirdparty ID does not belongs to an existing thirdparty
*
* @ url GET thirdparty / accounts / { site } / { key_account }
*/
public function getByThirdpartyAccounts ( $site , $key_account )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'societe' , 'lire' )) {
throw new RestException ( 403 );
}
$sql = " SELECT rowid, fk_soc, key_account, site, date_creation, tms FROM " . MAIN_DB_PREFIX . " societe_account " ;
$sql .= " WHERE site = ' " . $this -> db -> escape ( $site ) . " ' AND key_account = ' " . $this -> db -> escape ( $key_account ) . " ' " ;
$sql .= " AND entity IN ( " . getEntity ( 'adherent' ) . " ) " ;
$result = $this -> db -> query ( $sql );
if ( $result && $this -> db -> num_rows ( $result ) == 1 ) {
$obj = $this -> db -> fetch_object ( $result );
$thirdparty = new Societe ( $this -> db );
$result = $thirdparty -> fetch ( $obj -> fk_soc );
if ( $result <= 0 ) {
throw new RestException ( 404 , 'thirdparty not found' );
}
$member = new Adherent ( $this -> db );
$result = $member -> fetch ( 0 , '' , $thirdparty -> id );
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
} else {
2025-01-20 15:55:19 +01:00
throw new RestException ( 404 , 'This account have many thirdparties attached or does not exist.' );
2024-10-07 14:20:27 +02:00
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> id )) {
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
return $this -> _cleanObjectDatas ( $member );
}
2020-10-31 18:51:30 +01:00
/**
* Get properties of a member object by linked thirdparty email
*
2024-01-12 20:58:09 +01:00
* Return an array with member information
2020-10-31 18:51:30 +01:00
*
* @ param string $email Email of third party
*
2023-09-26 18:43:25 +02:00
* @ return Object Data without useless information
2020-10-31 18:51:30 +01:00
*
* @ url GET thirdparty / email / { email }
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Member or ThirdParty not found
2020-10-31 18:51:30 +01:00
*/
public function getByThirdpartyEmail ( $email )
{
2022-12-21 08:41:19 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
$thirdparty = new Societe ( $this -> db );
2024-07-31 18:54:52 +02:00
$result = $thirdparty -> fetch ( 0 , '' , '' , '' , '' , '' , '' , '' , '' , '' , $email );
2020-10-31 18:51:30 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'thirdparty not found' );
}
$member = new Adherent ( $this -> db );
2024-07-31 18:54:52 +02:00
$result = $member -> fetch ( 0 , '' , $thirdparty -> id );
2020-10-31 18:51:30 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> 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
}
return $this -> _cleanObjectDatas ( $member );
}
/**
* Get properties of a member object by linked thirdparty barcode
*
2024-01-12 20:58:09 +01:00
* Return an array with member information
2020-10-31 18:51:30 +01:00
*
2023-09-26 18:43:25 +02:00
* @ param string $barcode Barcode of third party
2020-10-31 18:51:30 +01:00
*
2023-09-26 18:43:25 +02:00
* @ return Object Data without useless information
2020-10-31 18:51:30 +01:00
*
* @ url GET thirdparty / barcode / { barcode }
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Member or ThirdParty not found
2020-10-31 18:51:30 +01:00
*/
public function getByThirdpartyBarcode ( $barcode )
{
2022-12-21 08:41:19 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
$thirdparty = new Societe ( $this -> db );
2024-07-31 18:54:52 +02:00
$result = $thirdparty -> fetch ( 0 , '' , '' , $barcode );
2020-10-31 18:51:30 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'thirdparty not found' );
}
$member = new Adherent ( $this -> db );
2024-07-31 18:54:52 +02:00
$result = $member -> fetch ( 0 , '' , $thirdparty -> id );
2020-10-31 18:51:30 +01:00
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> 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
}
return $this -> _cleanObjectDatas ( $member );
}
/**
* List members
*
* Get a list of members
*
2024-08-29 17:42:29 +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 $typeid ID of the type of member
* @ param int $category Use this param to filter list by category
* @ param string $sqlfilters Other criteria to filter answers separated by a comma .
* Example : " (t.ref:like:'SO-%') and ((t.date_creation:<:'20160101') or (t.nature:is:NULL)) "
* @ 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 array Array of member objects
2024-09-12 21:16:42 +02:00
* @ phan - return array < array < string , null | int | float | string >>
* @ phpstan - return array < array < string , null | int | float | string >>
2020-10-31 18:51:30 +01:00
*
2024-01-13 15:32:22 +01:00
* @ throws RestException 400 Error on SQL filters
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 No Member found
* @ throws RestException 503 Error when retrieving Member list
2020-10-31 18:51:30 +01:00
*/
2024-08-29 17:42:29 +02:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $typeid = '' , $category = 0 , $sqlfilters = '' , $properties = '' , $pagination_data = false )
2020-10-31 18:51:30 +01:00
{
$obj_ret = array ();
2022-12-21 08:41:19 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
$sql = " SELECT t.rowid " ;
2023-04-28 09:31:33 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " adherent AS t LEFT JOIN " . MAIN_DB_PREFIX . " adherent_extrafields AS ef ON (ef.fk_object = t.rowid) " ; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call
2020-10-31 18:51:30 +01:00
if ( $category > 0 ) {
2020-04-15 18:39:15 +02:00
$sql .= " , " . MAIN_DB_PREFIX . " categorie_member as c " ;
2020-10-31 18:51:30 +01:00
}
$sql .= ' WHERE t.entity IN (' . getEntity ( 'adherent' ) . ')' ;
if ( ! empty ( $typeid )) {
2021-06-09 15:36:47 +02:00
$sql .= ' AND t.fk_adherent_type=' . (( int ) $typeid );
2020-10-31 18:51:30 +01:00
}
// Select members of given category
if ( $category > 0 ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND c.fk_categorie = " . (( int ) $category );
$sql .= " AND c.fk_member = t.rowid " ;
2020-10-31 18:51:30 +01:00
}
// Add sql filters
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 18:51:30 +01:00
}
}
2024-08-29 17:42:29 +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 18:51:30 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
if ( $page < 0 ) {
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
$result = $this -> db -> query ( $sql );
if ( $result ) {
$i = 0 ;
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
while ( $i < $min ) {
$obj = $this -> db -> fetch_object ( $result );
$member = new Adherent ( $this -> db );
if ( $member -> fetch ( $obj -> rowid )) {
2023-09-26 18:04:48 +02:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $member ), $properties );
2020-10-31 18:51:30 +01:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve member list : ' . $this -> db -> lasterror ());
}
2024-08-29 17:42:29 +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
];
}
2020-10-31 18:51:30 +01:00
return $obj_ret ;
}
/**
* Create member object
*
2025-01-27 03:53:46 +01:00
* @ param array $request_data Request data
2025-01-20 15:55:19 +01:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2020-10-31 18:51:30 +01:00
* @ return int ID of member
2024-01-13 15:32:22 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 500 Error when creating Member
2020-10-31 18:51:30 +01:00
*/
public function post ( $request_data = null )
{
2022-09-09 10:28:00 +02:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'creer' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
$member = new Adherent ( $this -> db );
foreach ( $request_data as $field => $value ) {
2023-12-15 12:15:33 +01:00
if ( $field === 'caller' ) {
2024-01-12 20:58:09 +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
$member -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 12:15:33 +01:00
continue ;
}
2024-04-02 12:28:55 +02:00
$member -> $field = $this -> _checkValForAPI ( $field , $value , $member );
2020-10-31 18:51:30 +01:00
}
if ( $member -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , 'Error creating member' , array_merge ( array ( $member -> error ), $member -> errors ));
}
return $member -> id ;
}
/**
* Update member
*
2024-02-22 01:32:55 +01:00
* @ param int $id ID of member to update
* @ param array $request_data Datas
2024-09-12 21:16:42 +02:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-02-22 01:32:55 +01:00
* @ return Object Updated object
2024-01-13 15:32:22 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Member not found
* @ throws RestException 500 Error when resiliating , validating , excluding , updating a Member
2020-10-31 18:51:30 +01:00
*/
public function put ( $id , $request_data = null )
{
2022-09-09 10:28:00 +02:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'creer' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
$member = new Adherent ( $this -> db );
$result = $member -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'member' , $member -> 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
}
foreach ( $request_data as $field => $value ) {
2021-03-01 00:19:52 +01:00
if ( $field == 'id' ) {
continue ;
}
2023-12-15 12:15:33 +01:00
if ( $field === 'caller' ) {
2024-01-12 20:58:09 +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
$member -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 12:15:33 +01:00
continue ;
}
2024-04-01 12:37:05 +02:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2025-01-20 14:57:45 +01:00
$member -> array_options [ $index ] = $this -> _checkValForAPI ( $field , $val , $member );
2024-04-01 12:37:05 +02:00
}
2023-12-15 12:15:33 +01:00
continue ;
}
2020-10-31 18:51:30 +01:00
// Process the status separately because it must be updated using
2021-03-13 19:21:44 +01:00
// the validate(), resiliate() and exclude() methods of the class Adherent.
2020-10-31 18:51:30 +01:00
if ( $field == 'statut' ) {
if ( $value == '0' ) {
$result = $member -> resiliate ( DolibarrApiAccess :: $user );
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when resiliating member: ' . $member -> error );
}
} elseif ( $value == '1' ) {
$result = $member -> validate ( DolibarrApiAccess :: $user );
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when validating member: ' . $member -> error );
}
2021-03-13 19:21:44 +01:00
} elseif ( $value == '-2' ) {
$result = $member -> exclude ( DolibarrApiAccess :: $user );
if ( $result < 0 ) {
throw new RestException ( 500 , 'Error when excluding member: ' . $member -> error );
}
2020-10-31 18:51:30 +01:00
}
} else {
2024-04-02 12:28:55 +02:00
$member -> $field = $this -> _checkValForAPI ( $field , $value , $member );
2020-10-31 18:51:30 +01:00
}
}
// If there is no error, update() returns the number of affected rows
// so if the update is a no op, the return value is zero.
if ( $member -> update ( DolibarrApiAccess :: $user ) >= 0 ) {
return $this -> get ( $id );
} else {
2022-03-18 16:14:20 +01:00
throw new RestException ( 500 , 'Error when updating member: ' . $member -> error );
2020-10-31 18:51:30 +01:00
}
}
/**
* Delete member
*
* @ param int $id member ID
* @ return array
2024-09-12 21:16:42 +02:00
* @ phan - return array < string , array { code : int , message : string } >
* @ phpstan - return array < string , array { code : int , message : string } >
2024-01-13 15:32:22 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Member not found
* @ throws RestException 500 Error when deleting a Member
2020-10-31 18:51:30 +01:00
*/
public function delete ( $id )
{
2022-12-21 08:41:19 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'supprimer' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
$member = new Adherent ( $this -> db );
$result = $member -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'member' , $member -> 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
}
2022-11-26 00:50:45 +01:00
2024-03-07 17:58:02 +01:00
$res = $member -> delete ( DolibarrApiAccess :: $user );
2022-11-26 00:57:35 +01:00
if ( $res < 0 ) {
2022-11-26 00:50:45 +01:00
throw new RestException ( 500 , " Can't delete, error occurs " );
2020-10-31 18:51:30 +01:00
}
return array (
'success' => array (
'code' => 200 ,
2022-11-26 00:50:45 +01:00
'message' => 'Member deleted'
2020-10-31 18:51:30 +01:00
)
);
}
/**
* Validate fields before creating an object
*
2024-09-12 21:16:42 +02:00
* @ param array < string , null | int | float | string > $data Data to validate
* @ return array < string , null | int | float | string > Return array with validated mandatory fields and their value
* @ phan - return array < string , ? int | ? float | ? string > Return array with validated mandatory fields and their value
2020-10-31 18:51:30 +01:00
*
* @ throws RestException
*/
private function _validate ( $data )
{
$member = array ();
2024-03-08 21:21:27 +01:00
$mandatoryfields = array (
'morphy' ,
'typeid'
);
foreach ( $mandatoryfields as $field ) {
2021-03-01 00:19:52 +01:00
if ( ! isset ( $data [ $field ])) {
2020-10-31 18:51:30 +01:00
throw new RestException ( 400 , " $field field missing " );
2021-03-01 00:19:52 +01:00
}
2020-10-31 18:51:30 +01:00
$member [ $field ] = $data [ $field ];
}
return $member ;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
*
2024-01-26 03:35:00 +01:00
* @ param Object $object Object to clean
* @ return Object Object with cleaned properties
2020-10-31 18:51:30 +01:00
*/
protected function _cleanObjectDatas ( $object )
{
// phpcs:enable
$object = parent :: _cleanObjectDatas ( $object );
// Remove the subscriptions because they are handled as a subresource.
2024-03-08 11:00:19 +01:00
if ( $object instanceof Adherent ) {
unset ( $object -> subscriptions );
unset ( $object -> fk_incoterms );
unset ( $object -> label_incoterms );
unset ( $object -> location_incoterms );
unset ( $object -> fk_delivery_address );
unset ( $object -> shipping_method_id );
unset ( $object -> total_ht );
unset ( $object -> total_ttc );
unset ( $object -> total_tva );
unset ( $object -> total_localtax1 );
unset ( $object -> total_localtax2 );
}
if ( $object instanceof AdherentType ) {
unset ( $object -> linkedObjectsIds );
unset ( $object -> context );
unset ( $object -> canvas );
unset ( $object -> fk_project );
unset ( $object -> contact );
unset ( $object -> contact_id );
unset ( $object -> thirdparty );
unset ( $object -> user );
unset ( $object -> origin );
unset ( $object -> origin_id );
unset ( $object -> ref_ext );
unset ( $object -> country );
unset ( $object -> country_id );
unset ( $object -> country_code );
unset ( $object -> barcode_type );
unset ( $object -> barcode_type_code );
unset ( $object -> barcode_type_label );
unset ( $object -> barcode_type_coder );
unset ( $object -> mode_reglement_id );
unset ( $object -> cond_reglement_id );
unset ( $object -> cond_reglement );
unset ( $object -> fk_delivery_address );
unset ( $object -> shipping_method_id );
unset ( $object -> model_pdf );
unset ( $object -> fk_account );
unset ( $object -> note_public );
unset ( $object -> note_private );
unset ( $object -> fk_incoterms );
unset ( $object -> label_incoterms );
unset ( $object -> location_incoterms );
unset ( $object -> name );
unset ( $object -> lastname );
unset ( $object -> firstname );
unset ( $object -> civility_id );
unset ( $object -> total_ht );
unset ( $object -> total_tva );
unset ( $object -> total_localtax1 );
unset ( $object -> total_localtax2 );
unset ( $object -> total_ttc );
}
2020-10-31 18:51:30 +01:00
return $object ;
}
/**
* List subscriptions of a member
*
* Get a list of subscriptions
*
* @ param int $id ID of member
* @ return array Array of subscription objects
2024-09-12 21:16:42 +02:00
* @ phan - return Object []
* @ phpstan - return Object []
2020-10-31 18:51:30 +01:00
*
* @ url GET { id } / subscriptions
2024-01-13 15:32:22 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Member not found
2020-10-31 18:51:30 +01:00
*/
public function getSubscriptions ( $id )
{
2022-12-21 08:41:19 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'cotisation' , 'lire' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
$member = new Adherent ( $this -> db );
$result = $member -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
$obj_ret = array ();
foreach ( $member -> subscriptions as $subscription ) {
$obj_ret [] = $this -> _cleanObjectDatas ( $subscription );
}
return $obj_ret ;
}
/**
* Add a subscription for a member
*
2023-09-26 18:43:25 +02:00
* @ param int $id ID of member
* @ param string $start_date Start date { @ from body } { @ type timestamp }
* @ param string $end_date End date { @ from body } { @ type timestamp }
* @ param float $amount Amount ( may be 0 ) { @ from body }
* @ param string $label Label { @ from body }
2020-10-31 18:51:30 +01:00
* @ return int ID of subscription
*
* @ url POST { id } / subscriptions
2024-01-13 15:32:22 +01:00
*
2024-02-01 13:34:55 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Member not found
2020-10-31 18:51:30 +01:00
*/
public function createSubscription ( $id , $start_date , $end_date , $amount , $label = '' )
{
2022-12-21 08:41:19 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'cotisation' , 'creer' )) {
2024-02-01 13:34:55 +01:00
throw new RestException ( 403 );
2020-10-31 18:51:30 +01:00
}
$member = new Adherent ( $this -> db );
$result = $member -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
return $member -> subscription ( $start_date , $amount , 0 , '' , $label , '' , '' , '' , $end_date );
}
/**
* Get categories for a member
*
* @ param int $id ID of member
* @ 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
2024-01-13 15:32:22 +01:00
*
2024-02-01 19:16:58 +01:00
* @ throws RestException 403 Access denied
2024-01-13 15:32:22 +01:00
* @ throws RestException 404 Category not found
* @ throws RestException 503 Error when retrieving Category list
2020-10-31 18:51:30 +01:00
*/
2019-02-27 17:53:52 +01:00
public function getCategories ( $id , $sortfield = " s.rowid " , $sortorder = 'ASC' , $limit = 0 , $page = 0 )
2017-10-03 13:07:07 +02:00
{
2024-02-01 19:16:58 +01:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'categorie' , 'lire' )) {
throw new RestException ( 403 );
2017-10-03 19:09:46 +02:00
}
2024-11-15 23:57:58 +01:00
$member = new Adherent ( $this -> db );
$result = $member -> fetch ( $id );
if ( 0 === $result ) {
throw new RestException ( 404 , 'Member not found' );
}
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 , 'member' , $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 ;
2017-10-03 13:07:07 +02:00
}
2024-03-08 11:00:19 +01:00
/**
* Get properties of a member type object
*
* Return an array with member type information
*
* @ param int $id ID of member type
* @ return Object Object with cleaned properties
*
* @ url GET / types / { id }
*
* @ throws RestException 403 Access denied
* @ throws RestException 404 No Member Type found
*/
public function getType ( $id )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
throw new RestException ( 403 );
}
$membertype = new AdherentType ( $this -> db );
$result = $membertype -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'member type not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'member' , $membertype -> id , 'adherent_type' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2024-03-08 11:00:19 +01:00
}
return $this -> _cleanObjectDatas ( $membertype );
}
/**
* List members types
*
* Get a list of members types
*
2024-08-29 17:42:29 +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 $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " (t.libelle:like:'SO-%') and (t.subscription:=:'1') "
* @ 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 array Array of member type objects
2024-09-12 21:16:42 +02:00
* @ phan - return array < array < string , null | int | float | string >>
* @ phpstan - return array < array < string , null | int | float | string >>
2024-03-08 11:00:19 +01:00
*
* @ url GET / types /
*
* @ throws RestException 403 Access denied
* @ throws RestException 404 No Member Type found
* @ throws RestException 503 Error when retrieving Member list
*/
2024-08-29 17:42:29 +02:00
public function indexType ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $sqlfilters = '' , $properties = '' , $pagination_data = false )
2024-03-08 11:00:19 +01:00
{
$obj_ret = array ();
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
throw new RestException ( 403 );
}
$sql = " SELECT t.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " adherent_type AS t LEFT JOIN " . MAIN_DB_PREFIX . " adherent_type_extrafields AS ef ON (ef.fk_object = t.rowid) " ; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
$sql .= ' WHERE t.entity IN (' . getEntity ( 'member_type' ) . ')' ;
// Add sql filters
if ( $sqlfilters ) {
$errormessage = '' ;
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage );
if ( $errormessage ) {
throw new RestException ( 503 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
}
}
2024-08-29 17:42:29 +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 );
2024-03-08 11:00:19 +01:00
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
if ( $page < 0 ) {
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
$result = $this -> db -> query ( $sql );
if ( $result ) {
$i = 0 ;
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
while ( $i < $min ) {
$obj = $this -> db -> fetch_object ( $result );
$membertype = new AdherentType ( $this -> db );
if ( $membertype -> fetch ( $obj -> rowid )) {
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $membertype ), $properties );
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve member type list : ' . $this -> db -> lasterror ());
}
2024-08-29 17:42:29 +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
];
}
2024-03-08 11:00:19 +01:00
return $obj_ret ;
}
/**
* Create member type object
*
* @ param array $request_data Request data
2024-09-12 21:16:42 +02:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-03-08 11:00:19 +01:00
* @ return int ID of member type
*
2024-03-08 16:32:44 +01:00
* @ url POST / types /
2024-03-08 11:00:19 +01:00
*
* @ throws RestException 403 Access denied
* @ throws RestException 500 Error when creating Member Type
*/
public function postType ( $request_data = null )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'configurer' )) {
throw new RestException ( 403 );
}
// Check mandatory fields
$result = $this -> _validateType ( $request_data );
$membertype = new AdherentType ( $this -> db );
foreach ( $request_data as $field => $value ) {
if ( $field === 'caller' ) {
// 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
$membertype -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2024-03-08 11:00:19 +01:00
continue ;
}
2024-04-02 12:28:55 +02:00
$membertype -> $field = $this -> _checkValForAPI ( $field , $value , $membertype );
2024-03-08 11:00:19 +01:00
}
if ( $membertype -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , 'Error creating member type' , array_merge ( array ( $membertype -> error ), $membertype -> errors ));
}
return $membertype -> id ;
}
/**
* Update member type
*
* @ param int $id ID of member type to update
* @ param array $request_data Datas
2024-09-12 21:16:42 +02:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-03-08 11:00:19 +01:00
* @ return Object Updated object
*
* @ url PUT / types / { id }
*
* @ throws RestException 403 Access denied
* @ throws RestException 404 No Member Type found
* @ throws RestException 500 Error when updating Member Type
*/
public function putType ( $id , $request_data = null )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'configurer' )) {
throw new RestException ( 403 );
}
$membertype = new AdherentType ( $this -> db );
$result = $membertype -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'member type not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'member' , $membertype -> id , 'adherent_type' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2024-03-08 11:00:19 +01:00
}
foreach ( $request_data as $field => $value ) {
if ( $field == 'id' ) {
continue ;
}
if ( $field === 'caller' ) {
// 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
$membertype -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2024-03-08 11:00:19 +01:00
continue ;
}
2024-04-01 19:11:16 +02:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
$membertype -> array_options [ $index ] = $val ;
}
continue ;
}
2024-03-08 11:00:19 +01:00
// Process the status separately because it must be updated using
// the validate(), resiliate() and exclude() methods of the class AdherentType.
2024-04-02 12:28:55 +02:00
$membertype -> $field = $this -> _checkValForAPI ( $field , $value , $membertype );
2024-03-08 11:00:19 +01:00
}
// If there is no error, update() returns the number of affected rows
// so if the update is a no op, the return value is zero.
if ( $membertype -> update ( DolibarrApiAccess :: $user ) >= 0 ) {
return $this -> get ( $id );
} else {
throw new RestException ( 500 , 'Error when updating member type: ' . $membertype -> error );
}
}
/**
* Delete member type
*
* @ param int $id member type ID
* @ return array
2024-09-12 21:16:42 +02:00
* @ phan - return array < string , array { code : int , message : string } >
* @ phpstan - return array < string , array { code : int , message : string } >
2024-03-08 11:00:19 +01:00
*
2024-04-20 23:21:47 +02:00
* @ url DELETE / types / { id }
2024-03-08 11:00:19 +01:00
*
* @ throws RestException 403 Access denied
* @ throws RestException 404 No Member Type found
* @ throws RestException 500 Error when deleting Member Type
*/
public function deleteType ( $id )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'configurer' )) {
throw new RestException ( 403 );
}
$membertype = new AdherentType ( $this -> db );
$result = $membertype -> fetch ( $id );
2024-04-20 23:21:47 +02:00
if ( $result < 1 ) {
2024-03-08 11:00:19 +01:00
throw new RestException ( 404 , 'member type not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'member' , $membertype -> id , 'adherent_type' )) {
2024-04-02 14:47:49 +02:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2024-03-08 11:00:19 +01:00
}
$res = $membertype -> delete ( DolibarrApiAccess :: $user );
if ( $res < 0 ) {
throw new RestException ( 500 , " Can't delete, error occurs " );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Member type deleted'
)
);
}
/**
* Validate fields before creating an object
*
2024-09-12 21:16:42 +02:00
* @ param ? array < string , null | int | float | string > $data Data to validate
* @ return array < string , null | int | float | string >
2024-03-08 11:00:19 +01:00
*
* @ throws RestException
*/
private function _validateType ( $data )
{
$membertype = array ();
2024-03-08 21:21:27 +01:00
$mandatoryfields = array ( 'label' );
foreach ( $mandatoryfields as $field ) {
2024-03-08 11:00:19 +01:00
if ( ! isset ( $data [ $field ])) {
throw new RestException ( 400 , " $field field missing " );
}
$membertype [ $field ] = $data [ $field ];
}
return $membertype ;
}
2016-07-07 16:13:45 +02:00
}