Skip to content

#2498 - Metrics - Personal Layers Map #2542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions dt-mapping/mapping-queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -1267,26 +1267,28 @@ public static function query_user_location_grid_totals( $status = null ) {
}


private static function format_results( $results, $post_type ){
private static function format_results( $results, $post_type, $can_list_all = true, $allowed_post_ids = [] ){
$features = [];
foreach ( $results as $result ) {
$features[] = array(
'type' => 'Feature',
'properties' => array(
'address' => $result['address'] ?? '',
'post_id' => $result['post_id'],
'name' => $result['name'],
'post_type' => $post_type
),
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
$result['lng'],
$result['lat'],
1
if ( $can_list_all || in_array( $result['post_id'], $allowed_post_ids ) ) {
$features[] = array(
'type' => 'Feature',
'properties' => array(
'address' => $result['address'] ?? '',
'post_id' => $result['post_id'],
'name' => $result['name'],
'post_type' => $post_type
),
),
);
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
$result['lng'],
$result['lat'],
1
),
),
);
}
}

$new_data = array(
Expand Down Expand Up @@ -1336,7 +1338,7 @@ public static function post_type_geojson( $post_type, $args = [], $offset = 0, $
$results = $wpdb->get_results( $prepared_query, ARRAY_A );
//phpcs:enable

return self::format_results( $results, $post_type );
return self::format_results( $results, $post_type, $args['list_all'], $args['assigned_post_ids'] );
}

public static function cluster_geojson( $post_type, $query = [], $offset = 0, $limit = 50000 ){
Expand All @@ -1360,7 +1362,7 @@ public static function cluster_geojson( $post_type, $query = [], $offset = 0, $l
);
//phpcs:enable

return self::format_results( $results, $post_type );
return self::format_results( $results, $post_type, $query['list_all'], $query['assigned_post_ids'] );
}

public static function points_geojson( $post_type, $query = [], $offset = 0, $limit = 50000 ){
Expand All @@ -1383,7 +1385,7 @@ public static function points_geojson( $post_type, $query = [], $offset = 0, $li
);
//phpcs:enable

return self::format_results( $results, $post_type );
return self::format_results( $results, $post_type, $query['list_all'], $query['assigned_post_ids'] );
}

public static function get_location_grid_meta_label( $location_grid_meta_id ){
Expand Down
4 changes: 3 additions & 1 deletion dt-metrics/metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function __construct(){
// 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/records/dynamic-records-map.php' );
new DT_Metrics_Dynamic_Records_Map( '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' );
Expand Down Expand Up @@ -80,7 +82,7 @@ public function __construct(){

/* Record Types */
new DT_Metrics_Groups_Genmap( 'records', __( 'Genmap', 'disciple_tools' ) );
require_once( get_template_directory() . '/dt-metrics/records/dynamic-records-map.php' );
new DT_Metrics_Dynamic_Records_Map( 'records', __( 'Records Map', 'disciple_tools' ) );
}
if ( !empty( $modules['access_module']['enabled'] ) ){
require_once( get_template_directory() . '/dt-metrics/combined/critical-path.php' );
Expand Down
70 changes: 43 additions & 27 deletions dt-metrics/records/dynamic-records-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jQuery(document).ready(function ($) {

body.offset = offset;
body.limit = limit;
body.list_all = mapbox_library_api.obj.settings.can_list_all;
let query = await window.makeRequest(
'POST',
mapbox_library_api.obj.settings.post_type_rest_url,
Expand Down Expand Up @@ -1502,13 +1503,15 @@ jQuery(document).ready(function ($) {

points_map: {
setup: async function () {
let payload = {
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
};
payload.query.list_all = mapbox_library_api.obj.settings.can_list_all;
let points = await window.makeRequest(
'POST',
mapbox_library_api.obj.settings.points_rest_url,
{
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
},
payload,
mapbox_library_api.obj.settings.rest_base_url,
);
this.load_layer(points);
Expand Down Expand Up @@ -1645,13 +1648,15 @@ jQuery(document).ready(function ($) {

let cluster_map = {
default_setup: async function () {
let payload = {
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
};
payload.query.list_all = mapbox_library_api.obj.settings.can_list_all;
let geojson = await window.makeRequest(
'POST',
mapbox_library_api.obj.settings.rest_url,
{
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
},
payload,
mapbox_library_api.obj.settings.rest_base_url,
);
cluster_map.load_layer(geojson);
Expand Down Expand Up @@ -1792,13 +1797,15 @@ jQuery(document).ready(function ($) {
behind_layer: null,
setup: async function (behind_layer = null) {
area_map.behind_layer = behind_layer;
let payload = {
post_type: mapbox_library_api.obj.settings.post_type,
query: mapbox_library_api.query_args || {},
};
payload.query.list_all = mapbox_library_api.obj.settings.can_list_all;
area_map.grid_data = await window.makeRequest(
'POST',
mapbox_library_api.obj.settings.totals_rest_url,
{
post_type: mapbox_library_api.obj.settings.post_type,
query: mapbox_library_api.query_args || {},
},
payload,
mapbox_library_api.obj.settings.rest_base_url,
);
await area_map.load_layer();
Expand Down Expand Up @@ -2051,15 +2058,18 @@ jQuery(document).ready(function ($) {

if (details.admin2_grid_id !== null) {
jQuery('#admin2_list').html(spinner_html);
let payload = {
grid_id: details.admin2_grid_id,
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
};
payload.query.list_all =
mapbox_library_api.obj.settings.can_list_all;
window
.makeRequest(
'POST',
mapbox_library_api.obj.settings.list_by_grid_rest_url,
{
grid_id: details.admin2_grid_id,
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
},
payload,
mapbox_library_api.obj.settings.rest_base_url,
)
.done((list_by_grid) => {
Expand All @@ -2071,15 +2081,18 @@ jQuery(document).ready(function ($) {
});
} else if (details.admin1_grid_id !== null) {
jQuery('#admin1_list').html(spinner_html);
let payload = {
grid_id: details.admin1_grid_id,
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
};
payload.query.list_all =
mapbox_library_api.obj.settings.can_list_all;
window
.makeRequest(
'POST',
mapbox_library_api.obj.settings.list_by_grid_rest_url,
{
grid_id: details.admin1_grid_id,
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
},
payload,
mapbox_library_api.obj.settings.rest_base_url,
)
.done((list_by_grid) => {
Expand All @@ -2091,15 +2104,18 @@ jQuery(document).ready(function ($) {
});
} else if (details.admin0_grid_id !== null) {
jQuery('#admin0_list').html(spinner_html);
let payload = {
grid_id: details.admin0_grid_id,
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
};
payload.query.list_all =
mapbox_library_api.obj.settings.can_list_all;
window
.makeRequest(
'POST',
mapbox_library_api.obj.settings.list_by_grid_rest_url,
{
grid_id: details.admin0_grid_id,
post_type: mapbox_library_api.post_type,
query: mapbox_library_api.query_args || {},
},
payload,
mapbox_library_api.obj.settings.rest_base_url,
)
.done((list_by_grid) => {
Expand Down
57 changes: 47 additions & 10 deletions dt-metrics/records/dynamic-records-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DT_Metrics_Dynamic_Records_Map extends DT_Metrics_Chart_Base
{

//slug and title of the top menu folder
public $base_slug = 'records'; // lowercase
public $base_slug; // lowercase
public $base_title;
public $post_type = 'contacts';
public $post_types = [];
Expand All @@ -18,17 +18,21 @@ class DT_Metrics_Dynamic_Records_Map extends DT_Metrics_Chart_Base
public $slug = 'dynamic_records_map'; // lowercase
public $js_object_name = 'wp_js_object'; // This object will be loaded into the metrics.js file by the wp_localize_script.
public $js_file_name = '/dt-metrics/records/dynamic-records-map.js'; // should be full file name plus extension
public $permissions = [ 'dt_all_access_contacts', 'view_project_metrics' ];
public $permissions = [ 'dt_all_access_contacts', 'view_project_metrics', 'multiplier' ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kodinkat multiplier is not a permission. Please explain

public $namespace = 'dt-metrics/records';
public $base_filter = [];

public function __construct() {
parent::__construct();
public function __construct( $base_slug, $base_title ) {
if ( !$this->has_permission() ){
return;
}

$this->base_slug = $base_slug;
$this->base_title = $base_title;

parent::__construct();

$this->title = __( 'Records Map', 'disciple_tools' );
$this->base_title = __( 'Contacts', 'disciple_tools' );

// Build post types array, ignoring some for now.
// TODO: Only select post types with valid location field types!
Expand Down Expand Up @@ -69,6 +73,8 @@ public function __construct() {
if ( "metrics/$this->base_slug/$this->slug" === $url_path ) {
add_action( 'wp_enqueue_scripts', [ $this, 'scripts' ], 99 );
}

$this->namespace = "dt-metrics/$this->base_slug/$this->slug";
add_action( 'rest_api_init', [ $this, 'add_api_routes' ] );
}

Expand Down Expand Up @@ -101,6 +107,7 @@ public function scripts() {
'map_key' => DT_Mapbox_API::get_key(),
'no_map_key_msg' => _x( 'To view this map, a mapbox key is needed; click here to add.', 'install mapbox key to view map', 'disciple_tools' ),
'map_mirror' => dt_get_location_grid_mirror( true ),
'can_list_all' => in_array( $this->base_slug, [ 'records' ] ),
'menu_slug' => $this->base_slug,
'post_type' => $this->post_type,
'post_types' => $this->post_type_options,
Expand Down Expand Up @@ -183,8 +190,14 @@ public function post_type_geojson( WP_REST_Request $request ){
break;
}

// Determine if record restriction (listing-all) should take effect.
$params = self::capture_list_all_setting( $params );

// Execute request query.
$response = Disciple_Tools_Mapping_Queries::post_type_geojson( $params['post_type'], $params, $offset, $limit );

// Ensure to unset assigned_post_ids for security reasons.
unset( $params['assigned_post_ids'] );
}

return [
Expand All @@ -202,7 +215,7 @@ public function cluster_geojson( WP_REST_Request $request ) {
$query = ( isset( $params['query'] ) && !empty( $params['query'] ) ) ? $params['query'] : [];
$query = dt_array_merge_recursive_distinct( $query, $this->base_filter );

return Disciple_Tools_Mapping_Queries::cluster_geojson( $post_type, $query );
return Disciple_Tools_Mapping_Queries::cluster_geojson( $post_type, self::capture_list_all_setting( $query ) );
}


Expand All @@ -215,7 +228,7 @@ public function get_grid_totals( WP_REST_Request $request ) {
$post_type = $params['post_type'];
$query = ( isset( $params['query'] ) && !empty( $params['query'] ) ) ? $params['query'] : [];
$query = dt_array_merge_recursive_distinct( $query, $this->base_filter );
$results = Disciple_Tools_Mapping_Queries::query_location_grid_meta_totals( $post_type, $query );
$results = Disciple_Tools_Mapping_Queries::query_location_grid_meta_totals( $post_type, self::capture_list_all_setting( $query ) );

$list = [];
foreach ( $results as $result ) {
Expand All @@ -236,7 +249,7 @@ public function get_list_by_grid_id( WP_REST_Request $request ) {
$query = ( isset( $params['query'] ) && !empty( $params['query'] ) ) ? $params['query'] : [];
$query = dt_array_merge_recursive_distinct( $query, $this->base_filter );

return Disciple_Tools_Mapping_Queries::query_under_location_grid_meta_id( $post_type, $grid_id, $query );
return Disciple_Tools_Mapping_Queries::query_under_location_grid_meta_id( $post_type, $grid_id, self::capture_list_all_setting( $query ) );
}


Expand All @@ -252,9 +265,33 @@ public function points_geojson( WP_REST_Request $request ) {
$query = ( isset( $params['query'] ) && !empty( $params['query'] ) ) ? $params['query'] : [];
$query = dt_array_merge_recursive_distinct( $query, $this->base_filter );

return Disciple_Tools_Mapping_Queries::points_geojson( $post_type, $query );
return Disciple_Tools_Mapping_Queries::points_geojson( $post_type, self::capture_list_all_setting( $query ) );
}

private function capture_list_all_setting( $params ) {
if ( !isset( $params['list_all'] ) || !$params['list_all'] ) {
$params['list_all'] = false;
$params['assigned_post_ids'] = self::list_assigned_post_ids( ( ( $params['post_type'] === 'system-users' ) ? 'contacts' : $params['post_type'] ), get_current_user_id() );
} else {
$params['list_all'] = true;
$params['assigned_post_ids'] = [];
}

return $params;
}

private function list_assigned_post_ids( $post_type, $user_id, $fields = [ 'ID' ], $limit = 500000 ): array {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kodinkat, lets add an extra param post_type_geojson with the restrict param and add the sql directly if it is present.
Only query subassigned of the post_type is contacts.
Also remember that "subassinged" does not use the $user_id, it needs to be the contact id.

$assigned_posts = DT_Posts::search_viewable_post( $post_type, [
'limit' => $limit,
'fields' => $fields,
'assigned_to' => [ $user_id ],
'subassigned' => [ $user_id ],
'shared_with' => [ $user_id ]
] );

return array_map( function ( $post ) {
return $post->ID;
}, $assigned_posts['posts'] ?? [] );
}

}
new DT_Metrics_Dynamic_Records_Map();
Loading