Skip to content

Commit

Permalink
CRM-21677 - Report improvements
Browse files Browse the repository at this point in the history
CRM-21677 - fix DB error and more clean up

CRM-21677 - generalised function

minor fix

CRM-21677 - keep the naming consistent

style fix
  • Loading branch information
yashodha authored and eileenmcnaughton committed Mar 28, 2018
1 parent 6d81d64 commit a8fa004
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 201 deletions.
9 changes: 9 additions & 0 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4304,8 +4304,12 @@ public function fiscalYearOffset($fieldName) {

/**
* Add Address into From Table if required.
*
* @deprecated use joinAddressFromContact
* (left here in case extensions use it).
*/
public function addAddressFromClause() {
Civi::log()->warning('Deprecated function addAddressFromClause. Use joinAddressFromContact.', array('civi.tag' => 'deprecated'));
// include address field if address column is to be included
if ((isset($this->_addressField) &&
$this->_addressField
Expand All @@ -4322,8 +4326,13 @@ public function addAddressFromClause() {

/**
* Add Phone into From Table if required.
*
* @deprecated use joinPhoneFromContact
* (left here in case extensions use it).
*/
public function addPhoneFromClause() {

Civi::log()->warning('Deprecated function addPhoneFromClause. Use joinPhoneFromContact.', array('civi.tag' => 'deprecated'));
// include address field if address column is to be included
if ($this->isTableSelected('civicrm_phone')) {
$this->_from .= "
Expand Down
16 changes: 5 additions & 11 deletions CRM/Report/Form/Contact/CurrentEmployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,11 @@ public function from() {
LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
AND {$this->_aliases['civicrm_relationship']}.relationship_type_id={$relType})
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 )
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)
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) ";
AND {$this->_aliases['civicrm_relationship']}.relationship_type_id={$relType}) ";

$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();
}

public function where() {
Expand Down
3 changes: 0 additions & 3 deletions CRM/Report/Form/Contribute/Bookkeeping.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form {
protected $_addressField = FALSE;

protected $_emailField = FALSE;

protected $_summary = NULL;

Expand Down
15 changes: 3 additions & 12 deletions CRM/Report/Form/Contribute/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Report_Form_Contribute_Detail extends CRM_Report_Form {
protected $_addressField = FALSE;

protected $_emailField = FALSE;

protected $_summary = NULL;

Expand Down Expand Up @@ -941,16 +938,10 @@ public function appendAdditionalFromJoins() {
INNER JOIN (SELECT c.id, IF(COUNT(oc.id) = 0, 0, 1) AS ordinality FROM civicrm_contribution c LEFT JOIN civicrm_contribution oc ON c.contact_id = oc.contact_id AND oc.receive_date < c.receive_date GROUP BY c.id) {$this->_aliases['civicrm_contribution_ordinality']}
ON {$this->_aliases['civicrm_contribution_ordinality']}.id = {$this->_aliases['civicrm_contribution']}.id";
}
$this->addPhoneFromClause();

$this->addAddressFromClause();
$this->joinPhoneFromContact();
$this->joinAddressFromContact();
$this->joinEmailFromContact();

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";
}
// include contribution note
if (!empty($this->_params['fields']['contribution_note']) ||
!empty($this->_params['note_value'])
Expand Down
31 changes: 4 additions & 27 deletions CRM/Report/Form/Contribute/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,10 @@
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
* $Id$
*
*/
class CRM_Report_Form_Contribute_History extends CRM_Report_Form {
// Primary Contacts count limitCONSTROW_COUNT_LIMIT = 10;

protected $_addressField = FALSE;
protected $_emailField = FALSE;
protected $_phoneField = FALSE;
protected $_relationshipColumns = array();

protected $_customGroupExtends = array(
Expand Down Expand Up @@ -327,6 +322,7 @@ public function preProcess() {

public function select() {
$select = array();
// @todo remove this & use parent (with maybe some override in this or better yet selectWhere fn)
$this->_columnHeaders = array();

foreach ($this->_columns as $tableName => $table) {
Expand All @@ -336,15 +332,6 @@ public function select() {
if (!empty($field['required']) ||
!empty($this->_params['fields'][$fieldName])
) {
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
}
if ($tableName == 'civicrm_email') {
$this->_emailField = TRUE;
}
if ($tableName == 'civicrm_phone') {
$this->_phoneField = TRUE;
}
if ($tableName == 'civicrm_relationship') {
$this->_relationshipColumns["{$tableName}_{$fieldName}"] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
$this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
Expand Down Expand Up @@ -398,23 +385,13 @@ public function from() {
ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_contribution']}.contact_id AND
{$this->_aliases['civicrm_contribution']}.is_test = 0 ";

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) ";
}

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) ";
}

$relContacAlias = 'contact_relationship';
$this->_relationshipFrom = " INNER JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
ON (({$this->_aliases['civicrm_relationship']}.contact_id_a = {$relContacAlias}.id OR {$this->_aliases['civicrm_relationship']}.contact_id_b = {$relContacAlias}.id ) AND {$this->_aliases['civicrm_relationship']}.is_active = 1) ";

$this->addAddressFromClause();
$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();

// for credit card type
$this->addFinancialTrxnFromClause();
Expand Down
33 changes: 3 additions & 30 deletions CRM/Report/Form/Contribute/HouseholdSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
* $Id$
*
*/
class CRM_Report_Form_Contribute_HouseholdSummary extends CRM_Report_Form {

protected $_addressField = FALSE;

protected $_emailField = FALSE;

public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');

protected $_summary = NULL;
Expand Down Expand Up @@ -232,24 +226,15 @@ public function __construct() {
parent::__construct();
}

public function preProcess() {
parent::preProcess();
}

public function select() {
// @todo remove this & use parent select.
$this->_columnHeaders = $select = array();
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('fields', $table)) {
foreach ($table['fields'] as $fieldName => $field) {
if (!empty($field['required']) ||
!empty($this->_params['fields'][$fieldName])
) {
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
}
elseif ($tableName == 'civicrm_email') {
$this->_emailField = TRUE;
}

if (!empty($field['statistics'])) {
foreach ($field['statistics'] as $stat => $label) {
Expand All @@ -274,8 +259,6 @@ public function select() {
}

public function from() {

$this->_from = NULL;
$this->_from = "
FROM civicrm_relationship {$this->_aliases['civicrm_relationship']}
LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact_household']} ON
Expand All @@ -286,18 +269,8 @@ public function from() {
INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";

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 ";
}
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 ";
}
$this->joinAddressFromContact();
$this->joinEmailFromContact();

// for credit card type
$this->addFinancialTrxnFromClause();
Expand Down
17 changes: 4 additions & 13 deletions CRM/Report/Form/Contribute/Lybunt.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,10 @@ public function from() {
AND {$this->_aliases['civicrm_contribution']}.is_test = 0
INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
ON restricted_contacts.cid = {$this->_aliases['civicrm_contact']}.id";
if ($this->isTableSelected('civicrm_email')) {
$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";
}
if ($this->isTableSelected('civicrm_phone')) {
$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";
}
$this->addAddressFromClause();

$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();
}
else {
$this->setFromBase('civicrm_contact');
Expand Down
25 changes: 3 additions & 22 deletions CRM/Report/Form/Contribute/OrganizationSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
*/
class CRM_Report_Form_Contribute_OrganizationSummary extends CRM_Report_Form {

protected $_addressField = FALSE;

protected $_emailField = FALSE;

public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');

protected $_summary = NULL;
Expand Down Expand Up @@ -238,17 +234,12 @@ public function preProcess() {
}

public function select() {
// @todo remove this in favour of using parent function
$this->_columnHeaders = $select = array();
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('fields', $table)) {
foreach ($table['fields'] as $fieldName => $field) {
if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
}
elseif ($tableName == 'civicrm_email') {
$this->_emailField = TRUE;
}

if (!empty($field['statistics'])) {
foreach ($field['statistics'] as $stat => $label) {
Expand Down Expand Up @@ -285,18 +276,8 @@ public function from() {
INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']} ON
({$this->_aliases['civicrm_contribution']}.contact_id = {$this->_aliases['civicrm_relationship']}.$this->otherContact ) AND {$this->_aliases['civicrm_contribution']}.is_test = 0 ";

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 ";
}
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 ";
}
$this->joinAddressFromContact();
$this->joinEmailFromContact();

// for credit card type
$this->addFinancialTrxnFromClause();
Expand Down
11 changes: 3 additions & 8 deletions CRM/Report/Form/Contribute/Recur.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,9 @@ public function from() {
$this->_from .= "
LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
ON {$this->_aliases['civicrm_contribution_recur']}.id = {$this->_aliases['civicrm_contribution']}.contribution_recur_id";
$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->_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)";

$this->joinPhoneFromContact();
$this->joinEmailFromContact();

// for credit card type
$this->addFinancialTrxnFromClause();
Expand Down
7 changes: 1 addition & 6 deletions CRM/Report/Form/Contribute/RecurSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ public function setDefaultValues($freeze = TRUE) {
}

public function select() {
// @todo remove & only adjust parent with selectWhere fn (if needed)
$select = array();
$this->_columnHeaders = array();
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('group_bys', $table)) {
foreach ($table['group_bys'] as $fieldName => $field) {
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
}
if (!empty($this->_params['group_bys'][$fieldName])) {
switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
case 'YEARWEEK':
Expand Down Expand Up @@ -150,9 +148,6 @@ public function select() {

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])
) {
Expand Down
18 changes: 4 additions & 14 deletions CRM/Report/Form/Contribute/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Report_Form_Contribute_Summary extends CRM_Report_Form {
protected $_addressField = FALSE;

protected $_charts = array(
'' => 'Tabular',
Expand Down Expand Up @@ -344,9 +343,6 @@ public function select() {
foreach ($this->_columns as $tableName => $table) {
if (array_key_exists('group_bys', $table)) {
foreach ($table['group_bys'] as $fieldName => $field) {
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
}
if (!empty($this->_params['group_bys'][$fieldName])) {
switch (CRM_Utils_Array::value($fieldName, $this->_params['group_bys_freq'])) {
case 'YEARWEEK':
Expand Down Expand Up @@ -411,9 +407,6 @@ public function select() {

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])
) {
Expand Down Expand Up @@ -515,15 +508,12 @@ public function from($entity = NULL) {
{$softCreditJoin}
LEFT JOIN civicrm_financial_type {$this->_aliases['civicrm_financial_type']}
ON {$this->_aliases['civicrm_contribution']}.financial_type_id ={$this->_aliases['civicrm_financial_type']}.id
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)
";

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)";
$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();

$this->addAddressFromClause();
//for contribution batches
if ($this->isTableSelected('civicrm_batch')) {
$this->_from .= "
Expand Down
Loading

0 comments on commit a8fa004

Please sign in to comment.