From c0cc3f94e9ce16a97666fa4ce6bdd91ef8dc89b9 Mon Sep 17 00:00:00 2001 From: Herb v/d Dool Date: Wed, 11 Jan 2023 15:09:35 -0500 Subject: [PATCH] dev/core#4028 make CRM_Admin_Form_Setting_UF more CMS agnostic --- CRM/Admin/Form/Setting/UF.php | 49 +++---------------------- CRM/Utils/System/Backdrop.php | 41 +++++++++++++++++++++ CRM/Utils/System/Base.php | 30 +++++++++++++++ CRM/Utils/System/Drupal.php | 40 ++++++++++++++++++++ CRM/Utils/System/Drupal8.php | 7 ++++ CRM/Utils/System/DrupalBase.php | 7 ++++ CRM/Utils/System/WordPress.php | 7 ++++ templates/CRM/Admin/Form/Setting/UF.tpl | 5 +-- 8 files changed, 140 insertions(+), 46 deletions(-) diff --git a/CRM/Admin/Form/Setting/UF.php b/CRM/Admin/Form/Setting/UF.php index f6da3da6de..041a3f5ff9 100644 --- a/CRM/Admin/Form/Setting/UF.php +++ b/CRM/Admin/Form/Setting/UF.php @@ -34,62 +34,25 @@ class CRM_Admin_Form_Setting_UF extends CRM_Admin_Form_Setting { $this->assign('wpBasePageEnabled', FALSE); $this->assign('userFrameworkUsersTableNameEnabled', FALSE); + $this->assign('viewsIntegration', FALSE); $this->setTitle( ts('Settings - %1 Integration', [1 => $this->_uf]) ); - if ($this->_uf === 'WordPress') { + if ($config->userSystem->canSetBasePage()) { $this->_settings['wpBasePage'] = CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME; $this->assign('wpBasePageEnabled', TRUE); } - if ($config->userSystem->is_drupal) { + if ($config->userSystem->hasUsersTable()) { $this->_settings['userFrameworkUsersTableName'] = CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME; $this->assign('userFrameworkUsersTableNameEnabled', TRUE); } - // find out if drupal has its database prefixed - if ($this->_uf === 'Drupal8') { - $databases['default'] = Drupal\Core\Database\Database::getConnectionInfo('default'); - } - else { - global $databases; - } - - $drupal_prefix = ''; - if (isset($databases['default']['default']['prefix'])) { - if (is_array($databases['default']['default']['prefix'])) { - $drupal_prefix = $databases['default']['default']['prefix']['default']; - } - else { - $drupal_prefix = $databases['default']['default']['prefix']; - } - } - - $this->assign('tablePrefixes', FALSE); - - if ($config->userSystem->viewsExists() && - ( - $config->dsn != $config->userFrameworkDSN || !empty($drupal_prefix) - ) - ) { - - $dsnArray = DB::parseDSN(CRM_Utils_SQL::autoSwitchDSN($config->dsn)); - $tableNames = CRM_Core_DAO::getTableNames(); - asort($tableNames); - $tablePrefixes = '$databases[\'default\'][\'default\'][\'prefix\']= ['; - if ($config->userFramework === 'Backdrop') { - $tablePrefixes = '$database_prefix = ['; - } - // add default prefix: the drupal database prefix - $tablePrefixes .= "\n 'default' => '$drupal_prefix',"; - $prefix = $config->userSystem->getCRMDatabasePrefix(); - foreach ($tableNames as $tableName) { - $tablePrefixes .= "\n '" . str_pad($tableName . "'", 41) . " => '{$prefix}',"; - } - $tablePrefixes .= "\n];"; - $this->assign('tablePrefixes', $tablePrefixes); + $viewsIntegration = $config->userSystem->viewsIntegration(); + if ($viewsIntegration) { + $this->assign('viewsIntegration', $viewsIntegration); } parent::buildQuickForm(); diff --git a/CRM/Utils/System/Backdrop.php b/CRM/Utils/System/Backdrop.php index 014144bc0f..8940e5da20 100644 --- a/CRM/Utils/System/Backdrop.php +++ b/CRM/Utils/System/Backdrop.php @@ -1116,4 +1116,45 @@ AND u.status = 1 ]; } + /** + * @inheritdoc + */ + public function viewsIntegration(): string { + global $databases; + $config = CRM_Core_Config::singleton(); + $text = ''; + $backdrop_prefix = ''; + if (isset($databases['default']['default']['prefix'])) { + if (is_array($databases['default']['default']['prefix'])) { + $backdrop_prefix = $databases['default']['default']['prefix']['default']; + } + else { + $backdrop_prefix = $databases['default']['default']['prefix']; + } + } + + if ($this->viewsExists() && + ( + $config->dsn != $config->userFrameworkDSN || !empty($backdrop_prefix) + ) + ) { + $text = '
' . ts('To enable CiviCRM Views integration, add or update the following item in the settings.php file:') . '
'; + + $tableNames = CRM_Core_DAO::getTableNames(); + asort($tableNames); + + $text .= '
$database_prefix = [';
+
+      // Add default prefix.
+      $text .= "\n  'default' => '$backdrop_prefix',";
+      $prefix = $this->getCRMDatabasePrefix();
+      foreach ($tableNames as $tableName) {
+        $text .= "\n  '" . str_pad($tableName . "'", 41) . " => '{$prefix}',";
+      }
+      $text .= "\n];
"; + } + + return $text; + } + } diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index 78e5f64601..897942307c 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1159,4 +1159,34 @@ abstract class CRM_Utils_System_Base { public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') { } + /** + * Has CMS users table + * + * @return bool + */ + public function hasUsersTable():bool { + return FALSE; + } + + /** + * CiviCRM Table prefixes + * To display for CMS integration. + * + * @return string + */ + public function viewsIntegration():string { + return ''; + } + + /** + * Can set base page for CiviCRM + * By default, CiviCRM will generate front-facing pages + * using the home page. This allows a different template + * for CiviCRM pages. + * @return bool + */ + public function canSetBasePage():bool { + return FALSE; + } + } diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php index ee2c36eaad..d1a3890d77 100644 --- a/CRM/Utils/System/Drupal.php +++ b/CRM/Utils/System/Drupal.php @@ -852,4 +852,44 @@ AND u.status = 1 } } + /** + * @inheritdoc + */ + public function viewsIntegration(): string { + global $databases; + $config = CRM_Core_Config::singleton(); + $text = ''; + $drupal_prefix = ''; + if (isset($databases['default']['default']['prefix'])) { + if (is_array($databases['default']['default']['prefix'])) { + $drupal_prefix = $databases['default']['default']['prefix']['default']; + } + else { + $drupal_prefix = $databases['default']['default']['prefix']; + } + } + + if ($this->viewsExists() && + ( + $config->dsn != $config->userFrameworkDSN || !empty($drupal_prefix) + ) + ) { + $text = '
' . ts('To enable CiviCRM Views integration, add or update the following item in the settings.php file:') . '
'; + + $tableNames = CRM_Core_DAO::getTableNames(); + asort($tableNames); + $text .= '
$databases[\'default\'][\'default\'][\'prefix\']= [';
+
+      // Add default prefix.
+      $text .= "\n  'default' => '$drupal_prefix',";
+      $prefix = $this->getCRMDatabasePrefix();
+      foreach ($tableNames as $tableName) {
+        $text .= "\n  '" . str_pad($tableName . "'", 41) . " => '{$prefix}',";
+      }
+      $text .= "\n];
"; + } + + return $text; + } + } diff --git a/CRM/Utils/System/Drupal8.php b/CRM/Utils/System/Drupal8.php index c65589d53a..6e6dd1eded 100644 --- a/CRM/Utils/System/Drupal8.php +++ b/CRM/Utils/System/Drupal8.php @@ -908,4 +908,11 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { return FALSE; } + /** + * @inheritdoc + */ + public function viewsIntegration(): string { + return '

' . ts('To enable CiviCRM Views integration, install the CiviCRM Entity module.', [1 => 'href="https://www.drupal.org/project/civicrm_entity"']) . '

'; + } + } diff --git a/CRM/Utils/System/DrupalBase.php b/CRM/Utils/System/DrupalBase.php index d3360c2de0..e65c03d200 100644 --- a/CRM/Utils/System/DrupalBase.php +++ b/CRM/Utils/System/DrupalBase.php @@ -528,6 +528,13 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base { return $this->url($_GET['q']); } + /** + * @inheritdoc + */ + public function hasUsersTable():bool { + return TRUE; + } + /** * Get an array of user details for a contact, containing at minimum the user ID & name. * diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 2856631fc8..df7fcaad2e 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -1624,4 +1624,11 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { } + /** + * @inheritdoc + */ + public function canSetBasePage():bool { + return TRUE; + } + } diff --git a/templates/CRM/Admin/Form/Setting/UF.tpl b/templates/CRM/Admin/Form/Setting/UF.tpl index be63b58336..9938e4d90c 100644 --- a/templates/CRM/Admin/Form/Setting/UF.tpl +++ b/templates/CRM/Admin/Form/Setting/UF.tpl @@ -34,12 +34,11 @@
{include file="CRM/common/formButtons.tpl" location="bottom"}
-{if $tablePrefixes} +{if $viewsIntegration}
{ts}Views integration settings{/ts} -
{ts}To enable CiviCRM Views integration, add or update the following item in the settings.php file:{/ts}
-
{$tablePrefixes}
+
{$viewsIntegration}
{/if} -- 2.25.1