View source
<?php
$plugin = array(
'label' => t('CiviCRM Core'),
'handler' => array(
'class' => 'CiviCrmCoreFields',
),
);
class CiviCrmCoreFields extends CRMCoreConversionHandler {
public function compatible() {
return array(
'CivicrmDataSourceHandler',
);
}
public function prepareRow(&$row, $importer) {
foreach ($row as $key => $value) {
$converted_value = NULL;
$civicrm_entity = array();
list($civicrm_entity['type'], $civicrm_entity['bundle'], $civicrm_entity['field'], $civicrm_entity['property'], $civicrm_entity['sub_property']) = explode(':', $key);
switch ($civicrm_entity['field']) {
case 'country':
$converted_value = $this
->countryCode($value);
break;
case 'prefix_id':
$converted_value = $this
->fetchFieldValue('prefix_id', $value, $civicrm_entity['type']);
break;
case 'suffix_id':
$converted_value = $this
->fetchFieldValue('suffix_id', $value, $civicrm_entity['type']);
break;
case 'gender_id':
$converted_value = $this
->fetchFieldValue('gender_id', $value, $civicrm_entity['type']);
break;
case 'priority_id':
$row->{$civicrm_entity['type'] . ':' . $civicrm_entity['bundle'] . ':priority'} = $this
->fetchFieldValue('priority_id', $value, $civicrm_entity['type']);
break;
case 'status_id':
$row->{$civicrm_entity['type'] . ':' . $civicrm_entity['bundle'] . ':status'} = $this
->fetchOptionsValue('activity_status', $value);
break;
case 'email':
if (isset($civicrm_entity['property'])) {
switch ($civicrm_entity['property']) {
case 'location_type_id':
foreach ($value as &$item) {
$item = $this
->fetchLocationType($item);
}
break;
}
$converted_value = $value;
}
break;
case 'website':
if (isset($civicrm_entity['property'])) {
switch ($civicrm_entity['property']) {
case 'website_type_id':
foreach ($value as &$item) {
$item = $this
->fetchOptionsValue('website_type', $item);
}
break;
}
$converted_value = $value;
}
break;
case 'phone':
if (isset($civicrm_entity['property'])) {
switch ($civicrm_entity['property']) {
case 'location_type_id':
foreach ($value as &$item) {
$item = $this
->fetchLocationType($item);
}
break;
case 'phone_type_id':
foreach ($value as &$item) {
$item = $this
->fetchLocationType($item);
}
break;
}
$converted_value = $value;
}
break;
case 'address':
if (isset($civicrm_entity['property'])) {
switch ($civicrm_entity['property']) {
case 'location_type_id':
foreach ($value as &$item) {
$item = $this
->fetchLocationType($item);
}
break;
case 'country_id':
foreach ($value as &$item) {
$item = $this
->fetchCountryById($item);
}
break;
case 'state_province_id':
foreach ($value as &$item) {
$item = $this
->fetchStateById($item);
}
break;
}
$converted_value = $value;
}
break;
case 'location_type_id':
$converted_value = $this
->fetchLocationType($value);
break;
case 'country_id':
$converted_value = $this
->fetchCountryById($value);
break;
case 'state_province_id':
$converted_value = $this
->fetchStateById($value);
break;
}
if (!empty($converted_value)) {
$row->{$key} = $converted_value;
}
}
}
public function fetchFieldValue($field, $value, $entity_type) {
$fields = crm_core_data_import_civicrm_api($entity_type, 'getfields');
if (!empty($fields[$field]['options'][$value])) {
return $fields[$field]['options'][$value];
}
return $value;
}
public function fetchOptionsValue($group_name, $value) {
$params = array(
'name' => $group_name,
);
$option_group = crm_core_data_import_civicrm_api('option_group', 'get', $params);
$option_group = reset($option_group);
if (!empty($option_group['id'])) {
$options = array(
'option_group_id' => $option_group['id'],
'value' => $value,
);
$type = crm_core_data_import_civicrm_api('option_value', 'get', $options);
$type = reset($type);
return $type['name'];
}
return FALSE;
}
public function fetchLocationType($id) {
$type = crm_core_data_import_civicrm_api('location_type', 'get', array(
'id' => $id,
));
$type = reset($type);
if (!empty($type['id'])) {
return $type['name'];
}
return $id;
}
public function fetchCountryById($id) {
return CRM_Core_PseudoConstant::countryIsoCode($id);
}
public function fetchStateById($id) {
return CRM_Core_PseudoConstant::stateProvinceAbbreviation($id);
}
}