From 31f25ce14ec89ded93a4dfdcde3f7b4a32abdfc4 Mon Sep 17 00:00:00 2001 From: Wladston Viana Ferreira Filho Date: Thu, 25 Oct 2018 13:14:37 -0300 Subject: [PATCH 1/2] Add configurable optional filter. This adds a new configuration option to reports: filters. It passes data in it as a JavaScript object inside the `filters` configuration option. This lets users set filters automatically for their Power BI reports. --- README.md | 1 + includes/class-power-bi-post-types.php | 12 ++++++++++++ includes/class-power-bi-shortcodes.php | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/README.md b/README.md index 124fdd4..db6af64 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ The Embed Type determines the remaining fields to fill out. * Group ID: Enter the unique identifier for the group. You can find the identifier by viewing a dashboard or report in the Power BI Service. The identifier is in the URL. * Dataset ID: Enter the unique identifier for the dataset. You can find the identifier by viewing a dashboard in the Power BI Service. The identifier is in the URL. This is only needed for Create Mode. * Page Name: Enter the unique identifier for the Page. You can find the identifier by viewing a dashboard in the Power BI Service. The identifier is in the URL. This is is an optional parameter. If left blank, the report's default page will be shown. + * Filter: An optional pre-set filter, given as a JavaScript object. ### Report Visual diff --git a/includes/class-power-bi-post-types.php b/includes/class-power-bi-post-types.php index 7dae739..fb85639 100644 --- a/includes/class-power-bi-post-types.php +++ b/includes/class-power-bi-post-types.php @@ -240,6 +240,18 @@ public function power_bi_metaboxs() { ), ) ); + $metabox_details->add_field( array( + 'name' => 'Filter', + 'desc' => 'Enter a filter object. Refer to the Power BI JavaScript Wiki for more information about filters.', + 'id' => $prefix . 'filter', + 'type' => 'textarea', + 'default' => '', + 'attributes' => array( + 'data-conditional-id' => $prefix . 'embed_type', + 'data-conditional-value' => wp_json_encode( array( 'report' ) ), + ), + ) ); + $metabox_details->add_field( array( 'name' => 'Visual Name', 'desc' => 'The Visual Name can be retrieved using the GetVisuals method on the Page object.', diff --git a/includes/class-power-bi-shortcodes.php b/includes/class-power-bi-shortcodes.php index dbd37e6..eee16a3 100644 --- a/includes/class-power-bi-shortcodes.php +++ b/includes/class-power-bi-shortcodes.php @@ -90,6 +90,7 @@ public function powerbi_js( $id ) { if( 'report' === $embed_type ) { $report_mode = get_post_meta( $id, '_power_bi_report_mode', true ); $page_name = get_post_meta( $id, '_power_bi_page_name', true ); + $filter = get_post_meta( $id, '_power_bi_filter', true ); if ( 'create' === $report_mode ) { $embed_url = $api_url . "reportEmbed?groupId=" . $group_id; @@ -152,6 +153,9 @@ public function powerbi_js( $id ) { id: '', pageName: '', + + filters: [], + From c8e946175c7b094d8bc54994cd977e5083bff652 Mon Sep 17 00:00:00 2001 From: Wladston Viana Ferreira Filho Date: Thu, 25 Oct 2018 14:28:31 -0300 Subject: [PATCH 2/2] Pass filters via shortcode. This allows one to setup custom filters via the powerbi shortcode. For instance, to show a report with a filter applied, one could do: ``` [powerbi id='10' filter="{$schema: 'http://powerbi.com/product/schema#basic', target: {table: 'Product', column: 'id'}, operator: 'In', values: new Array('p1', 'p2', 'p3')}"] ``` --- includes/class-power-bi-shortcodes.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/class-power-bi-shortcodes.php b/includes/class-power-bi-shortcodes.php index eee16a3..c44e26a 100644 --- a/includes/class-power-bi-shortcodes.php +++ b/includes/class-power-bi-shortcodes.php @@ -43,6 +43,7 @@ public function power_bi_html( $atts ) { 'id' => '', 'width' => '', 'height' => '', + 'filter' => '', ), $atts ) ); if ( empty( $id ) ) { @@ -52,7 +53,7 @@ public function power_bi_html( $atts ) { $container_width = empty( $width ) ? get_post_meta( $id, '_power_bi_width', true ) : $width; $container_height = empty( $height ) ? get_post_meta( $id, '_power_bi_height', true ) : $height; - $powerbi_js = $this->powerbi_js( $id ); + $powerbi_js = $this->powerbi_js( $id, $filter ); ob_start(); echo '
'; @@ -60,7 +61,7 @@ public function power_bi_html( $atts ) { return ob_get_clean(); } - public function powerbi_js( $id ) { + public function powerbi_js( $id, $filter ) { $power_bi_credentials = get_option('power_bi_credentials'); if( isset( $power_bi_credentials['access_token'] ) ) { @@ -90,7 +91,7 @@ public function powerbi_js( $id ) { if( 'report' === $embed_type ) { $report_mode = get_post_meta( $id, '_power_bi_report_mode', true ); $page_name = get_post_meta( $id, '_power_bi_page_name', true ); - $filter = get_post_meta( $id, '_power_bi_filter', true ); + $filter = empty( $filter ) ? get_post_meta( $id, '_power_bi_filter', true ) : $filter; if ( 'create' === $report_mode ) { $embed_url = $api_url . "reportEmbed?groupId=" . $group_id;