implemented sync from CiviCRM to Twingle

This commit is contained in:
Marc Michalsky forumZFD 2020-10-06 19:57:09 +02:00
parent aef6b64004
commit 953adaf319
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
6 changed files with 229 additions and 156 deletions

View file

@ -123,10 +123,10 @@
"help_post": "Choose the project type. Allow users to create own events or to pay a membership fee.", "help_post": "Choose the project type. Allow users to create own events or to pay a membership fee.",
"default_value": "default" "default_value": "default"
}, },
"twingle_project_target": { "twingle_project_project_target": {
"custom_group_id": "Twingle_Project_Information", "custom_group_id": "Twingle_Project_Information",
"label": "Twingle Project target", "label": "Twingle Project target",
"name": "twingle_project_target", "name": "twingle_project_project_target",
"is_required": 0, "is_required": 0,
"is_searchable": 1, "is_searchable": 1,
"data_type": "Money", "data_type": "Money",

View file

@ -123,32 +123,43 @@ class TwingleApiCall {
if (is_array($values)) { if (is_array($values)) {
$project = new TwingleProject($values, TwingleProject::TWINGLE); $project = new TwingleProject($values, TwingleProject::TWINGLE);
$result = $project->create($is_test);
// Check if the TwingleProject campaign already exists
if (!$project->exists()) {
// ... if not, create it
$result = $project->create($is_test);
}
else {
$result = $project->getResponse('TwingleProject exists');
}
// If Twingle's version of the project is newer than the CiviCRM // If Twingle's version of the project is newer than the CiviCRM
// TwingleProject campaign update the campaign // TwingleProject campaign update the campaign
if ( if (
$result['state'] == 'TwingleProject already exists' && $result['state'] == 'TwingleProject exists' &&
$values['last_update'] > $project->lastUpdate() $values['last_update'] > $project->lastUpdate()
) { ) {
$result = $project->update($is_test); $project->update($values, TwingleProject::TWINGLE);
$result = $project->create();
} }
// If the CiviCRM TwingleProject campaign was changed, update the project // If the CiviCRM TwingleProject campaign was changed, update the project
// on Twingle's side // on Twingle's side
elseif ( elseif (
$result['state'] == 'TwingleProject already exists' && $result['state'] == 'TwingleProject exists' &&
$values['last_update'] < $project->lastUpdate() $values['last_update'] < $project->lastUpdate()
) { ) {
// If this is a test do not make database changes // If this is a test do not make database changes
if ($is_test) { if ($is_test) {
$result = TwingleProject::fetch($values['id'])->getResponse( $result = $project->getResponse(
'TwingleProject ready to push' 'TwingleProject ready to push'
); );
} }
else { else {
$result = $this->updateProject($project->export()); $result = $this->updateProject($project);
} }
}
elseif ($result['state'] == 'TwingleProject exists') {
$result = $project->getResponse('TwingleProject up to date');
} }
// Return a response of the synchronization // Return a response of the synchronization
@ -161,14 +172,19 @@ class TwingleApiCall {
} }
/** /**
* @param array $values * Sends an curl post call to Twingle to update an existing project and then
* @param bool $is_test * updates the TwingleProject campaign.
*
* @param \CRM\TwingleCampaign\BAO\TwingleProject $project
* The TwingleProject object that should get pushed to Twingle
* *
* @return array * @return array
* @throws \CiviCRM_API3_Exception * Returns a response array that contains title, id, project_id and state
* @throws \Exception *
*/ */
public function updateProject(array $values) { public function updateProject(TwingleProject $project) {
$values = $project->export();
// Prepare url for curl // Prepare url for curl
$url = $this->protocol . 'project' . $this->baseUrl . $values['id']; $url = $this->protocol . 'project' . $this->baseUrl . $values['id'];
@ -177,13 +193,23 @@ class TwingleApiCall {
$result = $this->curlPost($url, $values); $result = $this->curlPost($url, $values);
// Update TwingleProject in Civi with results from api call // Update TwingleProject in Civi with results from api call
$updated_project = new TwingleProject($result, TwingleProject::TWINGLE); if (is_array($result) && !array_key_exists('message', $result)) {
$updated_project->create(); $project->update($result, TwingleProject::TWINGLE);
return $updated_project->getResponse("TwingleProject pushed to Twingle"); $project->create();
return $project->getResponse('TwingleProject pushed to Twingle');
}
else {
$message = $result['message'];
return $project->getResponse(
$message
? "TwingleProject could not get pushed to Twingle: $message"
: 'TwingleProject could not get pushed to Twingle'
);
}
} }
public function updateEvent() { public function updateEvent() {
} }
@ -202,7 +228,8 @@ class TwingleApiCall {
* *
* @param null $params * @param null $params
* *
* @return mixed * @return array|bool
* Returns the result array of the curl or FALSE, if the curl failed
*/ */
private function curlGet($url, $params = NULL) { private function curlGet($url, $params = NULL) {
if (!empty($params)) { if (!empty($params)) {
@ -230,10 +257,11 @@ class TwingleApiCall {
"x-access-code: $this->apiKey", "x-access-code: $this->apiKey",
'Content-Type: application/json', 'Content-Type: application/json',
]); ]);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); $json = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$response = json_decode(curl_exec($curl), TRUE); $response = json_decode(curl_exec($curl), TRUE);
if (empty($response)) { if (empty($response)) {
$response = curl_error($curl); $response = FALSE;
} }
curl_close($curl); curl_close($curl);
return $response; return $response;

View file

@ -108,37 +108,35 @@ class TwingleProject {
// Create campaign only if it does not already exist // Create campaign only if it does not already exist
if (!$this->exists()) { 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 = $this->values;
self::translateCustomFields($translatedFields, self::OUT); self::translateCustomFields($translatedFields, self::IN);
// Create campaign // Set id
$result = civicrm_api3('Campaign', 'create', $translatedFields); $translatedFields['id'] = $this->id;
// Set id attribute // Create campaign
$this->id = $result['id']; $result = civicrm_api3('Campaign', 'create', $translatedFields);
// Check if campaign was created successfully // Update id
if ($result['is_error'] == 0) { $this->id = $result['id'];
$response = $this->getResponse('TwingleProject created');
}
else {
$response = $this->getResponse('TwingleProject creation failed');
}
// Check if campaign was created successfully
if ($result['is_error'] == 0) {
$response = $this->getResponse('TwingleProject created');
} }
// If this is a test, do not create campaign
else { else {
$response = $this->getResponse('TwingleProject not yet created'); $response = $this->getResponse('TwingleProject creation failed');
} }
} }
// If this is a test, do not create campaign
else { else {
// Give information back if campaign already exists $response = $this->getResponse('TwingleProject not yet created');
$response = $this->getResponse('TwingleProject already exists');
} }
return $response; return $response;
} }
@ -146,38 +144,35 @@ class TwingleProject {
/** /**
* Update an existing project * Update an existing project
* *
* If true: don't do any changes * @param array $values
* Array with values to update
* *
* @param bool $is_test * @param string $origin
* Origin of the array. It can be one of two constants:
* TwingleProject::TWINGLE|CIVICRM
* *
* @return array
* @throws \CiviCRM_API3_Exception
*/ */
public function update(bool $is_test = FALSE) { public function update(array $values, string $origin) {
$response = ''; if ($origin == self::TWINGLE) {
// Format values and translate keys
self::translateKeys($values, self::IN);
self::formatValues($values, self::IN);
}
elseif ($origin == self::CIVICRM) {
$this->id = $values['id'];
self::translateCustomFields($values, self::OUT);
}
// Translate Twingle field names to custom field names // Update attributes
$this->values = array_merge($this->values, $values);
// Translate Twingle field names into custom field names
$translatedFields = $this->values; $translatedFields = $this->values;
self::translateCustomFields($translatedFields, self::OUT); self::translateCustomFields($translatedFields, self::IN);
if (!$is_test) { // Set id
$result = civicrm_api3('Campaign', 'create', $translatedFields); $translatedFields['id'] = $this->id;
// If the TwingleProject campaign was updated successfully
if ($result['is_error'] == 0) {
$response = $this->getResponse('TwingleProject updated from Twingle');
}
// If the update failed for any reason
else {
$response = $this->getResponse('Updated from Twingle failed');
}
}
// If the TwingleProjects campaign has to get updated but this is in test mode
else {
$response = $this->getResponse('TwingleProject outdated');
}
return $response;
} }
@ -191,7 +186,23 @@ class TwingleProject {
$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);
unset($values['campaign_type_id']);
$json_file = file_get_contents(E::path() .
'/api/v3/TwingleSync/resources/twingle_api_templates.json');
$twingle_api_templates = json_decode($json_file, TRUE);
$project_template = $twingle_api_templates['project'];
if (!$project_template) {
\Civi::log()->error("Could not read json file");
throw new \Exception('Could not read json file');
}
foreach ($values as $key => $value) {
if (!in_array($key, $project_template)) {
unset($values[$key]);
}
}
return $values; return $values;
} }
@ -214,8 +225,7 @@ class TwingleProject {
while (!$single) { while (!$single) {
$result = civicrm_api3('Campaign', 'get', [ $result = civicrm_api3('Campaign', 'get', [
'sequential' => 1, 'sequential' => 1,
'return' => ['id', 'last_modified_date'], 'is_active' => 1,
'is_active' => '1',
$cf_project_id => $this->values['id'], $cf_project_id => $this->values['id'],
]); ]);
@ -231,12 +241,8 @@ 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) {
// set campaign id attribute // Set attributes to the values of the existing TwingleProject campaign
$this->id = $result['values'][0]['id']; $this->update($result['values'][0], self::CIVICRM);
// set last_modified_date
$this->values['last_modified_date'] =
$result['values'][0]['last_modified_date'];
return TRUE; return TRUE;
} }
@ -283,10 +289,9 @@ class TwingleProject {
// Delete the newest project from array to keep it active // Delete the newest project from array to keep it active
array_shift($result['values']); array_shift($result['values']);
// Instantiate the left projects to deactivate them // deactivate the projects
foreach ($result['values'] as $p) { foreach ($result['values'] as $p) {
$project = TwingleProject::fetch($p['id']); self::deactivateById($p['id']);
$project->deactivate();
} }
} }
@ -384,6 +389,9 @@ class TwingleProject {
? '' ? ''
: $values['type']; : $values['type'];
// Cast project_target to integer
$values['project_target'] = (int) $values['project_target'];
} }
else { else {
@ -426,7 +434,11 @@ class TwingleProject {
'', '',
$field $field
)]; )];
unset($values[$field]); unset($values[str_replace(
'twingle_project_',
'',
$field
)]);
} }
} }
} }
@ -451,7 +463,7 @@ class TwingleProject {
/** /**
* Deactivate a TwingleProject campaign * Deactivate this TwingleProject campaign
* *
* @return bool * @return bool
* TRUE if deactivation was successful * TRUE if deactivation was successful
@ -459,9 +471,32 @@ class TwingleProject {
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
*/ */
public function deactivate() { public function deactivate() {
return self::deactivateByid($this->id);
}
/**
* Deactivate a TwingleProject campaign by ID
*
* @param $id
* ID of the TwingleProject campaign that should get deactivated
*
* @return bool
* TRUE if deactivation was successful
*
* @throws \CiviCRM_API3_Exception
*/
public static function deactivateById($id) {
$result = civicrm_api3('Campaign', 'getsingle', [
'id' => $id,
'sequential' => 1,
]);
$result = civicrm_api3('Campaign', 'create', [ $result = civicrm_api3('Campaign', 'create', [
'title' => $this->values['title'], 'title' => $result['title'],
'id' => $this->id, 'id' => $id,
'is_active' => '0', 'is_active' => '0',
]); ]);

View file

@ -21,7 +21,7 @@ function _civicrm_api3_twingle_sync_Get_spec(&$spec) {
'api.required' => 0, 'api.required' => 0,
'description' => E::ts('The key to access the Twingle API'), 'description' => E::ts('The key to access the Twingle API'),
]; ];
$spec['test'] = [ $spec['is_test'] = [
'name' => 'is_test', 'name' => 'is_test',
'title' => E::ts('Test'), 'title' => E::ts('Test'),
'type' => CRM_Utils_Type::T_BOOLEAN, 'type' => CRM_Utils_Type::T_BOOLEAN,

View file

@ -1,73 +0,0 @@
{
"id": 2237,
"has_confirmation_mail": false,
"has_confirmation_mail_api": false,
"has_donation_receipt": true,
"has_contact_data": false,
"donation_rhythm": {
"yearly": true,
"halfyearly": false,
"quarterly": false,
"monthly": true,
"one_time": true
},
"has_newsletter_registration": false,
"has_postinfo_registration": false,
"design_background_color": "",
"design_primary_color": "",
"design_font": "",
"design_font_color": "",
"design_button_font_color": "",
"design_button_font_color_light": "",
"image": "",
"bcc_email_address": "demo-zfd@twingle.de",
"donation_value_min": 5,
"donation_value_max": 500,
"donation_value_default": 50,
"exclude_contact_fields": "birthday",
"custom_js": null,
"custom_css": null,
"share_url": null,
"has_contact_mandatory": false,
"has_doi": true,
"doi_redirect": null,
"has_force_donation_target_buttons": false,
"has_show_single_target": false,
"betterpayment_credit_card_theme": null,
"last_update": 1600433614,
"app_js": null,
"slidericon": "heart",
"extra_field": [],
"has_hidden_logo": false,
"has_projecttarget_as_money": false,
"rapidmail_recipient_list": null,
"mailchimp_recipient_list": null,
"has_mailchimp_add_all_user": null,
"has_mail_hide_paymentblock": false,
"has_mail_hide_paymentblock_api": false,
"has_donationtarget_textfield": false,
"has_civi_crm_activated": false,
"has_step_index": false,
"gift_donation": "",
"gift_donation_icon": null,
"gift_donation_image": null,
"languages": "de",
"has_buttons": false,
"has_no_slider": false,
"buttons": {
"button1": {
"amount": ""
},
"button2": {
"amount": ""
},
"button3": {
"amount": ""
}
},
"has_newsletter_namerequest": false,
"has_show_donator_data": false,
"has_donation_letter": false,
"has_donation_letter_api": false,
"amount_images": []
}

View file

@ -0,0 +1,83 @@
{ "project": [
"id",
"allow_more",
"name",
"organisation_id",
"project_target",
"transaction_type",
"type"
],
"project_settings": {
"id": 2237,
"has_confirmation_mail": false,
"has_confirmation_mail_api": false,
"has_donation_receipt": true,
"has_contact_data": false,
"donation_rhythm": {
"yearly": true,
"halfyearly": false,
"quarterly": false,
"monthly": true,
"one_time": true
},
"has_newsletter_registration": false,
"has_postinfo_registration": false,
"design_background_color": "",
"design_primary_color": "",
"design_font": "",
"design_font_color": "",
"design_button_font_color": "",
"design_button_font_color_light": "",
"image": "",
"bcc_email_address": "demo-zfd@twingle.de",
"donation_value_min": 5,
"donation_value_max": 500,
"donation_value_default": 50,
"exclude_contact_fields": "birthday",
"custom_js": null,
"custom_css": null,
"share_url": null,
"has_contact_mandatory": false,
"has_doi": true,
"doi_redirect": null,
"has_force_donation_target_buttons": false,
"has_show_single_target": false,
"betterpayment_credit_card_theme": null,
"last_update": 1600433614,
"app_js": null,
"slidericon": "heart",
"extra_field": [],
"has_hidden_logo": false,
"has_projecttarget_as_money": false,
"rapidmail_recipient_list": null,
"mailchimp_recipient_list": null,
"has_mailchimp_add_all_user": null,
"has_mail_hide_paymentblock": false,
"has_mail_hide_paymentblock_api": false,
"has_donationtarget_textfield": false,
"has_civi_crm_activated": false,
"has_step_index": false,
"gift_donation": "",
"gift_donation_icon": null,
"gift_donation_image": null,
"languages": "de",
"has_buttons": false,
"has_no_slider": false,
"buttons": {
"button1": {
"amount": ""
},
"button2": {
"amount": ""
},
"button3": {
"amount": ""
}
},
"has_newsletter_namerequest": false,
"has_show_donator_data": false,
"has_donation_letter": false,
"has_donation_letter_api": false,
"amount_images": []
}
}