<?php
/**
 *
 * functions to displaying the map with all members
 * depens on plugins: LeafletMaps, ACF fields
 * 
 *
 * @link       https://xarxaprod.cat
 * @since      1.0.0
 * @package    xarxaprod-wp-plugin
 * @subpackage xarxaprod-wp-plugin/includes
 * @author     Jorge - vitrubio.net <jorge@vitrubio.net>
 *
 */

// Detect plugin. For use on Front End only.
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// check for plugin using plugin name
if ( ! is_plugin_active( 'leaflet-map/leaflet-map.php' ) ) {
//  echo '<p class="warning">The plugin <a hfref="https://wordpress.org/plugins/leaflet-map/">LeafletMap</a> is needed to show this map</p>';
} else {
  if (! function_exists( 'xarxaprod_show_leaflet_map' )) {
    function xarxaprod_show_leaflet_map() {
    // show the map 
      echo do_shortcode( '[leaflet-map fitbounds max_zoom="16" lat="41.3922" lng="2.1755" height="100%" width="100%" tileurl=https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png  scrollwheel detect-retina]');
    }
  };

  if ( ! is_plugin_active( 'acf-openstreetmap-field/index.php' ) ) {
    echo '<p class="warning">The plugin <a hfref="https://wordpress.org/plugins/acf-openstreetmap-field/">ACF OpenStreetMap Field</a> is needed to show the points in the map</p>';
  } else {
    if (! function_exists( 'xarxaprod_show_leaflet_associat_blob' )) {
      function xarxaprod_show_leaflet_associat_blob() {
        // get the field values
        // https://www.advancedcustomfields.com/resources/get_field/
        // var_dump get_field('');
        $args = get_field('xxp_associat_osm_map',false, false);
    
        if ( count( $args['markers'] ) ) { 
          foreach ( $args['markers'] as $marker ) {
            // define the fields for the blob
            if ( get_field('xxp_associat_citymap') ){
              // if city for map written use it
              $associat_address = get_field('xxp_associat_citymap'); 
            } else {
              // else use the ACF OpenStreetMap nominatim value
              $associat_address = $marker['label'];
            };
            $associat_name = esc_html( get_the_title() );
            $associat_xxp_weblink = '<a href="'. get_the_permalink() . '">' . $associat_name . '</a>';
            $associat_own_weblink = '<a href="' . get_field('xxp_associat_web') . '" class="">' . get_field('xxp_associat_web') . '</a>';
            $associat_email = '<a href="mailto:' . get_field('xxp_associat_mail') . '" class="">' . get_field('xxp_associat_mail') . '</a>';
            $associat_lat = $marker['lat'];
            $associat_lng = $marker['lng'];
            if( get_field('xxp_associat_colormap') ){ $associat_blobcolor = get_field('xxp_associat_colormap'); }else{ $associat_blobcolor = '#555'; };
            echo do_shortcode( '
                              [leaflet-marker svg background="'. $associat_blobcolor . '" color="'. $associat_blobcolor . '" iconSize="22,22" iconClass="dashicons dashicons-info" opacity="0.6" title="' . $associat_name . '" lat=' . $associat_lat . ' lng=' . $associat_lng . ']' 
               . '<p>' . $associat_address . '</p>'
               . '<h5><b>' . $associat_xxp_weblink . '</b></h5>'
               . '<p>' . $associat_own_weblink . '</p>'
               . '<p>' . $associat_email . '</p>'
               . '[/leaflet-marker]' 
           ); // end shortcode each marker
         }; // end each marker     
        }; // end count all markers
      };
    }; //end function xarxaprod_show_leaflet_associat_blob
  };//end ACF OpenStreetMap active 
};