Initial commit
This commit is contained in:
commit
0ea5953d8c
24 changed files with 4762 additions and 0 deletions
63
api/v3/TwingleSync/Get.php
Normal file
63
api/v3/TwingleSync/Get.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
use TwingleCampaign\Models\TwingleApiCall as TwingleApiCall;
|
||||
|
||||
include_once E::path() . '/api/v3/TwingleSync/models/TwingleApiCall.php';
|
||||
|
||||
/**
|
||||
* TwingleSync.Get API specification (optional)
|
||||
* This is used for documentation and validation.
|
||||
*
|
||||
* @param array $spec description of fields supported by this API call
|
||||
*
|
||||
* @see https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
|
||||
*/
|
||||
function _civicrm_api3_twingle_sync_Get_spec(&$spec) {
|
||||
$spec['twingle_api_key'] = [
|
||||
'name' => 'twingle_api_key',
|
||||
'title' => E::ts('Twingle API key'),
|
||||
'type' => CRM_Utils_Type::T_STRING,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('The key you need to access the Twingle API'),
|
||||
];
|
||||
$spec['test'] = [
|
||||
'name' => 'test',
|
||||
'title' => E::ts('Test'),
|
||||
'type' => CRM_Utils_Type::T_BOOLEAN,
|
||||
'api.required' => 0,
|
||||
'description' => E::ts('If this is set true, no database change will be made'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* TwingleSync.Get API
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return array
|
||||
* API result descriptor
|
||||
*
|
||||
* @throws \CiviCRM_API3_Exception|\API_Exception
|
||||
* @see civicrm_api3_create_success
|
||||
*
|
||||
*/
|
||||
function civicrm_api3_twingle_sync_Get($params) {
|
||||
$result_values = [];
|
||||
|
||||
$apiKey = empty($params['twingle_api_key'])
|
||||
? CRM_Core_BAO_Setting::getItem('', 'twingle_api_key')
|
||||
: $params['twingle_api_key'];
|
||||
|
||||
// TODO: Do the magic!
|
||||
|
||||
$twingleApi = new TwingleApiCall($apiKey);
|
||||
$result_values['projects'] = $twingleApi->getProject();
|
||||
foreach ($result_values['projects'] as $project) {
|
||||
if (is_array($project)) {
|
||||
$result_values['campaigns_created'][$project['id']] = $twingleApi->createProject($project);
|
||||
}
|
||||
}
|
||||
|
||||
return civicrm_api3_create_success($result_values);
|
||||
}
|
155
api/v3/TwingleSync/models/TwingleApiCall.php
Normal file
155
api/v3/TwingleSync/models/TwingleApiCall.php
Normal file
|
@ -0,0 +1,155 @@
|
|||
<?php
|
||||
|
||||
namespace TwingleCampaign\Models;
|
||||
|
||||
use CRM_Core_BAO_Setting;
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
use TwingleCampaign\Models\TwingleProject as TwingleProject;
|
||||
|
||||
include_once E::path() . '/api/v3/TwingleSync/models/TwingleProject.php';
|
||||
|
||||
class TwingleApiCall {
|
||||
|
||||
private $apiKey;
|
||||
|
||||
private $baseUrl = '.twingle.de/api/';
|
||||
|
||||
private $protocol = 'https://';
|
||||
|
||||
private $organisationIds;
|
||||
|
||||
/**
|
||||
* TwingleApiCall constructor.
|
||||
*
|
||||
* @param $apiKey
|
||||
*
|
||||
* @throws \API_Exception
|
||||
*/
|
||||
public function __construct($apiKey) {
|
||||
$this->apiKey = $apiKey;
|
||||
|
||||
$curl = curl_init($this->protocol . 'organisation' . $this->baseUrl);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
||||
"x-access-code: $apiKey",
|
||||
'Content-Type: application/json',
|
||||
]);
|
||||
|
||||
$response = json_decode(curl_exec($curl), TRUE);
|
||||
curl_close($curl);
|
||||
|
||||
if (empty($response)) {
|
||||
throw new \API_Exception("Twingle API call failed");
|
||||
}
|
||||
|
||||
$this->organisationIds = array_column($response, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* If $id parameter is empty, this function returns all projects for all
|
||||
* organisations this API key is assigned to.
|
||||
*
|
||||
* If $id parameter is given, this function returns a single project.
|
||||
*
|
||||
* @param null $projectId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProject($projectId = NULL) {
|
||||
$response = [];
|
||||
foreach ($this->organisationIds as $organisationId) {
|
||||
$url = empty($projectId)
|
||||
? $this->protocol . 'project' . $this->baseUrl . 'by-organisation/' . $organisationId
|
||||
: $this->protocol . 'project' . $this->baseUrl . $projectId;
|
||||
|
||||
$response = array_merge($this->curlGet($url));
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns all Events for the given $projectId
|
||||
*
|
||||
* @param $projectId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getEvent($projectId) {
|
||||
$result = [];
|
||||
$url = $this->protocol . 'project' . $this->baseUrl . $projectId . '/event';
|
||||
$limit = CRM_Core_BAO_Setting::getItem('', 'twingle_request_size');
|
||||
$offset = 0;
|
||||
$finished = FALSE;
|
||||
|
||||
while (!$finished) {
|
||||
$params = [
|
||||
'orderby' => 'id',
|
||||
'direction' => 'desc',
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'image' => 'as-boolean',
|
||||
'public' => 0,
|
||||
];
|
||||
$response = $this->curlGet($url, $params);
|
||||
$finished = count($response['data']) < $limit;
|
||||
$offset = $offset + $limit;
|
||||
$result = array_merge($result, $response['data']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function createProject($values) {
|
||||
try {
|
||||
if (is_array($values)) {
|
||||
$project = new TwingleProject($values);
|
||||
return $project->create();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// TODO: Handle Exception
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function updateProject() {
|
||||
}
|
||||
|
||||
public function updateEvent() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getOrganisationIds() {
|
||||
return $this->organisationIds;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Does a cURL and gives back the result array.
|
||||
*
|
||||
* @param $url
|
||||
*
|
||||
* @param null $params
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function curlGet($url, $params = NULL) {
|
||||
if (!empty($params)) {
|
||||
$url = $url . '?' . http_build_query($params);
|
||||
}
|
||||
$curl = curl_init($url);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
||||
"x-access-code: $this->apiKey",
|
||||
'Content-Type: application/json',
|
||||
]);
|
||||
$response = json_decode(curl_exec($curl), TRUE);
|
||||
if (empty($response)) {
|
||||
$response = curl_error($curl);
|
||||
}
|
||||
curl_close($curl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
9
api/v3/TwingleSync/models/TwingleCampaign.php
Normal file
9
api/v3/TwingleSync/models/TwingleCampaign.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace TwingleCampaign\Models;
|
||||
|
||||
|
||||
class TwingleCampaign {
|
||||
|
||||
}
|
9
api/v3/TwingleSync/models/TwingleEvent.php
Normal file
9
api/v3/TwingleSync/models/TwingleEvent.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace TwingleCampaign\Models;
|
||||
|
||||
|
||||
class TwingleEvent {
|
||||
|
||||
}
|
122
api/v3/TwingleSync/models/TwingleProject.php
Normal file
122
api/v3/TwingleSync/models/TwingleProject.php
Normal file
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace TwingleCampaign\Models;
|
||||
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
use DateTime;
|
||||
use TwingleCampaign\Models\CustomField as CustomField;
|
||||
|
||||
include E::path() . '/CRM/Twingle/Upgrader/models/CustomField.php';
|
||||
|
||||
|
||||
class TwingleProject {
|
||||
|
||||
private static $bInitialized = FALSE;
|
||||
|
||||
private $values;
|
||||
|
||||
private $timestamp;
|
||||
|
||||
private $settings;
|
||||
|
||||
private static $customFields = [];
|
||||
|
||||
/**
|
||||
* TwingleProject constructor.
|
||||
*
|
||||
* @param array $values
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(array $values) {
|
||||
|
||||
$this->timestamp = $values['last_update'];
|
||||
|
||||
// Format data types of the values for import into CiviCRM
|
||||
$this->formatForImport($values);
|
||||
|
||||
// Add necessary attributes
|
||||
$this->values['title'] = $values['name'];
|
||||
$this->values['campaign_type_id'] = 'twingle_project';
|
||||
|
||||
// Fetch custom fields once and store them in static attribute
|
||||
self::init();
|
||||
|
||||
// Map parameters to custom field names (e.g. "custom_21")
|
||||
foreach (self::$customFields as $customField) {
|
||||
if (!empty($values[str_replace('twingle_project_', '', $customField->getName())])) {
|
||||
$this->values['custom_' . $customField->getId()] =
|
||||
$values[str_replace('twingle_project_', '', $customField->getName())];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function init() {
|
||||
if (self::$bInitialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
$json_file = file_get_contents(E::path() .
|
||||
'/CRM/Twingle/Upgrader/resources/campaigns.json');
|
||||
$campaign_info = json_decode($json_file, TRUE);
|
||||
|
||||
if (!$campaign_info) {
|
||||
\Civi::log()->error("Could not read json file");
|
||||
throw new \Exception('Could not read json file');
|
||||
}
|
||||
|
||||
foreach ($campaign_info['custom_fields'] as $custom_field) {
|
||||
$result = CustomField::fetch($custom_field['name']);
|
||||
array_push(self::$customFields, $result);
|
||||
}
|
||||
|
||||
self::$bInitialized = TRUE;
|
||||
}
|
||||
|
||||
public function create() {
|
||||
$values = $this->values;
|
||||
try {
|
||||
return civicrm_api3('Campaign', 'create', $values);
|
||||
} catch (\CiviCRM_API3_Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function formatForImport(&$values) {
|
||||
|
||||
// Change timestamp into DateTime string
|
||||
if (!empty($values['last_update'])) {
|
||||
$date = DateTime::createFromFormat('U', $values['last_update'] );
|
||||
$values['last_update'] = $date->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
// Change event type empty string into 'default'
|
||||
if ($values['type'] == ''){
|
||||
$values['type'] = 'default';
|
||||
}
|
||||
}
|
||||
|
||||
private function formatForExport(&$values) {
|
||||
|
||||
// Change DateTime string into timestamp
|
||||
if (!empty($values['last_update'])) {
|
||||
$date = DateTime::createFromFormat('Y-m-d H:i:s', $values['last_update'] );
|
||||
$values['last_update'] = $date->getTimestamp();
|
||||
}
|
||||
|
||||
// Change event type 'default' into empty string
|
||||
if ($values['type'] == 'default'){
|
||||
$values['type'] = '';
|
||||
}
|
||||
}
|
||||
|
||||
public function syncSettings() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
73
api/v3/TwingleSync/resources/project_settings_template.json
Normal file
73
api/v3/TwingleSync/resources/project_settings_template.json
Normal file
|
@ -0,0 +1,73 @@
|
|||
{
|
||||
"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": []
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue