Skip to content

Commit

Permalink
refs #39633, gh-111, add ajax sync button
Browse files Browse the repository at this point in the history
  • Loading branch information
jimyhuang committed Jan 22, 2024
1 parent 8c2d9d6 commit 94114fe
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CRM/Core/BAO/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
* @param string $group (required) The group name of the item
* @param string $path (required) The path under which this item is stored
* @param int $componentID The optional component ID (so componenets can share the same name space)
* @param date $createdTime minimal create date time in int or mysql datetime format
* @param int $createdTime minimal create date time in int or mysql datetime format
*
* @return mixed The data if present in cache, else null
* @static
Expand Down
49 changes: 44 additions & 5 deletions CRM/Group/Form/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
*/
protected $_smartMarketingService;

/**
* the smart marketing group type id
*
* @var object
*/
protected $_smartMarketingTypeId;

/**
* set up variables to build the form
*
Expand Down Expand Up @@ -151,6 +158,9 @@ function preProcess() {

//build custom data
CRM_Custom_Form_Customdata::preProcess($this, NULL, NULL, 1, 'Group', $this->_id);

// smart marketing
$this->initSmartMarketingGroup();
}

/*
Expand Down Expand Up @@ -188,6 +198,14 @@ function setDefaultValues() {

$this->assign('organizationID', $defaults['organization_id']);
}

// smart marketing
if (!empty($defaults['is_sync']) && !empty($defaults['sync_data'])) {
$data = json_decode($defaults['sync_data'], TRUE);
if (!empty($data['remote_group_id'])) {
$defaults['remote_group_id'] = $data['remote_group_id'];
}
}
}

if (!CRM_Utils_Array::value('parents', $defaults)) {
Expand All @@ -196,6 +214,7 @@ function setDefaultValues() {

// custom data set defaults
$defaults += CRM_Custom_Form_Customdata::setDefaultValues($this);

return $defaults;
}

Expand Down Expand Up @@ -261,21 +280,24 @@ public function buildQuickForm() {
$gtId = $ele->_attributes['id'];
$ele->_attributes['data-filter'] = $filterGroupTypes[$gtId];
if (strstr($filterGroupTypes[$gtId], 'Smart-Marketing')) {
// freeze when smart_marketing_group has value
// freeze when remote_group_id has value
if (!empty($this->_groupValues['is_sync'])) {
$ele->freeze();
}
}
}
}

$this->initSmartMarketingGroup();
if (isset($this->_smartMarketingService)) {
// dropdown box
$remoteGroups = $this->_smartMarketingService->getRemoteGroups();
$remoteGroups = CRM_Core_BAO_Cache::getItem('group editing', 'remote-groups-'.get_class($this->_smartMarketingService), NULL, CRM_REQUEST_TIME-180);
if (empty($remoteGroups)) {
$remoteGroups = $this->_smartMarketingService->getRemoteGroups();
CRM_Core_BAO_Cache::setItem($remoteGroups, 'group editing', 'remote-groups-'.get_class($this->_smartMarketingService), NULL, CRM_REQUEST_TIME+180);
}
// flydove doesn't support create group
// $remoteGroups = array('-1' => '-- '.ts('Create New Group').' --') + $remoteGroups;
$eleSmGroup = $this->addSelect('smart_marketing_group', ts('Smart Marketing Group'), $remoteGroups);
$eleSmGroup = $this->addSelect('remote_group_id', ts('Smart Marketing Group'), $remoteGroups);
if (!empty($this->_groupValues['is_sync'])) {
$eleSmGroup->freeze();
}
Expand Down Expand Up @@ -474,6 +496,21 @@ public function postProcess() {
'Group'
);

if (!empty($params['group_type']) && !empty($this->_smartMarketingTypeId)) {
foreach($params['group_type'] as $typeId => $dontCare) {
if ($typeId == $this->_smartMarketingTypeId) {
// save smart marketing related fields
$params['is_sync'] = 1;
if (!empty($params['remote_group_id'])) {
$params['sync_data'] = json_encode(array(
'remote_group_id' => $params['remote_group_id'],
));
}
break;
}
}
}

require_once 'CRM/Contact/BAO/Group.php';
$group = &CRM_Contact_BAO_Group::create($params);

Expand Down Expand Up @@ -520,8 +557,9 @@ public function postProcess() {
*/
private function initSmartMarketingGroup() {
$groupTypes = CRM_Core_OptionGroup::values('group_type');
foreach($groupTypes as $smartMarketingName) {
foreach($groupTypes as $typeId => $smartMarketingName) {
if (strstr($smartMarketingName, 'Smart Marketing')) {
$this->_smartMarketingTypeId = $typeId;
list($smartMarketingVendor) = explode(' ', $smartMarketingName);
if (strlen($smartMarketingVendor) > 0) {
$smartMarketingVendor = ucfirst($smartMarketingVendor);
Expand All @@ -531,6 +569,7 @@ private function initSmartMarketingGroup() {
if (!empty($providers)) {
$provider = reset($providers);
$this->_smartMarketingService = new $smartMarketingClass($provider['id']);
$this->assign('smart_marketing_provider_id', $provider['id']);
return TRUE;
}
}
Expand Down
9 changes: 9 additions & 0 deletions CRM/Mailing/External/SmartMarketing/Flydove.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ private function apiRequestSend($apiType, $data = NULL) {
}
}

/**
* Undocumented function
*
* @param array $contactIds
* @param int $destRemoteGroup
* @param int $providerId
* @return int batch id when success
*/
public function batchSchedule($contactIds, $destRemoteGroup, $providerId) {
$remoteGroups = $this->getRemoteGroups();
if (isset($remoteGroups[$destRemoteGroup])) {
Expand All @@ -159,6 +167,7 @@ public function batchSchedule($contactIds, $destRemoteGroup, $providerId) {
'processed' => 0,
);
$batch->start($batchParams);
return $batch->_id;
}
else {
throw new CRM_Core_Exception(ts("Please provide at least 1 contact."));
Expand Down
16 changes: 12 additions & 4 deletions CRM/Mailing/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ static function template() {
* @return string
*/
public static function addContactToRemote() {
$providerId = CRM_Utils_Request::retrieve('provider_id', 'Integer', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST');
$groupId = CRM_Utils_Request::retrieve('group_id', 'Integer', CRM_Core_DAO::$_nullObject, TRUE, NULL, 'POST');
$providerId = CRM_Utils_Request::retrieve('provider_id', 'Integer', CRM_Core_DAO::$_nullObject, FALSE , NULL, 'POST');
$groupId = CRM_Utils_Request::retrieve('group_id', 'Integer', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST');
if (empty($providerId) || empty($groupId)) {
http_response_code(400);
echo json_encode(array('success' => FALSE, 'message' => 'please provide correct arguments'));
CRM_Utils_System::civiExit();
}
$providers = CRM_SMS_BAO_Provider::getProviders(NULL, array('id' => $providerId));

$groupParams = array(
Expand All @@ -94,8 +99,11 @@ public static function addContactToRemote() {
$smartMarketingClass = 'CRM_Mailing_External_SmartMarketing_'.$vendorName;
$smartMarketingService = new $smartMarketingClass($providerId);
try {
$smartMarketingService->batchSchedule($contactIds, $syncData['remote_group_id'], $providerId);
$remoteResult = array('success' => TRUE, 'message' => '');
$batchId = $smartMarketingService->batchSchedule($contactIds, $syncData['remote_group_id'], $providerId);
$remoteResult = array(
'success' => TRUE,
'message' => ts('Because of the large amount of data you are about to perform, we have scheduled this job for the batch process. You will receive an email notification when the work is completed.'). ' &raquo; <a href="'.CRM_Utils_System::url('civicrm/admin/batch', "reset=1&id=$batchId").'" target="_blank">'.ts('Batch Job List').'</a>',
);
}
catch(CRM_Core_Exception $e) {
$remoteResult = array('success' => FALSE, 'message' => $e->getMessage());
Expand Down
40 changes: 27 additions & 13 deletions templates/CRM/Group/Form/Edit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
</tr>
{/if}

{if $form.smart_marketing_group}
<tr class="crm-group-form-block-smart_marketing_group">
<td class="label">{$form.smart_marketing_group.label}</td>
{if $form.remote_group_id}
<tr class="crm-group-form-block-remote_group_id">
<td class="label">{$form.remote_group_id.label}</td>
<td>
{$form.smart_marketing_group.html}
{$form.remote_group_id.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" class="hide-block">
Expand All @@ -69,18 +69,21 @@
{/if}
<script>{literal}
cj(function($){
$('.crm-group-form-block-smart_marketing_group').hide();
$('.crm-group-form-block-remote_group_id').hide();
if ($('.crm-group-form-block-remote_group_id input[type=hidden][name=remote_group_id]')) {
$('.crm-group-form-block-remote_group_id').show();
}
$('input[name*=group_type]').each(function(){
if($(this).data('filter').match('Smart-Marketing')) {
if($(this).data('filter') && $(this).data('filter').match('Smart-Marketing')) {
if ($(this).prop("checked")) {
$('.crm-group-form-block-smart_marketing_group').show();
$('.crm-group-form-block-remote_group_id').show();
}
$(this).click(function(){
if ($(this).prop("checked")) {
$('.crm-group-form-block-smart_marketing_group').show();
$('.crm-group-form-block-remote_group_id').show();
}
else {
$('.crm-group-form-block-smart_marketing_group').hide();
$('.crm-group-form-block-remote_group_id').hide();
}
});
}
Expand All @@ -94,12 +97,23 @@ cj(function($){
dialogClass: "smart-marketing-sync-confirm-box",
buttons: {
"{/literal}{ts}Sync Now{/ts}{literal}": function() {
console.log('sync group contacts to remote');
$(this).dialog("close");
let dataURL = "{/literal}{crmURL p='civicrm/ajax/addContactToRemote' q='snippet=5'}{literal}";
let providerId = "{/literal}{$smart_marketing_provider_id}{literal}";
let groupId = "{/literal}{$group.id}{literal}";
$.ajax({
url: dataURL,
type: "POST",
data: {"provider_id":providerId, "group_id":groupId},
dataType: "json",
success: function(data) {
if (data.success) {
}
}
});
return true;
},
"{/literal}{ts}Cancel{/ts}{literal}": function() {
console.log('cancel action');
cj( this ).dialog( "close" );
return false;
}
Expand Down Expand Up @@ -207,4 +221,4 @@ cj('#organization').autocomplete( dataUrl, {
{/literal}
</div>
{include file="CRM/common/chosen.tpl" selector="#smart_marketing_group,#parents"}
{include file="CRM/common/chosen.tpl" selector="#parents"}

0 comments on commit 94114fe

Please sign in to comment.