From b97cc2a5fea2d316bde3b83d4fc0dddf984aed29 Mon Sep 17 00:00:00 2001 From: Justin Freeman Date: Thu, 19 Jan 2023 15:40:12 +1100 Subject: [PATCH] PROJ-2524 Update ux_civicrm_listing shortcode. Add hide_fields, css_id features. Update USAGE.md --- USAGE.md | 42 +++++++++++++++++++++++++++------- shortcodes/civicrm/listing.php | 22 ++++++++++++++---- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/USAGE.md b/USAGE.md index 9ad602b..38f4282 100644 --- a/USAGE.md +++ b/USAGE.md @@ -306,16 +306,42 @@ Return the membership summary of the login user. 1. `[ux_membership_type]` Return the membership type of the login user. -## CiviCRM Data List using the CiviCRM Data Processor Shortcode +## CiviCRM Data Processor Shortcode + +This shortcode will output a [CiviCRM Data Processor](https://lab.civicrm.org/extensions/dataprocessor), in a table format. The Data Processor being used must have a API output defined and for this API output, the API Entity must also be set. `[ux_civicrm_listing]` - - **dpid**: data processor id - - **limit**: the limit of result default is 0(no limit) - - **sort**: the order of result - - **autopop_user_id**: get the logged in user id and pass it to the parameter. State the parameter name, like *contact_id* - - **format**: default is table - -This shortcode requires the [CiviCRM Data Processor](https://lab.civicrm.org/extensions/dataprocessor) extension to be installed on the CiviCRM site. + - **dpid**: Data Processor ID + - **limit**: the limit of result default is 0 (no limit) + - **sort**: the order of result, either: asc (ascending) or desc (descending) + - **autopop_user_id**: return the logged-in user ID and passas a parameter to the Data Processor. Define the parameter name, like *contact_id* + - **hide_fields**: comma separated list of fields to be excluded from the output. + - **css_id**: CSS ID to assign to the table. + +The example usage below uses a Data Processor with the ID of 4, excludes the contact_id column from display and assigns the CSS ID of member_directory to the table. + +[Datatables](https://datatables.net/) can be used to add features to the table displayed on the page, as shown below. + +``` +[ux_civicrm_listing dpid=4 autopop_user_id=0 hide_fields=contact_id css_id=member_directory format=datatable][/ux_civicrm_listing] + + + + +``` + ## Activity Shortcode diff --git a/shortcodes/civicrm/listing.php b/shortcodes/civicrm/listing.php index 4b9cab0..ad80540 100644 --- a/shortcodes/civicrm/listing.php +++ b/shortcodes/civicrm/listing.php @@ -26,9 +26,13 @@ public function shortcode_callback( $atts = [], $content = null, $tag = '' ) { 'limit' => 0, 'format' => 'default', 'sort' => '', + 'hide_fields' => '', + 'css_id' => '', 'autopop_user_id' => false ], $atts, $tag ); + $hide_fields = explode(',', $mod_atts['hide_fields']); + // get data processor information try { $dp = civicrm_api3( "DataProcessorOutput", 'get', [ @@ -55,6 +59,10 @@ public function shortcode_callback( $atts = [], $content = null, $tag = '' ) { } $header = $header['values']; + foreach ( $hide_fields as $hide_field ) { + unset($header[$hide_field]); + } + // the main data $params = [ 'sequential' => 1, @@ -81,22 +89,26 @@ public function shortcode_callback( $atts = [], $content = null, $tag = '' ) { return "CiviCRM API error."; } - return $this->render( $result, $header, $content, $mod_atts['format'] ); + foreach ( $hide_fields as $hide_field ) { + foreach($result['values'] as &$value) { unset($value[$hide_field]); } + } + + return $this->render( $result, $header, $content, $mod_atts['format'],$mod_atts['css_id']); } - function render( $info, $header, $template = null, $format = 'default' ) { + function render( $info, $header, $template = null, $format = 'default', $css_id = null ) { if ( count( $info ) == 0 ) { return "empty data"; } switch ( $format ) { default: - return $this->renderTable( $info['values'], $template, $header ); + return $this->renderTable( $info['values'], $template, $header, $css_id); break; } } - function renderTable( $values, $template, $header ) { + function renderTable( $values, $template, $header, $css_id) { $header_html = ""; $tbody_html = ""; foreach ( $header as $key => $value ) { @@ -124,7 +136,7 @@ function renderTable( $values, $template, $header ) { $tbody_html .= "$row_html"; } - $html = "$header_html$tbody_html
"; + $html = "$header_html$tbody_html
"; return $html; }