195 lines
4.5 KiB
PHP
195 lines
4.5 KiB
PHP
<?php
|
|
|
|
|
|
namespace CRM\TwingleCampaign\BAO;
|
|
|
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
|
use CRM\TwingleCampaign\Utils\ExtensionCache as Cache;
|
|
use DateTime;
|
|
use Exception;
|
|
use CiviCRM_API3_Exception;
|
|
|
|
|
|
class TwingleProject extends Campaign {
|
|
|
|
/**
|
|
* TwingleProject constructor.
|
|
*
|
|
* @param array $project
|
|
* Result array of Twingle API call to
|
|
* https://project.twingle.de/api/by-organisation/$organisation_id
|
|
*
|
|
* @param string $origin
|
|
* Origin of the arrays. It can be one of two constants:
|
|
* TwingleProject::TWINGLE|CIVICRM
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function __construct(array $project, string $origin) {
|
|
parent::__construct($project, $origin);
|
|
|
|
$this->className = get_class($this);
|
|
|
|
// Add value for campaign type
|
|
$this->values['campaign_type_id'] = 'twingle_project';
|
|
|
|
// Get custom field name for project_id
|
|
$this->id_custom_field = Cache::getInstance()
|
|
->getCustomFieldMapping()['twingle_project_id'];
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 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, self::OUT);
|
|
|
|
// Get template for project
|
|
$project = Cache::getInstance()->getTemplates()['project'];
|
|
|
|
// Replace array items which the Twingle API does not expect
|
|
foreach ($values as $key => $value) {
|
|
if (!in_array($key, $project)) {
|
|
unset($values[$key]);
|
|
}
|
|
}
|
|
|
|
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
|
|
*/
|
|
private function formatValues(array &$values, string $direction) {
|
|
|
|
if ($direction == self::IN) {
|
|
|
|
// Change timestamp into DateTime string
|
|
if ($values['last_update']) {
|
|
$values['last_update'] =
|
|
self::getDateTime($values['last_update']);
|
|
}
|
|
|
|
// empty project_type to 'default'
|
|
if (!$values['type']) {
|
|
$values['type'] = 'default';
|
|
}
|
|
}
|
|
elseif ($direction == self::OUT) {
|
|
|
|
// Change DateTime string into timestamp
|
|
$values['last_update'] =
|
|
self::getTimestamp($values['last_update']);
|
|
|
|
// Default project_type to ''
|
|
$values['type'] = $values['type'] == 'default'
|
|
? ''
|
|
: $values['type'];
|
|
|
|
// Cast project target to integer
|
|
$values['project_target'] = (int) $values['project_target'];
|
|
|
|
}
|
|
else {
|
|
|
|
throw new Exception(
|
|
"Invalid Parameter $direction for formatValues()"
|
|
);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Set embed data fields
|
|
*
|
|
* @param array $embedData
|
|
* Array with embed data from Twingle API
|
|
*/
|
|
public function setEmbedData(array $embedData) {
|
|
|
|
// Get all embed_data keys from template
|
|
$embed_data_keys = Cache::getInstance()
|
|
->getTemplates()['project_embed_data'];
|
|
|
|
// Transfer all embed_data values
|
|
foreach ($embed_data_keys as $key) {
|
|
$this->values[$key] = htmlspecialchars($embedData[$key]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set counter url
|
|
*
|
|
* @param String $counterUrl
|
|
* URL of the counter
|
|
*/
|
|
public function setCounterUrl(string $counterUrl) {
|
|
$this->values['counter'] = $counterUrl;
|
|
}
|
|
|
|
/**
|
|
* Deactivate this TwingleProject campaign
|
|
*
|
|
* @return bool
|
|
* TRUE if deactivation was successful
|
|
*
|
|
* @throws CiviCRM_API3_Exception
|
|
*/
|
|
public function deactivate() {
|
|
|
|
return self::deactivateByid($this->id);
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Get a response that describes the status of a TwingleProject
|
|
*
|
|
* @param string $status
|
|
* status of the TwingleProject you want the response for
|
|
*
|
|
* @return array
|
|
* Returns a response array that contains title, id, project_id and status
|
|
*/
|
|
public function getResponse(string $status) {
|
|
return [
|
|
'title' => $this->values['name'],
|
|
'id' => (int) $this->id,
|
|
'project_id' => (int) $this->values['id'],
|
|
'project_type' => $this->values['type'],
|
|
'status' => $status,
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns the project_id of a TwingleProject
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getProjectId() {
|
|
return (int) $this->values['id'];
|
|
}
|
|
|
|
}
|