Skip to content

Commit

Permalink
Merge pull request #13491 from colemanw/campaign
Browse files Browse the repository at this point in the history
Convert Campaign widgets to entityRef
  • Loading branch information
colemanw authored Jan 25, 2019
2 parents e3cbde6 + df7b009 commit 9493687
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 203 deletions.
68 changes: 22 additions & 46 deletions CRM/Campaign/BAO/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,66 +596,24 @@ public static function addCampaign(&$form, $connectedCampaignId = NULL) {
$$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;
$alreadyIncludedPastCampaigns = FALSE;
if ($connectedCampaignId || ($isCampaignEnabled && $hasAccessCampaign)) {
$showAddCampaign = TRUE;
//lets add past campaigns as options to quick-form element.
if ($hasPastCampaigns && $form->getElementValue('included_past_campaigns')) {
$campaigns = $allActiveCampaigns;
$alreadyIncludedPastCampaigns = TRUE;
}
$campaign = &$form->add('select',
'campaign_id',
ts('Campaign'),
array('' => ts('- select -')) + $campaigns,
FALSE,
array('class' => 'crm-select2')
);
$campaign = $form->addEntityRef('campaign_id', ts('Campaign'), [
'entity' => 'campaign',
'create' => TRUE,
]);
//lets freeze when user does not has access or campaign is disabled.
if (!$isCampaignEnabled || !$hasAccessCampaign) {
$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.
$infoFields = array(
'hasCampaigns',
'addCampaignURL',
'showAddCampaign',
'hasPastCampaigns',
'hasAccessCampaign',
'isCampaignEnabled',
'includePastCampaignURL',
'alreadyIncludedPastCampaigns',
);
foreach ($infoFields as $fld) {
$campaignInfo[$fld] = $$fld;
Expand Down Expand Up @@ -707,4 +665,22 @@ public static function addCampaignInComponentSearch(&$form, $elementName = 'camp
$form->assign('campaignInfo', $campaignInfo);
}

/**
* Links to create new campaigns from entityRef widget
*
* @return array|bool
*/
public static function entityRefCreateLinks() {
if (CRM_Core_Permission::check([['administer CiviCampaign', 'manage campaign']])) {
return [
[
'label' => ts('New Campaign'),
'url' => CRM_Utils_System::url('civicrm/campaign/add', "reset=1",
NULL, NULL, FALSE, FALSE, TRUE),
'type' => 'Campaign',
]];
}
return FALSE;
}

}
40 changes: 23 additions & 17 deletions CRM/Campaign/Form/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,27 @@ public function buildQuickForm() {
// is this Campaign active
$this->addElement('checkbox', 'is_active', ts('Is Active?'));

$this->addButtons(array(
array(
'type' => 'upload',
'name' => ts('Save'),
'isDefault' => TRUE,
),
array(
'type' => 'upload',
'name' => ts('Save and New'),
'subName' => 'new',
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
)
);
$buttons = [
[
'type' => 'upload',
'name' => ts('Save'),
'isDefault' => TRUE,
],
];
// Skip this button when adding a new campaign from an entityRef
if (empty($_GET['snippet']) || empty($_GET['returnExtra'])) {
$buttons[] = [
'type' => 'upload',
'name' => ts('Save and New'),
'subName' => 'new',
];
}
$buttons[] = [
'type' => 'cancel',
'name' => ts('Cancel'),
];

$this->addButtons($buttons);
}

/**
Expand Down Expand Up @@ -340,6 +344,8 @@ public function postProcess() {
if ($result) {
CRM_Core_Session::setStatus(ts('Campaign %1 has been saved.', array(1 => $result->title)), ts('Saved'), 'success');
$session->pushUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=campaign'));
$this->ajaxResponse['id'] = $result->id;
$this->ajaxResponse['label'] = $result->title;
}

$buttonName = $this->controller->getButtonName();
Expand Down
7 changes: 4 additions & 3 deletions CRM/Campaign/Form/Petition.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ public function buildQuickForm() {
// script / instructions / description of petition purpose
$this->add('wysiwyg', 'instructions', ts('Introduction'), $attributes['instructions']);

// Campaign id
$campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values));
$this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns);
$this->addEntityRef('campaign_id', ts('Campaign'), [
'entity' => 'campaign',
'create' => TRUE,
]);

$customContactProfiles = CRM_Core_BAO_UFGroup::getProfiles(array('Individual'));
// custom group id
Expand Down
8 changes: 4 additions & 4 deletions CRM/Campaign/Form/Survey/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ public function setDefaultValues() {
public function buildQuickForm() {
$this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);

$surveyActivityTypes = CRM_Campaign_BAO_Survey::getSurveyActivityType();
// Activity Type id
$this->addSelect('activity_type_id', array('option_url' => 'civicrm/admin/campaign/surveyType'), TRUE);

// Campaign id
$campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values));
$this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns);
$this->addEntityRef('campaign_id', ts('Campaign'), [
'entity' => 'campaign',
'create' => TRUE,
]);

// script / instructions
$this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), array('rows' => 5, 'cols' => 40));
Expand Down
33 changes: 0 additions & 33 deletions CRM/Campaign/Page/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,39 +494,6 @@ public function processVoterData() {
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() {
$surveyId = CRM_Utils_Request::retrieve('survey_id', 'Positive',
CRM_Core_DAO::$_nullObject, FALSE, NULL, 'POST'
Expand Down
12 changes: 12 additions & 0 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3642,4 +3642,16 @@ public static function isFieldHasLocationType($fieldTitle) {
return FALSE;
}

/**
* Checks permission to create new contacts from entityRef widget
*
* Note: other components must return an array of links from this function,
* but Contacts are given special treatment - the links are in javascript already.
*
* @return bool
*/
public static function entityRefCreateLinks() {
return CRM_Core_Permission::check([['edit all contacts', 'add contacts']]);
}

}
6 changes: 1 addition & 5 deletions CRM/Contact/Form/Edit/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,7 @@ public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharin
$form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));

// Override the default profile links to add address form
$profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array(
'new_individual',
'new_organization',
'new_household',
), 'shared_address');
$profileLinks = CRM_Contact_BAO_Contact::entityRefCreateLinks() ? CRM_Core_BAO_UFGroup::getCreateLinks('', 'shared_address') : FALSE;
$form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks));
}
}
Expand Down
6 changes: 6 additions & 0 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3340,13 +3340,19 @@ public static function getCreateLinks($profiles = '', $appendProfiles = array())
));
$links = $append = array();
if (!empty($retrieved['values'])) {
$icons = [
'individual' => 'fa-user',
'organization' => 'fa-building',
'household' => 'fa-home',
];
foreach ($retrieved['values'] as $id => $profile) {
if (in_array($profile['name'], $profiles)) {
$links[] = array(
'label' => $profile['title'],
'url' => CRM_Utils_System::url('civicrm/profile/create', "reset=1&context=dialog&gid=$id",
NULL, NULL, FALSE, FALSE, TRUE),
'type' => ucfirst(str_replace('new_', '', $profile['name'])),
'icon' => CRM_Utils_Array::value(str_replace('new_', '', $profile['name']), $icons),
);
}
else {
Expand Down
7 changes: 6 additions & 1 deletion CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1951,7 +1951,12 @@ public function addEntityRef($name, $label = '', $props = array(), $required = F
$props['entity'] = _civicrm_api_get_entity_name_from_camel(CRM_Utils_Array::value('entity', $props, 'contact'));
$props['class'] = ltrim(CRM_Utils_Array::value('class', $props, '') . ' crm-form-entityref');

if ($props['entity'] == 'contact' && isset($props['create']) && !(CRM_Core_Permission::check('edit all contacts') || CRM_Core_Permission::check('add contacts'))) {
if (isset($props['create']) && $props['create'] === TRUE) {
require_once "api/v3/utils.php";
$baoClass = _civicrm_api3_get_BAO($props['entity']);
$props['create'] = $baoClass && is_callable([$baoClass, 'entityRefCreateLinks']) ? $baoClass::entityRefCreateLinks() : FALSE;
}
if (array_key_exists('create', $props) && empty($props['create'])) {
unset($props['create']);
}

Expand Down
35 changes: 35 additions & 0 deletions CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,41 @@ public static function getEntityRefFilters() {
}
}

if (in_array('CiviCampaign', $config->enableComponents)) {
$filters['campaign'] = [
['key' => 'campaign_type_id', 'value' => ts('Campaign Type')],
['key' => 'status_id', 'value' => ts('Status')],
[
'key' => 'start_date',
'value' => ts('Start Date'),
'options' => [
['key' => '{">":"now"}', 'value' => ts('Upcoming')],
[
'key' => '{"BETWEEN":["now - 3 month","now"]}',
'value' => ts('Past 3 Months'),
],
[
'key' => '{"BETWEEN":["now - 6 month","now"]}',
'value' => ts('Past 6 Months'),
],
[
'key' => '{"BETWEEN":["now - 1 year","now"]}',
'value' => ts('Past Year'),
],
],
],
[
'key' => 'end_date',
'value' => ts('End Date'),
'options' => [
['key' => '{">":"now"}', 'value' => ts('In the future')],
['key' => '{"<":"now"}', 'value' => ts('In the past')],
['key' => '{"IS NULL":"1"}', 'value' => ts('Not set')],
],
],
];
}

CRM_Utils_Hook::entityRefFilters($filters);

return $filters;
Expand Down
57 changes: 57 additions & 0 deletions api/v3/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,60 @@ function civicrm_api3_campaign_get($params) {
function civicrm_api3_campaign_delete($params) {
return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}

/**
* Get campaign list parameters.
*
* @see _civicrm_api3_generic_getlist_params
*
* @param array $request
*/
function _civicrm_api3_campaign_getlist_params(&$request) {
$fieldsToReturn = ['title', 'campaign_type_id', 'status_id', 'start_date', 'end_date'];
$request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
if (empty($request['params']['id'])) {
$request['params'] += [
'is_active' => 1,
];
}
}

/**
* Get campaign list output.
*
* @see _civicrm_api3_generic_getlist_output
*
* @param array $result
* @param array $request
*
* @return array
*/
function _civicrm_api3_campaign_getlist_output($result, $request) {
$output = [];
if (!empty($result['values'])) {
$config = CRM_Core_Config::singleton();
foreach ($result['values'] as $row) {
$data = [
'id' => $row[$request['id_field']],
'label' => $row[$request['label_field']],
'description' => [
CRM_Core_Pseudoconstant::getLabel('CRM_Campaign_BAO_Campaign', 'campaign_type_id', $row['campaign_type_id']),
],
];
if (!empty($row['status_id'])) {
$data['description'][0] .= ': ' . CRM_Core_Pseudoconstant::getLabel('CRM_Campaign_BAO_Campaign', 'status_id', $row['status_id']);
}
$dateString = CRM_Utils_Date::customFormat($row['start_date'], $config->dateformatFull) . ' -';
if (!empty($row['end_date'])) {
// Remove redundant years
if (substr($row['start_date'], 0, 4) == substr($row['end_date'], 0, 4)) {
$dateString = preg_replace('/[, ]*' . substr($row['start_date'], 0, 4) . '/', '', $dateString);
}
$dateString .= ' ' . CRM_Utils_Date::customFormat($row['end_date'], $config->dateformatFull);
}
$data['description'][] = $dateString;
$output[] = $data;
}
}
return $output;
}
Loading

0 comments on commit 9493687

Please sign in to comment.