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,52 +130,74 @@ 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;
|
||||||
|
|
||||||
try {
|
if (key_exists('option_group_id', $attributes)) {
|
||||||
$this->result = civicrm_api3(
|
$this->addOptions($attributes);
|
||||||
'CustomField',
|
}
|
||||||
'create',
|
|
||||||
$this->getSetAttributes());
|
|
||||||
|
|
||||||
// Log field creation
|
foreach ($this as $var => $value) {
|
||||||
if ($this->result['is_error'] == 0) {
|
// put array items into attributes
|
||||||
Civi::log()->info("$this->extensionName has updated a custom field.
|
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
|
label: $this->label
|
||||||
name: $this->name
|
name: $this->name
|
||||||
id: $this->id
|
id: $this->id
|
||||||
group: $this->custom_group_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;
|
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 = [];
|
$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