-
Bastien Ho authoredad3b1b08
<?php
use CRM_ConnecteurApiEducationNationaleFrance_ExtensionUtil as E;
/**
* educationnationale.getactions API
*
* @param array $params
* @return array API result descriptor
* @throws API_Exception
*/
function civicrm_api3_education_nationale_getactions($params) {
return civicrm_api3_create_success([
'get',
'getactions',
'getdatasets',
'getsearch',
]);
}
function _civicrm_api3_education_nationale_get_spec(&$spec) {
$spec = \API_EducationNationale::public_fields();
}
function civicrm_api3_education_nationale_get($params) {
$dataset = \API_EducationNationale::DATASETS_MAPPING[array_keys(\API_EducationNationale::DATASETS_MAPPING)[0]];
// Parse values to be returned
if(isset($params['return'])){
$return = $params['return'];
if(is_string($return)){
$return = explode(',', $return);
}
}
else{
$return = ['id'];
foreach ($dataset['datasrc'] as $local_field => $attribute) {
if(isset($custom_fields[$local_field])){
$local_field = $custom_fields[$local_field];
}
$return[] = $local_field;
}
foreach ($dataset['addressdatasrc'] as $local_field => $attribute) {
$return[] = $local_field;
}
}
// Manage custom fields
$fields = civicrm_api3('Contact', 'getfields', []);
$custom_fields = [];
foreach ($fields['values'] as $field_id => $field) {
if(isset($field['column_name']) && isset($dataset['datasrc'][$field['column_name']])){
$custom_fields[$field['column_name']] = $field_id;
if(false !== $index = array_search($field['column_name'], $return)){
$return[] = $field_id;
unset($return[$index]);
}
if(isset($params[$field['column_name']])){
$params[$field_id] = $params[$field['column_name']];
unset($return[$field['column_name']]);
}
}
}
// Build Query
$restricted_params = [
'return' => $return,
'contact_sub_type' => ['EducationalInstitution'],
];
$query = array_merge($params, $restricted_params);
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
// Get raw results
$raw_result = civicrm_api3('Contact', 'get', $query);
$institutions = $raw_result['values'];
// Cleanup results
foreach($institutions as $key=>$institution){
unset($institution['contact_id']);
unset($institution['contact_sub_type']);
unset($institution['contact_is_deleted']);
unset($institution['address_id']);
foreach($custom_fields as $pretty_name=>$uggly_name){
if(isset($institution[$uggly_name])){
$institution[$pretty_name] = $institution[$uggly_name];
unset($institution[$uggly_name]);
}
}
$institutions[$key] = $institution;
}
return civicrm_api3_create_success($institutions);
}
function civicrm_api3_education_nationale_getdatasets(&$spec) {
$datasets = [];
foreach(\API_EducationNationale::DATASETS_MAPPING as $name=>$data){
$datasets[] = [
'name'=>$name,
'label'=>$data['name'],
'datasrc'=>$data['datasrc'],
];
}
return civicrm_api3_create_success($datasets);
}
function _civicrm_api3_education_nationale_getsearch_spec(&$spec) {
$spec = \API_EducationNationale::fields();
}
/**
* educationnationale.getsearch API
*
* @param array $params
* @return array API result descriptor
* @see civicrm_api3_create_success
* @see civicrm_api3_create_error
* @throws API_Exception
*/
function civicrm_api3_education_nationale_getsearch($params) {
$data = \API_EducationNationale::Call($params);
if(!isset($data->records) || empty($data->records)){
return;
}
$existing_contacts_identifiers = \API_EducationNationale::get_existing();
$dataset = \API_EducationNationale::DATASETS_MAPPING[$params['dataset']];
$external_identifier_field = $dataset['datasrc']['external_identifier'];
foreach($data->records as $index=>$record){
$external_identifier = $record->fields->$external_identifier_field;
$record->fields->civicrm_id = isset($existing_contacts_identifiers[$external_identifier]) ? $existing_contacts_identifiers[$external_identifier]['id'] : null;
$data->records[$index] = $record;
}
return civicrm_api3_create_success($data->records);
}
function _civicrm_api3_education_nationale_populate_spec(&$spec) {
$spec = \API_EducationNationale::fields();
}
/**