add static attributes for arrays from json files

This commit is contained in:
Marc Michalsky forumZFD 2020-10-13 09:07:54 +02:00
parent 8cb0a48009
commit 39fa2cc603
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9

View file

@ -24,6 +24,12 @@ class TwingleProject {
private static $customFieldMapping; private static $customFieldMapping;
private static $translations;
private static $campaigns;
private static $templates;
private $id; private $id;
private $values; private $values;
@ -50,6 +56,9 @@ class TwingleProject {
*/ */
public function __construct(array $project, array $options, string $origin) { public function __construct(array $project, array $options, string $origin) {
// Fetch custom field mapping once
self::init();
// If values come from CiviCRM Campaign API // If values come from CiviCRM Campaign API
if ($origin == self::CIVICRM) { if ($origin == self::CIVICRM) {
@ -79,9 +88,6 @@ class TwingleProject {
// Import project values and options // Import project values and options
$this->values = $project; $this->values = $project;
$this->options = $options; $this->options = $options;
// Fetch custom field mapping once
self::init();
} }
@ -93,11 +99,43 @@ class TwingleProject {
* @throws \Exception * @throws \Exception
*/ */
private static function init() { private static function init() {
// Initialize custom field mapping
if (self::$bInitialized) { if (self::$bInitialized) {
return; return;
} }
self::$customFieldMapping = CustomField::getMapping(); self::$customFieldMapping = CustomField::getMapping();
// Initialize json files as arrays
$file_paths = [
'translations' => '/api/v3/TwingleSync/resources/dictionary.json',
'templates' => '/api/v3/TwingleSync/resources/twingle_api_templates.json',
'campaigns' => '/CRM/TwingleCampaign/Upgrader/resources/campaigns.json',
];
foreach ($file_paths as $key => $file_path) {
// Get array from json file
$file_path = E::path() . $file_path;
$json_file = file_get_contents($file_path);
$json_file_name = pathinfo($file_path)['filename'];
$array = json_decode($json_file, TRUE);
// Throw and log an error if json file can't be read
if (!$array) {
$message = ($json_file_name)
? "Could not read json file $json_file_name"
: "Could not locate json file in path: $file_path";
\Civi::log()->error($message);
throw new \Exception($message);
}
// Set attribute
self::$$key = $array;
}
self::$bInitialized = TRUE; self::$bInitialized = TRUE;
} }
@ -199,20 +237,12 @@ class TwingleProject {
self::formatValues($values, self::OUT); self::formatValues($values, self::OUT);
self::translateKeys($values, self::OUT); self::translateKeys($values, self::OUT);
// Get json file with template for project // Get template for project
$json_file = file_get_contents(E::path() . $project = self::$templates['project'];
'/api/v3/TwingleSync/resources/twingle_api_templates.json');
$template = json_decode($json_file, TRUE)['project'];
// Throw an error if json file can't be read
if (!$template) {
\Civi::log()->error("Could not read json file");
throw new \Exception('Could not read json file');
}
// Replace array items which the Twingle API does not expect // Replace array items which the Twingle API does not expect
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if (!in_array($key, $template)) { if (!in_array($key, $project)) {
unset($values[$key]); unset($values[$key]);
} }
} }
@ -237,30 +267,18 @@ class TwingleProject {
self::formatValues($options, self::OUT); self::formatValues($options, self::OUT);
self::translateKeys($options, self::OUT); self::translateKeys($options, self::OUT);
// Get json file with template for project options // Get Template for project options
$file_path = E::path() . $project_options_template = self::$templates['project_options'];
'/api/v3/TwingleSync/resources/twingle_api_templates.json';
$json_file = file_get_contents($file_path);
$json_file_name = pathinfo($file_path)['filename'];
$template = json_decode($json_file, TRUE)['project_options'];
// Throw an error if json file can't be read
if (!$template) {
$message = ($json_file_name)
? "Could not read json file $json_file_name"
: "Could not locate json file in path: $file_path";
throw new \Exception($message);
}
// Replace array items which the Twingle API does not expect // Replace array items which the Twingle API does not expect
foreach ($options as $key => $value) { foreach ($options as $key => $value) {
if (!key_exists($key, $template)) { if (!key_exists($key, $project_options_template)) {
unset($options[$key]); unset($options[$key]);
} }
} }
// Complement missing options with default values // Complement missing options with default values
return array_merge($template, $options); return array_merge($project_options_template, $options);
} }
@ -370,28 +388,12 @@ class TwingleProject {
*/ */
private static function translateKeys(array &$values, string $direction) { private static function translateKeys(array &$values, string $direction) {
// Get json file with translations // Get translations for fields
$file_path = E::path() . $field_translations = self::$translations['fields'];
'/api/v3/TwingleSync/resources/dictionary.json';
$json_file = file_get_contents($file_path);
$json_file_name = pathinfo($file_path)['filename'];
$translations = json_decode($json_file, TRUE);
// Throw an error if json file can't be read
if (!$translations) {
$message = ($json_file_name)
? "Could not read json file $json_file_name"
: "Could not locate json file in path: $file_path";
throw new \Exception($message);
//TODO: use specific exception or create own
}
// Select only fields
$translations = $translations['fields'];
// Set the direction of the translation // Set the direction of the translation
if ($direction == self::OUT) { if ($direction == self::OUT) {
$translations = array_flip($translations); $field_translations = array_flip($field_translations);
} }
// Throw error if $direction constant does not match IN or OUT // Throw error if $direction constant does not match IN or OUT
elseif ($direction != self::IN) { elseif ($direction != self::IN) {
@ -402,7 +404,7 @@ class TwingleProject {
} }
// Translate keys // Translate keys
foreach ($translations as $origin => $translation) { foreach ($field_translations as $origin => $translation) {
$values[$translation] = $values[$origin]; $values[$translation] = $values[$origin];
unset($values[$origin]); unset($values[$origin]);
} }