Commit 7f403ebc authored by jaapjansma's avatar jaapjansma

Refactor handling of custom data

parent 11b0f04d
......@@ -6,6 +6,7 @@ use \Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use \Civi\ActionProvider\Parameter\SpecificationBag;
use \Civi\ActionProvider\Parameter\Specification;
use \Civi\ActionProvider\Utils\CustomField;
use CRM_ActionProvider_ExtensionUtil as E;
......@@ -29,7 +30,8 @@ class ContactDataById extends AbstractAction {
// Get custom data
$custom_data = civicrm_api3('CustomValue', 'get', array('entity_id' => $parameters->getParameter('contact_id'), 'entity_table' => 'civicrm_contact'));
foreach($custom_data['values'] as $custom) {
$output->setParameter('custom_'.$custom['id'], $custom['latest']);
$fieldName = CustomField::getCustomFieldName($custom['id']);
$output->setParameter($fieldName, $custom['latest']);
}
}
......@@ -76,12 +78,26 @@ class ContactDataById extends AbstractAction {
$type = 'Integer';
break;
}
$fieldSpec = new Specification(
$field['name'],
$type,
$field['title'],
false
);
if (stripos($field['name'], 'custom_') === 0) {
// It is a custom field
$customFieldId = str_replace("custom_", "", $field['name']);
$fieldName = CustomField::getCustomFieldName($customFieldId);
$fieldSpec = new Specification(
$fieldName,
$type,
$field['title'],
false
);
$fieldSpec->setApiFieldName($field['name']);
} else {
$fieldSpec = new Specification(
$field['name'],
$type,
$field['title'],
false
);
}
$bag->addSpecification($fieldSpec);
}
......
......@@ -28,7 +28,7 @@ class UpdateCustomData extends AbstractAction {
continue;
}
if ($parameters->getParameter($spec->getName())) {
$apiParams[$spec->getName()] = $parameters->getParameter($spec->getName());
$apiParams[$spec->getApiFieldName()] = $parameters->getParameter($spec->getName());
}
}
......
......@@ -133,7 +133,7 @@ class CreateOrUpdateParticipant extends AbstractAction {
continue;
}
if ($parameters->getParameter($spec->getName())) {
$participantParams[$spec->getName()] = $parameters->getParameter($spec->getName());
$participantParams[$spec->getApiFieldName()] = $parameters->getParameter($spec->getName());
}
}
......
......@@ -42,6 +42,11 @@ class Specification {
* @var string
*/
protected $fkEntity;
/**
* @var string
*/
protected $apiFieldName;
/**
* @param string $name
......@@ -104,6 +109,23 @@ class Specification {
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getApiFieldName() {
return $this->apiFieldName;
}
/**
* @param string $name
*
* @return $this
*/
public function setApiFieldName($name) {
$this->apiFieldName = $name;
return $this;
}
/**
* @return string
......
......@@ -13,6 +13,10 @@ use CRM_ActionProvider_ExtensionUtil as E;
*/
class CustomField {
static $customGroupNames = array();
static $customFields = array();
/**
* Gets the data type of the custom field.
*/
......@@ -30,6 +34,35 @@ class CustomField {
return $type;
}
/**
* Returns the name of the custom group
*
* @param int $custom_group_id
* @return string
*/
public static function getCustomGroupName($custom_group_id) {
if (!isset(self::$customGroupNames[$custom_group_id])) {
self::$customGroupNames[$custom_group_id] = civicrm_api3('CustomGroup', 'getvalue', array('return' => 'name', 'id' => $custom_group_id));
}
return self::$customGroupNames[$custom_group_id];
}
/**
* Returns a formatted name as custom_CustomGroupName_CustomFieldName
*
* @param int $custom_field_id
* @return string
*/
public static function getCustomFieldName($custom_field_id) {
if (!isset(self::$customFields[$custom_field_id])) {
self::$customFields[$custom_field_id] = civicrm_api3('CustomField', 'getsingle', array('id' => $custom_field_id));
}
$custom_group_name = self::getCustomGroupName(self::$customFields[$custom_field_id]['custom_group_id']);
$name = 'custom_'.$custom_group_name.'_'.self::$customFields[$custom_field_id]['name'];
return $name;
}
/**
* Converts a specifcation object to a custom field.
*
......@@ -42,20 +75,27 @@ class CustomField {
* @return Specification|null
*/
public static function getSpecFromCustomField($customField, $titlePrefix='', $useRequiredFromCustomField=false) {
$name = 'custom_'.$customField['id'];
self::$customFields[$customField['id']] = $customField;
$name = self::getCustomFieldName($customField['id']);
$apiFieldName = 'custom_'.$customField['id'];
$type = self::getTypeForCustomField($customField);
$title = trim($titlePrefix.$customField['label']);
$is_required = $customField['is_required'] && $useRequiredFromCustomField ? true : false;
$multiple = false;
$default = null;
$spec = null;
if (isset($customField['option_group_id']) && $customField['option_group_id']) {
return new OptionGroupSpecification($name, $customField['option_group_id'], $title, $is_required, $default, $multiple);
$spec = new OptionGroupSpecification($name, $customField['option_group_id'], $title, $is_required, $default, $multiple);
} elseif($type) {
return new Specification($name, $type, $title, $is_required, $default, null, array(), $multiple);
$spec = new Specification($name, $type, $title, $is_required, $default, null, array(), $multiple);
}
if ($spec) {
$spec->setApiFieldName($apiFieldName);
}
return null;
return $spec;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment