Skip to content

Commit

Permalink
Merge pull request #11880 from eileenmcnaughton/min_report
Browse files Browse the repository at this point in the history
 CRM-21677 reduce unnecessary joins in Case, Grant, Survey reports
  • Loading branch information
eileenmcnaughton authored Mar 28, 2018
2 parents 7aed41f + 6d81d64 commit 1f1d0ed
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 106 deletions.
5 changes: 4 additions & 1 deletion CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4374,7 +4374,10 @@ protected function joinAddressFromContact($prefix = '', $extra = array()) {
*/
protected function joinCountryFromAddress($prefix = '', $extra = array()) {
// include country field if country column is to be included
if ($this->isTableSelected($prefix . 'civicrm_country')) {
if ($this->isTableSelected($prefix . 'civicrm_country') || $this->isTableSelected($prefix . 'civicrm_worldregion')) {
if (empty($this->_aliases[$prefix . 'civicrm_country'])) {
$this->_aliases[$prefix . 'civicrm_country'] = $prefix . '_report_country';
}
$this->_from .= "
LEFT JOIN civicrm_country {$this->_aliases[$prefix . 'civicrm_country']}
ON {$this->_aliases[$prefix . 'civicrm_address']}.country_id = {$this->_aliases[$prefix . 'civicrm_country']}.id AND
Expand Down
16 changes: 3 additions & 13 deletions CRM/Report/Form/Campaign/SurveyDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,9 @@ public function from() {
$this->_from .= " INNER JOIN civicrm_activity_contact activity_contact_civireport ON
( {$this->_aliases['civicrm_activity']}.id = activity_contact_civireport.activity_id AND activity_contact_civireport.record_type_id = {$assigneeID} )\n";

//get the address table.
$this->_from .= " LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
{$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";

if ($this->_emailField) {
$this->_from .= "LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
{$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
}

if ($this->_phoneField) {
$this->_from .= "LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} ON
{$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
}
$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();

if ($this->_locationBasedPhoneField) {
foreach ($this->_surveyResponseFields as $key => $value) {
Expand Down
14 changes: 2 additions & 12 deletions CRM/Report/Form/Case/Demographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,9 @@ public function from() {
}
}

if ($this->_emailField) {
$this->_from .= "
LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
{$this->_aliases['civicrm_email']}.is_primary = 1) ";
}
$this->joinPhoneFromContact();
$this->joinEmailFromContact();

if ($this->_phoneField) {
$this->_from .= "
LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
{$this->_aliases['civicrm_phone']}.is_primary = 1 ";
}
}

public function where() {
Expand Down
50 changes: 7 additions & 43 deletions CRM/Report/Form/Case/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ class CRM_Report_Form_Case_Detail extends CRM_Report_Form {

protected $_relField = FALSE;

protected $_addressField = FALSE;

protected $_emailField = FALSE;

protected $_phoneField = FALSE;

protected $_worldRegionField = FALSE;
protected $_addressField = TRUE;

protected $_activityLast = FALSE;

Expand Down Expand Up @@ -344,19 +338,10 @@ public function select() {
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('fields', $table)) {
foreach ($table['fields'] as $fieldName => $field) {
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
}
if (!empty($field['required']) ||
!empty($this->_params['fields'][$fieldName])
) {
if ($tableName == 'civicrm_email') {
$this->_emailField = TRUE;
}
elseif ($tableName == 'civicrm_phone') {
$this->_phoneField = TRUE;
}
elseif ($tableName == 'civicrm_relationship') {
if ($tableName == 'civicrm_relationship') {
$this->_relField = TRUE;
}
if ($fieldName == 'sort_name') {
Expand Down Expand Up @@ -414,25 +399,11 @@ public function from() {
";
}

if ($this->_addressField) {
$this->_from .= "
LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
ON {$conact}.id = {$this->_aliases['civicrm_address']}.contact_id AND
{$this->_aliases['civicrm_address']}.is_primary = 1 ";
}
if ($this->_emailField) {
$this->_from .= "
LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
ON {$conact}.id = {$this->_aliases['civicrm_email']}.contact_id AND
{$this->_aliases['civicrm_email']}.is_primary = 1 ";
}
if ($this->_phoneField) {
$this->_from .= "
LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
ON ( {$conact}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
{$this->_aliases['civicrm_phone']}.is_primary = 1) ";
}
if ($this->_worldRegionField) {
$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();

if ($this->isTableSelected('civicrm_worldregion')) {
$this->_from .= "
LEFT JOIN civicrm_country {$this->_aliases['civicrm_country']}
ON {$this->_aliases['civicrm_country']}.id ={$this->_aliases['civicrm_address']}.country_id
Expand Down Expand Up @@ -601,13 +572,6 @@ public function postProcess() {
}

public function checkEnabledFields() {
if (isset($this->_params['worldregion_id_value']) &&
!empty($this->_params['worldregion_id_value'])
) {
$this->_addressField = TRUE;
$this->_worldRegionField = TRUE;
}

if (isset($this->_params['case_role_value'])
&& !empty($this->_params['case_role_value'])
) {
Expand Down
16 changes: 4 additions & 12 deletions CRM/Report/Form/Grant/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
*/
class CRM_Report_Form_Grant_Detail extends CRM_Report_Form {

protected $_addressField = FALSE;

protected $_customGroupExtends = array(
'Grant',
);
Expand Down Expand Up @@ -212,13 +210,11 @@ public function __construct() {
}

public function select() {
// @todo remove this override - seems to do nothing parent doesn't.
$select = array();

$this->_columnHeaders = array();
foreach ($this->_columns as $tableName => $table) {
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
}
if (array_key_exists('fields', $table)) {
foreach ($table['fields'] as $fieldName => $field) {
if (!empty($field['required']) ||
Expand All @@ -242,16 +238,11 @@ public function from() {
FROM civicrm_grant {$this->_aliases['civicrm_grant']}
LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
ON ({$this->_aliases['civicrm_grant']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) ";
if ($this->_addressField) {
$this->_from .= "
LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
ON {$this->_aliases['civicrm_contact']}.id =
{$this->_aliases['civicrm_address']}.contact_id AND
{$this->_aliases['civicrm_address']}.is_primary = 1\n";
}
$this->joinAddressFromContact();
}

public function where() {
// @todo this function appears to do nothing more than parent, test & remove
$clauses = array();
$this->_where = '';
foreach ($this->_columns as $tableName => $table) {
Expand Down Expand Up @@ -291,6 +282,7 @@ public function where() {
}

public function groupBy() {
// @todo this function appears to do nothing more than parent, test & remove
$this->_groupBy = "";
if (!empty($this->_params['group_bys']) &&
is_array($this->_params['group_bys']) &&
Expand Down
37 changes: 12 additions & 25 deletions CRM/Report/Form/Grant/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
*/
class CRM_Report_Form_Grant_Statistics extends CRM_Report_Form {

protected $_addressField = FALSE;

protected $_customGroupExtends = array('Grant');

protected $_add2groupSupported = FALSE;
Expand Down Expand Up @@ -163,7 +161,7 @@ public function __construct() {
),
'grouping' => 'contact-fields',
),
'civicrm_world_region' => array(
'civicrm_worldregion' => array(
'dao' => 'CRM_Core_DAO_Worldregion',
'fields' => array(
'id' => array(
Expand Down Expand Up @@ -210,13 +208,6 @@ public function select() {

$this->_columnHeaders = array();
foreach ($this->_columns as $tableName => $table) {
if (in_array($tableName, array(
'civicrm_address',
'civicrm_world_region',
))) {
$this->_addressField = TRUE;
}

if (array_key_exists('fields', $table)) {
foreach ($table['fields'] as $fieldName => $field) {
if (!empty($field['required']) ||
Expand All @@ -241,18 +232,14 @@ public function from() {
FROM civicrm_grant {$this->_aliases['civicrm_grant']}
LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
ON ({$this->_aliases['civicrm_grant']}.contact_id = {$this->_aliases['civicrm_contact']}.id ) ";
if ($this->_addressField) {

$this->joinAddressFromContact();
$this->joinCountryFromAddress();
if ($this->isTableSelected('civicrm_worldregion')) {
$this->_from .= "
LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
ON {$this->_aliases['civicrm_contact']}.id =
{$this->_aliases['civicrm_address']}.contact_id AND
{$this->_aliases['civicrm_address']}.is_primary = 1\n
LEFT JOIN civicrm_country country
ON {$this->_aliases['civicrm_address']}.country_id =
country.id
LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_world_region']}
ON country.region_id =
{$this->_aliases['civicrm_world_region']}.id";
LEFT JOIN civicrm_worldregion {$this->_aliases['civicrm_worldregion']}
ON {$this->_aliases['civicrm_country']}.region_id =
{$this->_aliases['civicrm_worldregion']}.id";
}
}

Expand Down Expand Up @@ -416,11 +403,11 @@ public function alterDisplay(&$rows) {
);
}

if (array_key_exists('civicrm_world_region_name', $values)) {
$region = CRM_Utils_Array::value('civicrm_world_region_name', $values);
if (array_key_exists('civicrm_worldregion_name', $values)) {
$region = CRM_Utils_Array::value('civicrm_worldregion_name', $values);
$region = ($region) ? $region : 'Unassigned';
$grantStatistics['civicrm_world_region_name']['title'] = ts('By Region');
self::getStatistics($grantStatistics['civicrm_world_region_name'], $region, $values,
$grantStatistics['civicrm_worldregion_name']['title'] = ts('By Region');
self::getStatistics($grantStatistics['civicrm_worldregion_name'], $region, $values,
$awardedGrants, $awardedGrantsAmount
);
}
Expand Down

0 comments on commit 1f1d0ed

Please sign in to comment.