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
|
||||
* 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
|
||||
*/
|
||||
public function create(bool $upgrade = FALSE): ?array {
|
||||
public function create(bool $upgrade = FALSE) {
|
||||
|
||||
// Check if the field already exists
|
||||
$field = civicrm_api3(
|
||||
|
@ -129,52 +130,74 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
|||
return NULL;
|
||||
}
|
||||
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;
|
||||
|
||||
try {
|
||||
$this->result = civicrm_api3(
|
||||
'CustomField',
|
||||
'create',
|
||||
$this->getSetAttributes());
|
||||
if (key_exists('option_group_id', $attributes)) {
|
||||
$this->addOptions($attributes);
|
||||
}
|
||||
|
||||
// Log field creation
|
||||
if ($this->result['is_error'] == 0) {
|
||||
Civi::log()->info("$this->extensionName has updated a custom field.
|
||||
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 {
|
||||
$this->result = civicrm_api3(
|
||||
'CustomField',
|
||||
'create',
|
||||
$this->getSetAttributes());
|
||||
|
||||
// Log field creation
|
||||
if ($this->result['is_error'] == 0) {
|
||||
Civi::log()->info("$this->extensionName has updated a custom field.
|
||||
label: $this->label
|
||||
name: $this->name
|
||||
id: $this->id
|
||||
group: $this->custom_group_id"
|
||||
);
|
||||
);
|
||||
return $this->result;
|
||||
}
|
||||
else {
|
||||
throw new CiviCRM_API3_Exception($this->result['error_message']);
|
||||
}
|
||||
}
|
||||
catch (CiviCRM_API3_Exception $e) {
|
||||
// If the field could not get created: log error
|
||||
$errorMessage = $e->getMessage();
|
||||
if ($this->name && $this->custom_group_id) {
|
||||
Civi::log()
|
||||
->error("$this->extensionName could not create new custom field \"$this->name\" for group \"$this->custom_group_id\": $errorMessage");
|
||||
CRM_Utils_System::setUFMessage(E::ts('Creation of custom field \'%1\' failed. Find more information in the logs.', [1 => $this->name]));
|
||||
}
|
||||
// If there is not enough information: log simple error message
|
||||
else {
|
||||
Civi::log()
|
||||
->error("$this->extensionName could not create new custom field: $errorMessage");
|
||||
CRM_Utils_System::setUFMessage(E::ts("Creation of custom field failed. Find more information in the logs."));
|
||||
}
|
||||
return $this->result;
|
||||
}
|
||||
else {
|
||||
throw new CiviCRM_API3_Exception($this->result['error_message']);
|
||||
}
|
||||
} catch (CiviCRM_API3_Exception $e) {
|
||||
// If the field could not get created: log error
|
||||
$errorMessage = $e->getMessage();
|
||||
if ($this->name && $this->custom_group_id) {
|
||||
Civi::log()
|
||||
->error("$this->extensionName could not create new custom field \"$this->name\" for group \"$this->custom_group_id\": $errorMessage");
|
||||
CRM_Utils_System::setUFMessage(E::ts('Creation of custom field \'%1\' failed. Find more information in the logs.', [1 => $this->name]));
|
||||
}
|
||||
// If there is not enough information: log simple error message
|
||||
else {
|
||||
Civi::log()
|
||||
->error("$this->extensionName could not create new custom field: $errorMessage");
|
||||
CRM_Utils_System::setUFMessage(E::ts("Creation of custom field failed. Find more information in the logs."));
|
||||
}
|
||||
return $this->result;
|
||||
}
|
||||
return False;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -188,30 +211,14 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
|||
$result = [];
|
||||
|
||||
try {
|
||||
$option_group_id = civicrm_api3(
|
||||
'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) {
|
||||
foreach ($this->option_values as $key => $value) {
|
||||
|
||||
$option_value_exists = civicrm_api3(
|
||||
'OptionValue',
|
||||
'get',
|
||||
[
|
||||
'sequential' => 1,
|
||||
'option_group_id' => $option_group_id,
|
||||
'option_group_id' => $options['option_group_id'],
|
||||
'value' => $key,
|
||||
]
|
||||
);
|
||||
|
@ -222,7 +229,7 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
|||
'OptionValue',
|
||||
'create',
|
||||
[
|
||||
'option_group_id' => $option_group_id,
|
||||
'option_group_id' => $options['option_group_id'],
|
||||
'value' => $key,
|
||||
'label' => $value,
|
||||
]
|
||||
|
@ -347,12 +354,12 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
|||
if ($this->label && $this->custom_group_id) {
|
||||
Civi::log()
|
||||
->error("$this->extensionName could not delete custom field
|
||||
\"$this->label\" for group \"$this->custom_group_id\":
|
||||
\"$this->label\" for group \"$this->custom_group_id\":
|
||||
$this->result['error_message']");
|
||||
}
|
||||
else {
|
||||
Civi::log()
|
||||
->error("$this->extensionName could not delete custom field:
|
||||
->error("$this->extensionName could not delete custom field:
|
||||
$this->result['error_message']");
|
||||
}
|
||||
}
|
||||
|
@ -496,4 +503,4 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
|||
$this->option_values = $option_values;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue