Commit df7b009b authored by colemanw's avatar colemanw

Convert addCampaignToComponent widget to entityRef

parent b7b528bc
...@@ -596,66 +596,24 @@ INNER JOIN civicrm_group grp ON ( grp.id = campgrp.entity_id ) ...@@ -596,66 +596,24 @@ INNER JOIN civicrm_group grp ON ( grp.id = campgrp.entity_id )
$$fld = CRM_Utils_Array::value($fld, $campaignDetails); $$fld = CRM_Utils_Array::value($fld, $campaignDetails);
} }
//lets see do we have past campaigns.
$hasPastCampaigns = FALSE;
$allActiveCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, TRUE, FALSE);
if (count($allActiveCampaigns) > count($campaigns)) {
$hasPastCampaigns = TRUE;
}
$hasCampaigns = FALSE;
if (!empty($campaigns)) {
$hasCampaigns = TRUE;
}
if ($hasPastCampaigns) {
$hasCampaigns = TRUE;
$form->add('hidden', 'included_past_campaigns');
}
$showAddCampaign = FALSE; $showAddCampaign = FALSE;
$alreadyIncludedPastCampaigns = FALSE;
if ($connectedCampaignId || ($isCampaignEnabled && $hasAccessCampaign)) { if ($connectedCampaignId || ($isCampaignEnabled && $hasAccessCampaign)) {
$showAddCampaign = TRUE; $showAddCampaign = TRUE;
//lets add past campaigns as options to quick-form element. $campaign = $form->addEntityRef('campaign_id', ts('Campaign'), [
if ($hasPastCampaigns && $form->getElementValue('included_past_campaigns')) { 'entity' => 'campaign',
$campaigns = $allActiveCampaigns; 'create' => TRUE,
$alreadyIncludedPastCampaigns = TRUE; ]);
}
$campaign = &$form->add('select',
'campaign_id',
ts('Campaign'),
array('' => ts('- select -')) + $campaigns,
FALSE,
array('class' => 'crm-select2')
);
//lets freeze when user does not has access or campaign is disabled. //lets freeze when user does not has access or campaign is disabled.
if (!$isCampaignEnabled || !$hasAccessCampaign) { if (!$isCampaignEnabled || !$hasAccessCampaign) {
$campaign->freeze(); $campaign->freeze();
} }
} }
$addCampaignURL = NULL;
if (empty($campaigns) && $hasAccessCampaign && $isCampaignEnabled) {
$addCampaignURL = CRM_Utils_System::url('civicrm/campaign/add', 'reset=1');
}
$includePastCampaignURL = NULL;
if ($hasPastCampaigns && $isCampaignEnabled && $hasAccessCampaign) {
$includePastCampaignURL = CRM_Utils_System::url('civicrm/ajax/rest',
'className=CRM_Campaign_Page_AJAX&fnName=allActiveCampaigns',
FALSE, NULL, FALSE
);
}
//carry this info to templates. //carry this info to templates.
$infoFields = array( $infoFields = array(
'hasCampaigns',
'addCampaignURL',
'showAddCampaign', 'showAddCampaign',
'hasPastCampaigns',
'hasAccessCampaign', 'hasAccessCampaign',
'isCampaignEnabled', 'isCampaignEnabled',
'includePastCampaignURL',
'alreadyIncludedPastCampaigns',
); );
foreach ($infoFields as $fld) { foreach ($infoFields as $fld) {
$campaignInfo[$fld] = $$fld; $campaignInfo[$fld] = $$fld;
......
...@@ -494,39 +494,6 @@ class CRM_Campaign_Page_AJAX { ...@@ -494,39 +494,6 @@ class CRM_Campaign_Page_AJAX {
CRM_Utils_JSON::output(array('status' => $status)); CRM_Utils_JSON::output(array('status' => $status));
} }
public function allActiveCampaigns() {
$currentCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns();
$campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, TRUE, FALSE, TRUE);
$options = array(
array(
'value' => '',
'title' => ts('- select -'),
),
);
foreach ($campaigns as $value => $title) {
$class = NULL;
if (!array_key_exists($value, $currentCampaigns)) {
$class = 'status-past';
}
$options[] = array(
'value' => $value,
'title' => $title,
'class' => $class,
);
}
$status = 'fail';
if (count($options) > 1) {
$status = 'success';
}
$results = array(
'status' => $status,
'campaigns' => $options,
);
CRM_Utils_JSON::output($results);
}
public function campaignGroups() { public function campaignGroups() {
$surveyId = CRM_Utils_Request::retrieve('survey_id', 'Positive', $surveyId = CRM_Utils_Request::retrieve('survey_id', 'Positive',
CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST' CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'
......
...@@ -2,82 +2,20 @@ ...@@ -2,82 +2,20 @@
{if $campaignContext eq 'componentSearch'} {if $campaignContext eq 'componentSearch'}
{* add campaign in component search *} {* add campaign in component search *}
<tr class="{$campaignTrClass}"> <tr class="{$campaignTrClass}">
{assign var=elementName value=$campaignInfo.elementName} {assign var=elementName value=$campaignInfo.elementName}
<td class="{$campaignTdClass}"> <td class="{$campaignTdClass}">
{$form.$elementName.label} {$form.$elementName.html} {$form.$elementName.label} {$form.$elementName.html}
</td> </td>
</tr> </tr>
{else} {elseif $campaignInfo.showAddCampaign}
{if $campaignInfo.showAddCampaign} <tr class="{$campaignTrClass}">
<td class="label">{$form.campaign_id.label} {help id="id-campaign_id" file="CRM/Campaign/Form/addCampaignToComponent.hlp"}</td>
<tr class="{$campaignTrClass}"> <td class="view-value">{$form.campaign_id.html}</td>
<td class="label">{$form.campaign_id.label} {help id="id-campaign_id" file="CRM/Campaign/Form/addCampaignToComponent.hlp"}</td> </tr>
<td class="view-value">
{* lets take a call, either show campaign select drop-down or show add campaign link *}
{if $campaignInfo.hasCampaigns}
{$form.campaign_id.html|crmAddClass:huge}
{* show for add and edit actions *}
{if ( $action eq 1 or $action eq 2 )
and !$campaignInfo.alreadyIncludedPastCampaigns and $campaignInfo.includePastCampaignURL}
<br />
<a id='include-past-campaigns' href='#' onClick='includePastCampaigns( "campaign_id" ); return false;'>
&raquo;
{ts}Show past campaign(s) in this select list.{/ts}
</a>
{/if}
{else}
<div class="status">
{ts}There are currently no active Campaigns.{/ts}
{if $campaignInfo.addCampaignURL}
{capture assign="link"}href="{$campaignInfo.addCampaignURL}" class="action-item"{/capture}
{ts 1=$link}If you want to associate this record with a campaign, you can <a %1>create a campaign here</a>.{/ts}
{/if} {help id="id-campaign_id" file="CRM/Campaign/Form/addCampaignToComponent.hlp"}
</div>
{/if}
</td>
</tr>
{literal}
<script type="text/javascript">
function includePastCampaigns()
{
//hide past campaign link.
cj( "#include-past-campaigns" ).hide( );
var campaignUrl = {/literal}'{$campaignInfo.includePastCampaignURL}'{literal};
cj.post( campaignUrl,
null,
function( data ) {
if ( data.status != 'success' ) return;
//first reset all select options.
cj( "#campaign_id" ).val( '' );
cj( "#campaign_id" ).html( '' );
cj('input[name="included_past_campaigns"]').val( 1 );
var campaigns = data.campaigns;
//build the new options.
for ( campaign in campaigns ) {
title = campaigns[campaign].title;
value = campaigns[campaign].value;
className = campaigns[campaign].class;
if ( !title ) continue;
cj('#campaign_id').append( cj('<option></option>').val(value).html(title).addClass(className) );
}
},
'json');
}
</script>
{/literal}
{/if}{* add campaign to component if closed. *}
{/if}{* add campaign to component search if closed. *} {/if}{* add campaign to component search if closed. *}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment