From bf30fd3c993737fc13f9ac30c6659cd2d52e4c1d Mon Sep 17 00:00:00 2001 From: kodinkat Date: Mon, 20 May 2024 16:12:38 +0100 Subject: [PATCH 1/7] Support using of dynamic gen-map within multiplier personal section --- dt-metrics/metrics.php | 8 ++++++++ dt-metrics/records/genmap.js | 12 ++++++++++-- dt-metrics/records/genmap.php | 26 ++++++++++++++++++-------- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/dt-metrics/metrics.php b/dt-metrics/metrics.php index ea56cd849c..5872b38d2d 100644 --- a/dt-metrics/metrics.php +++ b/dt-metrics/metrics.php @@ -35,6 +35,14 @@ public function __construct(){ // wait for D.T post type classes to be set up before building metrics for them add_action( 'after_setup_theme', function (){ $modules = dt_get_option( 'dt_post_type_modules' ); + + if ( dt_has_permissions( [ 'multiplier' ] ) ) { + /* Record Types */ + $genmap_base_slug = 'personal'; + $genmap_base_title = __( 'Personal', 'disciple_tools' ); + require_once( get_template_directory() . '/dt-metrics/records/genmap.php' ); + } + // Personal require_once( get_template_directory() . '/dt-metrics/personal/coaching-tree.php' ); require_once( get_template_directory() . '/dt-metrics/personal/baptism-tree.php' ); diff --git a/dt-metrics/records/genmap.js b/dt-metrics/records/genmap.js index 9a1ea09994..5072f352c2 100644 --- a/dt-metrics/records/genmap.js +++ b/dt-metrics/records/genmap.js @@ -1,5 +1,9 @@ jQuery(document).ready(function ($) { - if (window.wpApiShare.url_path.startsWith('metrics/records/genmap')) { + if ( + window.wpApiShare.url_path.startsWith( + `metrics/${window.dtMetricsProject.base_slug}/genmap`, + ) + ) { project_records_genmap(); } @@ -88,7 +92,11 @@ jQuery(document).ready(function ($) { // Fetch generational map chart. window - .makeRequest('POST', 'metrics/records/genmap', payload) + .makeRequest( + 'POST', + `metrics/${window.dtMetricsProject.base_slug}/genmap`, + payload, + ) .promise() .then((response) => { let container = jQuery('#genmap'); diff --git a/dt-metrics/records/genmap.php b/dt-metrics/records/genmap.php index 451df082b3..b144df1db9 100644 --- a/dt-metrics/records/genmap.php +++ b/dt-metrics/records/genmap.php @@ -3,24 +3,27 @@ exit; } // Exit if accessed directly. - class DT_Metrics_Groups_Genmap extends DT_Metrics_Chart_Base { //slug and title of the top menu folder - public $base_slug = 'records'; // lowercase + public $base_slug; // lowercase public $slug = 'genmap'; // lowercase public $base_title; public $title; public $js_object_name = 'wp_js_object'; // This object will be loaded into the metrics.js file by the wp_localize_script. - public $permissions = [ 'dt_all_access_contacts', 'view_project_metrics' ]; + public $permissions = [ 'dt_all_access_contacts', 'view_project_metrics', 'multiplier' ]; public $namespace = null; - public function __construct() { - parent::__construct(); + public function __construct( $base_slug, $base_title ) { if ( !$this->has_permission() ){ return; } - $this->base_title = __( 'Genmap', 'disciple_tools' ); + + $this->base_slug = $base_slug; + $this->base_title = $base_title; + + parent::__construct(); + $this->title = __( 'Generation Map', 'disciple_tools' ); $url_path = dt_get_url_path( true ); @@ -36,7 +39,7 @@ public function add_api_routes() { $version = '1'; $namespace = 'dt/v' . $version; register_rest_route( - $namespace, '/metrics/records/genmap', [ + $namespace, "/metrics/$this->base_slug/genmap", [ [ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [ $this, 'tree' ], @@ -75,6 +78,7 @@ public function scripts() { wp_localize_script( 'dt_metrics_project_script', 'dtMetricsProject', [ 'root' => esc_url_raw( rest_url() ), + 'base_slug' => $this->base_slug, 'site_url' => esc_url_raw( site_url() ), 'theme_uri' => get_template_directory_uri(), 'nonce' => wp_create_nonce( 'wp_rest' ), @@ -243,5 +247,11 @@ public function has_infinite_loop( $parent_id, $children ): bool { return false; } } -new DT_Metrics_Groups_Genmap(); + +if ( isset( $genmap_base_slug, $genmap_base_title ) ) { + new DT_Metrics_Groups_Genmap( $genmap_base_slug, $genmap_base_title ); + +} else { + new DT_Metrics_Groups_Genmap( 'records', __( 'Genmap', 'disciple_tools' ) ); +} From 5aedf5bbae2a2712e54ebf60e68557a08f3ee822 Mon Sep 17 00:00:00 2001 From: kodinkat Date: Tue, 21 May 2024 14:20:47 +0100 Subject: [PATCH 2/7] Obfuscate non-shared item records --- dt-metrics/records/genmap.js | 44 +++++++++++++++++++++++++++++++++-- dt-metrics/records/genmap.php | 11 ++++++--- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/dt-metrics/records/genmap.js b/dt-metrics/records/genmap.js index f2c7f4a750..6497fabba8 100644 --- a/dt-metrics/records/genmap.js +++ b/dt-metrics/records/genmap.js @@ -141,6 +141,39 @@ jQuery(document).ready(function ($) { initCompleted: function (chart) { const post_types = window.dtMetricsProject.post_types; + /** + * Non-Shared Items + */ + + // Identify and obfuscate items not shared with current user. + const non_shared_items = identify_items_by_field_value( + response, + 'shared', + 0, + {}, + ); + + // Obfuscate identified items. + if (non_shared_items) { + for (const [id, item] of Object.entries(non_shared_items)) { + const node = $(chart).find(`#${id}.node`); + if (node) { + const color = '#808080'; + $(node).css('background-color', color); + $(node).find('.title').text('.......'); + $(node).find('.title').css('background-color', color); + $(node).find('.content').css('background-color', color); + $(node).find('.content').css('border', '0px'); + + $(node).data('shared', '0'); + } + } + } + + /** + * Archived Items + */ + // Identify archived items, in order to update corresponding node color. if ( post_types && @@ -182,7 +215,13 @@ jQuery(document).ready(function ($) { let node_id = node.attr('id'); let node_parent_id = node.data('parent'); - open_modal_details(node_id, node_parent_id, selected_post_type); + // Ensure non-shared item nodes are ignored. + const node_shared = node.data('shared'); + if (!node_shared || String(node_shared) !== '0') { + open_modal_details(node_id, node_parent_id, selected_post_type); + } else { + jQuery('#genmap-details').empty(); + } }); }) .catch((error) => { @@ -264,11 +303,12 @@ jQuery(document).ready(function ($) { } function identify_items_by_field_value(data, field, value, items) { - if (data?.[field] === value) { + if (String(data?.[field]) === String(value)) { items[data['id']] = { id: data['id'], name: data['name'], status: data['status'], + shared: data['shared'], }; } diff --git a/dt-metrics/records/genmap.php b/dt-metrics/records/genmap.php index f1a2ffced8..bc74e648b9 100644 --- a/dt-metrics/records/genmap.php +++ b/dt-metrics/records/genmap.php @@ -161,6 +161,8 @@ public function get_query( $post_type, $p2p_type, $p2p_direction, $filters = [] $select_parent_id = 'p2p_from'; } + $user = wp_get_current_user(); + // Determine archived meta values. $status_key = $filters['status_key'] ?? ''; $query = $wpdb->get_results( $wpdb->prepare( " @@ -168,7 +170,8 @@ public function get_query( $post_type, $p2p_type, $p2p_direction, $filters = [] a.ID as id, 0 as parent_id, a.post_title as name, - ( SELECT p_status.meta_value FROM $wpdb->postmeta as p_status WHERE ( p_status.post_id = a.ID ) AND ( p_status.meta_key = %s ) ) as status + ( SELECT p_status.meta_value FROM $wpdb->postmeta as p_status WHERE ( p_status.post_id = a.ID ) AND ( p_status.meta_key = %s ) ) as status, + ( SELECT EXISTS( SELECT p_shared.user_id FROM $wpdb->dt_share as p_shared WHERE p_shared.user_id = %d AND p_shared.post_id = a.ID ) ) as shared FROM $wpdb->posts as a WHERE a.post_type = %s AND a.ID %1s IN ( @@ -188,10 +191,11 @@ public function get_query( $post_type, $p2p_type, $p2p_direction, $filters = [] p.%1s as id, p.%1s as parent_id, (SELECT sub.post_title FROM $wpdb->posts as sub WHERE sub.ID = p.%1s ) as name, - ( SELECT u_status.meta_value FROM $wpdb->postmeta as u_status WHERE ( u_status.post_id = p.%1s ) AND ( u_status.meta_key = %s ) ) as status + ( SELECT u_status.meta_value FROM $wpdb->postmeta as u_status WHERE ( u_status.post_id = p.%1s ) AND ( u_status.meta_key = %s ) ) as status, + ( SELECT EXISTS( SELECT u_shared.user_id FROM $wpdb->dt_share as u_shared WHERE u_shared.user_id = %d AND u_shared.post_id = p.%1s ) ) as shared FROM $wpdb->p2p as p WHERE p.p2p_type = %s; - ", $status_key, $post_type, $not_from, $p2p_type, $not_to, $p2p_type, $select_id, $select_parent_id, $select_id, $select_id, $status_key, $p2p_type ), ARRAY_A ); + ", $status_key, $user->ID, $post_type, $not_from, $p2p_type, $not_to, $p2p_type, $select_id, $select_parent_id, $select_id, $select_id, $status_key, $user->ID, $select_id, $p2p_type ), ARRAY_A ); return $query; } @@ -240,6 +244,7 @@ public function build_array( $parent_id, $menu_data, $gen, $depth_limit, $filter 'id' => $parent_id, 'name' => $menu_data['items'][ $parent_id ]['name'] ?? 'SYSTEM', 'status' => $menu_data['items'][ $parent_id ]['status'] ?? '', + 'shared' => $menu_data['items'][ $parent_id ]['shared'] ?? 0, 'content' => 'Gen ' . $gen ]; From ec79192d1fa12013a945733384b4f1460fb4f661 Mon Sep 17 00:00:00 2001 From: kodinkat Date: Wed, 22 May 2024 15:30:41 +0100 Subject: [PATCH 3/7] Ensure non-shared generations are removed + Additional security --- dt-metrics/records/genmap.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/dt-metrics/records/genmap.php b/dt-metrics/records/genmap.php index bc74e648b9..c61b58604d 100644 --- a/dt-metrics/records/genmap.php +++ b/dt-metrics/records/genmap.php @@ -240,14 +240,18 @@ public function build_array( $parent_id, $menu_data, $gen, $depth_limit, $filter } } + $shared = intval( $menu_data['items'][ $parent_id ]['shared'] ?? 0 ); $array = [ 'id' => $parent_id, - 'name' => $menu_data['items'][ $parent_id ]['name'] ?? 'SYSTEM', + 'name' => ( ( $shared === 1 ) || ( $gen === 0 ) ) ? ( $menu_data['items'][ $parent_id ]['name'] ?? 'SYSTEM' ) : '', 'status' => $menu_data['items'][ $parent_id ]['status'] ?? '', - 'shared' => $menu_data['items'][ $parent_id ]['shared'] ?? 0, + 'shared' => $shared, 'content' => 'Gen ' . $gen ]; + // Ensure to exclude non-shared generations. + $children = $this->exclude_non_shared_generations( $children ); + // Determine if archived records are to be excluded. if ( !$filters['show_archived'] ) { @@ -303,6 +307,20 @@ public function exclude_archived_children( $children, $archived_key ): array { return $updated_children; } + + public function exclude_non_shared_generations( $children ): array { + $updated_children = []; + foreach ( $children ?? [] as $child ) { + if ( !empty( $child['children'] ) ) { + $child['children'] = $this->exclude_non_shared_generations( $child['children'] ); + } + if ( !empty( $child['children'] ) || ( isset( $child['shared'] ) && intval( $child['shared'] ) === 1 ) ) { + $updated_children[] = $child; + } + } + + return $updated_children; + } } if ( isset( $genmap_base_slug, $genmap_base_title ) ) { From cbb1aaf93527a18a90dce791e253de56d8b861e8 Mon Sep 17 00:00:00 2001 From: kodinkat Date: Fri, 24 May 2024 15:04:37 +0100 Subject: [PATCH 4/7] Displayed within both personal and dynamic metrics sections for admin users --- dt-metrics/metrics.php | 11 +++++------ dt-metrics/records/genmap.php | 7 ------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/dt-metrics/metrics.php b/dt-metrics/metrics.php index 5872b38d2d..a408ce4f61 100644 --- a/dt-metrics/metrics.php +++ b/dt-metrics/metrics.php @@ -36,11 +36,10 @@ public function __construct(){ add_action( 'after_setup_theme', function (){ $modules = dt_get_option( 'dt_post_type_modules' ); - if ( dt_has_permissions( [ 'multiplier' ] ) ) { - /* Record Types */ - $genmap_base_slug = 'personal'; - $genmap_base_title = __( 'Personal', 'disciple_tools' ); - require_once( get_template_directory() . '/dt-metrics/records/genmap.php' ); + require_once( get_template_directory() . '/dt-metrics/records/genmap.php' ); + + if ( dt_has_permissions( [ 'multiplier' ] ) || dt_has_permissions( [ 'dt_all_access_contacts', 'view_project_metrics' ] ) ) { + new DT_Metrics_Groups_Genmap( 'personal', __( 'Personal', 'disciple_tools' ) ); } // Personal @@ -84,7 +83,7 @@ public function __construct(){ require_once( get_template_directory() . '/dt-metrics/combined/hover-map.php' ); /* Record Types */ - require_once( get_template_directory() . '/dt-metrics/records/genmap.php' ); + new DT_Metrics_Groups_Genmap( 'records', __( 'Genmap', 'disciple_tools' ) ); require_once( get_template_directory() . '/dt-metrics/records/dynamic-records-map.php' ); } if ( !empty( $modules['access_module']['enabled'] ) ){ diff --git a/dt-metrics/records/genmap.php b/dt-metrics/records/genmap.php index c61b58604d..6e046a724f 100644 --- a/dt-metrics/records/genmap.php +++ b/dt-metrics/records/genmap.php @@ -323,10 +323,3 @@ public function exclude_non_shared_generations( $children ): array { } } -if ( isset( $genmap_base_slug, $genmap_base_title ) ) { - new DT_Metrics_Groups_Genmap( $genmap_base_slug, $genmap_base_title ); - -} else { - new DT_Metrics_Groups_Genmap( 'records', __( 'Genmap', 'disciple_tools' ) ); -} - From cab1781a0a271dfbfebd5592f7b750330b40cee4 Mon Sep 17 00:00:00 2001 From: kodinkat Date: Tue, 28 May 2024 11:39:43 +0100 Subject: [PATCH 5/7] Ensure all user roles have personal metrics access to gen-map --- dt-metrics/metrics.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/dt-metrics/metrics.php b/dt-metrics/metrics.php index a408ce4f61..7fb85fb95e 100644 --- a/dt-metrics/metrics.php +++ b/dt-metrics/metrics.php @@ -36,13 +36,9 @@ public function __construct(){ add_action( 'after_setup_theme', function (){ $modules = dt_get_option( 'dt_post_type_modules' ); - require_once( get_template_directory() . '/dt-metrics/records/genmap.php' ); - - if ( dt_has_permissions( [ 'multiplier' ] ) || dt_has_permissions( [ 'dt_all_access_contacts', 'view_project_metrics' ] ) ) { - new DT_Metrics_Groups_Genmap( 'personal', __( 'Personal', 'disciple_tools' ) ); - } - // Personal + require_once( get_template_directory() . '/dt-metrics/records/genmap.php' ); + new DT_Metrics_Groups_Genmap( 'personal', __( 'Personal', 'disciple_tools' ) ); require_once( get_template_directory() . '/dt-metrics/personal/coaching-tree.php' ); require_once( get_template_directory() . '/dt-metrics/personal/baptism-tree.php' ); require_once( get_template_directory() . '/dt-metrics/personal/group-tree.php' ); From b7a5297d107a4d499cd4f94789e5abf92aeb760f Mon Sep 17 00:00:00 2001 From: kodinkat Date: Tue, 28 May 2024 16:36:06 +0100 Subject: [PATCH 6/7] Ensure correct side menu option is selected --- dt-metrics/records/genmap.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dt-metrics/records/genmap.js b/dt-metrics/records/genmap.js index 6497fabba8..f8dc993521 100644 --- a/dt-metrics/records/genmap.js +++ b/dt-metrics/records/genmap.js @@ -14,7 +14,10 @@ jQuery(document).ready(function ($) { let spinner = ' '; chart.empty().html(spinner); - jQuery('#metrics-sidemenu').foundation('down', jQuery('#records-menu')); + jQuery('#metrics-sidemenu').foundation( + 'down', + jQuery(`#${window.dtMetricsProject.base_slug}-menu`), + ); let translations = window.dtMetricsProject.translations; From e1955c6fad8a8dc51b03a03e4cac09a6b002e81f Mon Sep 17 00:00:00 2001 From: corsac Date: Wed, 29 May 2024 09:30:12 +0100 Subject: [PATCH 7/7] Update Title --- dt-metrics/records/genmap.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dt-metrics/records/genmap.php b/dt-metrics/records/genmap.php index 6e046a724f..a0f0d2edb1 100644 --- a/dt-metrics/records/genmap.php +++ b/dt-metrics/records/genmap.php @@ -24,7 +24,7 @@ public function __construct( $base_slug, $base_title ) { parent::__construct(); - $this->title = __( 'Generation Map', 'disciple_tools' ); + $this->title = __( 'Generational Trees', 'disciple_tools' ); $url_path = dt_get_url_path( true ); if ( "metrics/$this->base_slug/$this->slug" === $url_path ) { @@ -96,7 +96,7 @@ public function scripts() { 'map_key' => empty( DT_Mapbox_API::get_key() ) ? '' : DT_Mapbox_API::get_key(), 'data' => [], 'translations' => [ - 'title' => __( 'Generation Map', 'disciple_tools' ), + 'title' => __( 'Generational Trees', 'disciple_tools' ), 'show_archived' => __( 'Show Archived', 'disciple_tools' ), 'highlight_active' => __( 'Highlight Active', 'disciple_tools' ), 'highlight_churches' => __( 'Highlight Churches', 'disciple_tools' ),