solve merge conflict

prepared to include project options into campaign

not workin yet
This commit is contained in:
Marc Michalsky forumZFD 2020-10-13 09:48:27 +02:00
parent 36ff75843a
commit ea15f7d597
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
2 changed files with 58 additions and 8 deletions

View file

@ -215,7 +215,7 @@ class TwingleApiCall {
$values['last_update'] > $project->lastUpdate() $values['last_update'] > $project->lastUpdate()
) { ) {
try { try {
$project->update($values, TwingleProject::TWINGLE); $project->update($values, $project_options, TwingleProject::TWINGLE);
$result = $project->create(); $result = $project->create();
} catch (\Exception $e){ } catch (\Exception $e){
// Log Exception // Log Exception

View file

@ -197,20 +197,26 @@ class TwingleProject {
* *
* @throws \Exception * @throws \Exception
*/ */
public function update(array $values, string $origin) { public function update(array $values, array $options, string $origin) {
if ($origin == self::TWINGLE) { if ($origin == self::TWINGLE) {
// Format values and translate keys // Format values and translate keys
self::translateKeys($values, self::IN); self::translateKeys($values, self::IN);
self::formatValues($values, self::IN); self::formatValues($values, self::IN);
//Format options and translate keys
self::translateKeys($options, self::IN);
self::formatValues($options, self::IN);
} }
elseif ($origin == self::CIVICRM) { elseif ($origin == self::CIVICRM) {
$this->id = $values['id']; $this->id = $values['id'];
self::translateCustomFields($values, self::OUT); self::translateCustomFields($values, self::OUT);
} }
// Update attributes // Update values and options
$this->values = array_merge($this->values, $values); $this->values = array_merge($this->values, $values);
$this->options = array_merge($this->options, $options);
} }
@ -279,15 +285,15 @@ class TwingleProject {
* *
* @return bool * @return bool
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
* @throws \Exception
*/ */
public function exists() { public function exists() {
$result = [];
// Get custom field name for project_id // Get custom field name for project_id
$cf_project_id = TwingleProject::$customFieldMapping['twingle_project_id']; $cf_project_id = TwingleProject::$customFieldMapping['twingle_project_id'];
$single = FALSE;
$result = [];
// If there is more than one campaign for a project, handle the duplicates // If there is more than one campaign for a project, handle the duplicates
while (!$single) { while (!$single) {
$result = civicrm_api3('Campaign', 'get', [ $result = civicrm_api3('Campaign', 'get', [
@ -308,8 +314,15 @@ 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) {
// Split result array into project values and options
$values_and_options = self::splitValues($result['values'][0]);
// Set attributes to the values of the existing TwingleProject campaign // Set attributes to the values of the existing TwingleProject campaign
$this->update($result['values'][0], self::CIVICRM); $this->update(
$values_and_options['values'],
$values_and_options['options'],
self::CIVICRM
);
return TRUE; return TRUE;
} }
@ -461,7 +474,6 @@ class TwingleProject {
unset($possible_contact_fields[$exclude_contact_field]); unset($possible_contact_fields[$exclude_contact_field]);
} }
$values['exclude_contact_fields'] = $values['exclude_contact_fields'] =
\CRM_Utils_Array::implodePadded($possible_contact_fields); \CRM_Utils_Array::implodePadded($possible_contact_fields);
} }
@ -561,6 +573,44 @@ class TwingleProject {
} }
} }
/**
* A function that will split one array coming from a TwingleProject campaign
* into a value array (containing basic project information) and another
* array containing the project options.
*
* @param array $input
* Array that comes from TwingleProject campaign
*
* @return array[]
* Associative array that contains two arrays: $values & $options
*
* @throws \Exception
*/
private function splitValues(array $input) {
$values = [];
$options = [];
// Get array with template for project values and options
$values_template = self::$templates['project'];
$options_template = self::$templates['project_options'];
// Map array items into $values and $options array
foreach ($input as $key => $value) {
if (in_array($key, $values_template)) {
$values[$key] = $value;
}
if (key_exists($key, $options_template)) {
$options[$key] = $value;
}
}
return [
'values' => $values,
'options' => $options
];
}
/** /**
* Deactivate this TwingleProject campaign * Deactivate this TwingleProject campaign