create own class for project options
This commit is contained in:
parent
1acbbb7c78
commit
bb373f71b1
3 changed files with 390 additions and 191 deletions
|
@ -50,21 +50,23 @@ class TwingleProject {
|
||||||
* Result array of Twingle API call to
|
* Result array of Twingle API call to
|
||||||
* https://project.twingle.de/api/by-organisation/$organisation_id
|
* https://project.twingle.de/api/by-organisation/$organisation_id
|
||||||
*
|
*
|
||||||
* @param array $options
|
|
||||||
* Result array of Twingle Api call to
|
|
||||||
* https://project.twingle.de/api/$project_id/options
|
|
||||||
*
|
|
||||||
* @param string $origin
|
* @param string $origin
|
||||||
* Origin of the arrays. It can be one of two constants:
|
* Origin of the arrays. It can be one of two constants:
|
||||||
* TwingleProject::TWINGLE|CIVICRM
|
* TwingleProject::TWINGLE|CIVICRM
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(array $project, array $options, string $origin) {
|
public function __construct(array $project, string $origin) {
|
||||||
|
|
||||||
// Fetch custom field mapping once
|
// Fetch custom field mapping once
|
||||||
self::init();
|
self::init();
|
||||||
|
|
||||||
|
// Create TwingleProjectOptions object
|
||||||
|
$this->options = new TwingleProjectOptions($project['options'], $origin);
|
||||||
|
|
||||||
|
// Unset project options in $project array
|
||||||
|
unset($project['options']);
|
||||||
|
|
||||||
// If values come from CiviCRM Campaign API
|
// If values come from CiviCRM Campaign API
|
||||||
if ($origin == self::CIVICRM) {
|
if ($origin == self::CIVICRM) {
|
||||||
|
|
||||||
|
@ -74,25 +76,17 @@ class TwingleProject {
|
||||||
// Translate custom field names into Twingle field names
|
// Translate custom field names into Twingle field names
|
||||||
self::translateCustomFields($project, self::OUT);
|
self::translateCustomFields($project, self::OUT);
|
||||||
|
|
||||||
}
|
// Translate keys and values
|
||||||
// If values come from Twingle API
|
self::formatValues($project, self::OUT);
|
||||||
elseif ($origin == self::TWINGLE) {
|
self::translateKeys($project, self::OUT);
|
||||||
|
|
||||||
// Translate keys for import
|
|
||||||
self::translateKeys($project, self::IN);
|
|
||||||
|
|
||||||
// Format values for import
|
|
||||||
self::formatValues($project, self::IN);
|
|
||||||
self::formatValues($options, self::IN);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add value for campaign type
|
// Add value for campaign type
|
||||||
$project['campaign_type_id'] = 'twingle_project';
|
$project['campaign_type_id'] = 'twingle_project';
|
||||||
|
|
||||||
// Import project values and options
|
// Set project values attribute
|
||||||
$this->values = $project;
|
$this->values = $project;
|
||||||
$this->options = $options;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -161,15 +155,43 @@ class TwingleProject {
|
||||||
// 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
|
// Prepare project values for import into database
|
||||||
$translatedFields = array_merge($this->options, $this->values);
|
$values_prepared_for_import = $this->values;
|
||||||
self::translateCustomFields($translatedFields, self::IN);
|
self::formatValues(
|
||||||
|
$values_prepared_for_import,
|
||||||
|
self::IN
|
||||||
|
);
|
||||||
|
self::translateKeys(
|
||||||
|
$values_prepared_for_import,
|
||||||
|
self::IN
|
||||||
|
);
|
||||||
|
self::translateCustomFields(
|
||||||
|
$values_prepared_for_import,
|
||||||
|
self::IN
|
||||||
|
);
|
||||||
|
|
||||||
|
// Prepare project option values for import into database
|
||||||
|
$options_prepared_for_import = $this->options->getValues();
|
||||||
|
TwingleProjectOptions::formatValues(
|
||||||
|
$options_prepared_for_import,
|
||||||
|
self::IN
|
||||||
|
);
|
||||||
|
self::translateCustomFields(
|
||||||
|
$options_prepared_for_import,
|
||||||
|
self::IN
|
||||||
|
);
|
||||||
|
|
||||||
|
// Merge project values and project options values
|
||||||
|
$merged = array_merge(
|
||||||
|
$values_prepared_for_import,
|
||||||
|
$options_prepared_for_import
|
||||||
|
);
|
||||||
|
|
||||||
// Set id
|
// Set id
|
||||||
$translatedFields['id'] = $this->id;
|
$merged['id'] = $this->id;
|
||||||
|
|
||||||
// Create campaign
|
// Create campaign
|
||||||
$result = civicrm_api3('Campaign', 'create', $translatedFields);
|
$result = civicrm_api3('Campaign', 'create', $merged);
|
||||||
|
|
||||||
// Update id
|
// Update id
|
||||||
$this->id = $result['id'];
|
$this->id = $result['id'];
|
||||||
|
@ -198,35 +220,37 @@ class TwingleProject {
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* Array with values to update
|
* Array with values to update
|
||||||
*
|
*
|
||||||
* @param array $options
|
|
||||||
* Array with options to update
|
|
||||||
*
|
|
||||||
* @param string|null $origin
|
* @param string|null $origin
|
||||||
* Origin of the array. It can be one of two constants:
|
* Origin of the array. It can be one of two constants:
|
||||||
* TwingleProject::TWINGLE|CIVICRM
|
* TwingleProject::TWINGLE|CIVICRM
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function update(array $values, array $options, string $origin = NULL) {
|
public function update(array $values, string $origin = NULL) {
|
||||||
|
|
||||||
if ($origin == self::TWINGLE) {
|
if ($origin == self::CIVICRM) {
|
||||||
// Format values and translate keys
|
|
||||||
self::translateKeys($values, self::IN);
|
|
||||||
self::formatValues($values, self::IN);
|
|
||||||
|
|
||||||
//Format options and translate keys
|
// Set project id
|
||||||
self::translateKeys($options, self::IN);
|
|
||||||
self::formatValues($options, self::IN);
|
|
||||||
}
|
|
||||||
elseif ($origin == self::CIVICRM) {
|
|
||||||
$this->id = $values['id'];
|
$this->id = $values['id'];
|
||||||
|
|
||||||
|
// Translate custom field names
|
||||||
self::translateCustomFields($values, self::OUT);
|
self::translateCustomFields($values, self::OUT);
|
||||||
|
|
||||||
|
// Format project values
|
||||||
|
self::formatValues($values, self::OUT);
|
||||||
|
|
||||||
|
// Separate options from project values
|
||||||
|
self::separateOptions($values);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update values and options
|
// Update project options
|
||||||
$this->values = array_merge($this->values, $values);
|
$this->options->update($values['options'], $origin);
|
||||||
$this->options = array_merge($this->options, $options);
|
|
||||||
|
|
||||||
|
// Unset options array in project values
|
||||||
|
unset($values['options']);
|
||||||
|
|
||||||
|
// Update project values
|
||||||
|
$this->values = array_merge($this->values, $values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -271,7 +295,7 @@ class TwingleProject {
|
||||||
*/
|
*/
|
||||||
public function exportOptions() {
|
public function exportOptions() {
|
||||||
|
|
||||||
$options = $this->options;
|
$options = $this->values['options'];
|
||||||
self::formatValues($options, self::OUT);
|
self::formatValues($options, self::OUT);
|
||||||
self::translateKeys($options, self::OUT);
|
self::translateKeys($options, self::OUT);
|
||||||
|
|
||||||
|
@ -291,10 +315,14 @@ class TwingleProject {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a project already exists
|
* Check if a TwingleProject campaign already exists and if so set attributes
|
||||||
|
* to the values of the existing campaign.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* Returns TRUE id the TwingleProject campaign already exists
|
||||||
|
*
|
||||||
* @throws CiviCRM_API3_Exception
|
* @throws CiviCRM_API3_Exception
|
||||||
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function exists() {
|
public function exists() {
|
||||||
|
@ -325,28 +353,27 @@ class TwingleProject {
|
||||||
// project's attributes must be updated from the campaign
|
// project's attributes must be updated from the campaign
|
||||||
if ($result['count'] == 1) {
|
if ($result['count'] == 1) {
|
||||||
|
|
||||||
|
// Extract project values from result array
|
||||||
|
$values = $result['values'][0];
|
||||||
|
|
||||||
// Set campaign id attribute
|
// Set campaign id attribute
|
||||||
$this->id = $result['values'][0]['id'];
|
$this->id = $values['id'];
|
||||||
|
|
||||||
// Translate custom field names back
|
// Translate custom field names back
|
||||||
self::translateCustomFields($result['values'][0], self::OUT);
|
self::translateCustomFields($values, self::OUT);
|
||||||
|
|
||||||
// Translate keys from CiviCRM format to Twingle format
|
// Translate keys from CiviCRM format to Twingle format
|
||||||
self::translateKeys($result['values'][0], self::OUT);
|
self::translateKeys($values, self::OUT);
|
||||||
|
|
||||||
// Split result array into project values and options
|
// Separate options from project values
|
||||||
$values_and_options = self::splitValues($result['values'][0]);
|
self::separateOptions($values);
|
||||||
|
|
||||||
// Translate keys from Twingle format to CiviCRM format
|
// Translate keys from Twingle format to CiviCRM format
|
||||||
self::translateKeys($values_and_options['values'], self::IN);
|
self::translateKeys($values, self::IN);
|
||||||
self::translateKeys($values_and_options['options'], self::IN);
|
|
||||||
|
|
||||||
// Set attributes to the values of the existing TwingleProject campaign
|
// Set attributes to the values of the existing TwingleProject campaign
|
||||||
// to reflect the state of the actual campaign in the database
|
// to reflect the state of the actual campaign in the database
|
||||||
$this->update(
|
$this->update($values, self::TWINGLE);
|
||||||
$values_and_options['values'],
|
|
||||||
$values_and_options['options']
|
|
||||||
);
|
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@ -371,11 +398,11 @@ class TwingleProject {
|
||||||
'id' => $id,
|
'id' => $id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$values_and_options = self::splitValues($result);
|
// Separate options from project values
|
||||||
|
self::separateOptions($result['values']);
|
||||||
|
|
||||||
return new TwingleProject(
|
return new TwingleProject(
|
||||||
$values_and_options['values'],
|
$result['values'],
|
||||||
$values_and_options['options'],
|
|
||||||
self::CIVICRM
|
self::CIVICRM
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -421,7 +448,7 @@ class TwingleProject {
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private static function translateKeys(array &$values, string $direction) {
|
public static function translateKeys(array &$values, string $direction) {
|
||||||
|
|
||||||
// Get translations for fields
|
// Get translations for fields
|
||||||
$field_translations = self::$translations['fields'];
|
$field_translations = self::$translations['fields'];
|
||||||
|
@ -463,63 +490,16 @@ class TwingleProject {
|
||||||
if ($direction == self::IN) {
|
if ($direction == self::IN) {
|
||||||
|
|
||||||
// Change timestamp into DateTime string
|
// Change timestamp into DateTime string
|
||||||
if ($values['last_modified_date']) {
|
if ($values['last_update']) {
|
||||||
$values['last_modified_date'] =
|
$values['last_update'] =
|
||||||
self::getDateTime($values['last_modified_date']);
|
self::getDateTime($values['last_update']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// empty project_type to 'default
|
// empty project_type to 'default'
|
||||||
if ($values['type']) {
|
if (!$values['type']) {
|
||||||
$values['type'] = $values['type'] == ''
|
$values['type'] = 'default';
|
||||||
? 'default'
|
|
||||||
: $values['type'];
|
|
||||||
}
|
|
||||||
|
|
||||||
// format donation rhythm
|
|
||||||
if (is_array($values['donation_rhythm'])) {
|
|
||||||
$tmp = [];
|
|
||||||
foreach ($values['donation_rhythm'] as $key => $value) {
|
|
||||||
if ($value) {
|
|
||||||
$tmp[$key] = $key;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$values['donation_rhythm'] = CRM_Utils_Array::implodePadded($tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format project target format
|
|
||||||
if (key_exists('has_projecttarget_as_money', $values)) {
|
|
||||||
$values['has_projecttarget_as_money'] =
|
|
||||||
$values['has_projecttarget_as_money'] ? 'in Euro' : 'percentage';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format contact fields
|
|
||||||
if ($values['exclude_contact_fields']) {
|
|
||||||
$possible_contact_fields =
|
|
||||||
self::$campaigns['custom_fields']
|
|
||||||
['twingle_project_exclude_contact_fields']['option_values'];
|
|
||||||
|
|
||||||
$exclude_contact_fields = explode(',', $values['exclude_contact_fields']);
|
|
||||||
|
|
||||||
foreach ($exclude_contact_fields as $exclude_contact_field) {
|
|
||||||
unset($possible_contact_fields[$exclude_contact_field]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$values['exclude_contact_fields'] =
|
|
||||||
CRM_Utils_Array::implodePadded($possible_contact_fields);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format languages
|
|
||||||
if ($values['languages']) {
|
|
||||||
$values['languages'] =
|
|
||||||
CRM_Utils_Array::implodePadded(
|
|
||||||
explode(
|
|
||||||
',',
|
|
||||||
$values['languages']
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
elseif ($direction == self::OUT) {
|
elseif ($direction == self::OUT) {
|
||||||
|
|
||||||
// Change DateTime string into timestamp
|
// Change DateTime string into timestamp
|
||||||
|
@ -531,9 +511,6 @@ class TwingleProject {
|
||||||
? ''
|
? ''
|
||||||
: $values['type'];
|
: $values['type'];
|
||||||
|
|
||||||
// Cast project_target to integer
|
|
||||||
$values['project_target'] = (int) $values['project_target'];
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
||||||
|
@ -610,41 +587,27 @@ class TwingleProject {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A function that will split one array coming from a TwingleProject campaign
|
* A function that picks all option values from the values array and puts them
|
||||||
* into a value array (containing basic project information) and another
|
* into an own array.
|
||||||
* array containing the project options.
|
|
||||||
*
|
*
|
||||||
* @param array $input
|
* @param array $values
|
||||||
* Array that comes from TwingleProject campaign
|
|
||||||
*
|
|
||||||
* @return array[]
|
|
||||||
* Associative array that contains two arrays: $values & $options
|
|
||||||
*
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
private static function splitValues(array $input) {
|
private static function separateOptions(array &$values) {
|
||||||
|
|
||||||
$values = [];
|
|
||||||
$options = [];
|
$options = [];
|
||||||
|
|
||||||
// Get array with template for project values and options
|
// Get array with template for project values and options
|
||||||
$values_template = self::$templates['project'];
|
|
||||||
$options_template = self::$templates['project_options'];
|
$options_template = self::$templates['project_options'];
|
||||||
|
|
||||||
// Map array items into $values and $options array
|
// Map array items into $values and $options array
|
||||||
foreach ($input as $key => $value) {
|
foreach ($values as $key => $value) {
|
||||||
if (in_array($key, $values_template)) {
|
|
||||||
$values[$key] = $value;
|
|
||||||
}
|
|
||||||
if (key_exists($key, $options_template)) {
|
if (key_exists($key, $options_template)) {
|
||||||
$options[$key] = $value;
|
$options[$key] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
// Insert options array into values array
|
||||||
'values' => $values,
|
$values['options'] = $options;
|
||||||
'options' => $options,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -709,9 +672,9 @@ class TwingleProject {
|
||||||
*/
|
*/
|
||||||
public function getResponse(string $status) {
|
public function getResponse(string $status) {
|
||||||
return [
|
return [
|
||||||
'title' => $this->values['title'],
|
'title' => $this->values['name'],
|
||||||
'id' => $this->id,
|
'id' => (int) $this->id,
|
||||||
'project_id' => $this->values['id'],
|
'project_id' => (int) $this->values['id'],
|
||||||
'status' => $status,
|
'status' => $status,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -785,7 +748,12 @@ class TwingleProject {
|
||||||
* @return int|null
|
* @return int|null
|
||||||
*/
|
*/
|
||||||
public function lastUpdate() {
|
public function lastUpdate() {
|
||||||
return self::getTimestamp($this->values['last_modified_date']);
|
$lastProjectUpdate = self::getTimestamp($this->values['last_modified_date']);
|
||||||
|
$lastOptionsUpdate = self::getTimestamp($this->options->lastUpdate());
|
||||||
|
$lastUpdate = $lastProjectUpdate > $lastOptionsUpdate
|
||||||
|
? $lastProjectUpdate
|
||||||
|
: $lastOptionsUpdate;
|
||||||
|
return self::getTimestamp($lastUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,252 @@
|
||||||
|
|
||||||
namespace CRM\TwingleCampaign\BAO;
|
namespace CRM\TwingleCampaign\BAO;
|
||||||
|
|
||||||
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
|
use CRM_Utils_Array;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
include_once E::path() . '/CRM/TwingleCampaign/BAO/CustomField.php';
|
||||||
|
|
||||||
|
|
||||||
class TwingleProjectOptions {
|
class TwingleProjectOptions {
|
||||||
|
|
||||||
}
|
private $values;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TwingleProjectOptions constructor.
|
||||||
|
*
|
||||||
|
* @param array $options
|
||||||
|
* Result array of Twingle API call to
|
||||||
|
* https://project.twingle.de/api/by-organisation/$project_id/options
|
||||||
|
*
|
||||||
|
* @param string $origin
|
||||||
|
* Origin of the arrays. It can be one of two constants:
|
||||||
|
* TwingleProject::TWINGLE|CIVICRM
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function __construct(array $options, string $origin) {
|
||||||
|
|
||||||
|
// If values come from CiviCRM Campaign API
|
||||||
|
if ($origin == TwingleProject::CIVICRM) {
|
||||||
|
|
||||||
|
// Translate custom field names into Twingle field names
|
||||||
|
TwingleProject::translateCustomFields($options, TwingleProject::OUT);
|
||||||
|
|
||||||
|
// Format values
|
||||||
|
self::formatValues($options, TwingleProject::OUT);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unset project options id
|
||||||
|
unset($options['id']);
|
||||||
|
|
||||||
|
// Set project values attribute
|
||||||
|
$this->values = $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update an this object
|
||||||
|
*
|
||||||
|
* @param array $options
|
||||||
|
* Array with values to update
|
||||||
|
*
|
||||||
|
* @param string|null $origin
|
||||||
|
* Origin of the array. It can be one of two constants:
|
||||||
|
* TwingleProject::TWINGLE|CIVICRM
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function update(array $options, string $origin = NULL) {
|
||||||
|
|
||||||
|
if ($origin == TwingleProject::CIVICRM) {
|
||||||
|
self::formatValues($options, TwingleProject::OUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unset project options id
|
||||||
|
unset($options['id']);
|
||||||
|
|
||||||
|
// Update values
|
||||||
|
$this->values = array_merge($this->values, $options);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export values. Ensures that only those values will be exported which the
|
||||||
|
* Twingle API expects.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* Array with all values to send to the Twingle API
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function export() {
|
||||||
|
|
||||||
|
$values = $this->values;
|
||||||
|
self::formatValues($values, TwingleProject::OUT);
|
||||||
|
TwingleProject::translateKeys($values, TwingleProject::OUT);
|
||||||
|
|
||||||
|
// Get Template for project options
|
||||||
|
$project_options_template = TwingleProject::$templates['project_options'];
|
||||||
|
|
||||||
|
// Replace array items which the Twingle API does not expect
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
if (!key_exists($key, $project_options_template)) {
|
||||||
|
unset($values[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format project target format
|
||||||
|
if (key_exists('has_projecttarget_as_money', $values)) {
|
||||||
|
$values['has_projecttarget_as_money'] =
|
||||||
|
$values['has_projecttarget_as_money'] ? 'in Euro' : 'percentage';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $values;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Translate values between CiviCRM Campaigns and Twingle
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* array of which values shall be translated
|
||||||
|
*
|
||||||
|
* @param string $direction
|
||||||
|
* TwingleProject::IN -> translate array values from Twingle to CiviCRM <br>
|
||||||
|
* TwingleProject::OUT -> translate array values from CiviCRM to Twingle
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static function formatValues(array &$values, string $direction) {
|
||||||
|
|
||||||
|
if ($direction == TwingleProject::IN) {
|
||||||
|
|
||||||
|
// Change timestamp into DateTime string
|
||||||
|
if ($values['last_update']) {
|
||||||
|
$values['last_update'] =
|
||||||
|
TwingleProject::getDateTime($values['last_update']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// format donation rhythm
|
||||||
|
if (is_array($values['donation_rhythm'])) {
|
||||||
|
$tmp = [];
|
||||||
|
foreach ($values['donation_rhythm'] as $key => $value) {
|
||||||
|
if ($value) {
|
||||||
|
$tmp[$key] = $key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$values['donation_rhythm'] = CRM_Utils_Array::implodePadded($tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format contact fields
|
||||||
|
if ($values['exclude_contact_fields']) {
|
||||||
|
$possible_contact_fields =
|
||||||
|
TwingleProject::$campaigns['custom_fields']
|
||||||
|
['twingle_project_exclude_contact_fields']['option_values'];
|
||||||
|
|
||||||
|
$exclude_contact_fields = explode(
|
||||||
|
',',
|
||||||
|
$values['exclude_contact_fields']
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($exclude_contact_fields as $exclude_contact_field) {
|
||||||
|
unset($possible_contact_fields[$exclude_contact_field]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$values['exclude_contact_fields'] =
|
||||||
|
CRM_Utils_Array::implodePadded($possible_contact_fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format languages
|
||||||
|
if ($values['languages']) {
|
||||||
|
$values['languages'] =
|
||||||
|
CRM_Utils_Array::implodePadded(
|
||||||
|
explode(
|
||||||
|
',',
|
||||||
|
$values['languages']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format project target format
|
||||||
|
if (key_exists('has_projecttarget_as_money', $values)) {
|
||||||
|
$values['has_projecttarget_as_money'] =
|
||||||
|
$values['has_projecttarget_as_money'] ? 'in Euro' : 'percentage';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elseif ($direction == TwingleProject::OUT) {
|
||||||
|
|
||||||
|
// Change DateTime string into timestamp
|
||||||
|
$values['last_update'] =
|
||||||
|
TwingleProject::getTimestamp($values['last_update']);
|
||||||
|
|
||||||
|
// format donation rhythm
|
||||||
|
if (is_array($values['donation_rhythm'])) {
|
||||||
|
$tmp = [];
|
||||||
|
foreach ($values['donation_rhythm'] as $key => $value) {
|
||||||
|
if ($value) {
|
||||||
|
$tmp[$key] = $key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$values['donation_rhythm'] = CRM_Utils_Array::implodePadded($tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format contact fields
|
||||||
|
if ($values['exclude_contact_fields']) {
|
||||||
|
$possible_contact_fields =
|
||||||
|
TwingleProject::$campaigns['custom_fields']
|
||||||
|
['twingle_project_exclude_contact_fields']['option_values'];
|
||||||
|
|
||||||
|
$exclude_contact_fields = explode(
|
||||||
|
',',
|
||||||
|
$values['exclude_contact_fields']
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($exclude_contact_fields as $exclude_contact_field) {
|
||||||
|
unset($possible_contact_fields[$exclude_contact_field]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$values['exclude_contact_fields'] =
|
||||||
|
CRM_Utils_Array::implodePadded($possible_contact_fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format languages
|
||||||
|
if ($values['languages']) {
|
||||||
|
$values['languages'] =
|
||||||
|
CRM_Utils_Array::implodePadded(
|
||||||
|
explode(
|
||||||
|
',',
|
||||||
|
$values['languages']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cast project_target to integer
|
||||||
|
$values['project_target'] = (int) $values['project_target'];
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
throw new Exception(
|
||||||
|
"Invalid Parameter $direction for formatValues()"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getValues(): array {
|
||||||
|
return $this->values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function lastUpdate() {
|
||||||
|
return $this->values['last_modified_date'];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -140,8 +140,8 @@ class TwingleApiCall {
|
||||||
|
|
||||||
// Get project options
|
// Get project options
|
||||||
try {
|
try {
|
||||||
$project_options = $this->getProjectOptions($values['id']);
|
$values['options'] = $this->getProjectOptions($values['id']);
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
|
||||||
// Log Exception
|
// Log Exception
|
||||||
Civi::log()->error(
|
Civi::log()->error(
|
||||||
|
@ -157,19 +157,10 @@ class TwingleApiCall {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set latest_update
|
|
||||||
$values['last_update'] = $values['last_update'] > $project_options['last_update']
|
|
||||||
? $values['last_update']
|
|
||||||
: $project_options['last_update'];
|
|
||||||
|
|
||||||
// Delete $options['last_update']
|
|
||||||
unset($project_options['last_update']);
|
|
||||||
|
|
||||||
// Instantiate TwingleProject
|
// Instantiate TwingleProject
|
||||||
try {
|
try {
|
||||||
$project = new TwingleProject(
|
$project = new TwingleProject(
|
||||||
$values,
|
$values,
|
||||||
$project_options,
|
|
||||||
TwingleProject::TWINGLE
|
TwingleProject::TWINGLE
|
||||||
);
|
);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
@ -212,50 +203,46 @@ class TwingleApiCall {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$result = $project->getResponse('TwingleProject exists');
|
$result = $project->getResponse('TwingleProject exists');
|
||||||
|
|
||||||
|
// If Twingle's version of the project is newer than the CiviCRM
|
||||||
|
// TwingleProject campaign update the campaign
|
||||||
|
if ($values['last_update'] > $project->lastUpdate()) {
|
||||||
|
try {
|
||||||
|
$project->update($values, TwingleProject::TWINGLE);
|
||||||
|
$result = $project->create();
|
||||||
|
$result['status'] = $result['status'] == 'TwingleProject created'
|
||||||
|
? 'TwingleProject updated'
|
||||||
|
: 'TwingleProject Update failed';
|
||||||
|
} catch (Exception $e){
|
||||||
|
// Log Exception
|
||||||
|
Civi::log()->error(
|
||||||
|
"Could not update TwingleProject campaign: $e->getMessage()"
|
||||||
|
);
|
||||||
|
// Return result array with error description
|
||||||
|
$result = $project->getResponse(
|
||||||
|
"Could not update TwingleProject campaign: $e->getMessage()"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If the CiviCRM TwingleProject campaign was changed, update the project
|
||||||
|
// on Twingle's side
|
||||||
|
elseif ($values['last_update'] < $project->lastUpdate()) {
|
||||||
|
// If this is a test do not make database changes
|
||||||
|
if ($is_test) {
|
||||||
|
$result = $project->getResponse(
|
||||||
|
'TwingleProject ready to push'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$result = $this->updateProject($project);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif ($result['status'] == 'TwingleProject exists') {
|
||||||
|
$result = $project->getResponse('TwingleProject up to date');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If Twingle's version of the project is newer than the CiviCRM
|
|
||||||
// TwingleProject campaign update the campaign
|
|
||||||
if (
|
|
||||||
$result['status'] == 'TwingleProject exists' &&
|
|
||||||
$values['last_update'] > $project->lastUpdate()
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
$project->update($values, $project_options, TwingleProject::TWINGLE);
|
|
||||||
$result = $project->create();
|
|
||||||
$result['status'] = $result['status'] == 'TwingleProject created'
|
|
||||||
? 'TwingleProject updated'
|
|
||||||
: 'TwingleProject Update failed';
|
|
||||||
} catch (\Exception $e){
|
|
||||||
// Log Exception
|
|
||||||
\Civi::log()->error(
|
|
||||||
"Could not update TwingleProject campaign: $e->getMessage()"
|
|
||||||
);
|
|
||||||
// Return result array with error description
|
|
||||||
$result = $project->getResponse(
|
|
||||||
"Could not update TwingleProject campaign: $e->getMessage()"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If the CiviCRM TwingleProject campaign was changed, update the project
|
|
||||||
// on Twingle's side
|
|
||||||
elseif (
|
|
||||||
$result['status'] == 'TwingleProject exists' &&
|
|
||||||
$values['last_update'] < $project->lastUpdate()
|
|
||||||
) {
|
|
||||||
// If this is a test do not make database changes
|
|
||||||
if ($is_test) {
|
|
||||||
$result = $project->getResponse(
|
|
||||||
'TwingleProject ready to push'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$result = $this->updateProject($project);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif ($result['status'] == 'TwingleProject exists') {
|
|
||||||
$result = $project->getResponse('TwingleProject up to date');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return a response of the synchronization
|
// Return a response of the synchronization
|
||||||
return $result;
|
return $result;
|
||||||
|
@ -302,7 +289,6 @@ class TwingleApiCall {
|
||||||
if (is_array($result) && !array_key_exists('message', $result)) {
|
if (is_array($result) && !array_key_exists('message', $result)) {
|
||||||
// Try to update the local TwingleProject campaign
|
// Try to update the local TwingleProject campaign
|
||||||
try {
|
try {
|
||||||
// TODO: get options and add them as parameter to $project->update() call
|
|
||||||
$project->update($result, TwingleProject::TWINGLE);
|
$project->update($result, TwingleProject::TWINGLE);
|
||||||
$project->create();
|
$project->create();
|
||||||
return $project->getResponse('TwingleProject pushed to Twingle');
|
return $project->getResponse('TwingleProject pushed to Twingle');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue