Initial commit

This commit is contained in:
Marc Michalsky forumZFD 2020-09-25 15:10:38 +02:00
commit 0ea5953d8c
Signed by untrusted user who does not match committer: marc.koch
GPG key ID: 12406554CFB028B9
24 changed files with 4762 additions and 0 deletions

View 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;
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace TwingleCampaign\Models;
class TwingleCampaign {
}

View file

@ -0,0 +1,9 @@
<?php
namespace TwingleCampaign\Models;
class TwingleEvent {
}

View 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() {
}
}