You are here

function getlocations_setup_js in Get Locations 7

Same name and namespace in other branches
  1. 6.2 getlocations.module \getlocations_setup_js()
  2. 6 getlocations.module \getlocations_setup_js()
  3. 7.2 getlocations.module \getlocations_setup_js()

Function to setup the map scripts

Parameters

array $defaults: Required. the current settings. Sets up the call to googlemaps, css and the relevant getlocations js in html head

2 calls to getlocations_setup_js()
getlocations_fields_handler_filter_distance::value_form in modules/getlocations_fields/handlers/getlocations_fields_handler_filter_distance.inc
Options form subform for setting options.
getlocations_setup_map in ./getlocations.module
Function to setup the map scripts

File

./getlocations.module, line 1025
getlocations.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_setup_js($defaults, $googleonly = FALSE, $sv = FALSE) {
  global $language;
  $current_js = drupal_get_js();
  $current_css = drupal_get_css();
  static $getlocations_js_weight = 0;
  $getlocations_js_weight++;
  if (is_numeric($defaults['getlocations_js_weight']) && $defaults['getlocations_js_weight'] != 0) {
    $getlocations_js_weight += $defaults['getlocations_js_weight'];
  }
  $key = variable_get('getlocations_api3_key', '');
  $clientID = variable_get('getlocations_api3_clientID', '');
  $signature = variable_get('getlocations_api3_signature', '');
  $channel = variable_get('getlocations_api3_channel', '');
  $signed_in = variable_get('getlocations_api3_signed_in', 0);
  $aggr = getlocations_aggr_get() ? TRUE : FALSE;

  // v3 googlemaps API
  $query = array();
  if (!empty($key)) {
    $query['key'] = $key;
    if ($signed_in) {
      $query['signed_in'] = 'true';
    }
  }
  if (!empty($clientID)) {
    $query['client'] = $clientID;
  }
  if (!empty($signature)) {
    $query['signature'] = $signature;
  }
  if (!empty($channel)) {
    $query['channel'] = $channel;
  }
  $query['sensor'] = getlocations_is_mobile() ? 'true' : 'false';
  $map_version = variable_get('getlocations_map_version', '');
  if ($map_version) {
    $query['v'] = $map_version;
  }
  if (isset($language->language)) {
    $query['language'] = $language->language;
  }
  if ($defaults['region_bias']) {
    $query['region'] = $defaults['region_bias'];
  }
  $libraries = array();
  if ($defaults['places'] || module_exists('getlocations_search')) {
    $libraries['places'] = 'places';
  }

  // not active but can be used by other modules
  if ($defaults['geometry']) {
    $libraries['geometry'] = 'geometry';
  }
  if ($defaults['visualization']) {
    $libraries['visualization'] = 'visualization';
  }
  if ($defaults['drawing']) {
    $libraries['drawing'] = 'drawing';
  }
  if ($defaults['adsense']) {
    $libraries['adsense'] = 'adsense';
  }

  // more libraries here
  // check for places library
  if (getlocations_places_check()) {
    $libraries['places'] = 'places';
  }

  // load libraries if any
  if (count($libraries)) {
    $query['libraries'] = implode(',', $libraries);
  }
  if (!isset($defaults['is_https']) || !$defaults['is_https']) {
    global $is_https;
    $defaults['is_https'] = $is_https;
  }
  $scheme = $defaults['is_https'] ? 'https' : 'http';
  $gmapdomain = "maps.googleapis.com/maps/api/js";
  $gmaplink = url($scheme . '://' . $gmapdomain, array(
    'query' => $query,
  ));
  if (!stristr($current_js, $scheme . '://' . $gmapdomain)) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'external';
    drupal_add_js($gmaplink, $js_opts);
    $getlocations_js_weight++;
  }
  if ($defaults['use_jsapi']) {
    $jsapilink = url($scheme . '://' . 'www.google.com/jsapi');
    if (!stristr($current_js, $jsapilink)) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'external';
      drupal_add_js($jsapilink, $js_opts);
      $getlocations_js_weight++;
    }
  }

  // geocoder-js
  if ($defaults['geocoder_enable'] && ($geocoder_path = getlocations_get_geocoder_path())) {
    if (!stristr($current_js, $geocoder_path)) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($geocoder_path, $js_opts);
      $getlocations_js_weight++;
    }
  }
  if ($googleonly) {
    return;
  }
  $getlocations_paths = getlocations_paths_get();
  if (!stristr($current_css, GETLOCATIONS_PATH . '/getlocations.css')) {
    drupal_add_css(GETLOCATIONS_PATH . '/getlocations.css');
  }

  // streetview only
  if ($sv && module_exists('getlocations_fields')) {
    $getlocations_fields_paths = getlocations_fields_paths_get();
    if (!stristr($current_js, $getlocations_fields_paths['getlocations_fields_streetview_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_fields_paths['getlocations_fields_streetview_path'], $js_opts);
      $getlocations_js_weight++;
    }
    return;
  }
  if (!stristr($current_js, $getlocations_paths['getlocations_icons_path'])) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['getlocations_icons_path'], $js_opts);
    $getlocations_js_weight++;
  }
  $markerfile = getlocations_get_markerfile();
  if ($markerfile && !stristr($current_js, $markerfile)) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($markerfile, $js_opts);
    $getlocations_js_weight++;
  }
  if ($defaults['markermanagertype'] == 1 && !stristr($current_js, $getlocations_paths['markermanager_path'])) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['markermanager_path'], $js_opts);
    $getlocations_js_weight++;
  }
  elseif ($defaults['markermanagertype'] == 2 && !stristr($current_js, $getlocations_paths['markerclusterer_path'])) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['markerclusterer_path'], $js_opts);
    $getlocations_js_weight++;
    if (file_exists(GETLOCATIONS_PATH . '/getlocations.markercluster_override.inc')) {
      module_load_include('inc', 'getlocations', 'getlocations.markercluster_override');
    }
    else {
      module_load_include('inc', 'getlocations', 'getlocations.markercluster');
    }
    $markerclusterer_styles = getlocations_get_markerclusterer_styles();
    $js_opts = array();
    $js_opts['type'] = 'setting';
    drupal_add_js(array(
      'getlocations_markerclusterer' => $markerclusterer_styles,
    ), $js_opts);
  }
  if ($defaults['markeraction'] == 2 && !stristr($current_js, $getlocations_paths['infobubble_path'])) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['infobubble_path'], $js_opts);
    $getlocations_js_weight++;
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['infobubble_extended_path'], $js_opts);
    $getlocations_js_weight++;
    if (file_exists($getlocations_paths['infobubble_options_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['infobubble_options_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }
  elseif ($defaults['markeraction'] == 1 && file_exists($getlocations_paths['infowindow_options_path']) && !stristr($current_js, $getlocations_paths['infowindow_options_path'])) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['infowindow_options_path'], $js_opts);
    $getlocations_js_weight++;
  }

  // load main getlocations.js
  if (!stristr($current_js, $getlocations_paths['getlocations_path'])) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['getlocations_path'], $js_opts);
    $getlocations_js_weight++;
  }
  if ($defaults['polygons_enable']) {
    if (!stristr($current_js, $getlocations_paths['getlocations_polygons_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['getlocations_polygons_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }
  if ($defaults['rectangles_enable']) {
    if (!stristr($current_js, $getlocations_paths['getlocations_rectangles_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['getlocations_rectangles_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }
  if ($defaults['circles_enable']) {
    if (!stristr($current_js, $getlocations_paths['getlocations_circles_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['getlocations_circles_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }
  if ($defaults['polylines_enable']) {
    if (!stristr($current_js, $getlocations_paths['getlocations_polylines_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['getlocations_polylines_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }

  // search_places
  if ($defaults['search_places']) {
    if (!stristr($current_js, $getlocations_paths['getlocations_search_places_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['getlocations_search_places_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }

  // categories
  if ($defaults['category_method'] && $defaults['category_showhide_buttons']) {
    if (!stristr($current_js, $getlocations_paths['getlocations_categories_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['getlocations_categories_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }

  // preview map
  if ($defaults['extcontrol'] == 'preview_map') {
    if (!stristr($current_js, $getlocations_paths['getlocations_preview_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['getlocations_preview_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }

  // geojson
  if ($defaults['geojson_enable'] && ($geojson_path = getlocations_get_geojson_path())) {
    if (!stristr($current_js, $geojson_path)) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($geojson_path, $js_opts);
      $getlocations_js_weight++;
    }
    if ($defaults['geojson_data']) {
      if (!stristr($current_js, $getlocations_paths['getlocations_geojson_path'])) {
        $js_opts = array();
        $js_opts['weight'] = $getlocations_js_weight;
        $js_opts['type'] = 'file';
        $js_opts['preprocess'] = $aggr;
        drupal_add_js($getlocations_paths['getlocations_geojson_path'], $js_opts);
        $getlocations_js_weight++;
      }
    }
  }
  if ($defaults['geo_enable'] && !stristr($current_js, $getlocations_paths['getlocations_geo_path'])) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight + 10;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['getlocations_geo_path'], $js_opts);
    $getlocations_js_weight++;
  }
  if (module_exists('field_group') && !stristr($current_js, $getlocations_paths['getlocations_field_group_path'])) {
    $js_opts = array();
    $js_opts['weight'] = $getlocations_js_weight + 10;
    $js_opts['type'] = 'file';
    $js_opts['preprocess'] = $aggr;
    drupal_add_js($getlocations_paths['getlocations_field_group_path'], $js_opts);
    $getlocations_js_weight++;
  }

  // What3Words
  if ($defaults['what3words_enable']) {
    if (!stristr($current_js, $getlocations_paths['getlocations_what3words_path'])) {
      $js_opts = array();
      $js_opts['weight'] = $getlocations_js_weight;
      $js_opts['type'] = 'file';
      $js_opts['preprocess'] = $aggr;
      drupal_add_js($getlocations_paths['getlocations_what3words_path'], $js_opts);
      $getlocations_js_weight++;
    }
  }
}