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) »
  • Group as a column on custom report
Pages: [1]

Author Topic: Group as a column on custom report  (Read 1213 times)

mcarson

  • I post occasionally
  • **
  • Posts: 110
  • Karma: 5
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.x
  • MySQL version: 5.5
  • PHP version: 5.4.22
Group as a column on custom report
June 04, 2011, 05:45:17 pm
I did a search and found the post at http://forum.civicrm.org/index.php/topic,18502.msg76886.html#msg76886 which was a great help in getting a group column of such on a report. I didn't want to re-open an old thread.

How can I include the Group Column in a custom report (based on CRM_Report_Form_Contact_Summary)  that is filtered by a number of groups?

The challenge is to create a report template that can list contacts grouped by groups ::) they are added to. For example:

Code: [Select]
------------------------------------------------------
| Group A |
------------------------------------------------------
--| Contact Name 1 | Email Address |
--| Contact Name 2 | Email Address |
--| Contact Name 3 | Email Address |
--| Contact Name 6 | Email Address |
--| Contact Name 7 | Email Address |
------------------------------------------------------
| Group B |
------------------------------------------------------
--| Contact Name 4 | Email Address |
--| Contact Name 2 | Email Address |
------------------------------------------------------
| Group G |
------------------------------------------------------
--| Contact Name 5 | Email Address |
------------------------------------------------------
where the group(s) are chosen via the multiselect and, in the example, a contact can be added to more than one group...

I have spent almost 9 hours straight trying to figure this one!  :'(



“Anyone who has never made a mistake has never tried anything new.” - Albert Einstein
"If you are travelling at the speed of light and you turn on your headlights, would they work?" - Unknown

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Group as a column on custom report
June 04, 2011, 09:21:49 pm

I suspect you can accomplish this using a custom template for your custom report (i.e. dont use the default template)

if the group is a column in the DAO, you can basically output it in smarty using smarty commands when the group changes. So if you query gets a list of

contact, email, group name

ordered by group name, you can use smarty to decide when to output the group header

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Group as a column on custom report
June 05, 2011, 06:27:37 am
You could probably also achieve this using Views.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

mcarson

  • I post occasionally
  • **
  • Posts: 110
  • Karma: 5
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.x
  • MySQL version: 5.5
  • PHP version: 5.4.22
Re: Group as a column on custom report
June 05, 2011, 02:24:47 pm
Thought I would post back some results... a total of 15 hours, and that's only the scripting for the report. Still to do the template!

Took a bit of code from Contact Summary and a bit from Membership Summary to get the required columns (before I figured out how to do that myself!)

Declarations: (yes, a custom php file as well - stored in custom scripts directory)
Code: [Select]
require_once 'CRM/Core/Custom/Reports.php';

protected $_addressField = false;
protected $_emailField = false;
protected $_phoneField = false;
protected $_groupField = false;

The 'Group' column: (This was very hard to debug to get right - my knowledge of php and HTML_QuickForm is limited)
Code: [Select]
'civicrm_group' =>
    array(
        'dao' => 'CRM_Contact_DAO_Group',
        'fields' =>
        array(
            'gid' =>
            array(
                'name' => 'id',
                'title' => ts( 'Group Name'), 
                'default' => true
            ),
        ),
        'filters'=>
        array(
            'gid' =>
            array(
                'name' => 'id',
                'title' => ts( 'Group' ),
                'operatorType' => CRM_Report_Form::OP_MULTISELECT,
                'options' => CRM_Core_PseudoConstant::group( )
            ),
        ),
    ),

The 'Select SQL stuff:
Code: [Select]
function select( ) {
    $select = $this->_columnHeaders = array( );

    foreach ( $this->_columns as $tableName => $table ) {
        if ( array_key_exists('fields', $table) ) {
            foreach ( $table['fields'] as $fieldName => $field ) {
               
                if ( CRM_Utils_Array::value( 'required', $field ) ||
                    CRM_Utils_Array::value( $fieldName, $this->_params['fields'] ) ) {
                       
                    if ( $tableName == 'civicrm_address' ) {
                        $this->_addressField = true;
                    } else if ( $tableName == 'civicrm_email' ) {
                        $this->_emailField = true;
                    } else if ( $tableName == 'civicrm_phone' ) {
                        $this->_phoneField = true;
                    // add group field
                    } else if ( $tableName == 'civicrm_group' ) {
                        $this->_groupField = true;
                    }
                   
                    $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
                    $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
                    $this->_columnHeaders["{$tableName}_{$fieldName}"]['type']  = CRM_Utils_Array::value( 'type', $field );
                }
            }
        }
    }
    $this->_select = "SELECT " . implode( ', ', $select ) . " ";
}

The 'From' SQL stuff:
Code: [Select]
function from( ) {
    $this->_from = null;

    $this->_from = "FROM  civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} ";

    //used when address field is selected
    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";
    }
   
    //used when group field is selected
    if ( $this->_groupField ) {
        $this->_from .= "
            LEFT JOIN civicrm_group_contact gc ON {$this->_aliases['civicrm_contact']}.id = gc.contact_id AND gc.status = 'Added'
            LEFT JOIN civicrm_group {$this->_aliases['civicrm_group']} ON {$this->_aliases['civicrm_group']}.id = gc.group_id";
       
    }

    //used when email field is selected
    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";
    }
   
    //used when phone field is selected
    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";
    }
}

Additions to the 'alterDisplay' function (to replace the group id with the name/title of the group)
Code: [Select]
if ( array_key_exists('civicrm_group_gid', $row) ) {
    if ( $value = $row['civicrm_group_gid'] ) {
        $rows[$rowNum]['civicrm_group_gid'] = CRM_Core_Custom_Reports::groupGetGroupNameByID( $value );
    }
    $entryFound = true;
}

The 'custom' script:
Code: [Select]
class CRM_Core_Custom_Reports
{

    private static $groupGetGroupNameByID;

    public static function &groupGetGroupNameByID( $groupID = false ) {
        if ( $groupID != null ) {
            // Specify the single result query to be executed
            $query = "
                SELECT civicrm_group.title title, civicrm_group.id id
                FROM civicrm_group
                WHERE id = %1
                ";   
            $params = array( 1 => array( $groupID, 'Integer' ) );
            return CRM_Core_DAO::singleValueQuery( $query, $params );
        }
    }
}


This report script works well using the default tpl template ;D. Now I just have to create one that summarises and groups the groups, if required. I can attach the full code if required, but I'm assuming this will be better in the documentation section, let me know.

« Last Edit: June 05, 2011, 06:05:24 pm by mcarson »
“Anyone who has never made a mistake has never tried anything new.” - Albert Einstein
"If you are travelling at the speed of light and you turn on your headlights, would they work?" - Unknown

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviReport (Moderator: Dave Greenberg) »
  • Group as a column on custom report

This forum was archived on 2017-11-26.