-
Bastien Ho authoredb9865d81
<?php
/**
* 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([
'getactions',
'getdatasets',
'getsearch',
]);
}
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;
}
return civicrm_api3_create_success($data->records);
}
function _civicrm_api3_education_nationale_populate_spec(&$spec) {
$spec = \API_EducationNationale::fields();
}
/**
* educationnationale.populate 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_populate($params) {
$data = \API_EducationNationale::Call($params);
if(!isset($data->records) || empty($data->records)){
return;
}
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
// Get existing contacts in order to dedupe
$existing_contacts = civicrm_api3('Contact', 'get', [
'return' => ['external_identifier'],
'contact_sub_type' => ['EducationalInstitution'],
'limit' => -1,
'sequential' => true,
])['values'];
$existing_contacts_identifiers = [];
foreach($existing_contacts as $contact){
$existing_contacts_identifiers[$contact['external_identifier']] = $contact;
}
$dataset = \API_EducationNationale::DATASETS_MAPPING[$params['dataset']];
$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;
}
}
$EducationalInstitutions = [];
foreach($data->records as $record){
$external_identifier_field = $dataset['datasrc']['external_identifier'];
$external_identifier = $record->fields->$external_identifier_field;
if(isset($existing_contacts_identifiers[$external_identifier])){
continue;
}
$item = [
'contact_type' => "Organization",
'contact_type' => "Organization",
'contact_sub_type' => "EducationalInstitution",
'contact_source' => 'API Education Nationale',
'api.Address.create' => [
'location_type_id' => "Accueil",
'is_primary' => 1,
],
];
foreach ($dataset['datasrc'] as $local_field => $attribute) {
if(isset($custom_fields[$local_field])){
$local_field = $custom_fields[$local_field];
}
$item[$local_field] = $record->fields->$attribute;
}
foreach ($dataset['addressdatasrc'] as $local_field => $attribute) {
$item['api.Address.create'][$local_field] = $record->fields->$attribute;
}
// Post cleanup
if(strlen($item['organization_name']) > 144){
$item['organization_name'] = substr($item['organization_name'], 0, 142).'…';
}
$EducationalInstitutions[] = civicrm_api3('Contact', 'create', $item)['id'];
}
return civicrm_api3_create_success($EducationalInstitutions);
}