moved some BAO classes and json files + refactoring paths
This commit is contained in:
parent
66cd6b10a0
commit
bf0a4d7bfe
11 changed files with 11 additions and 11 deletions
228
CRM/TwingleCampaign/BAO/CampaignType.php
Normal file
228
CRM/TwingleCampaign/BAO/CampaignType.php
Normal file
|
@ -0,0 +1,228 @@
|
|||
<?php
|
||||
|
||||
namespace CRM\TwingleCampaign\BAO;
|
||||
|
||||
use CRM_Civirules_Utils_LoggerFactory as Civi;
|
||||
|
||||
class CampaignType {
|
||||
|
||||
private static $campaignTypes = [];
|
||||
private $id;
|
||||
private $name;
|
||||
private $label;
|
||||
private $value;
|
||||
private $option_group_id = 'campaign_type';
|
||||
private $results;
|
||||
|
||||
/**
|
||||
* CampaignType constructor.
|
||||
*
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function __construct(array $attributes) {
|
||||
foreach ($this as $var => $value) {
|
||||
if (array_key_exists($var, $attributes)) {
|
||||
$this->$var = $attributes[$var];
|
||||
}
|
||||
}
|
||||
|
||||
self::$campaignTypes[$this->name] = $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function create() {
|
||||
|
||||
$field = civicrm_api3(
|
||||
'OptionValue',
|
||||
'get',
|
||||
[
|
||||
'sequential' => 1,
|
||||
'option_group_id' => $this->option_group_id,
|
||||
'name' => $this->getName()
|
||||
]
|
||||
);
|
||||
|
||||
if ($field['count'] == 0)
|
||||
{
|
||||
$this->results = civicrm_api3('OptionValue', 'create', $this->getSetAttributes());
|
||||
|
||||
$this->value = array_column($this->results['values'], 'value')[0];
|
||||
|
||||
if ($this->results['is_error'] == 0) {
|
||||
\Civi::log()->info("Twingle Extension has created a new campaign type.\n
|
||||
label: $this->label\n
|
||||
name: $this->name"
|
||||
);
|
||||
}
|
||||
else {
|
||||
\Civi::log()->error("Twingle Extension could not create new campaign type
|
||||
for \"$this->label\": $this->results['error_message']");
|
||||
}
|
||||
}
|
||||
else {
|
||||
$campaignType = CampaignType::fetch($this->name);
|
||||
foreach ($this as $var => $value) {
|
||||
if (array_key_exists($var, $campaignType->getSetAttributes())) {
|
||||
$this->$var = $campaignType->getSetAttributes()[$var];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getSetAttributes() {
|
||||
$setAttributes = [];
|
||||
foreach ($this as $var => $value) {
|
||||
if (isset($value)) {
|
||||
$setAttributes[$var] = $value;
|
||||
}
|
||||
}
|
||||
return $setAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $values
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function alter($values) {
|
||||
|
||||
foreach ($values as $var => $value) {
|
||||
if (isset($this->$var)) {
|
||||
$this->$var = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$this->results = civicrm_api3('OptionValue', 'create', $this->getSetAttributes());
|
||||
|
||||
return $this->results['is_error'] == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return \CRM\TwingleCampaign\BAO\CampaignType|null
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public static function fetch($name) {
|
||||
$campaign_type = civicrm_api3(
|
||||
'OptionValue',
|
||||
'get',
|
||||
[
|
||||
'sequential' => 1,
|
||||
'name' => $name
|
||||
]
|
||||
);
|
||||
if ($campaign_type = array_shift($campaign_type['values'])) {
|
||||
return new CampaignType($campaign_type);
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function delete() {
|
||||
$this->results = civicrm_api3(
|
||||
'OptionValue',
|
||||
'delete',
|
||||
['id' => $this->id]
|
||||
);
|
||||
|
||||
if ($this->results['is_error'] == 0) {
|
||||
\Civi::log()->info("Twingle Extension has deleted campaign type.\n
|
||||
label: $this->label\n
|
||||
name: $this->name"
|
||||
);
|
||||
|
||||
foreach ($this->getSetAttributes() as $var => $attribute) {
|
||||
$this->$var = NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($this->label) {
|
||||
\Civi::log()->error("Twingle Extension could not delete campaign type
|
||||
\"$this->label\": $this->results['error_message']"
|
||||
);
|
||||
}
|
||||
else {
|
||||
\Civi::log()->error("Twingle Extension could not delete campaign type:
|
||||
$this->results['error_message']");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setName(string $name) {
|
||||
return $this->alter(['name', $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setValue(string $value) {
|
||||
return $this->alter(['value', $value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setLabel(string $label) {
|
||||
return $this->alter(['label', $label]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getCampaignTypes(): array {
|
||||
return self::$campaignTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getValue(): string {
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLabel(): string {
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getResults() {
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
|
||||
}
|
478
CRM/TwingleCampaign/BAO/CustomField.php
Normal file
478
CRM/TwingleCampaign/BAO/CustomField.php
Normal file
|
@ -0,0 +1,478 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace CRM\TwingleCampaign\BAO;
|
||||
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
|
||||
class CustomField {
|
||||
|
||||
private $id;
|
||||
|
||||
private $custom_group_id;
|
||||
|
||||
private $label;
|
||||
|
||||
private $name;
|
||||
|
||||
private $is_required;
|
||||
|
||||
private $is_searchable;
|
||||
|
||||
private $data_type;
|
||||
|
||||
private $html_type;
|
||||
|
||||
private $option_values;
|
||||
|
||||
private $text_length;
|
||||
|
||||
private $is_active;
|
||||
|
||||
private $is_view;
|
||||
|
||||
private $weight;
|
||||
|
||||
private $help_post;
|
||||
|
||||
private $default_value;
|
||||
|
||||
private $result;
|
||||
|
||||
/**
|
||||
* CustomField constructor.
|
||||
*
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function __construct(array $attributes) {
|
||||
foreach ($this as $var => $value) {
|
||||
if (array_key_exists($var, $attributes)) {
|
||||
$this->$var = $attributes[$var];
|
||||
}
|
||||
if ($this->help_post) {
|
||||
$this->help_post = E::ts($this->help_post);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function create() {
|
||||
|
||||
$field = civicrm_api3(
|
||||
'CustomField',
|
||||
'get',
|
||||
[
|
||||
'sequential' => 1,
|
||||
'name' => $this->getName(),
|
||||
]
|
||||
);
|
||||
|
||||
if ($field['count'] == 0) {
|
||||
$this->result = civicrm_api3(
|
||||
'CustomField',
|
||||
'create',
|
||||
$this->getSetAttributes());
|
||||
|
||||
$this->id = $this->result['id'];
|
||||
|
||||
if ($this->result['is_error'] == 0) {
|
||||
\Civi::log()->info("Twingle Extension has created a new custom field.\n
|
||||
label: $this->label\n
|
||||
name: $this->name\n
|
||||
id: $this->id\n
|
||||
group: $this->custom_group_id"
|
||||
);
|
||||
}
|
||||
else {
|
||||
if ($this->label && $this->custom_group_id) {
|
||||
\Civi::log()
|
||||
->error("Twingle Extension could not create new custom field
|
||||
\"$this->label\" for group \"$this->custom_group_id\":
|
||||
$this->result['error_message']");
|
||||
}
|
||||
else {
|
||||
\Civi::log()
|
||||
->error("Twingle Extension could not create new custom field:
|
||||
$this->result['error_message']");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getSetAttributes() {
|
||||
$setAttributes = [];
|
||||
foreach ($this as $var => $value) {
|
||||
if (isset($value)) {
|
||||
$setAttributes[$var] = $value;
|
||||
}
|
||||
}
|
||||
return $setAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alter a custom field
|
||||
*
|
||||
* @param $values
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function alter($values) {
|
||||
|
||||
foreach ($values as $var => $value) {
|
||||
if ($this->$var) {
|
||||
$this->$var = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$this->result = civicrm_api3('CustomField', 'create', $this->getSetAttributes());
|
||||
|
||||
return $this->result['is_error'] == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return array|\CRM\TwingleCampaign\BAO\CustomField
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public static function fetch($name = NULL) {
|
||||
|
||||
if (!$name) {
|
||||
$customFields = [];
|
||||
|
||||
$json_file = file_get_contents(E::path() .
|
||||
'/CRM/TwingleCampaign/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($customFields, $result);
|
||||
}
|
||||
return $customFields;
|
||||
}
|
||||
else {
|
||||
$custom_field = civicrm_api3(
|
||||
'CustomField',
|
||||
'get',
|
||||
[
|
||||
'sequential' => 1,
|
||||
'name' => $name,
|
||||
]
|
||||
);
|
||||
if ($custom_field = array_shift($custom_field['values'])) {
|
||||
return new CustomField($custom_field);
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a custom field
|
||||
*
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function delete() {
|
||||
$this->result = civicrm_api3(
|
||||
'CustomField',
|
||||
'delete',
|
||||
['id' => $this->id]
|
||||
);
|
||||
|
||||
if ($this->result['is_error'] == 0) {
|
||||
\Civi::log()->info("Twingle Extension has deleted custom field.\n
|
||||
label: $this->label\n
|
||||
name: $this->name\n
|
||||
id: $this->id\n
|
||||
group: $this->custom_group_id"
|
||||
);
|
||||
|
||||
foreach ($this->getSetAttributes() as $var => $attribute) {
|
||||
$this->$var = NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($this->label && $this->custom_group_id) {
|
||||
\Civi::log()
|
||||
->error("Twingle Extension could not delete custom field
|
||||
\"$this->label\" for group \"$this->custom_group_id\":
|
||||
$this->result['error_message']");
|
||||
}
|
||||
else {
|
||||
\Civi::log()
|
||||
->error("Twingle Extension could not delete custom field:
|
||||
$this->result['error_message']");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a custom field mapping
|
||||
*
|
||||
* @return array
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public static function getMapping() {
|
||||
|
||||
$customFields = CustomField::fetch();
|
||||
$customFieldMapping = [];
|
||||
|
||||
foreach ($customFields as $customField) {
|
||||
if ($customField) {
|
||||
$customFieldMapping[$customField->getName()] = 'custom_' . $customField->getId();
|
||||
}
|
||||
}
|
||||
|
||||
return $customFieldMapping;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $custom_group_id
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setCustomGroupId(string $custom_group_id) {
|
||||
return $this->alter(['custom_group_id', $custom_group_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $label
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setLabel(string $label) {
|
||||
return $this->alter(['label', $label]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setName(string $name) {
|
||||
return $this->alter(['name', $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $is_required
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setIsRequired(int $is_required) {
|
||||
return $this->alter(['is_required', $is_required]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $is_searchable
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setIsSearchable(int $is_searchable) {
|
||||
return $this->alter(['is_searchable', $is_searchable]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data_type
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setDataType(string $data_type) {
|
||||
return $this->alter(['data_type', $data_type]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $html_type
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setHtmlType(string $html_type) {
|
||||
return $this->alter(['html_type', $html_type]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $option_values
|
||||
*/
|
||||
public function setOptionValues($option_values) {
|
||||
$this->option_values = $option_values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $text_length
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setTextLength(int $text_length) {
|
||||
return $this->alter(['text_length', $text_length]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $is_active
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setIsActive(int $is_active) {
|
||||
return $this->alter(['is_active', $is_active]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $is_view
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setIsView(int $is_view) {
|
||||
return $this->alter(['is_view', $is_view]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $weight
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setWeight(int $weight) {
|
||||
return $this->alter(['weight', $weight]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $help_post
|
||||
*/
|
||||
public function setHelpPost($help_post) {
|
||||
$this->help_post = $help_post;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $default_value
|
||||
*/
|
||||
public function setDefaultValue($default_value) {
|
||||
$this->default_value = $default_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCustomGroupId(): string {
|
||||
return $this->custom_group_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLabel(): string {
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getIsRequired(): int {
|
||||
return $this->is_required;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getIsSearchable(): int {
|
||||
return $this->is_searchable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDataType(): string {
|
||||
return $this->data_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHtmlType(): string {
|
||||
return $this->html_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOptionValues() {
|
||||
return $this->option_values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTextLength(): int {
|
||||
return $this->text_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getIsActive(): int {
|
||||
return $this->is_active;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getIsView(): int {
|
||||
return $this->is_view;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getWeight(): int {
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHelpPost() {
|
||||
return $this->help_post;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDefaultValue() {
|
||||
return $this->default_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}#
|
||||
|
||||
}
|
260
CRM/TwingleCampaign/BAO/CustomGroup.php
Normal file
260
CRM/TwingleCampaign/BAO/CustomGroup.php
Normal file
|
@ -0,0 +1,260 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace CRM\TwingleCampaign\BAO;
|
||||
|
||||
use CRM_Civirules_Utils_LoggerFactory as Civi;
|
||||
|
||||
class CustomGroup {
|
||||
|
||||
private $id;
|
||||
private $title;
|
||||
private $name;
|
||||
private $extends;
|
||||
private $weight;
|
||||
private $extends_entity_column_value;
|
||||
private $results;
|
||||
|
||||
/**
|
||||
* CustomGroup constructor.
|
||||
*
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function __construct(array $attributes) {
|
||||
foreach ($this as $var => $value) {
|
||||
if (array_key_exists($var, $attributes))
|
||||
$this->$var = $attributes[$var];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function create() {
|
||||
|
||||
$field = civicrm_api3(
|
||||
'CustomGroup',
|
||||
'get',
|
||||
[
|
||||
'sequential' => 1,
|
||||
'name' => $this->getName()
|
||||
]
|
||||
);
|
||||
|
||||
if ($field['count'] == 0) {
|
||||
|
||||
$this->results = civicrm_api3('CustomGroup', 'create', $this->getSetAttributes());
|
||||
|
||||
$this->id = $this->results['id'];
|
||||
|
||||
if ($this->results['is_error'] == 0) {
|
||||
\Civi::log()->info("Twingle Extension has created a new custom group.\n
|
||||
title: $this->title\n
|
||||
name: $this->name\n
|
||||
extends: $this->extends\n
|
||||
id: $this->id\n
|
||||
column_value: $this->extends_entity_column_value"
|
||||
);
|
||||
}
|
||||
else {
|
||||
if ($this->title) {
|
||||
\Civi::log()->error("Twingle Extension could not create new custom group
|
||||
for \"$this->title\": $this->results['error_message']"
|
||||
);
|
||||
}
|
||||
else {
|
||||
\Civi::log()->error("Twingle Extension could not create new
|
||||
custom group: $this->results['error_message']");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getSetAttributes() {
|
||||
$setAttributes = [];
|
||||
foreach ($this as $var => $value) {
|
||||
if (isset($value)) {
|
||||
$setAttributes[$var] = $value;
|
||||
}
|
||||
}
|
||||
return $setAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $values
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function alter($values) {
|
||||
|
||||
foreach ($values as $var => $value) {
|
||||
if ($this->$var) {
|
||||
$this->$var = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$this->results = civicrm_api3('CustomGroup', 'create', $this->getSetAttributes());
|
||||
|
||||
return $this->results['is_error'] == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return BAO\CustomGroup
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public static function fetch($name) {
|
||||
|
||||
$custom_group = civicrm_api3(
|
||||
'CustomGroup',
|
||||
'get',
|
||||
[
|
||||
'sequential' => 1,
|
||||
'name' => $name
|
||||
]
|
||||
);
|
||||
if ($custom_group = array_shift($custom_group['values'])) {
|
||||
return new CustomGroup($custom_group);
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
public function delete() {
|
||||
$this->results = civicrm_api3(
|
||||
'CustomGroup',
|
||||
'delete',
|
||||
['id' => $this->id]
|
||||
);
|
||||
|
||||
if ($this->results['is_error'] == 0) {
|
||||
\Civi::log()->info("Twingle Extension has deleted custom group.\n
|
||||
title: $this->title\n
|
||||
name: $this->name\n
|
||||
extends: $this->extends\n
|
||||
id : $this->id"
|
||||
);
|
||||
|
||||
foreach ($this->getSetAttributes() as $var => $attribute) {
|
||||
$this->$var = NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($this->title) {
|
||||
\Civi::log()->error("Twingle Extension could not delete custom group
|
||||
\"$this->title\": $this->results['error_message']"
|
||||
);
|
||||
}
|
||||
else {
|
||||
\Civi::log()->error("Twingle Extension could not delete custom group:
|
||||
$this->results['error_message']");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setId($id) {
|
||||
return $this->alter(['id', $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setTitle(string $title) {
|
||||
return $this->alter(['title', $title]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setName(string $name) {
|
||||
return $this->alter(['name', $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $extends
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setExtends(string $extends) {
|
||||
return $this->alter(['extends', $extends]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $weight
|
||||
*/
|
||||
public function setWeight($weight) {
|
||||
$this->weight = $weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $column_value
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function setColumnValue(string $column_value) {
|
||||
return $this->alter(['column_value', $column_value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle(): string {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getExtends(): string {
|
||||
return $this->extends;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getWeight() {
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getColumnValue(): string {
|
||||
return $this->column_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getResult() {
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
|
||||
}
|
9
CRM/TwingleCampaign/BAO/TwingleCampaign.php
Normal file
9
CRM/TwingleCampaign/BAO/TwingleCampaign.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace CRM\TwingleCampaign\BAO;
|
||||
|
||||
|
||||
class TwingleCampaign {
|
||||
|
||||
}
|
9
CRM/TwingleCampaign/BAO/TwingleEvent.php
Normal file
9
CRM/TwingleCampaign/BAO/TwingleEvent.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace CRM\TwingleCampaign\BAO;
|
||||
|
||||
|
||||
class TwingleEvent {
|
||||
|
||||
}
|
788
CRM/TwingleCampaign/BAO/TwingleProject.php
Normal file
788
CRM/TwingleCampaign/BAO/TwingleProject.php
Normal file
|
@ -0,0 +1,788 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace CRM\TwingleCampaign\BAO;
|
||||
|
||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||
use DateTime;
|
||||
use CRM\TwingleCampaign\BAO\CustomField as CustomField;
|
||||
|
||||
include_once E::path() . '/CRM/TwingleCampaign/BAO/CustomField.php';
|
||||
|
||||
|
||||
class TwingleProject {
|
||||
|
||||
public const IN = 'IN';
|
||||
|
||||
public const OUT = 'OUT';
|
||||
|
||||
public const CIVICRM = 'CIVICRM';
|
||||
|
||||
public const TWINGLE = 'TWINGLE';
|
||||
|
||||
private static $bInitialized = FALSE;
|
||||
|
||||
private static $customFieldMapping;
|
||||
|
||||
private static $translations;
|
||||
|
||||
private static $campaigns;
|
||||
|
||||
private static $templates;
|
||||
|
||||
private $id;
|
||||
|
||||
private $values;
|
||||
|
||||
private $options;
|
||||
|
||||
|
||||
/**
|
||||
* TwingleProject constructor.
|
||||
*
|
||||
* @param array $project
|
||||
* Result array of Twingle API call to
|
||||
* https://project.twingle.de/api/by-organisation/$organisation_id
|
||||
*
|
||||
* @param array $options
|
||||
* Result array of Twingle Api call to
|
||||
* https://project.twingle.de/api/$project_id/options
|
||||
*
|
||||
* @param string $origin
|
||||
* Origin of the arrays. It can be one of two constants:
|
||||
* TwingleProject::TWINGLE|CIVICRM
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(array $project, array $options, string $origin) {
|
||||
|
||||
// Fetch custom field mapping once
|
||||
self::init();
|
||||
|
||||
// If values come from CiviCRM Campaign API
|
||||
if ($origin == self::CIVICRM) {
|
||||
|
||||
// Set id (campaign id) attribute
|
||||
$this->id = $project['id'];
|
||||
|
||||
// Translate custom field names into Twingle field names
|
||||
self::translateCustomFields($project, self::$OUT);
|
||||
|
||||
}
|
||||
// If values come from Twingle API
|
||||
elseif ($origin == self::TWINGLE) {
|
||||
|
||||
// Translate keys for import
|
||||
self::translateKeys($project, self::IN);
|
||||
|
||||
// Format values for import
|
||||
self::formatValues($project, self::IN);
|
||||
self::formatValues($options, self::IN);
|
||||
|
||||
}
|
||||
|
||||
// Add value for campaign type
|
||||
$project['campaign_type_id'] = 'twingle_project';
|
||||
|
||||
// Import project values and options
|
||||
$this->values = $project;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get custom field mapping.
|
||||
* This function will be fully executed only once, when the TwingleProject
|
||||
* class gets instantiated for the first time.
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function init() {
|
||||
|
||||
// Initialize custom field mapping
|
||||
if (self::$bInitialized) {
|
||||
return;
|
||||
}
|
||||
self::$customFieldMapping = CustomField::getMapping();
|
||||
|
||||
// Initialize json files as arrays
|
||||
$file_paths = [
|
||||
'translations' => '/CRM/TwingleCampaign/resources/dictionary.json',
|
||||
'templates' => '/CRM/TwingleCampaign/resources/twingle_api_templates.json',
|
||||
'campaigns' => '/CRM/TwingleCampaign/resources/campaigns.json',
|
||||
];
|
||||
|
||||
foreach ($file_paths as $key => $file_path) {
|
||||
|
||||
// Get array from json file
|
||||
$file_path = E::path() . $file_path;
|
||||
$json_file = file_get_contents($file_path);
|
||||
$json_file_name = pathinfo($file_path)['filename'];
|
||||
$array = json_decode($json_file, TRUE);
|
||||
|
||||
// Throw and log an error if json file can't be read
|
||||
if (!$array) {
|
||||
$message = ($json_file_name)
|
||||
? "Could not read json file $json_file_name"
|
||||
: "Could not locate json file in path: $file_path";
|
||||
\Civi::log()->error($message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
|
||||
// Set attribute
|
||||
self::$$key = $array;
|
||||
}
|
||||
|
||||
self::$bInitialized = TRUE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create the TwingleProject as a campaign in CiviCRM if it does not exist
|
||||
*
|
||||
* @param bool $is_test
|
||||
* If true: don't do any changes
|
||||
*
|
||||
* @return array
|
||||
* Returns a response array that contains title, id, project_id and status
|
||||
*
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
public function create(bool $is_test = FALSE) {
|
||||
|
||||
// Create campaign only if it does not already exist
|
||||
if (!$is_test) {
|
||||
|
||||
// Translate Twingle field names into custom field names
|
||||
$translatedFields = array_merge($this->options, $this->values);
|
||||
self::translateCustomFields($translatedFields, self::IN);
|
||||
|
||||
// Set id
|
||||
$translatedFields['id'] = $this->id;
|
||||
|
||||
// Create campaign
|
||||
$result = civicrm_api3('Campaign', 'create', $translatedFields);
|
||||
|
||||
// Update id
|
||||
$this->id = $result['id'];
|
||||
|
||||
// Check if campaign was created successfully
|
||||
if ($result['is_error'] == 0) {
|
||||
$response = $this->getResponse('TwingleProject created');
|
||||
}
|
||||
else {
|
||||
$response = $this->getResponse('TwingleProject creation failed');
|
||||
}
|
||||
|
||||
}
|
||||
// If this is a test, do not create campaign
|
||||
else {
|
||||
$response = $this->getResponse('TwingleProject not yet created');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update an existing project
|
||||
*
|
||||
* @param array $values
|
||||
* Array with values to update
|
||||
*
|
||||
* @param array $options
|
||||
* Array with options to update
|
||||
*
|
||||
* @param string $origin
|
||||
* Origin of the array. It can be one of two constants:
|
||||
* TwingleProject::TWINGLE|CIVICRM
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function update(array $values, array $options, string $origin = NULL) {
|
||||
|
||||
if ($origin == self::TWINGLE) {
|
||||
// Format values and translate keys
|
||||
self::translateKeys($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) {
|
||||
$this->id = $values['id'];
|
||||
self::translateCustomFields($values, self::OUT);
|
||||
}
|
||||
|
||||
// Update values and options
|
||||
$this->values = array_merge($this->values, $values);
|
||||
$this->options = array_merge($this->options, $options);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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);
|
||||
self::translateKeys($values, self::OUT);
|
||||
|
||||
// Get template for project
|
||||
$project = self::$templates['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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export options. Ensures that only those values will be exported which the
|
||||
* Twingle API expects. Missing values will get complemented with default
|
||||
* values.
|
||||
*
|
||||
* @return array
|
||||
* Array with all options to send to the Twingle API
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
*/
|
||||
public function exportOptions() {
|
||||
|
||||
$options = $this->options;
|
||||
self::formatValues($options, self::OUT);
|
||||
self::translateKeys($options, self::OUT);
|
||||
|
||||
// Get Template for project options
|
||||
$project_options_template = self::$templates['project_options'];
|
||||
|
||||
// Replace array items which the Twingle API does not expect
|
||||
foreach ($options as $key => $value) {
|
||||
if (!key_exists($key, $project_options_template)) {
|
||||
unset($options[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Complement missing options with default values
|
||||
return array_merge($project_options_template, $options);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a project already exists
|
||||
*
|
||||
* @return bool
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function exists() {
|
||||
|
||||
$result = [];
|
||||
|
||||
// Get custom field name for project_id
|
||||
$cf_project_id = TwingleProject::$customFieldMapping['twingle_project_id'];
|
||||
|
||||
// If there is more than one campaign for a project, handle the duplicates
|
||||
while (!$single) {
|
||||
$result = civicrm_api3('Campaign', 'get', [
|
||||
'sequential' => 1,
|
||||
'is_active' => 1,
|
||||
$cf_project_id => $this->values['id'],
|
||||
]);
|
||||
|
||||
if ($result['count'] > 1) {
|
||||
TwingleProject::handleDuplicates($result);
|
||||
}
|
||||
else {
|
||||
$single = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
// If the campaign for the TwingleProject already exists, some of the
|
||||
// project's attributes must be updated from the campaign
|
||||
if ($result['count'] == 1) {
|
||||
|
||||
// Set campaign id attribute
|
||||
$this->id = $result['values'][0]['id'];
|
||||
|
||||
// Translate custom field names back
|
||||
self::translateCustomFields($result['values'][0], self::OUT);
|
||||
|
||||
// Translate keys from CiviCRM format to Twingle format
|
||||
self::translateKeys($result['values'][0], self::OUT);
|
||||
|
||||
// Split result array into project values and options
|
||||
$values_and_options = self::splitValues($result['values'][0]);
|
||||
|
||||
// Translate keys from Twingle format to CiviCRM format
|
||||
self::translateKeys($values_and_options['values'], self::IN);
|
||||
self::translateKeys($values_and_options['options'], self::IN);
|
||||
|
||||
// Set attributes to the values of the existing TwingleProject campaign
|
||||
// to reflect the state of the actual campaign in the database
|
||||
$this->update(
|
||||
$values_and_options['values'],
|
||||
$values_and_options['options']
|
||||
);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Instantiate an existing project by campaign id
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return \CRM\TwingleCampaign\BAO\TwingleProject
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function fetch($id) {
|
||||
$result = civicrm_api3('Campaign', 'getsingle', [
|
||||
'sequential' => 1,
|
||||
'id' => $id,
|
||||
]);
|
||||
|
||||
return new TwingleProject($result, TRUE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deactivate all duplicates of a project but the newest one
|
||||
*
|
||||
* @param array $result
|
||||
* The $result array of a civicrm_api3-get-project call
|
||||
*
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
private function handleDuplicates(array $result) {
|
||||
|
||||
// Sort projects ascending by the value of the last_modified_date
|
||||
uasort($result['values'], function ($a, $b) {
|
||||
return $a['last_modified_date'] <=> $b['last_modified_date'];
|
||||
});
|
||||
|
||||
// Delete the newest project from array to keep it active
|
||||
array_shift($result['values']);
|
||||
|
||||
// deactivate the projects
|
||||
foreach ($result['values'] as $p) {
|
||||
self::deactivateById($p['id']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Translate array keys between CiviCRM Campaigns and Twingle
|
||||
*
|
||||
* @param array $values
|
||||
* array of which keys shall be translated
|
||||
*
|
||||
* @param string $direction
|
||||
* TwingleProject::IN -> translate array keys from Twingle format into
|
||||
* CiviCRM format <br>
|
||||
* TwingleProject::OUT -> translate array keys from CiviCRM format into
|
||||
* Twingle format
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function translateKeys(array &$values, string $direction) {
|
||||
|
||||
// Get translations for fields
|
||||
$field_translations = self::$translations['fields'];
|
||||
|
||||
// Set the direction of the translation
|
||||
if ($direction == self::OUT) {
|
||||
$field_translations = array_flip($field_translations);
|
||||
}
|
||||
// Throw error if $direction constant does not match IN or OUT
|
||||
elseif ($direction != self::IN) {
|
||||
throw new \Exception(
|
||||
"Invalid Parameter $direction for translateKeys()"
|
||||
);
|
||||
// TODO: use specific exception or create own
|
||||
}
|
||||
|
||||
// Translate keys
|
||||
foreach ($field_translations as $origin => $translation) {
|
||||
$values[$translation] = $values[$origin];
|
||||
unset($values[$origin]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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_modified_date']) {
|
||||
$values['last_modified_date'] =
|
||||
self::getDateTime($values['last_modified_date']);
|
||||
}
|
||||
|
||||
// empty project_type to 'default
|
||||
if ($values['type']) {
|
||||
$values['type'] = $values['type'] == ''
|
||||
? 'default'
|
||||
: $values['type'];
|
||||
}
|
||||
|
||||
// format donation rhythm
|
||||
if (is_array($values['donation_rhythm'])) {
|
||||
$tmp = [];
|
||||
foreach ($values['donation_rhythm'] as $key => $value) {
|
||||
if ($value) {
|
||||
$tmp[$key] = $key;
|
||||
}
|
||||
}
|
||||
$values['donation_rhythm'] = \CRM_Utils_Array::implodePadded($tmp);
|
||||
}
|
||||
|
||||
// Format project target format
|
||||
if (key_exists('has_projecttarget_as_money', $values)) {
|
||||
$values['has_projecttarget_as_money'] =
|
||||
$values['has_projecttarget_as_money'] ? 'in Euro' : 'percentage';
|
||||
}
|
||||
|
||||
// Format contact fields
|
||||
if ($values['exclude_contact_fields']) {
|
||||
$possible_contact_fields =
|
||||
self::$campaigns['custom_fields']
|
||||
['twingle_project_exclude_contact_fields']['option_values'];
|
||||
|
||||
$exclude_contact_fields = explode(',', $values['exclude_contact_fields']);
|
||||
|
||||
foreach ($exclude_contact_fields as $exclude_contact_field) {
|
||||
unset($possible_contact_fields[$exclude_contact_field]);
|
||||
}
|
||||
|
||||
$values['exclude_contact_fields'] =
|
||||
\CRM_Utils_Array::implodePadded($possible_contact_fields);
|
||||
}
|
||||
|
||||
// Format languages
|
||||
if ($values['languages']) {
|
||||
$values['languages'] =
|
||||
\CRM_Utils_Array::implodePadded(
|
||||
explode(
|
||||
',',
|
||||
$values['languages']
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
elseif ($direction == self::OUT) {
|
||||
|
||||
// Change DateTime string into timestamp
|
||||
$values['last_modified_date'] =
|
||||
self::getTimestamp($values['last_modified_date']);
|
||||
|
||||
// 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()"
|
||||
);
|
||||
// TODO: use specific exception or create own
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Translate between Twingle field names and custom field names
|
||||
*
|
||||
* @param array $values
|
||||
* array of which keys shall be translated
|
||||
*
|
||||
* @param string $direction
|
||||
* TwingleProject::IN -> translate field names into custom field names <br>
|
||||
* TwingleProject::OUT -> translate custom field names into Twingle field
|
||||
* names
|
||||
*
|
||||
*/
|
||||
private static function translateCustomFields(array &$values, string $direction) {
|
||||
|
||||
// Translate from Twingle field name to custom field name
|
||||
if ($direction == self::IN) {
|
||||
foreach (TwingleProject::$customFieldMapping as $field => $custom) {
|
||||
if (array_key_exists(
|
||||
str_replace(
|
||||
'twingle_project_',
|
||||
'',
|
||||
$field
|
||||
),
|
||||
$values)
|
||||
) {
|
||||
$values[$custom] = $values[str_replace(
|
||||
'twingle_project_',
|
||||
'',
|
||||
$field
|
||||
)];
|
||||
unset($values[str_replace(
|
||||
'twingle_project_',
|
||||
'',
|
||||
$field
|
||||
)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Translate from custom field name to Twingle field name
|
||||
elseif ($direction == self::OUT) {
|
||||
foreach (TwingleProject::$customFieldMapping as $field => $custom) {
|
||||
if (array_key_exists(
|
||||
$custom,
|
||||
$values
|
||||
)
|
||||
) {
|
||||
$values[str_replace(
|
||||
'twingle_project_',
|
||||
'',
|
||||
$field
|
||||
)] = $values[$custom];
|
||||
unset($values[$custom]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if deactivation was successful
|
||||
*
|
||||
* @throws \CiviCRM_API3_Exception
|
||||
*/
|
||||
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', [
|
||||
'title' => $result['title'],
|
||||
'id' => $id,
|
||||
'is_active' => '0',
|
||||
]);
|
||||
|
||||
// Return TRUE if TwingleProject campaign was deactivated successfully
|
||||
if ($result['is_error'] == 0) {
|
||||
return TRUE;
|
||||
}
|
||||
// Return FALSE if deactivation failed
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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['title'],
|
||||
'id' => $this->id,
|
||||
'project_id' => $this->values['id'],
|
||||
'status' => $status,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates $input to be either a DateTime string or an Unix timestamp
|
||||
*
|
||||
* @param $input
|
||||
* Pass a DateTime string or a Unix timestamp
|
||||
*
|
||||
* @return int
|
||||
* Returns a Unix timestamp or NULL if $input is invalid
|
||||
*/
|
||||
public static function getTimestamp($input) {
|
||||
|
||||
// Check whether $input is a Unix timestamp
|
||||
if (
|
||||
$dateTime = DateTime::createFromFormat('U', $input)
|
||||
) {
|
||||
return $input;
|
||||
}
|
||||
// ... or a DateTime string
|
||||
elseif (
|
||||
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $input)
|
||||
) {
|
||||
return $dateTime->getTimestamp();
|
||||
}
|
||||
// ... or invalid
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates $input to be either a DateTime string or an Unix timestamp
|
||||
*
|
||||
* @param $input
|
||||
* Pass a DateTime string or a Unix timestamp
|
||||
*
|
||||
* @return string
|
||||
* Returns a DateTime string or NULL if $input is invalid
|
||||
*/
|
||||
public static function getDateTime($input) {
|
||||
|
||||
// Check whether $input is a Unix timestamp
|
||||
if (
|
||||
$dateTime = DateTime::createFromFormat('U', $input)
|
||||
) {
|
||||
return $dateTime->format('Y-m-d H:i:s');
|
||||
}
|
||||
// ... or a DateTime string
|
||||
elseif (
|
||||
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $input)
|
||||
) {
|
||||
return $input;
|
||||
}
|
||||
// ... or invalid
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return a timestamp of the last update of the TwingleProject
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function lastUpdate() {
|
||||
return self::getTimestamp($this->values['last_modified_date']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the project_id of a TwingleProject
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getProjectId() {
|
||||
return $this->values['id'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue