2016-07-07 14:13:45 +00:00
< ? php
2024-11-10 10:16:23 +00: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 >
2025-09-14 09:17:01 +00:00
* Copyright ( C ) 2020 - 2025 Frédéric France < frederic . france @ free . fr >
2026-08-14 23:58:01 +00:00
* Copyright ( C ) 2024 - 2026 MDW < mdeweerd @ users . noreply . github . com >
2016-07-07 14:13:45 +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 />.
2016-07-07 14:13:45 +00:00
*/
use Luracast\Restler\RestException ;
2020-08-29 10:38:22 +00:00
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2016-07-07 14:13:45 +00:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php' ;
2016-09-25 23:15:17 +00:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/subscription.class.php' ;
2017-10-03 17:01:54 +00:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2024-03-08 10:00:19 +00:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent_type.class.php' ;
2025-10-21 00:05:51 +00:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherentstats.class.php' ;
2016-07-07 14:13:45 +00:00
2024-03-08 15:32:44 +00:00
2016-07-07 14:13:45 +00:00
/**
* API class for members
*
* @ access protected
* @ class DolibarrApiAccess { @ requires user , external }
*/
class Members extends DolibarrApi
{
2020-10-31 17:51:30 +00:00
/**
2025-01-20 14:55:19 +00:00
* @ var string [] Mandatory fields , checked when create and update object
2020-10-31 17:51:30 +00:00
*/
2021-02-28 23:19:52 +00:00
public static $FIELDS = array (
2020-10-31 17:51:30 +00:00
'morphy' ,
'typeid'
);
2025-10-21 00:05:51 +00:00
/**
* @ var AdherentStats
*/
public $memberstats ;
2020-10-31 17:51:30 +00:00
/**
* Constructor
*/
public function __construct ()
{
2024-01-12 21:09:51 +00:00
global $db ;
2020-10-31 17:51:30 +00:00
$this -> db = $db ;
2025-10-21 00:05:51 +00:00
$this -> memberstats = new AdherentStats ( $this -> db , DolibarrApiAccess :: $user -> socid , DolibarrApiAccess :: $user -> id );
2020-10-31 17:51:30 +00:00
}
/**
* Get properties of a member object
*
2024-01-12 19:58:09 +00:00
* Return an array with member information
2020-10-31 17:51:30 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param int $id ID of member
* @ return Object Object with cleaned properties
2020-10-31 17:51:30 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Member not found
2020-10-31 17:51:30 +00:00
*/
public function get ( $id )
{
2022-12-21 07:41:19 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
2016-07-07 14:13:45 +00:00
2020-10-30 06:57:53 +00:00
$member = new Adherent ( $this -> db );
if ( $id == 0 ) {
$result = $member -> initAsSpecimen ();
} else {
$result = $member -> fetch ( $id );
}
2020-10-31 17:51:30 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
2020-10-31 19:33:59 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> id ) && $id > 0 ) {
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
}
return $this -> _cleanObjectDatas ( $member );
}
/**
* Get properties of a member object by linked thirdparty
*
2024-01-12 19:58:09 +00:00
* Return an array with member information
2020-10-31 17:51:30 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param int $thirdparty ID of third party
2020-10-31 17:51:30 +00:00
*
2023-09-26 16:43:25 +00:00
* @ return Object Data without useless information
2020-10-31 17:51:30 +00:00
*
* @ url GET thirdparty / { thirdparty }
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Member not found
2020-10-31 17:51:30 +00:00
*/
public function getByThirdparty ( $thirdparty )
{
2022-12-21 07:41:19 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$member = new Adherent ( $this -> db );
2024-07-31 16:54:52 +00:00
$result = $member -> fetch ( 0 , '' , $thirdparty );
2020-10-31 17:51:30 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> 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
}
return $this -> _cleanObjectDatas ( $member );
}
2024-10-07 12:20:27 +00: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 );
}
2025-09-14 09:17:01 +00:00
$sql = " SELECT rowid, fk_soc as socid, key_account, site, date_creation, tms FROM " . MAIN_DB_PREFIX . " societe_account " ;
2024-10-07 12:20:27 +00:00
$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 );
2025-09-14 09:17:01 +00:00
$result = $thirdparty -> fetch ( $obj -> socid );
2024-10-07 12:20:27 +00:00
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 14:55:19 +00:00
throw new RestException ( 404 , 'This account have many thirdparties attached or does not exist.' );
2024-10-07 12:20:27 +00: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 17:51:30 +00:00
/**
* Get properties of a member object by linked thirdparty email
*
2024-01-12 19:58:09 +00:00
* Return an array with member information
2020-10-31 17:51:30 +00:00
*
* @ param string $email Email of third party
*
2023-09-26 16:43:25 +00:00
* @ return Object Data without useless information
2020-10-31 17:51:30 +00:00
*
* @ url GET thirdparty / email / { email }
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Member or ThirdParty not found
2020-10-31 17:51:30 +00:00
*/
public function getByThirdpartyEmail ( $email )
{
2022-12-21 07:41:19 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$thirdparty = new Societe ( $this -> db );
2024-07-31 16:54:52 +00:00
$result = $thirdparty -> fetch ( 0 , '' , '' , '' , '' , '' , '' , '' , '' , '' , $email );
2020-10-31 17:51:30 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'thirdparty not found' );
}
$member = new Adherent ( $this -> db );
2024-07-31 16:54:52 +00:00
$result = $member -> fetch ( 0 , '' , $thirdparty -> id );
2020-10-31 17:51:30 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> 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
}
return $this -> _cleanObjectDatas ( $member );
}
/**
* Get properties of a member object by linked thirdparty barcode
*
2024-01-12 19:58:09 +00:00
* Return an array with member information
2020-10-31 17:51:30 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param string $barcode Barcode of third party
2020-10-31 17:51:30 +00:00
*
2023-09-26 16:43:25 +00:00
* @ return Object Data without useless information
2020-10-31 17:51:30 +00:00
*
* @ url GET thirdparty / barcode / { barcode }
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Member or ThirdParty not found
2020-10-31 17:51:30 +00:00
*/
public function getByThirdpartyBarcode ( $barcode )
{
2022-12-21 07:41:19 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$thirdparty = new Societe ( $this -> db );
2024-07-31 16:54:52 +00:00
$result = $thirdparty -> fetch ( 0 , '' , '' , $barcode );
2020-10-31 17:51:30 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'thirdparty not found' );
}
$member = new Adherent ( $this -> db );
2024-07-31 16:54:52 +00:00
$result = $member -> fetch ( 0 , '' , $thirdparty -> id );
2020-10-31 17:51:30 +00:00
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'adherent' , $member -> 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
}
return $this -> _cleanObjectDatas ( $member );
}
/**
* List members
*
* Get a list of members
*
2024-08-29 15:42:29 +00: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 .
2026-05-29 10:16:42 +00:00
* Example : " (t.ref:like:'SO-%') and ((t.date_creation:>:'20160101') or (t.nature:is:NULL)) "
2024-08-29 15:42:29 +00:00
* @ 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 19:16:42 +00:00
* @ phan - return array < array < string , null | int | float | string >>
* @ phpstan - return array < array < string , null | int | float | string >>
2020-10-31 17:51:30 +00:00
*
2024-01-13 14:32:22 +00:00
* @ throws RestException 400 Error on SQL filters
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 No Member found
* @ throws RestException 503 Error when retrieving Member list
2020-10-31 17:51:30 +00:00
*/
2024-08-29 15:42:29 +00:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $typeid = '' , $category = 0 , $sqlfilters = '' , $properties = '' , $pagination_data = false )
2020-10-31 17:51:30 +00:00
{
$obj_ret = array ();
2022-12-21 07:41:19 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
$sql = " SELECT t.rowid " ;
2023-04-28 07:31:33 +00: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 17:51:30 +00:00
if ( $category > 0 ) {
2020-04-15 16:39:15 +00:00
$sql .= " , " . MAIN_DB_PREFIX . " categorie_member as c " ;
2020-10-31 17:51:30 +00:00
}
$sql .= ' WHERE t.entity IN (' . getEntity ( 'adherent' ) . ')' ;
if ( ! empty ( $typeid )) {
2021-06-09 13:36:47 +00:00
$sql .= ' AND t.fk_adherent_type=' . (( int ) $typeid );
2020-10-31 17:51:30 +00:00
}
// Select members of given category
if ( $category > 0 ) {
2021-06-09 13:36:47 +00:00
$sql .= " AND c.fk_categorie = " . (( int ) $category );
$sql .= " AND c.fk_member = t.rowid " ;
2020-10-31 17:51:30 +00:00
}
// Add sql filters
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 17:51:30 +00:00
}
}
2024-08-29 15:42:29 +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 17:51:30 +00: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 16:04:48 +00:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $member ), $properties );
2020-10-31 17:51:30 +00:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve member list : ' . $this -> db -> lasterror ());
}
2024-08-29 15:42:29 +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
];
}
2020-10-31 17:51:30 +00:00
return $obj_ret ;
}
/**
* Create member object
*
2025-01-27 02:53:46 +00:00
* @ param array $request_data Request data
2025-01-20 14:55:19 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2020-10-31 17:51:30 +00:00
* @ return int ID of member
2024-01-13 14:32:22 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 500 Error when creating Member
2020-10-31 17:51:30 +00:00
*/
public function post ( $request_data = null )
{
2022-09-09 08:28:00 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'creer' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
// Check mandatory fields
$result = $this -> _validate ( $request_data );
$member = new Adherent ( $this -> db );
foreach ( $request_data as $field => $value ) {
2026-08-14 17:47:17 +00:00
if ( in_array ( $field , array ( 'pass' , 'pass_crypted' , 'pass_indatabase' , 'pass_indatabase_crypted' , 'pass_temp' , 'api_key' ))) {
// This properties can't be set/modified with API
throw new RestException ( 405 , 'The property ' . $field . " can't be set/modified using the APIs " );
}
2026-08-14 23:58:01 +00:00
if ( in_array ( $field , array ( 'user_id' )) && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )) {
2026-08-14 18:00:37 +00:00
// This properties can't be set/modified with API without permission user->user->creer
throw new RestException ( 405 , 'The property ' . $field . " can't be set/modified using the APIs without permission user->user->create " );
}
2023-12-15 11:15:33 +00:00
if ( $field === 'caller' ) {
2024-01-12 19:58:09 +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
$member -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2024-04-02 10:28:55 +00:00
$member -> $field = $this -> _checkValForAPI ( $field , $value , $member );
2020-10-31 17:51:30 +00: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 00:32:55 +00:00
* @ param int $id ID of member to update
* @ param array $request_data Datas
2024-09-12 19:16:42 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-02-22 00:32:55 +00:00
* @ return Object Updated object
2024-01-13 14:32:22 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Member not found
* @ throws RestException 500 Error when resiliating , validating , excluding , updating a Member
2020-10-31 17:51:30 +00:00
*/
public function put ( $id , $request_data = null )
{
2022-09-09 08:28:00 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'creer' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00: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 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
}
2026-08-14 18:00:37 +00:00
$sav_user_id = $member -> user_id ;
2020-10-31 17:51:30 +00:00
foreach ( $request_data as $field => $value ) {
2026-08-14 17:45:47 +00:00
if ( in_array ( $field , array ( 'pass' , 'pass_crypted' , 'pass_indatabase' , 'pass_indatabase_crypted' , 'pass_temp' , 'api_key' ))) {
// This properties can't be set/modified with API
throw new RestException ( 405 , 'The property ' . $field . " can't be set/modified using the APIs " );
}
2026-08-14 23:58:01 +00:00
if ( in_array ( $field , array ( 'user_id' )) && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )) {
2026-08-14 18:00:37 +00:00
// This properties can't be set/modified with API without permission user->user->creer
throw new RestException ( 405 , 'The property ' . $field . " can't be set/modified using the APIs without the permission user->user->create " );
}
2021-02-28 23:19:52 +00:00
if ( $field == 'id' ) {
continue ;
}
2023-12-15 11:15:33 +00:00
if ( $field === 'caller' ) {
2024-01-12 19:58:09 +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
$member -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2024-04-01 10:37:05 +00:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2026-05-08 23:16:35 +00:00
$member -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $member );
2024-04-01 10:37:05 +00:00
}
2023-12-15 11:15:33 +00:00
continue ;
}
2020-10-31 17:51:30 +00:00
// Process the status separately because it must be updated using
2021-03-13 18:21:44 +00:00
// the validate(), resiliate() and exclude() methods of the class Adherent.
2020-10-31 17:51:30 +00: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 18:21:44 +00: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 17:51:30 +00:00
}
} else {
2024-04-02 10:28:55 +00:00
$member -> $field = $this -> _checkValForAPI ( $field , $value , $member );
2020-10-31 17:51:30 +00: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.
2026-08-14 18:00:37 +00:00
$nosyncuser = 0 ;
$nosyncpassword = 1 ; // Disable password sync. Management of password must be done using the user API only.
if ( $member -> update ( DolibarrApiAccess :: $user , 0 , $nosyncuser , $nosyncpassword ) >= 0 ) {
2020-10-31 17:51:30 +00:00
return $this -> get ( $id );
} else {
2022-03-18 15:14:20 +00:00
throw new RestException ( 500 , 'Error when updating member: ' . $member -> error );
2020-10-31 17:51:30 +00:00
}
}
/**
* Delete member
*
* @ param int $id member ID
* @ return array
2024-09-12 19:16:42 +00:00
* @ phan - return array < string , array { code : int , message : string } >
* @ phpstan - return array < string , array { code : int , message : string } >
2024-01-13 14:32:22 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Member not found
* @ throws RestException 500 Error when deleting a Member
2020-10-31 17:51:30 +00:00
*/
public function delete ( $id )
{
2022-12-21 07:41:19 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'supprimer' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00: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 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
}
2022-11-25 23:50:45 +00:00
2024-03-07 16:58:02 +00:00
$res = $member -> delete ( DolibarrApiAccess :: $user );
2022-11-25 23:57:35 +00:00
if ( $res < 0 ) {
2022-11-25 23:50:45 +00:00
throw new RestException ( 500 , " Can't delete, error occurs " );
2020-10-31 17:51:30 +00:00
}
return array (
'success' => array (
'code' => 200 ,
2022-11-25 23:50:45 +00:00
'message' => 'Member deleted'
2020-10-31 17:51:30 +00:00
)
);
}
/**
* Validate fields before creating an object
*
2025-03-02 11:13:32 +00:00
* @ param ? array < string , null | int | float | string > $data Data to validate
2024-09-12 19:16:42 +00:00
* @ 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 17:51:30 +00:00
*
* @ throws RestException
*/
private function _validate ( $data )
{
2025-03-02 11:13:32 +00:00
if ( $data === null ) {
$data = array ();
}
2020-10-31 17:51:30 +00:00
$member = array ();
2024-03-08 20:21:27 +00:00
$mandatoryfields = array (
'morphy' ,
'typeid'
);
foreach ( $mandatoryfields as $field ) {
2021-02-28 23:19:52 +00:00
if ( ! isset ( $data [ $field ])) {
2020-10-31 17:51:30 +00:00
throw new RestException ( 400 , " $field field missing " );
2021-02-28 23:19:52 +00:00
}
2020-10-31 17:51:30 +00:00
$member [ $field ] = $data [ $field ];
}
return $member ;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible object datas
2025-11-20 00:47:46 +00:00
* @ phpstan - template T
2020-10-31 17:51:30 +00:00
*
2024-01-26 02:35:00 +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
*/
2025-03-03 00:49:20 +00:00
public function _cleanObjectDatas ( $object )
2020-10-31 17:51:30 +00:00
{
// phpcs:enable
$object = parent :: _cleanObjectDatas ( $object );
// Remove the subscriptions because they are handled as a subresource.
2024-03-08 10:00:19 +00: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 );
2026-08-14 17:47:17 +00:00
unset ( $object -> pass );
unset ( $object -> pass_crypted );
2024-03-08 10:00:19 +00:00
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 17:51:30 +00:00
Fix #38279 expose Subscription dates and note under documented names in API (#38674)
GET /api/index.php/members/{id}/subscriptions returned the raw
Subscription object fields (dateh, datef, note_public, amount) while
POST /api/index.php/members/{id}/subscriptions takes start_date,
end_date, amount, label. A client that fetches existing subscriptions
to push them back has to translate the field names by hand.
Per @eldy review on PR #38556, address this with the renaming that
fits the rest of the codebase:
- Add date_start / date_end aliases on the Subscription class. The
date_start / date_end naming convention is already used by other
objects (task, expense, holiday), so the alias matches the existing
contract instead of inventing a new pair of names.
- Do not introduce a 'label' alias. note_public is already the
documented field on v24; declare it as a real property so phpstan
stops flagging the dynamic-property write.
- _cleanObjectDatas on the Subscription branch now sets only
date_start and date_end. note_public is left as the existing
property name. dateh / datef stay populated for backward
compatibility with existing consumers.
Develop-branch version of PR #38556 (closed).
Signed-off-by: Dolicraft <contact@dolicraft.com>
Co-authored-by: Dolicraft <contact@dolicraft.com>
2026-06-10 19:16:27 +00:00
// Expose POST-friendly aliases on the Subscription GET response so the
// payload returned by GET /members/{id}/subscriptions matches the field
// names POST /members/{id}/subscriptions expects (see issue #38279).
// $dateh / $datef stay in the response for backward compatibility with
// existing consumers; date_start / date_end are the documented names
// used by the rest of the codebase (e.g. tasks, expenses, holidays).
if ( $object instanceof Subscription ) {
$object -> date_start = $object -> dateh ;
$object -> date_end = $object -> datef ;
}
2020-10-31 17:51:30 +00: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 19:16:42 +00:00
* @ phan - return Object []
* @ phpstan - return Object []
2020-10-31 17:51:30 +00:00
*
* @ url GET { id } / subscriptions
2024-01-13 14:32:22 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Member not found
2020-10-31 17:51:30 +00:00
*/
public function getSubscriptions ( $id )
{
2022-12-21 07:41:19 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'cotisation' , 'lire' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00: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 16:43:25 +00: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 17:51:30 +00:00
* @ return int ID of subscription
*
* @ url POST { id } / subscriptions
2024-01-13 14:32:22 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Member not found
2025-10-15 00:31:08 +00:00
* @ throws RestException 422 Malformed data
2026-05-17 23:41:32 +00:00
* @ throws RestException 500 server error
2020-10-31 17:51:30 +00:00
*/
public function createSubscription ( $id , $start_date , $end_date , $amount , $label = '' )
{
2022-12-21 07:41:19 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'cotisation' , 'creer' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 );
2020-10-31 17:51:30 +00:00
}
2025-10-01 12:48:38 +00:00
if ( ! is_numeric ( $start_date ) || ! is_numeric ( $end_date ) || ! is_numeric ( $amount )) {
throw new RestException ( 422 , 'Malformed data: subscription start or end date, or subscription amount, is not numeric' );
2025-03-02 14:10:25 +00:00
}
2026-05-17 23:41:32 +00:00
if ( $start_date > $end_date ) {
throw new RestException ( 422 , 'Malformed data: subscription start is not larger than end date' );
}
2020-10-31 17:51:30 +00:00
$member = new Adherent ( $this -> db );
$result = $member -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'member not found' );
}
2026-05-17 23:41:32 +00:00
$result = $member -> subscription (( int ) $start_date , ( float ) $amount , 0 , '' , $label , '' , '' , '' , ( int ) $end_date );
if ( $result < 1 ) {
throw new RestException ( 500 , $member -> error );
} else {
return $result ;
}
2020-10-31 17:51:30 +00:00
}
/**
* 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 14:32:22 +00:00
*
2024-02-01 18:16:58 +00:00
* @ throws RestException 403 Access denied
2024-01-13 14:32:22 +00:00
* @ throws RestException 404 Category not found
* @ throws RestException 503 Error when retrieving Category list
2020-10-31 17:51:30 +00:00
*/
2019-02-27 16:53:52 +00:00
public function getCategories ( $id , $sortfield = " s.rowid " , $sortorder = 'ASC' , $limit = 0 , $page = 0 )
2017-10-03 11:07:07 +00:00
{
2024-02-01 18:16:58 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'categorie' , 'lire' )) {
throw new RestException ( 403 );
2017-10-03 17:09:46 +00:00
}
2024-11-15 22:57:58 +00:00
$member = new Adherent ( $this -> db );
$result = $member -> fetch ( $id );
if ( 0 === $result ) {
throw new RestException ( 404 , 'Member not found' );
}
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 , 'member' , $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 ;
2017-10-03 11:07:07 +00:00
}
2024-03-08 10:00:19 +00: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 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2024-03-08 10:00:19 +00:00
}
return $this -> _cleanObjectDatas ( $membertype );
}
/**
* List members types
*
* Get a list of members types
*
2024-08-29 15:42:29 +00: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 19:16:42 +00:00
* @ phan - return array < array < string , null | int | float | string >>
* @ phpstan - return array < array < string , null | int | float | string >>
2024-03-08 10:00:19 +00: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 15:42:29 +00:00
public function indexType ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $sqlfilters = '' , $properties = '' , $pagination_data = false )
2024-03-08 10:00:19 +00: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 15:42:29 +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 );
2024-03-08 10:00:19 +00: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 15:42:29 +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
];
}
2024-03-08 10:00:19 +00:00
return $obj_ret ;
}
/**
* Create member type object
*
* @ param array $request_data Request data
2024-09-12 19:16:42 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-03-08 10:00:19 +00:00
* @ return int ID of member type
*
2024-03-08 15:32:44 +00:00
* @ url POST / types /
2024-03-08 10:00:19 +00: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 10:28:55 +00:00
$membertype -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2024-03-08 10:00:19 +00:00
continue ;
}
2024-04-02 10:28:55 +00:00
$membertype -> $field = $this -> _checkValForAPI ( $field , $value , $membertype );
2024-03-08 10:00:19 +00: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 19:16:42 +00:00
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
2024-03-08 10:00:19 +00: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 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2024-03-08 10:00:19 +00: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 10:28:55 +00:00
$membertype -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2024-03-08 10:00:19 +00:00
continue ;
}
2024-04-01 17:11:16 +00:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
$membertype -> array_options [ $index ] = $val ;
}
continue ;
}
2024-03-08 10:00:19 +00:00
// Process the status separately because it must be updated using
// the validate(), resiliate() and exclude() methods of the class AdherentType.
2024-04-02 10:28:55 +00:00
$membertype -> $field = $this -> _checkValForAPI ( $field , $value , $membertype );
2024-03-08 10:00:19 +00: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 19:16:42 +00:00
* @ phan - return array < string , array { code : int , message : string } >
* @ phpstan - return array < string , array { code : int , message : string } >
2024-03-08 10:00:19 +00:00
*
2024-04-20 21:21:47 +00:00
* @ url DELETE / types / { id }
2024-03-08 10:00:19 +00: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 21:21:47 +00:00
if ( $result < 1 ) {
2024-03-08 10:00:19 +00:00
throw new RestException ( 404 , 'member type not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'member' , $membertype -> id , 'adherent_type' )) {
2024-04-02 12:47:49 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2024-03-08 10:00:19 +00: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'
)
);
}
2025-10-21 00:05:51 +00:00
/**
* Return an array with the number of members by month for a given year
*
* @ param int $year Year
* @ param int $format 0 = Label of abscissa is a translated text
* 1 = Label of abscissa is month number
* 2 = Label of abscissa is first letter of month
* @ return array Array of statistics for last modified members
* @ phan - return array < int < 0 , 11 > , array { 0 : int < 1 , 12 > , 1 : int } > Array of nb each month
* @ phpstan - return array < int < 0 , 11 > , array { 0 : int < 1 , 12 > , 1 : int } > Array of nb each month
*
* @ url GET stats / nbbymonth
* @ throws RestException 403 Access denied
*/
public function getNbByMonth ( $year , $format = 0 )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
throw new RestException ( 403 );
}
return $this -> memberstats -> getNbByMonth ( $year , $format );
}
/**
* Return an array with the number of subscriptions by year
*
* @ return array Array of statistics for last modified members
* @ phan - return array < array { 0 : int , 1 : int } > Array of nb each year
* @ phpstan - return array < array { 0 : int , 1 : int } > Array of nb each year
*
* @ url GET stats / nbbyyear
* @ throws RestException 403 Access denied
*/
public function getNbByYear ()
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
throw new RestException ( 403 );
}
return $this -> memberstats -> getNbByYear ();
}
/**
* Return the number of subscriptions by month for a given year
*
* @ param int $year Year
* @ param int $format 0 = Label of abscissa is a translated text , 1 = Label of abscissa is month number , 2 = Label of abscissa is first letter of month
* @ return array Array of statistics for last modified members
* @ phan - return array < int < 0 , 11 > , array { 0 : int < 1 , 12 > , 1 : int | float } > Array of values by month
* @ phpstan - return array < int < 0 , 11 > , array { 0 : int < 1 , 12 > , 1 : int | float } > Array of values by month
*
* @ url GET stats / amountbymonth
* @ throws RestException 403 Access denied
*/
public function getAmountByMonth ( $year , $format = 0 )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
throw new RestException ( 403 );
}
return $this -> memberstats -> getAmountByMonth ( $year , $format );
}
/**
* Last Modified Members
*
* Get an array of statistics for last modified members
*
* @ param int $max Max numbers of members
* @ return array Array of statistics for last modified members
* @ phan - return array < int , array { id : int , ref : string , firstname : string , lastname : string , company : string , fk_soc : ? int , datec : int | '' , datem : int | '' , status : int , date_end_subscription : int | '' , photo : null | string , email : string , gender : string , morphy : string , typeid : int , need_subscription : 0 | 1 | null , subscription : '0' | '1' | null , label : string } >
* @ phpstan - return array < int , array { id : int , ref : string , firstname : string , lastname : string , company : string , fk_soc : ? int , datec : int | '' , datem : int | '' , status : int , date_end_subscription : int | '' , photo : null | string , email : string , gender : string , morphy : string , typeid : int , need_subscription : 0 | 1 | null , subscription : '0' | '1' | null , label : string } >
*
* @ url GET stats / lastmodifiedmembers
* @ throws RestException 403 Access denied
*/
public function getLastModifiedMembers ( $max )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'adherent' , 'lire' )) {
throw new RestException ( 403 );
}
return $this -> memberstats -> getLastModifiedMembers ( $max );
}
2024-03-08 10:00:19 +00:00
/**
* Validate fields before creating an object
*
2024-09-12 19:16:42 +00:00
* @ param ? array < string , null | int | float | string > $data Data to validate
* @ return array < string , null | int | float | string >
2024-03-08 10:00:19 +00:00
*
* @ throws RestException
*/
private function _validateType ( $data )
{
$membertype = array ();
2024-03-08 20:21:27 +00:00
$mandatoryfields = array ( 'label' );
foreach ( $mandatoryfields as $field ) {
2024-03-08 10:00:19 +00:00
if ( ! isset ( $data [ $field ])) {
throw new RestException ( 400 , " $field field missing " );
}
$membertype [ $field ] = $data [ $field ];
}
return $membertype ;
}
2016-07-07 14:13:45 +00:00
}