Skip to content

Commit

Permalink
Merge pull request #28 from agileware/PROJ-2524
Browse files Browse the repository at this point in the history
Update ux_civicrm_listing shortcode. Add hide_fields, css_id features. Update USAGE.md
  • Loading branch information
agileware-justin authored Jan 19, 2023
2 parents ce828a0 + b97cc2a commit 6b793d8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
42 changes: 34 additions & 8 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
<script type="text/javascript" src="//cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js" id="datatable-js-js"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('#member_directory').DataTable({
'paging': true,
'pageLength': 50,
order: [[0, 'asc']],
columns: [
{ title: 'Member Name' },
{ title: 'Country' },
],
});
});
</script>
```


## Activity Shortcode

Expand Down
22 changes: 17 additions & 5 deletions shortcodes/civicrm/listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand All @@ -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,
Expand All @@ -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 ) {
Expand Down Expand Up @@ -124,7 +136,7 @@ function renderTable( $values, $template, $header ) {

$tbody_html .= "<tr>$row_html</tr>";
}
$html = "<table class='ux-cv-listing'><thead>$header_html</thead><tbody>$tbody_html</tbody></table>";
$html = "<table id='$css_id' class='ux-cv-listing'><thead>$header_html</thead><tbody>$tbody_html</tbody></table>";

return $html;
}
Expand Down

0 comments on commit 6b793d8

Please sign in to comment.