️ automatically add new case types to custom field options

This commit is contained in:
Marc Michalsky forumZFD 2021-05-28 23:05:11 +02:00
parent cef8c31f31
commit dcae7c6a69
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
6 changed files with 184 additions and 33 deletions

View file

@ -56,12 +56,12 @@ class CRM_TwingleCampaign_BAO_CustomField {
} }
} }
/** /**
* Creates a CustomField by calling CiviCRM API v.3 * Creates a CustomField by calling CiviCRM API v.3
* *
* @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 Result of custom field creation api call
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
*/ */
@ -79,38 +79,44 @@ class CRM_TwingleCampaign_BAO_CustomField {
// If the field does not exist, create it // If the field does not exist, create it
if ($field['count'] == 0) { if ($field['count'] == 0) {
$this->result = civicrm_api3(
'CustomField',
'create',
$this->getSetAttributes());
// Set field id try {
$this->id = $this->result['id']; $this->result = civicrm_api3(
'CustomField',
'create',
$this->getSetAttributes());
// Log field creation if ($this->result['is_error'] == 0) {
if ($this->result['is_error'] == 0) {
Civi::log()->info("$this->extensionName has created a new custom field. // Set field id
label: $this->label $this->id = $this->result['id'];
name: $this->name
id: $this->id // Log field creation
group: $this->custom_group_id" Civi::log()->info("$this->extensionName has created a new custom field.
); label: $this->label
return $this->result; name: $this->name
} id: $this->id
// If the field could not get created: log error group: $this->custom_group_id"
else { );
return $this->result;
}
else {
throw new CiviCRM_API3_Exception($this->result['error_message']);
}
} catch (CiviCRM_API3_Exception $e) {
$errorMessage = $e->getMessage();
// If the field could not get created: log error
if ($this->name && $this->custom_group_id) { if ($this->name && $this->custom_group_id) {
Civi::log() Civi::log()
->error("$this->extensionName could not create new custom field ->error("$this->extensionName could not create new custom field \"$this->name\" for group \"$this->custom_group_id\": $errorMessage");
\"$this->name\" for group \"$this->custom_group_id\": CRM_Utils_System::setUFMessage(E::ts('%1: Creation of custom field \'%2\' failed. Find more information in the logs.',
$this->result['error_message']"); [1 => $this->extensionName, 2 => $this->name]
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 // If there is not enough information: log simple error message
else { else {
Civi::log() Civi::log()
->error("$this->extensionName could not create new custom field: ->error("$this->extensionName could not create new custom field: $errorMessage");
$this->result['error_message']");
CRM_Utils_System::setUFMessage(E::ts("Creation of custom field failed. Find more information in the logs.")); CRM_Utils_System::setUFMessage(E::ts("Creation of custom field failed. Find more information in the logs."));
} }
return $this->result; return $this->result;
@ -127,6 +133,101 @@ class CRM_TwingleCampaign_BAO_CustomField {
} }
} }
/**
* Update an existing custom field
*
* @returns array Result of custom field creation api call
*/
public function update(): array {
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;
}
}
/**
* Add additional options to custom field
*
* @param array $options
*
* @return array
*/
public function addOptions(array $options): array {
$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) {
$result[] = civicrm_api3(
'OptionValue',
'create',
[
'option_group_id' => $option_group_id,
'value' => $key,
'label' => $value,
]
);
}
} catch (CiviCRM_API3_Exception $e) {
$errorMessage = $e->getMessage();
Civi::log()
->error("$this->extensionName could not create additional option values for custom field \"$this->name\": $errorMessage");
CRM_Utils_System::setUFMessage(
E::ts('%1 could not create additional option values for custom field \'%2\'. Find more information in the logs.',
[1 => $this->extensionName, 2 => $this->name])
);
}
return $result;
}
/** /**
* Gets all the set attributes of the object and returns them as an array. * Gets all the set attributes of the object and returns them as an array.
* *
@ -143,7 +244,6 @@ class CRM_TwingleCampaign_BAO_CustomField {
return $setAttributes; return $setAttributes;
} }
/** /**
* Get an instance of a CustomField by its name or get an array with all * Get an instance of a CustomField by its name or get an array with all
* custom fields by leaving parameters empty. * custom fields by leaving parameters empty.
@ -362,6 +462,13 @@ class CRM_TwingleCampaign_BAO_CustomField {
*/ */
public function getId() { public function getId() {
return $this->id; return $this->id;
}# }
/**
* @param mixed $option_values
*/
public function setOptionValues($option_values): void {
$this->option_values = $option_values;
}
} }

Binary file not shown.

View file

@ -14,10 +14,10 @@ msgstr ""
#: CRM/TwingleCampaign/BAO/CustomField.php #: CRM/TwingleCampaign/BAO/CustomField.php
msgid "" msgid ""
"Creation of custom field '%1' failed. Find more information in the logs." "%1: Creation of custom field '%2' failed. Find more information in the logs."
msgstr "" msgstr ""
"Erstellung von Custom Field '%1' fehlgeschlagen. Mehr Informationen in den " "%1: Erstellen des benutzerdefinierten Feldes '%2' fehlgeschlagen. Mehr "
"Logs." "Informationen in den Logs."
#: CRM/TwingleCampaign/BAO/CustomField.php #: CRM/TwingleCampaign/BAO/CustomField.php
msgid "Creation of custom field failed. Find more information in the logs." msgid "Creation of custom field failed. Find more information in the logs."
@ -33,6 +33,29 @@ msgstr ""
"Erstellung der Custom Field '%1' fehlgeschlagen, weil bereits eine Custom " "Erstellung der Custom Field '%1' fehlgeschlagen, weil bereits eine Custom "
"Field mit selbem Namen existiert. Mehr Informationen in den Logs." "Field mit selbem Namen existiert. Mehr Informationen in den Logs."
#: CRM/TwingleCampaign/BAO/CustomField.php
msgid ""
"Creation of custom field '%1' failed. Find more information in the logs."
msgstr ""
"Erstellung von Custom Field '%1' fehlgeschlagen. Mehr Informationen in den "
"Logs."
#: CRM/TwingleCampaign/BAO/CustomField.php
msgid ""
"%1 could not get option group id for custom field '%2'. Find more "
"information in the logs."
msgstr ""
"%1 konnte die Option-Group-ID für das benutzerdefinierte Feld '%2' nicht "
"finden. Mehr Informationen in den Logs."
#: CRM/TwingleCampaign/BAO/CustomField.php
msgid ""
"%1 could not create additional option values for custom field '%2'. Find "
"more information in the logs."
msgstr ""
"%1 konnte dem benutzerdefinierte Feld '%2' keine zusätzlichen Optionen "
"hinzufügen. Mehr Informationen in den Logs."
#: CRM/TwingleCampaign/BAO/CustomGroup.php #: CRM/TwingleCampaign/BAO/CustomGroup.php
msgid "" msgid ""
"Creation of custom group '%1' failed. Find more information in the logs." "Creation of custom group '%1' failed. Find more information in the logs."
@ -255,7 +278,7 @@ msgid "Case"
msgstr "Fall" msgstr "Fall"
#: CRM/TwingleCampaign/resources/campaigns.php #: CRM/TwingleCampaign/resources/campaigns.php
msgid "Which case should get triggered for event creators?" msgid "Which case should get opened for event creators?"
msgstr "Welcher Fall soll für Event-Initiatoren eröffnet werden?" msgstr "Welcher Fall soll für Event-Initiatoren eröffnet werden?"
#: CRM/TwingleCampaign/resources/campaigns.php #: CRM/TwingleCampaign/resources/campaigns.php

View file

@ -1,5 +1,5 @@
#: ./CRM/TwingleCampaign/BAO/CustomField.php #: ./CRM/TwingleCampaign/BAO/CustomField.php
msgid "Creation of custom field '%1' failed. Find more information in the logs." msgid "%1: Creation of custom field '%2' failed. Find more information in the logs."
msgstr "" msgstr ""
#: ./CRM/TwingleCampaign/BAO/CustomField.php #: ./CRM/TwingleCampaign/BAO/CustomField.php
@ -10,6 +10,18 @@ msgstr ""
msgid "Creation of custom field '%1' failed, because a custom field with that name already exists. Find more information in the logs." msgid "Creation of custom field '%1' failed, because a custom field with that name already exists. Find more information in the logs."
msgstr "" msgstr ""
#: ./CRM/TwingleCampaign/BAO/CustomField.php
msgid "Creation of custom field '%1' failed. Find more information in the logs."
msgstr ""
#: ./CRM/TwingleCampaign/BAO/CustomField.php
msgid "%1 could not get option group id for custom field '%2'. Find more information in the logs."
msgstr ""
#: ./CRM/TwingleCampaign/BAO/CustomField.php
msgid "%1 could not create additional option values for custom field '%2'. Find more information in the logs."
msgstr ""
#: ./CRM/TwingleCampaign/BAO/CustomGroup.php #: ./CRM/TwingleCampaign/BAO/CustomGroup.php
msgid "Creation of custom group '%1' failed. Find more information in the logs." msgid "Creation of custom group '%1' failed. Find more information in the logs."
msgstr "" msgstr ""
@ -191,7 +203,7 @@ msgid "Case"
msgstr "" msgstr ""
#: ./CRM/TwingleCampaign/resources/campaigns.php #: ./CRM/TwingleCampaign/resources/campaigns.php
msgid "Which case should get triggered for event creators?" msgid "Which case should get opened for event creators?"
msgstr "" msgstr ""
#: ./CRM/TwingleCampaign/resources/campaigns.php #: ./CRM/TwingleCampaign/resources/campaigns.php

View file

@ -4,6 +4,7 @@ use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache;
use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject; use CRM_TwingleCampaign_BAO_TwingleProject as TwingleProject;
use CRM_TwingleCampaign_BAO_TwingleApiCall as TwingleApiCall; use CRM_TwingleCampaign_BAO_TwingleApiCall as TwingleApiCall;
use CRM_TwingleCampaign_ExtensionUtil as E; use CRM_TwingleCampaign_ExtensionUtil as E;
use CRM_TwingleCampaign_BAO_CustomField as CustomField;
require_once 'twinglecampaign.civix.php'; require_once 'twinglecampaign.civix.php';
@ -46,6 +47,14 @@ function twinglecampaign_civicrm_config(&$config) {
} }
} }
function twinglecampaign_civicrm_postSave_civicrm_case_type() {
$twingle_project_case_custom_field =
CustomField::fetch('twingle_project_case');
$newCaseType = json_decode($_POST['json'], True);
$twingle_project_case_custom_field->addOptions(
[$newCaseType['name'] => $newCaseType['title']]
);
}
/** /**
* Implements hook_civicrm_postSave_Campaign(). * Implements hook_civicrm_postSave_Campaign().