Skip to content

Commit

Permalink
refs #39633, gh-111, add interface for smart marketing group sync in …
Browse files Browse the repository at this point in the history
…group editing page
  • Loading branch information
jimyhuang committed Jan 9, 2024
1 parent ab649e4 commit 5e8556a
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
67 changes: 64 additions & 3 deletions CRM/Group/Form/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
*/
protected $_groupOrganizationID;

/**
* the smart marketing object
*
* @var object
*/
protected $_smartMarketingService;

/**
* set up variables to build the form
*
Expand Down Expand Up @@ -238,20 +245,52 @@ public function buildQuickForm() {
}

if (!empty($groupTypes)) {
$tsGroupTypes = $filterGroupTypes = array();
foreach ($groupTypes as $k => $v) {
$gt = ts($k);
$tsGroupTypes[$gt] = $v;
$filterGroupTypes[$v] = str_replace(' ', '-', $k);
}
$this->addCheckBox('group_type',
ts('Group Type'),
$tsGroupTypes,
NULL, NULL, NULL, NULL, '   '
);
$groupTypeItems = $this->getElement('group_type');
foreach($groupTypeItems->_elements as &$ele) {
$gtId = $ele->_attributes['id'];
$ele->_attributes['data-filter'] = $filterGroupTypes[$gtId];
if (strstr($filterGroupTypes[$gtId], 'Smart-Marketing')) {
// freeze when smart_marketing_group has value
if (!empty($this->_groupValues['is_sync'])) {
$ele->freeze();
}
}
}
}

$this->add('select', 'visibility', ts('Visibility'),
CRM_Core_SelectValues::ufVisibility(TRUE), TRUE
);
$this->initSmartMarketingGroup();
if (isset($this->_smartMarketingService)) {
// dropdown box
$remoteGroups = $this->_smartMarketingService->getRemoteGroups();
$remoteGroups = array_merge(array('' => '-- '.ts('Create New Group').' --'), $remoteGroups);
$eleSmGroup = $this->addSelect('smart_marketing_group', ts('Smart Marketing Group'), $remoteGroups);
if (!empty($this->_groupValues['is_sync'])) {
$eleSmGroup->freeze();
}

// button
$vendorName = end(explode('_', get_class($this->_smartMarketingService)));
$this->assign('smart_marketing_vendor', $vendorName);
$this->assign('smart_marketing_sync', TRUE);
if (!empty($this->_groupValues['is_sync']) && !empty($this->_groupValues['sync_data'])) {
$isPrepared = $this->_smartMarketingService->parseSavedData($this->_groupValues['sync_data']);
if ($isPrepared !== FALSE) {
$this->assign('smart_marketing_sync', TRUE);
}
}
}
$this->addSelect('visibility', ts('Visibility'), CRM_Core_SelectValues::ufVisibility(TRUE), TRUE);

$groupNames = &CRM_Core_PseudoConstant::group();

Expand Down Expand Up @@ -472,5 +511,27 @@ public function postProcess() {
CRM_Contact_BAO_GroupNestingCache::update();
}
}

/**
* init first enabled smart marketing group to property
*
* @return void
*/
private function initSmartMarketingGroup() {
$groupTypes = CRM_Core_OptionGroup::values('group_type');
foreach($groupTypes as $smartMarketingName) {
if (strstr($smartMarketingName, 'Smart Marketing')) {
list($smartMarketingVendor) = explode(' ', $smartMarketingName);
if (strlen($smartMarketingVendor) > 0) {
$smartMarketingVendor = ucfirst($smartMarketingVendor);
$smartMarketingClass = 'CRM_Mailing_External_SmartMarketing_'.$smartMarketingVendor;
if (class_exists($smartMarketingClass)) {
$this->_smartMarketingService = new $smartMarketingClass();
return TRUE;
}
}
}
}
}
}

14 changes: 14 additions & 0 deletions CRM/Group/Page/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ function browse($action = NULL) {
require_once 'CRM/Core/OptionGroup.php';
$links = &$this->links();
$allTypes = CRM_Core_OptionGroup::values('group_type');
$smartMarketingTypes = array();
foreach($allTypes as $typeId => $typeName) {
if (strstr($typeName, 'Smart Marketing')) {
$smartMarketingTypes[$typeId] = $typeName;
}
}

$values = array();

while ($object->fetch()) {
Expand Down Expand Up @@ -321,6 +328,13 @@ function browse($action = NULL) {
$action -= CRM_Core_Action::DISABLE;
}
}
if (!empty($smartMarketingTypes)) {
foreach($smartMarketingTypes as $typeId => $typeName) {
if (strstr($object->group_type, CRM_Core_DAO::VALUE_SEPARATOR.$typeId.CRM_Core_DAO::VALUE_SEPARATOR)) {
$action -= CRM_Core_Action::DELETE;
}
}
}

$action = $action & CRM_Core_Action::mask($groupPermissions);

Expand Down
60 changes: 59 additions & 1 deletion templates/CRM/Group/Form/Edit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,63 @@
</tr>
{/if}

<tr class="crm-group-form-block-visibility">
{if $form.smart_marketing_group}
<tr class="crm-group-form-block-smart_marketing_group">
<td class="label">{$form.smart_marketing_group.label}</td>
<td>
{$form.smart_marketing_group.html}
{if $smart_marketing_sync}
<input class="form-submit default smart-marketing-button" name="sync_{$smart_marketing_vendor}" value="{ts}Manually Synchronize{/ts}" type="button" id="sync-{$smart_marketing_vendor}">
<div id="smart-marketing-sync-confirm">
{ts}The automated marketing journey does not start immediately and needs to be scheduled.{/ts}
</div>
{/if}
<script>{literal}
cj(function($){
$('.crm-group-form-block-smart_marketing_group').hide();
$('input[name*=group_type]').each(function(){
if($(this).data('filter').match('Smart-Marketing')) {
if ($(this).prop("checked")) {
$('.crm-group-form-block-smart_marketing_group').show();
}
$(this).click(function(){
if ($(this).prop("checked")) {
$('.crm-group-form-block-smart_marketing_group').show();
}
else {
$('.crm-group-form-block-smart_marketing_group').hide();
}
});
}
});
$('.smart-marketing-button').click(function(e){
e.preventDefault();
$("#smart-marketing-sync-confirm").dialog({
title: "{/literal}{ts}Manually Synchronize{/ts}{literal}",
autoOpen: false,
modal: true,
dialogClass: "smart-marketing-sync-confirm-box",
buttons: {
"{/literal}{ts}Sync Now{/ts}{literal}": function() {
console.log('sync group contacts to remote');
$(this).dialog("close");
return true;
},
"{/literal}{ts}Cancel{/ts}{literal}": function() {
console.log('cancel action');
cj( this ).dialog( "close" );
return false;
}
}
});
cj("#smart-marketing-sync-confirm").dialog('open');
});
});
{/literal}</script>
</td>
</tr>
{/if}
<tr class="crm-group-form-block-visibility">
<td class="label">{$form.visibility.label}</td>
<td>{$form.visibility.html|crmReplace:class:huge} {help id="id-group-visibility" file="CRM/Group/Page/Group.hlp"}</td>
</tr>
Expand Down Expand Up @@ -150,3 +206,5 @@ cj('#organization').autocomplete( dataUrl, {
</script>
{/literal}
</div>
{include file="CRM/common/chosen.tpl" selector="#smart_marketing_group,#parents"}

0 comments on commit 5e8556a

Please sign in to comment.