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) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Views improvements
Pages: [1]

Author Topic: Views improvements  (Read 929 times)

jalama

  • I post frequently
  • ***
  • Posts: 176
  • Karma: 22
    • Rooty Hollow LLC
  • CiviCRM version: 3.3.5
  • CMS version: Drupal 6 and 7
  • MySQL version: 5.1
  • PHP version: 5.2.5 and 5.3
Views improvements
August 11, 2011, 05:55:14 pm
Over the last couple of weeks I have been organizing some thoughts on upgrades I would like to make to views support, they can be found here http://wiki.civicrm.org/confluence/display/CRMDOC40/Drupal+Views%27+Future.

I'm diving into the code cleanup tasks listed and would like some feedback on the first task I'm working.  A pair of  generic Pseudo Constant classes (ie handlers) one for fields and one for filters.  The general idea here is that CiviCRM ships with close to 100 handlers, 25 of which are nearly identical field handlers another 25 of which are nearly identical filter handlers that are loading and rendering/filtering by Pseudo Constants.  I want to boil these down to 2 different handlers one for fields and one for filters.

If all goes well I'll test and commit this to the 4.1 branch and start working on cleaning up the gigantic civicrm.views.inc file.

Taking Activity Status as a sample:
There new handlers require a change int eh way hook_views_data() is called, we would need to pass the class and method name of the Pseudo
Class method to the handler from hook_views_data()

The hook_views_data() call changes from
Code: [Select]
    //Activity Status
    $data['civicrm_activity']['status'] = array(
                                                'title' => t('Activity Status'),
                                                'real field' => 'status_id',
                                                'help' => t('The Status of this Activity'),
                                                'field' => array(
                                                                 'handler' => 'civicrm_handler_field_activity_status',
                                                                 'click sortable' => TRUE,
                                                                 ),

To (ie adding the 'pseudo class' and 'pseudo method' elements tot he array, it will be necessary to add a 3rd element for Pseudo Constants that do not return the label of the field but instead return the name, ie Membership Status)
Code: [Select]
    //Activity Status
    $data['civicrm_activity']['status'] = array(
                                                'title' => t('Activity Status'),
                                                'real field' => 'status_id',
                                                'help' => t('The Status of this Activity'),
                                                'field' => array(
                                                                 'handler' => 'civicrm_handler_field_activity_status',
                                                                 'click sortable' => TRUE,
                                                                  'pseudo class' => 'CRM_Core_PseudoConstant',
                                                                  'pseudo method' => 'activityStatus',
                                                                 ),


Here's my proposed Pseudo Class field handler updates (the proposed filter handler is basically the same concept so I won't paste it here. 

Form:
Code: [Select]
class civicrm_handler_field_activity_status extends views_handler_field {
    static $_activityStatus;

    function construct( ) {
        if ( ! self::$_activityStatus ) {
            if ( ! civicrm_initialize( ) ) {
        return;
    }
            require_once 'CRM/Core/PseudoConstant.php';
            self::$_activityStatus = CRM_Core_PseudoConstant::activityStatus( );
        }
    }

    function render( $values ) {
        $sid = $values->{$this->field_alias};
        if ( empty( $sid ) ||
             (int ) $sid <= 0 ) {
            return null;
        }

        return self::$_activityStatus[$values->{$this->field_alias}];
    }
}

To (Note, I'm changing from a static property to a public one, there was no need for the static keyword as the construct() method is only called once when a view is rendered.):
Code: [Select]
class civicrm_handler_field_pseudo_constant extends views_handler_field {
    public $_pseudo_constant;

    function construct( ) {
        if ( ! civicrm_initialize( ) ||
             ! isset($this->definition['pseudo class']) ||
             ! isset($this->definition['pseudo method']) ) {
            return;
        }
        // Load arguments passed from hook_views_data
        $pseudo_args = isset($this->definition['pseudo args']) ? $this->definition['pseudo args'] : array();
        // Include and call the Pseudo Class method
        require_once str_replace('_',DIRECTORY_SEPARATOR,$this->definition['pseudo class']) .'.php';
        $this->_pseudo_constant = call_user_func_array($this->definition['pseudo class'] ."::". $this->definition['pseudo method'],$pseudo_args);
    }

    function render( $values ) {
        $sid = $values->{$this->field_alias};
        if ( empty( $sid ) ||
             (int ) $sid <= 0 ) {
            return null;
        }
        return $this->_pseudo_constant[$values->{$this->field_alias}];
    }
}

http://www.rootyhollow.com

jalama

  • I post frequently
  • ***
  • Posts: 176
  • Karma: 22
    • Rooty Hollow LLC
  • CiviCRM version: 3.3.5
  • CMS version: Drupal 6 and 7
  • MySQL version: 5.1
  • PHP version: 5.2.5 and 5.3
Re: Views improvements
August 17, 2011, 06:44:38 pm
Made a change the hook_views_data() call will look more like this, this way the same arguments can be passsed to the various handlers for a field without having to repeat them.

Code: [Select]
    //Activity Status
    $data['civicrm_activity']['status'] = array(
                                                'title' => t('Activity Status'),
                                                'real field' => 'status_id',
                                                'help' => t('The Status of this Activity'),
                                                'pseudo class' => 'CRM_Core_PseudoConstant',
                                                'pseudo method' => 'activityStatus',
                                                'field' => array(
                                                                 'handler' => 'civicrm_handler_field_pseudo_constant',
                                                                 'click sortable' => TRUE,
                                                                 ),
http://www.rootyhollow.com

jalama

  • I post frequently
  • ***
  • Posts: 176
  • Karma: 22
    • Rooty Hollow LLC
  • CiviCRM version: 3.3.5
  • CMS version: Drupal 6 and 7
  • MySQL version: 5.1
  • PHP version: 5.2.5 and 5.3
Re: Views improvements
August 17, 2011, 06:54:03 pm
This is the list of handlers that will be consolidated (the last three are extra that aren't being used and will be deleted), that's 51 total if you include the last three.  Not bad for a days work :)

Fields (civicrm_handler_field_pseudo_constant)
civicrm_handler_field_activity_status
civicrm_handler_field_activity_type
civicrm_handler_field_campaign_status
civicrm_handler_field_campaign_type
civicrm_handler_field_contribution_page
civicrm_handler_field_contribution_status
civicrm_handler_field_contribution_type
civicrm_handler_field_county
civicrm_handler_field_event_type
civicrm_handler_field_gender
civicrm_handler_field_grant_status
civicrm_handler_field_grant_type
civicrm_handler_field_location_type
civicrm_handler_field_membership_status
civicrm_handler_field_membership_type
civicrm_handler_field_participant_role
civicrm_handler_field_participant_status
civicrm_handler_field_payment_instrument
civicrm_handler_field_phone_type
civicrm_handler_field_priority
civicrm_handler_field_website_type

Filter (civicrm_handler_filter_pseudo_constant):
civicrm_handler_filter_activity_status
civicrm_handler_filter_activity_type
civicrm_handler_filter_campaign_status
civicrm_handler_filter_campaign_type
civicrm_handler_filter_contact_type
civicrm_handler_filter_contribution_page
civicrm_handler_filter_contribution_status
civicrm_handler_filter_contribution_type
civicrm_handler_filter_country
civicrm_handler_filter_county
civicrm_handler_filter_event_type
civicrm_handler_filter_gender
civicrm_handler_filter_grant_status
civicrm_handler_filter_grant_type
civicrm_handler_filter_group_title
civicrm_handler_filter_location_type
civicrm_handler_filter_mailing_name
civicrm_handler_filter_membership_status
civicrm_handler_filter_membership_type
civicrm_handler_filter_participant_status
civicrm_handler_filter_participant_role
civicrm_handler_filter_payment_instrument
civicrm_handler_filter_phone_type
civicrm_handler_filter_priority
civicrm_handler_filter_state
civicrm_handler_filter_tag_title
civicrm_handler_filter_website_type

Delete as they are not used:
civicrm_handler_filter_activity.inc
civicrm_handler_filter_activityType.inc
civicrm_handler_filter_location.inc
http://www.rootyhollow.com

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: Views improvements
August 19, 2011, 01:10:36 pm
Thanks for perservering with this Jim - I had a short discussion with Eileen about giving some feedback but neither of us felt we had any thing to add currently.
Except we were hitting the issue of needing a Handler so we could grab an 'autoreference' field for a civicrm contact in to a D7 node eg so each branch could have its own drupal page and link it to the Civicrm contact and hence also grab the officers who were related to that branch - but can't recall if that was a discussion relating to Views or not - will try and remember to raise with Torrance on Monday
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Views improvements

This forum was archived on 2017-11-26.