code cleanup
This commit is contained in:
parent
520fdb9f8d
commit
537ed5cb02
3 changed files with 55 additions and 196 deletions
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
|
use CRM_TwingleCampaign_Utils_ExtensionCache as ExtensionCache;
|
||||||
|
|
||||||
class CRM_TwingleCampaign_BAO_CustomField {
|
class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
|
|
||||||
|
@ -36,12 +37,17 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
|
|
||||||
private $result;
|
private $result;
|
||||||
|
|
||||||
|
private $extensionName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CustomField constructor.
|
* CustomField constructor.
|
||||||
*
|
*
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(array $attributes) {
|
public function __construct(array $attributes) {
|
||||||
|
|
||||||
|
$this->extensionName = E::LONG_NAME;
|
||||||
|
|
||||||
foreach ($this as $var => $value) {
|
foreach ($this as $var => $value) {
|
||||||
|
|
||||||
// put array items into attributes
|
// put array items into attributes
|
||||||
|
@ -86,7 +92,7 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
|
|
||||||
// Log field creation
|
// Log field creation
|
||||||
if ($this->result['is_error'] == 0) {
|
if ($this->result['is_error'] == 0) {
|
||||||
Civi::log()->info("Twingle Extension has created a new custom field.
|
Civi::log()->info("$this->extensionName has created a new custom field.
|
||||||
label: $this->label
|
label: $this->label
|
||||||
name: $this->name
|
name: $this->name
|
||||||
id: $this->id
|
id: $this->id
|
||||||
|
@ -97,14 +103,14 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
else {
|
else {
|
||||||
if ($this->label && $this->custom_group_id) {
|
if ($this->label && $this->custom_group_id) {
|
||||||
Civi::log()
|
Civi::log()
|
||||||
->error("Twingle Extension could not create new custom field
|
->error("$this->extensionName could not create new custom field
|
||||||
\"$this->label\" for group \"$this->custom_group_id\":
|
\"$this->label\" for group \"$this->custom_group_id\":
|
||||||
$this->result['error_message']");
|
$this->result['error_message']");
|
||||||
}
|
}
|
||||||
// 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("Twingle Extension could not create new custom field:
|
->error("$this->extensionName could not create new custom field:
|
||||||
$this->result['error_message']");
|
$this->result['error_message']");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,7 +123,7 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
* @return array
|
* @return array
|
||||||
* Array with all set attributes of this object.
|
* Array with all set attributes of this object.
|
||||||
*/
|
*/
|
||||||
private function getSetAttributes() {
|
private function getSetAttributes(): array {
|
||||||
$setAttributes = [];
|
$setAttributes = [];
|
||||||
foreach ($this as $var => $value) {
|
foreach ($this as $var => $value) {
|
||||||
if (isset($value)) {
|
if (isset($value)) {
|
||||||
|
@ -127,27 +133,6 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
return $setAttributes;
|
return $setAttributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Alter a custom field.
|
|
||||||
*
|
|
||||||
* @param $values
|
|
||||||
* Values to alter.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function alter($values) {
|
|
||||||
|
|
||||||
foreach ($values as $var => $value) {
|
|
||||||
if ($this->$var) {
|
|
||||||
$this->$var = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->result = civicrm_api3('CustomField', 'create', $this->getSetAttributes());
|
|
||||||
|
|
||||||
return $this->result['is_error'] == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
|
@ -166,28 +151,18 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
|
|
||||||
// If no specific custom field is requested
|
// If no specific custom field is requested
|
||||||
if (!$name) {
|
if (!$name) {
|
||||||
$customFields = [];
|
$result = [];
|
||||||
|
$customFields =
|
||||||
// Get json file with all custom fields for this extension
|
ExtensionCache::getInstance()->getCampaigns()['custom_fields'];
|
||||||
$json_file = file_get_contents(E::path() .
|
|
||||||
'/CRM/TwingleCampaign/resources/campaigns.json');
|
|
||||||
$campaign_info = json_decode($json_file, TRUE);
|
|
||||||
|
|
||||||
// Log an error and throw an exception if the file cannot get read
|
|
||||||
if (!$campaign_info) {
|
|
||||||
Civi::log()->error("Could not read json file");
|
|
||||||
throw new Exception('Could not read json file');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recursive method call with all custom field names from the json file
|
// Recursive method call with all custom field names from the json file
|
||||||
foreach ($campaign_info['custom_fields'] as $custom_field) {
|
foreach ($customFields as $customField) {
|
||||||
$result = self::fetch($custom_field['name']);
|
$result[] = self::fetch($customField['name']);
|
||||||
array_push($customFields, $result);
|
|
||||||
}
|
}
|
||||||
return $customFields;
|
return $result;
|
||||||
}
|
}
|
||||||
// If a specific custom field is required
|
// If a specific custom field is required
|
||||||
else {
|
try {
|
||||||
$custom_field = civicrm_api3(
|
$custom_field = civicrm_api3(
|
||||||
'CustomField',
|
'CustomField',
|
||||||
'get',
|
'get',
|
||||||
|
@ -196,12 +171,9 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
if ($custom_field = array_shift($custom_field['values'])) {
|
return new self(array_shift($custom_field['values']));
|
||||||
return new self($custom_field);
|
} catch (CiviCRM_API3_Exception $e) {
|
||||||
}
|
return NULL;
|
||||||
else {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,7 +193,7 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
|
|
||||||
// Check if custom field was deleted successfully
|
// Check if custom field was deleted successfully
|
||||||
if ($this->result['is_error'] == 0) {
|
if ($this->result['is_error'] == 0) {
|
||||||
Civi::log()->info("Twingle Extension has deleted custom field.
|
Civi::log()->info("$this->extensionName has deleted custom field.
|
||||||
label: $this->label
|
label: $this->label
|
||||||
name: $this->name
|
name: $this->name
|
||||||
id: $this->id
|
id: $this->id
|
||||||
|
@ -232,13 +204,13 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
else {
|
else {
|
||||||
if ($this->label && $this->custom_group_id) {
|
if ($this->label && $this->custom_group_id) {
|
||||||
Civi::log()
|
Civi::log()
|
||||||
->error("TwingleCampaign Extension could not delete custom field
|
->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']");
|
$this->result['error_message']");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Civi::log()
|
Civi::log()
|
||||||
->error("TwingleCampaign Extension could not delete custom field:
|
->error("$this->extensionName could not delete custom field:
|
||||||
$this->result['error_message']");
|
$this->result['error_message']");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -248,11 +220,12 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
* Get a custom field mapping (e.g. ['twingle_project_id' => 'custom_42'])
|
* Get a custom field mapping (e.g. ['twingle_project_id' => 'custom_42'])
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* Associative array with a mapping of all custom fields used by this extension
|
* Associative array with a mapping of all custom fields used by this
|
||||||
|
* extension
|
||||||
*
|
*
|
||||||
* @throws \CiviCRM_API3_Exception
|
* @throws \CiviCRM_API3_Exception
|
||||||
*/
|
*/
|
||||||
public static function getMapping() {
|
public static function getMapping(): array {
|
||||||
|
|
||||||
// Get an array with all custom fields
|
// Get an array with all custom fields
|
||||||
$customFields = self::fetch();
|
$customFields = self::fetch();
|
||||||
|
@ -269,139 +242,6 @@ class CRM_TwingleCampaign_BAO_CustomField {
|
||||||
return $customFieldMapping;
|
return $customFieldMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: Remove unnecessary getters and setters
|
|
||||||
/**
|
|
||||||
* @param string $custom_group_id
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setCustomGroupId(string $custom_group_id) {
|
|
||||||
return $this->alter(['custom_group_id', $custom_group_id]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $label
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setLabel(string $label) {
|
|
||||||
return $this->alter(['label', $label]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setName(string $name) {
|
|
||||||
return $this->alter(['name', $name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $is_required
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setIsRequired(int $is_required) {
|
|
||||||
return $this->alter(['is_required', $is_required]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $is_searchable
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setIsSearchable(int $is_searchable) {
|
|
||||||
return $this->alter(['is_searchable', $is_searchable]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $data_type
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setDataType(string $data_type) {
|
|
||||||
return $this->alter(['data_type', $data_type]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $html_type
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setHtmlType(string $html_type) {
|
|
||||||
return $this->alter(['html_type', $html_type]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $option_values
|
|
||||||
*/
|
|
||||||
public function setOptionValues($option_values) {
|
|
||||||
$this->option_values = $option_values;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $text_length
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setTextLength(int $text_length) {
|
|
||||||
return $this->alter(['text_length', $text_length]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $is_active
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setIsActive(int $is_active) {
|
|
||||||
return $this->alter(['is_active', $is_active]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $is_view
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setIsView(int $is_view) {
|
|
||||||
return $this->alter(['is_view', $is_view]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $weight
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
* @throws \CiviCRM_API3_Exception
|
|
||||||
*/
|
|
||||||
public function setWeight(int $weight) {
|
|
||||||
return $this->alter(['weight', $weight]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $help_post
|
|
||||||
*/
|
|
||||||
public function setHelpPost($help_post) {
|
|
||||||
$this->help_post = $help_post;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $default_value
|
|
||||||
*/
|
|
||||||
public function setDefaultValue($default_value) {
|
|
||||||
$this->default_value = $default_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -3,23 +3,38 @@
|
||||||
class CRM_TwingleCampaign_BAO_CustomGroup {
|
class CRM_TwingleCampaign_BAO_CustomGroup {
|
||||||
|
|
||||||
private $id;
|
private $id;
|
||||||
|
|
||||||
private $title;
|
private $title;
|
||||||
|
|
||||||
private $name;
|
private $name;
|
||||||
|
|
||||||
private $extends;
|
private $extends;
|
||||||
|
|
||||||
private $weight;
|
private $weight;
|
||||||
|
|
||||||
private $extends_entity_column_value;
|
private $extends_entity_column_value;
|
||||||
|
|
||||||
private $collapse_display;
|
private $collapse_display;
|
||||||
|
|
||||||
private $results;
|
private $results;
|
||||||
|
|
||||||
|
private $extensionName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CustomGroup constructor.
|
* CustomGroup constructor.
|
||||||
*
|
*
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
*/
|
*/
|
||||||
public function __construct(array $attributes) {
|
public function __construct(array $attributes) {
|
||||||
|
|
||||||
|
$this->extensionName = E::LONG_NAME;
|
||||||
|
|
||||||
foreach ($this as $var => $value) {
|
foreach ($this as $var => $value) {
|
||||||
if (array_key_exists($var, $attributes))
|
|
||||||
|
// put array items into attributes
|
||||||
|
if (array_key_exists($var, $attributes)) {
|
||||||
$this->$var = $attributes[$var];
|
$this->$var = $attributes[$var];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +48,7 @@ class CRM_TwingleCampaign_BAO_CustomGroup {
|
||||||
'get',
|
'get',
|
||||||
[
|
[
|
||||||
'sequential' => 1,
|
'sequential' => 1,
|
||||||
'name' => $this->getName()
|
'name' => $this->getName(),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -44,7 +59,7 @@ class CRM_TwingleCampaign_BAO_CustomGroup {
|
||||||
$this->id = $this->results['id'];
|
$this->id = $this->results['id'];
|
||||||
|
|
||||||
if ($this->results['is_error'] == 0) {
|
if ($this->results['is_error'] == 0) {
|
||||||
Civi::log()->info("TwingleCampaign Extension has created a new custom group.
|
Civi::log()->info("$this->extensionName has created a new custom group.
|
||||||
title: $this->title
|
title: $this->title
|
||||||
name: $this->name
|
name: $this->name
|
||||||
extends: $this->extends
|
extends: $this->extends
|
||||||
|
@ -54,12 +69,12 @@ class CRM_TwingleCampaign_BAO_CustomGroup {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ($this->title) {
|
if ($this->title) {
|
||||||
Civi::log()->error("TwingleCampaign Extension could not create new custom group
|
Civi::log()->error("$this->extensionName could not create new custom group
|
||||||
for \"$this->title\": $this->results['error_message']"
|
for \"$this->title\": $this->results['error_message']"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Civi::log()->error("TwingleCampaign Extension could not create new
|
Civi::log()->error("$this->extensionName could not create new
|
||||||
custom group: $this->results['error_message']");
|
custom group: $this->results['error_message']");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,7 +126,7 @@ class CRM_TwingleCampaign_BAO_CustomGroup {
|
||||||
'get',
|
'get',
|
||||||
[
|
[
|
||||||
'sequential' => 1,
|
'sequential' => 1,
|
||||||
'name' => $name
|
'name' => $name,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
if ($custom_group = array_shift($custom_group['values'])) {
|
if ($custom_group = array_shift($custom_group['values'])) {
|
||||||
|
@ -121,6 +136,7 @@ class CRM_TwingleCampaign_BAO_CustomGroup {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete() {
|
public function delete() {
|
||||||
$this->results = civicrm_api3(
|
$this->results = civicrm_api3(
|
||||||
'CustomGroup',
|
'CustomGroup',
|
||||||
|
@ -129,7 +145,7 @@ class CRM_TwingleCampaign_BAO_CustomGroup {
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($this->results['is_error'] == 0) {
|
if ($this->results['is_error'] == 0) {
|
||||||
Civi::log()->info("TwingleCampaign Extension has deleted custom group.
|
Civi::log()->info("$this->extensionName has deleted custom group.
|
||||||
title: $this->title
|
title: $this->title
|
||||||
name: $this->name
|
name: $this->name
|
||||||
extends: $this->extends
|
extends: $this->extends
|
||||||
|
@ -142,12 +158,12 @@ class CRM_TwingleCampaign_BAO_CustomGroup {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ($this->title) {
|
if ($this->title) {
|
||||||
Civi::log()->error("TwingleCampaign Extension could not delete custom group
|
Civi::log()->error("$this->extensionName could not delete custom group
|
||||||
\"$this->title\": $this->results['error_message']"
|
\"$this->title\": $this->results['error_message']"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Civi::log()->error("TwingleCampaign Extension could not delete custom group:
|
Civi::log()->error("$this->extensionName could not delete custom group:
|
||||||
$this->results['error_message']");
|
$this->results['error_message']");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,8 @@ use CRM_TwingleCampaign_BAO_CampaignType as CampaignType;
|
||||||
use CRM_TwingleCampaign_BAO_CustomField as CustomField;
|
use CRM_TwingleCampaign_BAO_CustomField as CustomField;
|
||||||
use CRM_TwingleCampaign_BAO_CustomGroup as CustomGroup;
|
use CRM_TwingleCampaign_BAO_CustomGroup as CustomGroup;
|
||||||
use CRM_TwingleCampaign_BAO_Configuration as Configuration;
|
use CRM_TwingleCampaign_BAO_Configuration as Configuration;
|
||||||
|
use CRM_TwingleCampaign_BAO_OptionValue as OptionValue;
|
||||||
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
|
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
|
||||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collection of upgrade steps.
|
* Collection of upgrade steps.
|
||||||
|
@ -56,6 +56,9 @@ class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base {
|
||||||
$ov->create();
|
$ov->create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create XCM-Profile
|
||||||
|
// TODO: create XCM-Profil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue