Skip to content

Commit

Permalink
Merge pull request #18885 from totten/master-add-mail
Browse files Browse the repository at this point in the history
dev/core#2141 - "Add Mail Account" - Allow hookable listing of setup links
  • Loading branch information
eileenmcnaughton authored Oct 30, 2020
2 parents 75f113a + 5f68c8c commit 23057ec
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CRM/Admin/Page/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions CRM/Core/BAO/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions CRM/Mailing/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
5 changes: 5 additions & 0 deletions CRM/Mailing/xml/Menu/Mailing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@
<page_callback>CRM_Mailing_Page_AJAX::getContactMailings</page_callback>
<access_arguments>access CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/ajax/setupMailAccount</path>
<page_callback>CRM_Mailing_Page_AJAX::setup</page_callback>
<access_arguments>access CiviCRM,access CiviMail</access_arguments>
</item>
<item>
<path>civicrm/mailing/url</path>
<page_callback>CRM_Mailing_Page_Url</page_callback>
Expand Down
18 changes: 18 additions & 0 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
33 changes: 29 additions & 4 deletions templates/CRM/Admin/Page/MailSettings.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,34 @@
{ts}None found.{/ts}
</div>
{/if}
<div class="action-link">
{crmButton q="action=add&reset=1" id="newMailSettings" icon="plus-circle"}{ts}Add Mail Account{/ts}{/crmButton}
{crmButton p="civicrm/admin" q="reset=1" class="cancel" icon="times"}{ts}Done{/ts}{/crmButton}
</div>
{if $setupActions}
<form>
<select id="crm-mail-setup" name="crm-mail-setup" class="crm-select2 crm-form-select" aria-label="{ts}Add Mail Account{/ts}">
<option value="" aria-hidden="true">{ts}Add Mail Account{/ts}</option>
{foreach from=$setupActions key=setupActionsName item=setupAction}
<option value="{$setupActionsName|escape}">{$setupAction.title|escape}</option>
{/foreach}
</select>
</form>
{else}
<div class="action-link">
{crmButton q="action=add&reset=1" id="newMailSettings" icon="plus-circle"}{ts}Add Mail Account{/ts}{/crmButton}
{crmButton p="civicrm/admin" q="reset=1" class="cancel" icon="times"}{ts}Done{/ts}{/crmButton}
</div>
{/if}

{/if}
</div>
{literal}
<script type="text/javascript">
cj('#crm-mail-setup').val('');
cj('#crm-mail-setup').on('select2-selecting', function(event) {
if (!event.val) {
return;
}
event.stopPropagation();
var url = CRM.url('civicrm/ajax/setupMailAccount', {type: event.val});
window.location = url;
});
</script>
{/literal}

0 comments on commit 23057ec

Please sign in to comment.