You are here

function gm3_region_get_points in Google Maps API V3 7

gm3_region_get_points

1 string reference to 'gm3_region_get_points'
gm3_region_menu in gm3_region/gm3_region.module
Implementation of hook_menu().

File

gm3_region/gm3_region.callback.inc, line 100

Code

function gm3_region_get_points($region_ids) {
  $region_ids = explode(",", $region_ids);
  print "[";
  $first = TRUE;
  foreach ($region_ids as $region_id) {
    if ($first) {
      $first = FALSE;
    }
    else {
      print ",";
    }
    $cid = $region_id;
    $polygons = cache_get($cid, 'cache_gm3_polygon');
    if ($polygons === false) {
      module_load_include('functions.inc', 'gm3');
      $region_id_parts = explode(":", $region_id);

      // Load the geoPHP library.
      gm3_load_geophp();
      $wkt_reader = new WKT();
      switch (count($region_id_parts)) {
        case 4:
          $region_id_4_parts = explode("-", $region_id_parts[3]);
          $row = db_select('gm3_region_data', 'g')
            ->condition('level_4_code', $region_id_4_parts[1])
            ->condition('level_3_code', $region_id_4_parts[0])
            ->fields('g')
            ->execute()
            ->fetch();
          break;
        case 3:
          $row = db_select('gm3_region_data', 'g')
            ->condition(db_or()
            ->condition('level_3_code', $region_id_parts[2])
            ->condition('name', $region_id_parts[2]))
            ->condition('level_4_code', '')
            ->fields('g')
            ->execute()
            ->fetch();
          break;
        case 2:
          if (is_numeric($region_id_parts[1])) {
            $row = db_select('gm3_region_data', 'g')
              ->condition('level_2_code', $region_id_parts[1])
              ->condition('level_3_code', '')
              ->fields('g')
              ->execute()
              ->fetch();
          }
          else {
            $row = db_select('gm3_region_data', 'g')
              ->condition('name', $region_id_parts[1])
              ->condition('level_3_code', '')
              ->fields('g')
              ->execute()
              ->fetch();
          }
          break;
        case 1:
          if (is_numeric($region_id_parts[0])) {
            $row = db_select('gm3_region_data', 'g')
              ->condition('level_1_code', $region_id_parts[0])
              ->condition('level_2_code', 0)
              ->fields('g')
              ->execute()
              ->fetch();
          }
          else {
            $row = db_select('gm3_region_data', 'g')
              ->condition('name', $region_id_parts[0])
              ->condition('level_2_code', 0)
              ->fields('g')
              ->execute()
              ->fetch();
          }
          break;
      }
      if ($row) {
        $polygons = $wkt_reader
          ->read($row->polygons, TRUE);
        $polygons = $polygons
          ->out('json');
        $polygons = '{"' . $region_id . '":{"name":"' . $row->name . '","shape":' . $polygons . '}}';
      }
      cache_set($cid, $polygons, 'cache_gm3_polygon', CACHE_PERMANENT);
    }
    else {
      $polygons = $polygons->data;
    }
    print $polygons;
  }
  print "]";
  exit;
}