Skip to content

Commit

Permalink
Merge pull request #26905 from larssandergreen/More-inbound-email-pro…
Browse files Browse the repository at this point in the history
…cessing-options

Allow for configuration of activity contacts, type and campaign for email-to-activity
  • Loading branch information
demeritcowboy authored Aug 19, 2023
2 parents 37b79e6 + df97bb6 commit a5b74bc
Show file tree
Hide file tree
Showing 15 changed files with 5,605 additions and 5,157 deletions.
40 changes: 34 additions & 6 deletions CRM/Admin/Form/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,28 @@ public function buildQuickForm() {
0 => ts('Email-to-Activity Processing'),
];
$this->add('select', 'is_default', ts('Used For?'), $usedfor);

$activityTypes =
[CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound Email') => 'Inbound Email']
+ [CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email') => 'Email']
+ CRM_Core_PseudoConstant::ActivityType(FALSE);
$this->add('select', 'activity_type_id', ts('Activity Type'),
$activityTypes,
TRUE,
['class' => 'crm-select2 required']
);
$this->addField('activity_status', ['placeholder' => FALSE]);
CRM_Campaign_BAO_Campaign::addCampaign($this);

$this->add('checkbox', 'is_non_case_email_skipped', ts('Skip emails which do not have a Case ID or Case hash'));
$this->add('checkbox', 'is_contact_creation_disabled_if_no_match', ts('Do not create new contacts when filing emails'));

$emailRecipients = ['from' => 'From', 'to' => 'To', 'cc' => 'CC', 'bcc' => 'BCC'];
$this->add('select', 'activity_source', ts('Activity Source'), $emailRecipients, TRUE, ['class' => 'crm-select2 required']);
$this->add('select', 'activity_targets', ts('Activity Targets'), $emailRecipients, FALSE, ['class' => 'crm-select2', 'multiple' => TRUE]);
$this->add('select', 'activity_assignees', ts('Activity Assignees'), $emailRecipients, FALSE, ['class' => 'crm-select2', 'multiple' => TRUE]);

$this->add('checkbox', 'is_active', ts('Enabled'));
}

/**
Expand All @@ -108,12 +126,15 @@ public function getDefaultEntity() {
public function setDefaultValues() {
$defaults = parent::setDefaultValues();

// Set activity status to "Completed" by default.
if ($this->_action != CRM_Core_Action::DELETE &&
(!$this->_id || !CRM_Core_DAO::getFieldValue('CRM_Core_BAO_MailSettings', $this->_id, 'activity_status'))
) {
$defaults['activity_status'] = 'Completed';
}
$defaults['is_ssl'] = $defaults['is_ssl'] ?? TRUE;
$defaults['is_default'] = $defaults['is_default'] ?? 0;
$defaults['activity_type_id'] = $defaults['activity_type_id'] ??
CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound Email');
$defaults['activity_status'] = $defaults['activity_status'] ?? 'Completed';
$defaults['activity_source'] = $defaults['activity_source'] ?? 'from';
$defaults['activity_targets'] = $defaults['activity_targets'] ?? 'to,cc,bcc';
$defaults['activity_assignees'] = $defaults['activity_assignees'] ?? 'from';
$defaults['is_active'] = $defaults['is_active'] ?? TRUE;

return $defaults;
}
Expand Down Expand Up @@ -173,6 +194,12 @@ public function postProcess() {
'activity_status',
'is_non_case_email_skipped',
'is_contact_creation_disabled_if_no_match',
'activity_type_id',
'campaign_id',
'activity_source',
'activity_targets',
'activity_assignees',
'is_active',
];

$params = [];
Expand All @@ -182,6 +209,7 @@ public function postProcess() {
'is_ssl',
'is_non_case_email_skipped',
'is_contact_creation_disabled_if_no_match',
'is_active',
])) {
$params[$f] = CRM_Utils_Array::value($f, $formValues, FALSE);
}
Expand Down
8 changes: 3 additions & 5 deletions CRM/Admin/Page/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function getBAOName() {
* Browse all mail settings.
*/
public function browse() {
//get all mail settings.
$allMailSettings = [];
$mailSetting = new CRM_Core_DAO_MailSettings();

Expand All @@ -45,20 +44,19 @@ public function browse() {
//multi-domain support for mail settings. CRM-5244
$mailSetting->domain_id = CRM_Core_Config::domainID();

//find all mail settings.
$mailSetting->find();
while ($mailSetting->fetch()) {
//replace protocol value with name
$mailSetting->protocol = $allProtocols[$mailSetting->protocol] ?? NULL;
CRM_Core_DAO::storeValues($mailSetting, $allMailSettings[$mailSetting->id]);

//form all action links
$action = array_sum(array_keys($this->links()));

// disallow the DELETE action for the default set of settings
if ($mailSetting->is_default) {
$action &= ~CRM_Core_Action::DELETE;
$action -= CRM_Core_Action::DELETE;
$action -= CRM_Core_Action::DISABLE;
}
$action -= ($mailSetting->is_active) ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE;

//add action links.
$allMailSettings[$mailSetting->id]['action'] = CRM_Core_Action::formLink(self::links(), $action,
Expand Down
206 changes: 205 additions & 1 deletion CRM/Core/DAO/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/MailSettings.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:2e7847f2338745c805f4d8c8939ac511)
* (GenCodeChecksum:7d9b4af7c9e04acac0e734ce036ea527)
*/

/**
Expand Down Expand Up @@ -193,6 +193,60 @@ class CRM_Core_DAO_MailSettings extends CRM_Core_DAO {
*/
public $is_contact_creation_disabled_if_no_match;

/**
* Ignored for bounce processing, only for email-to-activity
*
* @var bool|string
* (SQL type: tinyint)
* Note that values will be retrieved from the database as a string.
*/
public $is_active;

/**
* Implicit FK to civicrm_option_value where option_group = activity_type
*
* @var int|string|null
* (SQL type: int unsigned)
* Note that values will be retrieved from the database as a string.
*/
public $activity_type_id;

/**
* Foreign key to the Campaign.
*
* @var int|string|null
* (SQL type: int unsigned)
* Note that values will be retrieved from the database as a string.
*/
public $campaign_id;

/**
* Which email recipient to add as the activity source (from, to, cc, bcc).
*
* @var string|null
* (SQL type: varchar(4))
* Note that values will be retrieved from the database as a string.
*/
public $activity_source;

/**
* Which email recipients to add as the activity targets (from, to, cc, bcc).
*
* @var string|null
* (SQL type: varchar(16))
* Note that values will be retrieved from the database as a string.
*/
public $activity_targets;

/**
* Which email recipients to add as the activity assignees (from, to, cc, bcc).
*
* @var string|null
* (SQL type: varchar(16))
* Note that values will be retrieved from the database as a string.
*/
public $activity_assignees;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -221,6 +275,7 @@ public static function getReferenceColumns() {
if (!isset(Civi::$statics[__CLASS__]['links'])) {
Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'domain_id', 'civicrm_domain', 'id');
Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'campaign_id', 'civicrm_campaign', 'id');
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
}
return Civi::$statics[__CLASS__]['links'];
Expand Down Expand Up @@ -607,6 +662,155 @@ public static function &fields() {
],
'add' => '5.31',
],
'is_active' => [
'name' => 'is_active',
'type' => CRM_Utils_Type::T_BOOLEAN,
'title' => ts('Enabled?'),
'description' => ts('Ignored for bounce processing, only for email-to-activity'),
'required' => TRUE,
'usage' => [
'import' => FALSE,
'export' => FALSE,
'duplicate_matching' => FALSE,
'token' => FALSE,
],
'where' => 'civicrm_mail_settings.is_active',
'default' => '1',
'table_name' => 'civicrm_mail_settings',
'entity' => 'MailSettings',
'bao' => 'CRM_Core_BAO_MailSettings',
'localizable' => 0,
'html' => [
'type' => 'CheckBox',
],
'add' => '5.66',
],
'activity_type_id' => [
'name' => 'activity_type_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Activity Type'),
'description' => ts('Implicit FK to civicrm_option_value where option_group = activity_type'),
'usage' => [
'import' => FALSE,
'export' => FALSE,
'duplicate_matching' => FALSE,
'token' => FALSE,
],
'where' => 'civicrm_mail_settings.activity_type_id',
'table_name' => 'civicrm_mail_settings',
'entity' => 'MailSettings',
'bao' => 'CRM_Core_BAO_MailSettings',
'localizable' => 0,
'html' => [
'type' => 'Select',
],
'pseudoconstant' => [
'optionGroupName' => 'activity_type',
'optionEditPath' => 'civicrm/admin/options/activity_type',
],
'add' => '5.66',
],
'campaign_id' => [
'name' => 'campaign_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Campaign ID'),
'description' => ts('Foreign key to the Campaign.'),
'usage' => [
'import' => FALSE,
'export' => FALSE,
'duplicate_matching' => FALSE,
'token' => FALSE,
],
'where' => 'civicrm_mail_settings.campaign_id',
'default' => NULL,
'table_name' => 'civicrm_mail_settings',
'entity' => 'MailSettings',
'bao' => 'CRM_Core_BAO_MailSettings',
'localizable' => 0,
'FKClassName' => 'CRM_Campaign_DAO_Campaign',
'html' => [
'type' => 'EntityRef',
'label' => ts("Campaign"),
],
'pseudoconstant' => [
'table' => 'civicrm_campaign',
'keyColumn' => 'id',
'labelColumn' => 'title',
'prefetch' => 'FALSE',
],
'add' => '5.66',
],
'activity_source' => [
'name' => 'activity_source',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Activity Source'),
'description' => ts('Which email recipient to add as the activity source (from, to, cc, bcc).'),
'maxlength' => 4,
'size' => CRM_Utils_Type::FOUR,
'usage' => [
'import' => FALSE,
'export' => FALSE,
'duplicate_matching' => FALSE,
'token' => FALSE,
],
'where' => 'civicrm_mail_settings.activity_source',
'table_name' => 'civicrm_mail_settings',
'entity' => 'MailSettings',
'bao' => 'CRM_Core_BAO_MailSettings',
'localizable' => 0,
'html' => [
'type' => 'Select',
],
'add' => '5.66',
],
'activity_targets' => [
'name' => 'activity_targets',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Activity Targets'),
'description' => ts('Which email recipients to add as the activity targets (from, to, cc, bcc).'),
'maxlength' => 16,
'size' => CRM_Utils_Type::TWELVE,
'usage' => [
'import' => FALSE,
'export' => FALSE,
'duplicate_matching' => FALSE,
'token' => FALSE,
],
'where' => 'civicrm_mail_settings.activity_targets',
'table_name' => 'civicrm_mail_settings',
'entity' => 'MailSettings',
'bao' => 'CRM_Core_BAO_MailSettings',
'localizable' => 0,
'serialize' => self::SERIALIZE_COMMA,
'html' => [
'type' => 'Select',
],
'add' => '5.66',
],
'activity_assignees' => [
'name' => 'activity_assignees',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Activity Assignees'),
'description' => ts('Which email recipients to add as the activity assignees (from, to, cc, bcc).'),
'maxlength' => 16,
'size' => CRM_Utils_Type::TWELVE,
'usage' => [
'import' => FALSE,
'export' => FALSE,
'duplicate_matching' => FALSE,
'token' => FALSE,
],
'where' => 'civicrm_mail_settings.activity_assignees',
'table_name' => 'civicrm_mail_settings',
'entity' => 'MailSettings',
'bao' => 'CRM_Core_BAO_MailSettings',
'localizable' => 0,
'serialize' => self::SERIALIZE_COMMA,
'html' => [
'type' => 'Select',
],
'add' => '5.66',
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down
46 changes: 46 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveSixtySix.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,52 @@ public function upgrade_5_66_alpha1($rev): void {
// These run after the sql file
$this->addTask('Make ActionSchedule.name required', 'alterColumn', 'civicrm_action_schedule', 'name', "varchar(64) NOT NULL COMMENT 'Name of the action(reminder)'");
$this->addTask(ts('Create index %1', [1 => 'civicrm_action_schedule.UI_name']), 'addIndex', 'civicrm_action_schedule', 'name', 'UI');
$this->addTask('Add fields to civicrm_mail_settings to allow more flexibility for email to activity', 'addMailSettingsFields');
}

/**
* Add fields to civicrm_mail_settings to allow more flexibility for email to activity
*
* @param \CRM_Queue_TaskContext $ctx
*
* @return bool
*/
public static function addMailSettingsFields(CRM_Queue_TaskContext $ctx) {
$ctx->log->info('Adding field is_active');
self::addColumn($ctx, 'civicrm_mail_settings', 'is_active', 'tinyint NOT NULL DEFAULT 1 COMMENT "Ignored for bounce processing, only for email-to-activity"');
$ctx->log->info('Adding field activity_type_id');
self::addColumn($ctx, 'civicrm_mail_settings', 'activity_type_id', 'int unsigned COMMENT "Implicit FK to civicrm_option_value where option_group = activity_type"');
$ctx->log->info('Adding field campaign_id');
self::addColumn($ctx, 'civicrm_mail_settings', 'campaign_id', 'int unsigned DEFAULT NULL COMMENT "Foreign key to the Campaign."');
$ctx->log->info('Adding field activity_source');
self::addColumn($ctx, 'civicrm_mail_settings', 'activity_source', 'varchar(4) COMMENT "Which email recipient to add as the activity source (from, to, cc, bcc)."');
$ctx->log->info('Adding field activity_targets');
self::addColumn($ctx, 'civicrm_mail_settings', 'activity_targets', 'varchar(16) COMMENT "Which email recipients to add as the activity targets (from, to, cc, bcc)."');
$ctx->log->info('Adding field activity_assignees');
self::addColumn($ctx, 'civicrm_mail_settings', 'activity_assignees', 'varchar(16) COMMENT "Which email recipients to add as the activity assignees (from, to, cc, bcc)."');

$ctx->log->info('Adding FK_civicrm_mail_settings_campaign_id');
if (!self::checkFKExists('civicrm_mail_settings', 'FK_civicrm_mail_settings_campaign_id')) {
CRM_Core_DAO::executeQuery("
ALTER TABLE `civicrm_mail_settings`
ADD CONSTRAINT `FK_civicrm_mail_settings_campaign_id`
FOREIGN KEY (`campaign_id`) REFERENCES `civicrm_campaign`(`id`)
ON DELETE SET NULL;
");
}

$ctx->log->info('Setting default activity_source');
CRM_Core_DAO::executeQuery('UPDATE civicrm_mail_settings SET `activity_source` = "from" WHERE `activity_source` IS NULL;');
$ctx->log->info('Setting default activity_targets');
CRM_Core_DAO::executeQuery('UPDATE civicrm_mail_settings SET `activity_targets` = "to,cc,bcc" WHERE `activity_targets` IS NULL;');
$ctx->log->info('Setting default activity_assignees');
CRM_Core_DAO::executeQuery('UPDATE civicrm_mail_settings SET `activity_assignees` = "from" WHERE `activity_assignees` IS NULL;');
$ctx->log->info('Setting default activity_type_id');
$inboundEmailActivity = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound Email');
if ($inboundEmailActivity) {
CRM_Core_DAO::executeQuery('UPDATE civicrm_mail_settings SET `activity_type_id` = ' . $inboundEmailActivity . ' WHERE `activity_type_id` IS NULL;');
}
return TRUE;
}

}
Loading

0 comments on commit a5b74bc

Please sign in to comment.