Skip to content

Commit

Permalink
Allow for configuration of activity details from email to activity
Browse files Browse the repository at this point in the history
  • Loading branch information
larssandergreen committed Jul 24, 2023
1 parent c53f8eb commit c73c682
Show file tree
Hide file tree
Showing 15 changed files with 4,699 additions and 4,244 deletions.
37 changes: 33 additions & 4 deletions CRM/Admin/Form/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,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 @@ -109,11 +127,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'))
) {
if (!$this->_id) {
$defaults['is_ssl'] = TRUE;
$defaults['is_default'] = 0;
$defaults['activity_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound Email');
$defaults['activity_status'] = 'Completed';
$defaults['activity_source'] = 'from';
$defaults['activity_targets'] = 'to,cc,bcc';
$defaults['activity_assignees'] = 'from';
$defaults['is_active'] = TRUE;
}

return $defaults;
Expand Down Expand Up @@ -174,6 +196,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 @@ -183,6 +211,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
203 changes: 202 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:f78519e347dec6f6fde41e14b7da4370)
* (GenCodeChecksum:bac7aa97cdf676eb6803853d4e2a7a94)
*/

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

/**
* @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 @@ -220,6 +272,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 @@ -606,6 +659,154 @@ public static function &fields() {
],
'add' => '5.31',
],
'is_active' => [
'name' => 'is_active',
'type' => CRM_Utils_Type::T_BOOLEAN,
'title' => ts('Process emails?'),
'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.31',
],
'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.65',
],
'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.65',
],
'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.65',
],
'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.65',
],
'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.65',
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down
35 changes: 35 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveSixtyFive.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ public function upgrade_5_65_alpha1($rev): void {
$this->addTask('Make Group.frontend_title required', 'alterColumn', 'civicrm_group', 'frontend_title', "varchar(255) NOT NULL COMMENT 'Alternative public description of the group.'", TRUE);

$this->addTask('Update ActionSchedule.limit_to column', 'alterColumn', 'civicrm_action_schedule', 'limit_to', "int COMMENT 'Is this the recipient criteria limited to OR in addition to?'");
$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) {
self::addColumn($ctx, 'civicrm_mail_settings', 'is_active', 'tinyint NOT NULL DEFAULT 1 COMMENT "Ignored for bounce processing, only for email-to-activity"');
self::addColumn($ctx, 'civicrm_mail_settings', 'activity_type_id', 'int unsigned COMMENT "Implicit FK to civicrm_option_value where option_group = activity_type"');
self::addColumn($ctx, 'civicrm_mail_settings', 'campaign_id', 'int unsigned DEFAULT NULL COMMENT "Foreign key to the Campaign."');
self::addColumn($ctx, 'civicrm_mail_settings', 'activity_source', 'varchar(4) COMMENT "Which email recipient to add as the activity source (from, to, cc, bcc)."');
self::addColumn($ctx, 'civicrm_mail_settings', 'activity_targets', 'varchar(16) COMMENT "Which email recipients to add as the activity targets (from, to, cc, bcc)."');
self::addColumn($ctx, 'civicrm_mail_settings', 'activity_assignees', 'varchar(16) COMMENT "Which email recipients to add as the activity assignees (from, to, cc, bcc)."');

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;
");
}

CRM_Core_DAO::executeQuery('UPDATE civicrm_mail_settings SET `activity_source` = "from" WHERE `activity_source` IS NULL AND `is_default` = 0;');
CRM_Core_DAO::executeQuery('UPDATE civicrm_mail_settings SET `activity_targets` = "to,cc,bcc" WHERE `activity_targets` IS NULL AND `is_default` = 0;');
CRM_Core_DAO::executeQuery('UPDATE civicrm_mail_settings SET `activity_assignees` = "from" WHERE `activity_assignees` IS NULL AND `is_default` = 0;');
$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 AND `is_default` = 0;');
}
return TRUE;
}

}
Loading

0 comments on commit c73c682

Please sign in to comment.