Skip to content

Commit

Permalink
Merge pull request #433 from GinkgoFJG/VOL-284
Browse files Browse the repository at this point in the history
VOL-284: Use entityRef widgets for activity contact filters on the volunteer report
  • Loading branch information
ginkgomzd committed Jan 19, 2017
2 parents 3d626ef + aff74e7 commit 892bfb3
Showing 1 changed file with 78 additions and 17 deletions.
95 changes: 78 additions & 17 deletions CRM/Volunteer/Form/VolunteerReport.php
Expand Up @@ -86,25 +86,34 @@ function __construct() {
),
'filters' => array(
'contact_assignee' => array(
'name' => 'sort_name',
'name' => 'id',
'alias' => 'civicrm_contact_assignee_civireport',
'title' => ts('Volunteer Name', array('domain' => 'org.civicrm.volunteer')),
'operator' => 'like',
'type' => CRM_Report_Form::OP_STRING,
'title' => ts('Volunteer (assignee contact)', array('domain' => 'org.civicrm.volunteer')),
'operatorType' => CRM_Report_Form::OP_ENTITYREF,
'type' => CRM_Utils_Type::T_INT,
'attributes' => array(
'select' => array('minimumInputLength' => 0),
),
),
'contact_source' => array(
'name' => 'sort_name',
'name' => 'id',
'alias' => 'civicrm_contact_source',
'title' => ts('Source Contact Name', array('domain' => 'org.civicrm.volunteer')),
'operator' => 'like',
'type' => CRM_Report_Form::OP_STRING,
'title' => ts('Assigner (source contact)', array('domain' => 'org.civicrm.volunteer')),
'operatorType' => CRM_Report_Form::OP_ENTITYREF,
'type' => CRM_Utils_Type::T_INT,
'attributes' => array(
'select' => array('minimumInputLength' => 0),
),
),
'contact_target' => array(
'name' => 'sort_name',
'name' => 'id',
'alias' => 'contact_civireport',
'title' => ts('Target Contact Name', array('domain' => 'org.civicrm.volunteer')),
'operator' => 'like',
'type' => CRM_Report_Form::OP_STRING,
'title' => ts('Beneficiary (target contact)', array('domain' => 'org.civicrm.volunteer')),
'operatorType' => CRM_Report_Form::OP_ENTITYREF,
'type' => CRM_Utils_Type::T_INT,
'attributes' => array(
'select' => array('minimumInputLength' => 0),
),
),
'current_user' => array(
'name' => 'current_user',
Expand Down Expand Up @@ -296,7 +305,18 @@ function __construct() {
),
);

// forgive me for this terrible hack
$this->addPhoneFields();

$this->_groupFilter = TRUE;
$this->_tagFilter = TRUE;
parent::__construct();
}

/**
* Loops through the various activity contacts and phone types and builds a
* column for each phone type per contact.
*/
protected function addPhoneFields() {
$phoneTypes = CRM_Core_BAO_Phone::buildOptions('phone_type_id');
$activityRoles = array(
'assignee' => array(
Expand All @@ -316,6 +336,7 @@ function __construct() {
),
);

// a table for each contact related to the activity
foreach ($activityRoles as $roleKey => $role) {
$table = "phone_{$roleKey}";
$this->_columns[$table] = array(
Expand All @@ -325,6 +346,7 @@ function __construct() {
'fields' => array(),
);

// a field for each type of phone number
foreach ($phoneTypes as $phoneKey => $label) {
$this->_columns[$table]['fields']["{$table}_type{$phoneKey}"] = array(
'alias' => "{$table}_civireport_type{$phoneKey}",
Expand All @@ -333,10 +355,6 @@ function __construct() {
);
}
}

$this->_groupFilter = TRUE;
$this->_tagFilter = TRUE;
parent::__construct();
}

function select() {
Expand Down Expand Up @@ -614,6 +632,49 @@ function buildACLClause($tableAlias = 'contact_a') {
$this->_aclWhere = NULL;
}

function preProcessCommon() {
parent::preProcessCommon();
$this->handleLegacyContactParams();
}

/**
* Tries to adapt legacy contact filters and prompts the user to update the report.
*
* Previously the activity contact filters compared strings against contacts'
* sort names. Now the report uses autocompletes and stores a string of
* comma-separated contact IDs.
*/
protected function handleLegacyContactParams() {
$updatedFilters = array('contact_assignee', 'contact_source', 'contact_target');

$msgs = array();
foreach ($updatedFilters as $column) {
$formKey = $column . '_value';
$value = CRM_Utils_Array::value($formKey, $this->_formValues);

// bail out if nothing was retrieved or if value matches the new storage format
if (!$value || preg_match('#^\d+(,\d+)*$#', $value)) {
continue;
}

$api = civicrm_api3('Contact', 'get', array(
'sort_name' => $value,
));
$this->_formValues[$formKey] = implode(',', array_keys($api['values']));
$this->_formValues[$column . '_op'] = 'in';
$msgs[$column] = '<li><strong>' . $this->_columns['civicrm_contact']['filters'][$column]['title'] . '</strong><br />' .
ts('stored value:', array('domain' => 'org.civicrm.volunteer')) .
' <em>' . $value . '</em></li>';
}

if (count($msgs)) {
$msg = '<p>' . ts('This report template has been updated to enable contact autocomplete on the "Filters" tab. Please review the following filters and re-save the report to ensure there are no unexpected results:', array('domain' => 'org.civicrm.volunteer')) . '</p><ul>';
$msg .= implode('', $msgs);
$msg .= '</ul>';
CRM_Core_Session::setStatus($msg, ts('Time to Update Your Report!', array('domain' => 'org.civicrm.volunteer')), 'alert', array('expires' => 0));
}
}

function postProcess() {
$this->buildACLClause(array('civicrm_contact_source', 'contact_civireport', 'civicrm_contact_assignee'));
parent::postProcess();
Expand Down

0 comments on commit 892bfb3

Please sign in to comment.