From d0cebbbbaee382f7146290430e657cc63fb3a808 Mon Sep 17 00:00:00 2001
From: Tim Otten <totten@civicrm.org>
Date: Wed, 28 Jun 2023 01:41:01 -0700
Subject: [PATCH] InstallSettingsFile - Split out separate rules for Joomla
 settings files

---
 .../InstallJoomlaSettingsFile.civi-setup.php  | 53 +++++++++++++++++++
 .../InstallSettingsFile.civi-setup.php        |  5 ++
 2 files changed, 58 insertions(+)
 create mode 100644 setup/plugins/installFiles/InstallJoomlaSettingsFile.civi-setup.php

diff --git a/setup/plugins/installFiles/InstallJoomlaSettingsFile.civi-setup.php b/setup/plugins/installFiles/InstallJoomlaSettingsFile.civi-setup.php
new file mode 100644
index 0000000000..cbe3e42276
--- /dev/null
+++ b/setup/plugins/installFiles/InstallJoomlaSettingsFile.civi-setup.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * @file
+ *
+ * Generate the civicrm.settings.php file.
+ *
+ * The Joomla setup is unusual because it has two copies of the file, and they're
+ * slightly different.
+ */
+
+if (!defined('CIVI_SETUP')) {
+  exit("Installation plugins must only be loaded by the installer.\n");
+}
+
+/**
+ * Read the $model and create the "civicrm.settings.php" files for Joomla.
+ */
+\Civi\Setup::dispatcher()
+  ->addListener('civi.setup.installFiles', function (\Civi\Setup\Event\InstallFilesEvent $e) {
+    if ($e->getModel()->cms !== 'Joomla') {
+      return;
+    }
+
+    \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'installFiles'));
+
+    $liveSite = substr_replace(JURI::root(), '', -1, 1);
+
+    /**
+     * @var \Civi\Setup\Model $m
+     */
+    $m = $e->getModel();
+    $params = \Civi\Setup\SettingsUtil::createParams($m);
+    $files = [
+      'backend' => [
+        'file' => $m->settingsPath,
+        'params' => ['baseURL' => $liveSite . '/administrator/'] + $params,
+      ],
+      'frontend' => [
+        'file' => implode(DIRECTORY_SEPARATOR, [JPATH_SITE, 'components', 'com_civicrm', 'civicrm.settings.php']),
+        'params' => ['baseURL' => $liveSite . '/'] + $params,
+      ],
+    ];
+
+    $tplPath = implode(DIRECTORY_SEPARATOR,
+      [$m->srcPath, 'templates', 'CRM', 'common', 'civicrm.settings.php.template']
+    );
+
+    foreach ($files as $fileSpec) {
+      $str = \Civi\Setup\SettingsUtil::evaluate($tplPath, $fileSpec['params']);
+      JFile::write($fileSpec['file'], $str);
+    }
+
+  }, \Civi\Setup::PRIORITY_LATE);
diff --git a/setup/plugins/installFiles/InstallSettingsFile.civi-setup.php b/setup/plugins/installFiles/InstallSettingsFile.civi-setup.php
index 735909d643..7eab4536de 100644
--- a/setup/plugins/installFiles/InstallSettingsFile.civi-setup.php
+++ b/setup/plugins/installFiles/InstallSettingsFile.civi-setup.php
@@ -60,6 +60,11 @@ if (!defined('CIVI_SETUP')) {
  */
 \Civi\Setup::dispatcher()
   ->addListener('civi.setup.installFiles', function (\Civi\Setup\Event\InstallFilesEvent $e) {
+    if ($e->getModel()->cms === 'Joomla') {
+      // Complicated. Another plugin will do it.
+      return;
+    }
+
     \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'installFiles'));
 
     /**
-- 
2.25.1