create new option values when custom field is changed
This commit is contained in:
parent
54adee32a9
commit
2949ab0168
1 changed files with 62 additions and 55 deletions
|
@ -62,10 +62,11 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
* @param bool $upgrade
|
* @param bool $upgrade
|
||||||
* If true: Does not show UF message if custom field already exists
|
* If true: Does not show UF message if custom field already exists
|
||||||
*
|
*
|
||||||
* @returns array Result of custom field creation api call
|
* @returns array|False Result of custom field creation api call. False if
|
||||||
|
* field already existed und wasn't upgraded.
|
||||||
* @throws \CiviCRM_API3_Exception
|
* @throws \CiviCRM_API3_Exception
|
||||||
*/
|
*/
|
||||||
public function create(bool $upgrade = FALSE): ?array {
|
public function create(bool $upgrade = FALSE) {
|
||||||
|
|
||||||
// Check if the field already exists
|
// Check if the field already exists
|
||||||
$field = civicrm_api3(
|
$field = civicrm_api3(
|
||||||
|
@ -129,17 +130,36 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return NULL;
|
$this->id = $field['values'][0]['id'];
|
||||||
|
$this->custom_group_id = $field['values'][0]['custom_group_id'];
|
||||||
|
return $this->upgrade($field['values'][0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update an existing custom field
|
* Upgrade an existing custom field
|
||||||
*
|
*
|
||||||
* @returns array Result of custom field creation api call
|
* @param $attributes
|
||||||
|
* Custom Field data
|
||||||
|
* @returns array|False Result of custom field creation api call, False if
|
||||||
|
* field was not upgraded
|
||||||
*/
|
*/
|
||||||
public function update(): array {
|
private function upgrade($attributes) {
|
||||||
|
$upgrade_necessary = False;
|
||||||
|
|
||||||
|
if (key_exists('option_group_id', $attributes)) {
|
||||||
|
$this->addOptions($attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this as $var => $value) {
|
||||||
|
// put array items into attributes
|
||||||
|
if (array_key_exists($var, $attributes) && $attributes[$var] != $value) {
|
||||||
|
$this->$var = $attributes[$var];
|
||||||
|
$upgrade_necessary = True;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($upgrade_necessary) {
|
||||||
try {
|
try {
|
||||||
$this->result = civicrm_api3(
|
$this->result = civicrm_api3(
|
||||||
'CustomField',
|
'CustomField',
|
||||||
|
@ -159,7 +179,8 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
else {
|
else {
|
||||||
throw new CiviCRM_API3_Exception($this->result['error_message']);
|
throw new CiviCRM_API3_Exception($this->result['error_message']);
|
||||||
}
|
}
|
||||||
} catch (CiviCRM_API3_Exception $e) {
|
}
|
||||||
|
catch (CiviCRM_API3_Exception $e) {
|
||||||
// If the field could not get created: log error
|
// If the field could not get created: log error
|
||||||
$errorMessage = $e->getMessage();
|
$errorMessage = $e->getMessage();
|
||||||
if ($this->name && $this->custom_group_id) {
|
if ($this->name && $this->custom_group_id) {
|
||||||
|
@ -176,6 +197,8 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
return $this->result;
|
return $this->result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return False;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add additional options to custom field
|
* Add additional options to custom field
|
||||||
|
@ -188,30 +211,14 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$option_group_id = civicrm_api3(
|
foreach ($this->option_values as $key => $value) {
|
||||||
'CustomField',
|
|
||||||
'getsingle',
|
|
||||||
['id' => $this->id]
|
|
||||||
)['option_group_id'];
|
|
||||||
} catch (CiviCRM_API3_Exception $e) {
|
|
||||||
$errorMessage = $e->getMessage();
|
|
||||||
Civi::log()
|
|
||||||
->error("$this->extensionName could not get get option group id for custom field \"$this->name\": $errorMessage");
|
|
||||||
CRM_Utils_System::setUFMessage(
|
|
||||||
E::ts('%1 could not get option group id for custom field \'%2\'. Find more information in the logs.',
|
|
||||||
[1 => $this->extensionName, 2 => $this->name])
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
foreach ($options as $key => $value) {
|
|
||||||
|
|
||||||
$option_value_exists = civicrm_api3(
|
$option_value_exists = civicrm_api3(
|
||||||
'OptionValue',
|
'OptionValue',
|
||||||
'get',
|
'get',
|
||||||
[
|
[
|
||||||
'sequential' => 1,
|
'sequential' => 1,
|
||||||
'option_group_id' => $option_group_id,
|
'option_group_id' => $options['option_group_id'],
|
||||||
'value' => $key,
|
'value' => $key,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
@ -222,7 +229,7 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
'OptionValue',
|
'OptionValue',
|
||||||
'create',
|
'create',
|
||||||
[
|
[
|
||||||
'option_group_id' => $option_group_id,
|
'option_group_id' => $options['option_group_id'],
|
||||||
'value' => $key,
|
'value' => $key,
|
||||||
'label' => $value,
|
'label' => $value,
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue