prepared to include project options into campaign

not workin yet
This commit is contained in:
Marc Michalsky forumZFD 2020-10-07 16:56:13 +02:00
parent 8b8f96cff7
commit 0a2166ed1b
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
2 changed files with 77 additions and 19 deletions

View file

@ -63,6 +63,14 @@ class TwingleProject {
// If values come from Twingle API // If values come from Twingle API
elseif ($origin == self::TWINGLE) { elseif ($origin == self::TWINGLE) {
// Set latest_update
$project['last_update'] = $project['last_update'] > $options['last_update']
? $project['last_update']
: $options['last_update'];
// Delete $options['last_update']
unset($options['last_update']);
// Translate keys for import // Translate keys for import
self::translateKeys($project, self::IN); self::translateKeys($project, self::IN);
self::translateKeys($options, self::IN); self::translateKeys($options, self::IN);
@ -114,12 +122,11 @@ class TwingleProject {
*/ */
public function create(bool $is_test = FALSE) { public function create(bool $is_test = FALSE) {
// Create campaign only if it does not already exist // Create campaign only if it does not already exist
if (!$is_test) { if (!$is_test) {
// Translate Twingle field names into custom field names // Translate Twingle field names into custom field names
$translatedFields = $this->values; $translatedFields = array_merge($this->options, $this->values);
self::translateCustomFields($translatedFields, self::IN); self::translateCustomFields($translatedFields, self::IN);
// Set id // Set id
@ -186,28 +193,34 @@ class TwingleProject {
/** /**
* Export values * Export values. Ensures that only those values will be exported which the
* Twingle API expects.
* *
* @return array * @return array
* Array with all values to send to the Twingle API
*
* @throws \Exception * @throws \Exception
*/ */
public function export() { public function export() {
$values = $this->values; $values = $this->values;
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
$json_file = file_get_contents(E::path() . $json_file = file_get_contents(E::path() .
'/api/v3/TwingleSync/resources/twingle_api_templates.json'); '/api/v3/TwingleSync/resources/twingle_api_templates.json');
$twingle_api_templates = json_decode($json_file, TRUE); $template = json_decode($json_file, TRUE)['project'];
$project_template = $twingle_api_templates['project'];
if (!$project_template) { // Throw an error if json file can't be read
if (!$template) {
\Civi::log()->error("Could not read json file"); \Civi::log()->error("Could not read json file");
throw new \Exception('Could not read json file'); throw new \Exception('Could not read json file');
} }
// Replace array items which the Twingle API does not expect
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if (!in_array($key, $project_template)) { if (!in_array($key, $template)) {
unset($values[$key]); unset($values[$key]);
} }
} }
@ -215,6 +228,49 @@ class TwingleProject {
return $values; return $values;
} }
/**
* Export options. Ensures that only those values will be exported which the
* Twingle API expects. Missing values will get complemented with default
* values.
*
* @return array
* Array with all options to send to the Twingle API
*
* @throws \Exception
*
*/
public function exportOptions() {
$options = $this->options;
self::formatValues($options, self::OUT);
self::translateKeys($options, self::OUT);
// Get json file with template for project options
$file_path = E::path() .
'/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
foreach ($options as $key => $value) {
if (!key_exists($key, $template)) {
unset($options[$key]);
}
}
// Complement missing options with default values
return array_merge($template, $options);
}
/** /**
* Check if a project already exists * Check if a project already exists

View file

@ -1,4 +1,5 @@
{ "project": [ {
"project": [
"id", "id",
"allow_more", "allow_more",
"name", "name",
@ -6,8 +7,10 @@
"project_target", "project_target",
"transaction_type", "transaction_type",
"type" "type"
], ],
"project_settings": { "event": [
],
"project_options": {
"id": 2237, "id": 2237,
"has_confirmation_mail": false, "has_confirmation_mail": false,
"has_confirmation_mail_api": false, "has_confirmation_mail_api": false,
@ -43,7 +46,6 @@
"has_force_donation_target_buttons": false, "has_force_donation_target_buttons": false,
"has_show_single_target": false, "has_show_single_target": false,
"betterpayment_credit_card_theme": null, "betterpayment_credit_card_theme": null,
"last_update": 1600433614,
"app_js": null, "app_js": null,
"slidericon": "heart", "slidericon": "heart",
"extra_field": [], "extra_field": [],