CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviReport (Moderator: Dave Greenberg) »
  • Custom data for 'second party' in a report
Pages: [1]

Author Topic: Custom data for 'second party' in a report  (Read 697 times)

nickholden

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 1
  • CiviCRM version: 4.4.1
  • CMS version: Drupal 7
  • MySQL version: 5.5.32
  • PHP version: 5.4
Custom data for 'second party' in a report
March 08, 2013, 07:55:49 am
I'm working on a report related to CiviCase and relationships. It's almost working as desired, but there's a snag.

I have the report working so that the output shows all the contacts who have a case of a particular type, and also (optionally) their relationships (if any) of a specified type and also the contact at the other end of the relationship (by having a second entry in the $this->_columns array using the CRM_Contact_DAO_Contact dao.

That's great, but now we want to extend the report to also show some custom data, recorded not against the subject of the case, but against the relation (i.e. the second contact).

I have $_customGroupExtends = array('Individual', 'Case', 'Organization', 'Address'); and that results in the report template showing all the options, but of course the final report is blank for the desired column, because the data being retrieved is for the subject of the report, who doesn't have this custom data.

So how can I obtain second-hand custom data? Is it possible to have a second set of custom data obtained, keyed from the second contact id?

nickholden

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 1
  • CiviCRM version: 4.4.1
  • CMS version: Drupal 7
  • MySQL version: 5.5.32
  • PHP version: 5.4
Re: Custom data for 'second party' in a report
March 11, 2013, 11:28:34 am
I wanted to find a way to deliver this without editing any of the core code, just to add code to the customised report template itself. I've come up with a way of doing this, but I want to check that it doesn't do any serious damage to the logic of CiviCRM.

The process, as I understand it, is that custom data is added to the $this->_columns array by addCustomDataToColumns() from the CRM/Report/Form.php, which happens when form is built (triggered by the CRM_Report_Form __construct() function, itself triggered by the __construct() function in the specific class created by the report template). Later customDataFrom() turns the columns into a SQL 'from' clause, and this happens when the form is submitted, triggered by buildQuery() from the specific class for the report template.

What I've done is add a few lines in the postProcess() function within my report, before the call to $this->buildQuery() so that the column for the relevant item of custom data is re-defined as extending not the default 'civicrm_contact' table but an aliased version of the table which supplies the identity of the related contact, which I'm calling 'civicrm_relation'. This aliased version has already been defined in $this->_columns and has an appropriate left join already written in to the $this->_from string.

Code: [Select]
  function postProcess() {

    $this->beginPostProcess();

    $this->checkEnabledFields();

// Over-ride the custom data 'extends' relationship so that the GP surgery data is referenced to the 'relation' not the 'contact'
    if ($this->_columns['civicrm_value_gp_surgery_data_13']['extends'] == "Organization") {
      $this->_columns['civicrm_value_gp_surgery_data_13']['extends'] = "Relation";
      CRM_Core_BAO_CustomQuery::$extendsMap['Relation'] = 'civicrm_relation';
    }
   
    $this->buildQuery(TRUE);
... and so on

If the user asks the report to include custom data from the gp_surgery_data_13 table, such data only makes sense in the context of the related surgery, not the patient themselves. So this edit seems safe to me, in the context of this specific report, but it's an approach which wouldn't work if the same custom data might be needed relative to both the subject of the report and the related contact. But that's a stretch further than our use case.

So, does this look like a suitable solution? Is it useful? Dangerous?

Deepak Srivastava

  • Ask me questions
  • ****
  • Posts: 677
  • Karma: 65
Re: Custom data for 'second party' in a report
March 11, 2013, 10:28:35 pm
You seem to be on right track.

So before your fix, the final query would be something like :

Code: [Select]
....
LEFT JOIN civicrm_value_gp_surgery_data_13 surgery_data_13_civireport ON  surgery_data_13_civireport.entity_id = civicrm_contact.id
....

where civicrm_contact.id points to first contact. And after your change query would be something like:

Code: [Select]
...
LEFT JOIN civicrm_value_gp_surgery_data_13 surgery_data_13_civireport ON  surgery_data_13_civireport.entity_id = civicrm_relation.id
...

where i assume civicrm_relation.id is pointing to (alias of) second contact ? If yes, i think it's a nice trick, and safe, as long as it only applies to custom data extending contacts, which your fix seems to be aware of.
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

nickholden

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 1
  • CiviCRM version: 4.4.1
  • CMS version: Drupal 7
  • MySQL version: 5.5.32
  • PHP version: 5.4
Re: Custom data for 'second party' in a report
March 12, 2013, 06:48:57 am
Thanks, Deepak, and yes, that's exactly what's changing in the SQL.  :)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Custom data for 'second party' in a report
March 18, 2013, 11:34:23 am
As an aside I did a couple of reports (e.g. on relationship) which you can choose custom data from both sides of the relationship. They are in the extended report module.

I think I did maybe activity as well? But not quite sure
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviReport (Moderator: Dave Greenberg) »
  • Custom data for 'second party' in a report

This forum was archived on 2017-11-26.