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 Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Hierarchical groups in search screen
Pages: [1]

Author Topic: Hierarchical groups in search screen  (Read 962 times)

Joris

  • I’m new here
  • *
  • Posts: 9
  • Karma: 1
Hierarchical groups in search screen
June 10, 2010, 01:06:29 am
Hello,
I have a question about the groups in civi. We use version 3.1.2. and have a lot of nested groups. While I was searching for a way to change the group view in the ( basic ) search screen to be more like this:

Group A
….Sub group A1
……..Sub group A11
……..Sub group A12
….Sub group A2
Group B

I came across this piece of code in Form/Search/Basic.php:

Code: [Select]
$config =& CRM_Core_Config::singleton( );
            if ( $config->groupTree ) {
                $this->add('hidden', 'group', null, array('id' => 'group' ));

                $group = CRM_Utils_Array::value( 'group', $this->_formValues );

                $selectedGroups = explode( ',', $group );

                if ( is_array( $selectedGroups ) ) {
                    $groupNames = null;
                    $groupIds = array( );
                    foreach( $selectedGroups as $groupId ) {
                        if ( $groupNames ) {
                            $groupNames .= '<br/>';
                        }
                        $groupNames .= $this->_group[$groupId];
                    }
                    $groupIds[] = $groupId;
                }

                $this->assign('groupIds', implode( ',', $groupIds ) );
                $this->assign('groupNames', $groupNames );
          } else {
                // add select for groups
                $group               = array('' => ts('- any group -')) + $this->_group;
                $this->_groupElement =& $this->addElement('select', 'group', ts('in'), $group);
            }

I think ( and correct me if I'm wrong ) this says that when “groupTree = true” in the config file then use the tree layout. Now I’ve been searching this forum for a setting to activate this piece of code, but can’t seem to find it anywhere ( just commenting out the if statement didn’t work  ;) ).
Can somebody shed some light on this ( hidden? ) feature.

Thanks in advance.

« Last Edit: June 10, 2010, 07:20:01 am by Joris »

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: Hierarchical groups in search screen
June 10, 2010, 07:18:08 am

that was code that still had a few bugs in it and hence that setting was not exposed

if you are a php/mysql/js coder, you can try changing that conditional boolean to 1 and try fixing it

let us know how things go

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

Joris

  • I’m new here
  • *
  • Posts: 9
  • Karma: 1
Re: Hierarchical groups in search screen
June 11, 2010, 12:28:22 am
Unfortunately my knowledge of php is rather limited (I’m an ASP.NET developer). But I’ll try giving it a go when I’ve got some free time.

Joris

  • I’m new here
  • *
  • Posts: 9
  • Karma: 1
Re: Hierarchical groups in search screen
June 18, 2010, 10:11:54 am
I’ve tried to implement some sort of tree in the select box in the basic search screen. This is what I’ve come up width:
Bare in mind that this probably isn’t the best solution, but it works. I’m also not responsible if this breaks your civi crm site.
I’ve created 2 functions in the CRM/Contact/Form/Search/Basic.php file:

Code: [Select]
function buildTree( $parentGroupID, $numberOfStrips, &$group ) {

        $query = "SELECT id as gid, title as gtitle FROM civicrm_group WHERE parents is NULL order by title";

        if ( $parentGroupID != "NULL" )
        {
                $query = "SELECT id as gid, title as gtitle FROM civicrm_group WHERE parents = ".$parentGroupID." order by title";
        }

        $dao =& CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray );

        while ( $dao->fetch() ) {

                $strips = "";

                for ( $counter = 1; $counter <= $numberOfStrips; $counter += 1) {
                        $strips = "--".$strips;
}

//              CRM_Core_Error::debug_var('qryJORIS_strips', $strips);

                if ( array_key_exists( $dao->gid, $this->_group ) ) {
                        $group[$dao->gid] = $strips." ".ts($dao->gtitle);
//                      CRM_Core_Error::debug_var('qryJORIS_group', $group);
                }

                if ( $this->hasChildren( $dao->gid ) > 0 )
                {
                        $numberOfStrips = $numberOfStrips + 1;
//                      CRM_Core_Error::debug_var('qryJORIS_children', $dao->gtitle);
                        $this->buildTree( $dao->gid, $numberOfStrips, $group );
                        $numberOfStrips = $numberOfStrips - 1;
                }
                else
                {
//                      CRM_Core_Error::debug_var('qryJORIS_NO_children', $dao->gtitle);
                }
        }
    }

Code: [Select]
function hasChildren( $groupID ) {

        if ( ! isset($groupID) ) {
//              CRM_Core_Error::debug_var('qryJORIShasChildrenNotSet', $groupID );
                return 0;
        }
        else
        {
//              CRM_Core_Error::debug_var('qryJORIShasChildrenGrpId', $groupID);
                $query = "SELECT id as gidcount FROM civicrm_group WHERE parents = ".$groupID." LIMIT 2";
//              CRM_Core_Error::debug_var('qryJORIShasChildrenQuery', $query);
                $dao =& CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray );
//              CRM_Core_Error::debug_var('qryJORIShasChildrenCount', $dao->N );
                return $dao->N;
        }
    }

Now in the “buildQuickForm” function you can put this
Code: [Select]
//                 build tree view
                $groupH=array();
                $this->buildTree( "NULL", 0, $groupH );

//                 add select for groups (new)
                $group = array('' => ts('- any group -')) + $groupH;
                $this->_groupElement =& $this->addElement('select', 'group', ts('in'), $group);

//                 add select for groups (original)
//              $group = array('' => ts('- any group -')) + $this->_group;
//              $this->_groupElement =& $this->addElement('select', 'group', ts('in'), $group);
Instead of
Code: [Select]
$group = array('' => ts('- any group -')) + $this->_group;
$this->_groupElement =& $this->addElement('select', 'group', ts('in'), $group);

As I’ve said before, this can be improved quite a bit. I’m for instance misusing the original group “$this->_group” to filter out the new values. It would be far better if I create the new group in the same place where “$this->group” is filled (but I can’t seem to find that location).
 
This also doesn’t work for multiple parents, you could get the second item from the db if you change the "=" to a "like" or using the "civicrm_group_nesting" table, but it will not be able to place it in the tree sins the key already exists.

I hope somebody will find this useful somehow or maybe improve this.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Hierarchical groups in search screen

This forum was archived on 2017-11-26.