diff --git a/CRM/Admin/Page/MailSettings.php b/CRM/Admin/Page/MailSettings.php index 6ede83649efa..c3dca8c85342 100644 --- a/CRM/Admin/Page/MailSettings.php +++ b/CRM/Admin/Page/MailSettings.php @@ -107,6 +107,11 @@ public function browse() { } $this->assign('rows', $allMailSettings); + + $setupActions = CRM_Core_BAO_MailSettings::getSetupActions(); + if (count($setupActions) > 1 || !isset($setupActions['standard'])) { + $this->assign('setupActions', $setupActions); + } } /** diff --git a/CRM/Core/BAO/MailSettings.php b/CRM/Core/BAO/MailSettings.php index e7ea7b196b93..26086ea2e8e4 100644 --- a/CRM/Core/BAO/MailSettings.php +++ b/CRM/Core/BAO/MailSettings.php @@ -23,6 +23,30 @@ public function __construct() { parent::__construct(); } + /** + * Get a list of setup-actions. + * + * @return array + * List of available actions. See description in the hook-docs. + * @see CRM_Utils_Hook::mailSetupActions() + */ + public static function getSetupActions() { + $setupActions = []; + $setupActions['standard'] = [ + 'title' => ts('Standard Mail Account'), + 'callback' => ['CRM_Core_BAO_MailSettings', 'setupStandardAccount'], + ]; + + CRM_Utils_Hook::mailSetupActions($setupActions); + return $setupActions; + } + + public static function setupStandardAccount($setupAction) { + return [ + 'url' => CRM_Utils_System::url('civicrm/admin/mailSettings', 'action=add&reset=1', TRUE, NULL, FALSE), + ]; + } + /** * Return the DAO object containing to the default row of * civicrm_mail_settings and cache it for further calls diff --git a/CRM/Mailing/Page/AJAX.php b/CRM/Mailing/Page/AJAX.php index e5b4fb9e8fdc..5af3033f931f 100644 --- a/CRM/Mailing/Page/AJAX.php +++ b/CRM/Mailing/Page/AJAX.php @@ -20,6 +20,32 @@ */ class CRM_Mailing_Page_AJAX { + /** + * Kick off the "Add Mail Account" process for some given type of account. + * + * Ex: 'civicrm/ajax/setupMailAccount?type=standard' + * Ex: 'civicrm/ajax/setupMailAccount?type=oauth_1' + * + * @see CRM_Core_BAO_MailSettings::getSetupActions() + * @throws \CRM_Core_Exception + */ + public static function setup() { + $type = CRM_Utils_Request::retrieve('type', 'String'); + $setupActions = CRM_Core_BAO_MailSettings::getSetupActions(); + $setupAction = $setupActions[$type] ?? NULL; + if ($setupAction === NULL) { + throw new \CRM_Core_Exception("Cannot setup mail account. Invalid type requested."); + } + + $result = call_user_func($setupAction['callback'], $setupAction); + if (isset($result['url'])) { + CRM_Utils_System::redirect($result['url']); + } + else { + throw new \CRM_Core_Exception("Cannot setup mail account. Setup does not have a URL."); + } + } + /** * Fetch the template text/html messages */ diff --git a/CRM/Mailing/xml/Menu/Mailing.xml b/CRM/Mailing/xml/Menu/Mailing.xml index 78ac7ebe0654..3c4446704fb3 100644 --- a/CRM/Mailing/xml/Menu/Mailing.xml +++ b/CRM/Mailing/xml/Menu/Mailing.xml @@ -202,6 +202,11 @@ CRM_Mailing_Page_AJAX::getContactMailings access CiviCRM + + civicrm/ajax/setupMailAccount + CRM_Mailing_Page_AJAX::setup + access CiviCRM,access CiviMail + civicrm/mailing/url CRM_Mailing_Page_Url diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index a987971774b4..4eb7f470bcd1 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -1042,6 +1042,24 @@ public static function eventDiscount(&$form, &$params) { ); } + /** + * When adding a new "Mail Account" (`MailSettings`), present a menu of setup + * options. + * + * @param array $setupActions + * Each item has a symbolic-key, and it has the properties: + * - title: string + * - callback: string|array, the function which starts the setup process. + * The function is expected to return a 'url' for the config screen. + * @return mixed + */ + public static function mailSetupActions(&$setupActions) { + return self::singleton()->invoke(['setupActions'], $setupActions, self::$_nullObject, self::$_nullObject, + self::$_nullObject, self::$_nullObject, self::$_nullObject, + 'civicrm_mailSetupActions' + ); + } + /** * This hook is called when composing a mailing. You can include / exclude other groups as needed. * diff --git a/templates/CRM/Admin/Page/MailSettings.tpl b/templates/CRM/Admin/Page/MailSettings.tpl index f7e2bde5c4b9..18f1d67148d3 100644 --- a/templates/CRM/Admin/Page/MailSettings.tpl +++ b/templates/CRM/Admin/Page/MailSettings.tpl @@ -58,9 +58,34 @@ {ts}None found.{/ts} {/if} - + {if $setupActions} +
+ +
+ {else} + + {/if} + {/if} +{literal} + +{/literal}