<?php

/**
 * @file getlocations_mapbox.module
 * @author Bob Hutchinson http://drupal.org/user/52366
 * @copyright GNU GPL
 *
 * Provides mapbox maps integration for Leaflet.
 *
 *
 */

function getlocations_mapbox_get_map_layers() {
  $mapbox_info = variable_get('getlocations_leaflet_mapbox', '');
  $add = array();
  if (! empty($mapbox_info) && isset($mapbox_info['mapbox_info']) && is_array($mapbox_info['mapbox_info'])) {
    foreach ($mapbox_info['mapbox_info'] AS $k => $info) {
      if (isset($info['key']) && $info['key'] && isset($info['label']) && $info['label']) {
        $add['MapBox' . '.' . $info['label']] = array(
          'label' => $info['label'],
          'type' => (isset($info['type']) ? $info['type'] : 'base'),
          'options' => array(
            'mapid' => $info['key']
          ),
        );
      }
    }
  }
  return $add;
}

function getlocations_mapbox_settings_form() {
  $form = array();
  // mapbox
  $getlocations_leaflet_mapbox = variable_get('getlocations_leaflet_mapbox', array('mapbox_info' => array()));
  $mapnum = count($getlocations_leaflet_mapbox['mapbox_info']);
  $form['getlocations_leaflet_mapbox'] = array(
    '#type' => 'fieldset',
    '#title' => t('MapBox maps'),
    // This will store all the defaults in one variable.
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => ($mapnum ? FALSE : TRUE),
  );
  if ($mapnum == 0) {
    $mapnum = 2;
  }
  else {
    $mapnum++;
  }
  for ($ct = 0; $ct < $mapnum; $ct++) {
    $form['getlocations_leaflet_mapbox']['mapbox_info'][$ct]['label'] = getlocations_element_map_tf(
      t('Name of map @n', array('@n' => $ct + 1)),
      (isset($getlocations_leaflet_mapbox['mapbox_info'][$ct]['label']) ? $getlocations_leaflet_mapbox['mapbox_info'][$ct]['label'] : ''),
      t('The name of the map, to be shown on selection lists'),
      40
    );
    $form['getlocations_leaflet_mapbox']['mapbox_info'][$ct]['key'] = getlocations_element_map_tf(
      t('Key of map @n', array('@n' => $ct + 1)),
      (isset($getlocations_leaflet_mapbox['mapbox_info'][$ct]['key']) ? $getlocations_leaflet_mapbox['mapbox_info'][$ct]['key'] : ''),
      t('The identifier provided by <a target="_blank" href="@mapbox">MapBox</a>', array('@mapbox' => 'https://www.mapbox.com/')),
      40
    );
    $form['getlocations_leaflet_mapbox']['mapbox_info'][$ct]['type'] = getlocations_element_dd(
      t('Type of map @n', array('@n' => $ct + 1)),
      (isset($getlocations_leaflet_mapbox['mapbox_info'][$ct]['type']) ? $getlocations_leaflet_mapbox['mapbox_info'][$ct]['type'] : 'base'),
      array('base' => t('Base'), 'overlay' => t('Overlay'))
    );
  }
  // for theme
  $form['getlocations_leaflet_mapbox']['mapnum'] = array('#type' => 'value', '#value' => $mapnum);

  return $form;
}

function getlocations_mapbox_settings_validate($form_state) {
  // dump empty mapbox
  $mapnum = count($form_state['values']['getlocations_leaflet_mapbox']['mapbox_info']);
  $emptyfound = FALSE;
  for ($ct = 0; $ct < $mapnum; $ct++) {
    if (empty($form_state['values']['getlocations_leaflet_mapbox']['mapbox_info'][$ct]['key']) || empty($form_state['values']['getlocations_leaflet_mapbox']['mapbox_info'][$ct]['label'])) {
      unset($form_state['values']['getlocations_leaflet_mapbox']['mapbox_info'][$ct]);
      $emptyfound = TRUE;
    }
  }
  // reorder them in case some clown empties one before the last one
  if ($emptyfound) {
    $mapnum = count($form_state['values']['getlocations_leaflet_mapbox']['mapbox_info']);
    $temp = array();
    $ct = 0;
    foreach ($form_state['values']['getlocations_leaflet_mapbox']['mapbox_info'] AS $k => $v) {
      $temp[$ct] = $v;
      $ct++;
    }
    if (count($temp)) {
      $form_state['values']['getlocations_leaflet_mapbox']['mapbox_info'] = $temp;
    }
  }

  return $form_state;
}

