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,22 @@
<?php
use CRM_Twingle_ExtensionUtil as E;
/**
* Form controller class
*
* @see https://docs.civicrm.org/dev/en/latest/framework/quickform/
*/
class CRM_TwingleCampaign_Form_Settings extends CRM_Admin_Form_Setting {
protected $_settings = [
'twingle_api_key' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'twingle_request_size' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
];
public function buildQuickForm() {
parent::buildQuickForm();
$this->assign('elementNames', array_keys($this->_settings));
}
}

View file

@ -0,0 +1,195 @@
<?php
use CRM_TwingleCampaign_ExtensionUtil as E;
use TwingleCampaign\Models as Models;
include E::path() . '/CRM/TwingleCampaign/Upgrader/models/CampaignType.php';
include E::path() . '/CRM/TwingleCampaign/Upgrader/models/CustomField.php';
include E::path() . '/CRM/TwingleCampaign/Upgrader/models/CustomGroup.php';
/**
* Collection of upgrade steps.
*/
class CRM_TwingleCampaign_Upgrader extends CRM_TwingleCampaign_Upgrader_Base {
// By convention, functions that look like "function upgrade_NNNN()" are
// upgrade tasks. They are executed in order (like Drupal's hook_update_N).
/**
* Example: Run an external SQL script when the module is installed.
*
* @throws \Exception
*/
public function install() {
}
/**
* Example: Run an external SQL script when the module is uninstalled.
*
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public function uninstall() {
}
/**
* Example: Run a simple query when a module is enabled.
*
* @throws \Exception
*/
public function enable() {
// Create campaign types, custom fields and custom groups by the contents
// of the json file "campaigns.json"
$json_file = file_get_contents(E::path() . '/CRM/TwingleCampaign/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');
}
// Create campaign types
foreach ($campaign_info['campaign_types'] as $campaign_type) {
new Models\CampaignType($campaign_type);
}
foreach (Models\CampaignType::getCampaignTypes() as $campaign_type) {
$campaign_type->create();
}
// Create custom groups
foreach ($campaign_info['custom_groups'] as $custom_group) {
foreach (Models\CampaignType::getCampaignTypes() as $campaign_type) {
if ($campaign_type->getName() == $custom_group['campaign_type']) {
$custom_group['extends_entity_column_value'] = $campaign_type->getValue();
}
}
new Models\CustomGroup($custom_group);
}
foreach (Models\CustomGroup::getCustomGroups() as $custom_group) {
$custom_group->create();
}
// Create custom fields
foreach ($campaign_info['custom_fields'] as $custom_field) {
new Models\CustomField($custom_field);
}
foreach (Models\CustomField::getCustomFields() as $custom_field) {
$custom_field->create();
}
}
/**
* Example: Run a simple query when a module is disabled.
*
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public function disable() {
$json_file = file_get_contents(E::path() . '/CRM/TwingleCampaign/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');
}
// Delete campaign types
foreach ($campaign_info['campaign_types'] as $campaign_type) {
$result = Models\CampaignType::fetch($campaign_type['name']);
if ($result) {
$result->delete();
}
}
// Delete custom groups
foreach ($campaign_info['custom_groups'] as $custom_group) {
$result = Models\CustomGroup::fetch($custom_group['name']);
if ($result) {
$result->delete();
}
}
}
/**
* Example: Run a couple simple queries.
*
* @return TRUE on success
* @throws Exception
*
* public function upgrade_4200() {
* $this->ctx->log->info('Applying update 4200');
* CRM_Core_DAO::executeQuery('UPDATE foo SET bar = "whiz"');
* CRM_Core_DAO::executeQuery('DELETE FROM bang WHERE willy = wonka(2)');
* return TRUE;
* } // */
/**
* Example: Run an external SQL script.
*
* @return TRUE on success
* @throws Exception
* public function upgrade_4201() {
* $this->ctx->log->info('Applying update 4201');
* // this path is relative to the extension base dir
* $this->executeSqlFile('sql/upgrade_4201.sql');
* return TRUE;
* } // */
/**
* Example: Run a slow upgrade process by breaking it up into smaller chunk.
*
* @return TRUE on success
* @throws Exception
* public function upgrade_4202() {
* $this->ctx->log->info('Planning update 4202'); // PEAR Log interface
*
* $this->addTask(E::ts('Process first step'), 'processPart1', $arg1, $arg2);
* $this->addTask(E::ts('Process second step'), 'processPart2', $arg3, $arg4);
* $this->addTask(E::ts('Process second step'), 'processPart3', $arg5);
* return TRUE;
* }
* public function processPart1($arg1, $arg2) { sleep(10); return TRUE; }
* public function processPart2($arg3, $arg4) { sleep(10); return TRUE; }
* public function processPart3($arg5) { sleep(10); return TRUE; }
* // */
/**
* Example: Run an upgrade with a query that touches many (potentially
* millions) of records by breaking it up into smaller chunks.
*
* @return TRUE on success
* @throws Exception
* public function upgrade_4203() {
* $this->ctx->log->info('Planning update 4203'); // PEAR Log interface
*
* $minId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM
* civicrm_contribution');
* $maxId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM
* civicrm_contribution'); for ($startId = $minId; $startId <= $maxId;
* $startId += self::BATCH_SIZE) {
* $endId = $startId + self::BATCH_SIZE - 1;
* $title = E::ts('Upgrade Batch (%1 => %2)', array(
* 1 => $startId,
* 2 => $endId,
* ));
* $sql = '
* UPDATE civicrm_contribution SET foobar = whiz(wonky()+wanker)
* WHERE id BETWEEN %1 and %2
* ';
* $params = array(
* 1 => array($startId, 'Integer'),
* 2 => array($endId, 'Integer'),
* );
* $this->addTask($title, 'executeSql', $sql, $params);
* }
* return TRUE;
* } // */
}

View file

@ -0,0 +1,376 @@
<?php
// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
use CRM_TwingleCampaign_ExtensionUtil as E;
/**
* Base class which provides helpers to execute upgrade logic
*/
class CRM_TwingleCampaign_Upgrader_Base {
/**
* @var varies, subclass of this
*/
static $instance;
/**
* @var CRM_Queue_TaskContext
*/
protected $ctx;
/**
* @var string, eg 'com.example.myextension'
*/
protected $extensionName;
/**
* @var string, full path to the extension's source tree
*/
protected $extensionDir;
/**
* @var array(revisionNumber) sorted numerically
*/
private $revisions;
/**
* @var boolean
* Flag to clean up extension revision data in civicrm_setting
*/
private $revisionStorageIsDeprecated = FALSE;
/**
* Obtain a reference to the active upgrade handler.
*/
static public function instance() {
if (!self::$instance) {
// FIXME auto-generate
self::$instance = new CRM_TwingleCampaign_Upgrader(
'de.forumzfd.twinglecampaign',
realpath(__DIR__ . '/../../../')
);
}
return self::$instance;
}
/**
* Adapter that lets you add normal (non-static) member functions to the queue.
*
* Note: Each upgrader instance should only be associated with one
* task-context; otherwise, this will be non-reentrant.
*
* @code
* CRM_TwingleCampaign_Upgrader_Base::_queueAdapter($ctx, 'methodName', 'arg1', 'arg2');
* @endcode
*/
static public function _queueAdapter() {
$instance = self::instance();
$args = func_get_args();
$instance->ctx = array_shift($args);
$instance->queue = $instance->ctx->queue;
$method = array_shift($args);
return call_user_func_array(array($instance, $method), $args);
}
public function __construct($extensionName, $extensionDir) {
$this->extensionName = $extensionName;
$this->extensionDir = $extensionDir;
}
// ******** Task helpers ********
/**
* Run a CustomData file.
*
* @param string $relativePath the CustomData XML file path (relative to this extension's dir)
* @return bool
*/
public function executeCustomDataFile($relativePath) {
$xml_file = $this->extensionDir . '/' . $relativePath;
return $this->executeCustomDataFileByAbsPath($xml_file);
}
/**
* Run a CustomData file
*
* @param string $xml_file the CustomData XML file path (absolute path)
*
* @return bool
*/
protected static function executeCustomDataFileByAbsPath($xml_file) {
$import = new CRM_Utils_Migrate_Import();
$import->run($xml_file);
return TRUE;
}
/**
* Run a SQL file.
*
* @param string $relativePath the SQL file path (relative to this extension's dir)
*
* @return bool
*/
public function executeSqlFile($relativePath) {
CRM_Utils_File::sourceSQLFile(
CIVICRM_DSN,
$this->extensionDir . DIRECTORY_SEPARATOR . $relativePath
);
return TRUE;
}
/**
* @param string $tplFile
* The SQL file path (relative to this extension's dir).
* Ex: "sql/mydata.mysql.tpl".
* @return bool
*/
public function executeSqlTemplate($tplFile) {
// Assign multilingual variable to Smarty.
$upgrade = new CRM_Upgrade_Form();
$tplFile = CRM_Utils_File::isAbsolute($tplFile) ? $tplFile : $this->extensionDir . DIRECTORY_SEPARATOR . $tplFile;
$smarty = CRM_Core_Smarty::singleton();
$smarty->assign('domainID', CRM_Core_Config::domainID());
CRM_Utils_File::sourceSQLFile(
CIVICRM_DSN, $smarty->fetch($tplFile), NULL, TRUE
);
return TRUE;
}
/**
* Run one SQL query.
*
* This is just a wrapper for CRM_Core_DAO::executeSql, but it
* provides syntatic sugar for queueing several tasks that
* run different queries
*/
public function executeSql($query, $params = array()) {
// FIXME verify that we raise an exception on error
CRM_Core_DAO::executeQuery($query, $params);
return TRUE;
}
/**
* Syntatic sugar for enqueuing a task which calls a function in this class.
*
* The task is weighted so that it is processed
* as part of the currently-pending revision.
*
* After passing the $funcName, you can also pass parameters that will go to
* the function. Note that all params must be serializable.
*/
public function addTask($title) {
$args = func_get_args();
$title = array_shift($args);
$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
$args,
$title
);
return $this->queue->createItem($task, array('weight' => -1));
}
// ******** Revision-tracking helpers ********
/**
* Determine if there are any pending revisions.
*
* @return bool
*/
public function hasPendingRevisions() {
$revisions = $this->getRevisions();
$currentRevision = $this->getCurrentRevision();
if (empty($revisions)) {
return FALSE;
}
if (empty($currentRevision)) {
return TRUE;
}
return ($currentRevision < max($revisions));
}
/**
* Add any pending revisions to the queue.
*/
public function enqueuePendingRevisions(CRM_Queue_Queue $queue) {
$this->queue = $queue;
$currentRevision = $this->getCurrentRevision();
foreach ($this->getRevisions() as $revision) {
if ($revision > $currentRevision) {
$title = ts('Upgrade %1 to revision %2', array(
1 => $this->extensionName,
2 => $revision,
));
// note: don't use addTask() because it sets weight=-1
$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
array('upgrade_' . $revision),
$title
);
$this->queue->createItem($task);
$task = new CRM_Queue_Task(
array(get_class($this), '_queueAdapter'),
array('setCurrentRevision', $revision),
$title
);
$this->queue->createItem($task);
}
}
}
/**
* Get a list of revisions.
*
* @return array(revisionNumbers) sorted numerically
*/
public function getRevisions() {
if (!is_array($this->revisions)) {
$this->revisions = array();
$clazz = new ReflectionClass(get_class($this));
$methods = $clazz->getMethods();
foreach ($methods as $method) {
if (preg_match('/^upgrade_(.*)/', $method->name, $matches)) {
$this->revisions[] = $matches[1];
}
}
sort($this->revisions, SORT_NUMERIC);
}
return $this->revisions;
}
public function getCurrentRevision() {
$revision = CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName);
if (!$revision) {
$revision = $this->getCurrentRevisionDeprecated();
}
return $revision;
}
private function getCurrentRevisionDeprecated() {
$key = $this->extensionName . ':version';
if ($revision = CRM_Core_BAO_Setting::getItem('Extension', $key)) {
$this->revisionStorageIsDeprecated = TRUE;
}
return $revision;
}
public function setCurrentRevision($revision) {
CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision);
// clean up legacy schema version store (CRM-19252)
$this->deleteDeprecatedRevision();
return TRUE;
}
private function deleteDeprecatedRevision() {
if ($this->revisionStorageIsDeprecated) {
$setting = new CRM_Core_BAO_Setting();
$setting->name = $this->extensionName . ':version';
$setting->delete();
CRM_Core_Error::debug_log_message("Migrated extension schema revision ID for {$this->extensionName} from civicrm_setting (deprecated) to civicrm_extension.\n");
}
}
// ******** Hook delegates ********
/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
public function onInstall() {
$files = glob($this->extensionDir . '/sql/*_install.sql');
if (is_array($files)) {
foreach ($files as $file) {
CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
}
}
$files = glob($this->extensionDir . '/sql/*_install.mysql.tpl');
if (is_array($files)) {
foreach ($files as $file) {
$this->executeSqlTemplate($file);
}
}
$files = glob($this->extensionDir . '/xml/*_install.xml');
if (is_array($files)) {
foreach ($files as $file) {
$this->executeCustomDataFileByAbsPath($file);
}
}
if (is_callable(array($this, 'install'))) {
$this->install();
}
}
/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
*/
public function onPostInstall() {
$revisions = $this->getRevisions();
if (!empty($revisions)) {
$this->setCurrentRevision(max($revisions));
}
if (is_callable(array($this, 'postInstall'))) {
$this->postInstall();
}
}
/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
*/
public function onUninstall() {
$files = glob($this->extensionDir . '/sql/*_uninstall.mysql.tpl');
if (is_array($files)) {
foreach ($files as $file) {
$this->executeSqlTemplate($file);
}
}
if (is_callable(array($this, 'uninstall'))) {
$this->uninstall();
}
$files = glob($this->extensionDir . '/sql/*_uninstall.sql');
if (is_array($files)) {
foreach ($files as $file) {
CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file);
}
}
}
/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
public function onEnable() {
// stub for possible future use
if (is_callable(array($this, 'enable'))) {
$this->enable();
}
}
/**
* @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
*/
public function onDisable() {
// stub for possible future use
if (is_callable(array($this, 'disable'))) {
$this->disable();
}
}
public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) {
switch ($op) {
case 'check':
return array($this->hasPendingRevisions());
case 'enqueue':
return $this->enqueuePendingRevisions($queue);
default:
}
}
}

View file

@ -0,0 +1,228 @@
<?php
namespace TwingleCampaign\Models;
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 \TwingleCampaign\Models\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;
}
}

View file

@ -0,0 +1,427 @@
<?php
namespace TwingleCampaign\Models;
use CRM_TwingleCampaign_ExtensionUtil as E;
class CustomField {
private static $customFields = [];
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);
}
}
array_push(self::$customFields, $this);
}
/**
* @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;
}
/**
* @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 \TwingleCampaign\Models\CustomField
* @throws \CiviCRM_API3_Exception
*/
public static function fetch($name) {
$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;
}
}
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']");
}
}
}
/**
* @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 array
*/
public static function getCustomFields(): array {
return self::$customFields;
}
/**
* @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;
}#
}

View file

@ -0,0 +1,270 @@
<?php
namespace TwingleCampaign\Models;
use CRM_Civirules_Utils_LoggerFactory as Civi;
class CustomGroup {
private static $customGroups = [];
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];
}
array_push(self::$customGroups, $this);
}
/**
* @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 Models\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 array
*/
public static function getCustomGroups() {
return self::$customGroups;
}
/**
* @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;
}
}

File diff suppressed because it is too large Load diff