aboutsummaryrefslogtreecommitdiff
path: root/gmap_arcgis.module
blob: 1a739287a5eae1573388e4fef52104ca484120d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php

// $Id$

/**
 * @file
 * Support for ArcGIS into GMap.
 *
 * This module implements support for ArcGIS API into a GMap.
 */

/**
 * Implementation of hook_perm()
 */
function gmap_arcgis_perm() {
  return array('access gmap_arcgis content');
}

/**
 * Implementation of hook_gmap().
 */
function gmap_arcgis_gmap($op, &$map) {
    if ($op == 'pre_theme_map') {
        /**
         * Use $map to change and add custom overlays into
         * a given map instance.
         */
        if (isset($map['arcgis'])) {
            global $base_url;

            /**
             * Make sure that shapes js code are loaded.
             *
             * This is needed as a workaround to the current GMap
             * module that doesn't add shapes js if $map['shapes']
             * wasn't previously defined.
             */
            if (!empty($map['shapes'])) {
                $gmap_path = drupal_get_path('module', 'gmap') . '/js/';
                drupal_add_js($gmap_path . 'shapeloader_static.js');
                drupal_add_js($gmap_path . 'gmap_shapes.js');
            }

            /**
             * The order to load the following js code is important.
             */
            // Add Google js API
            $key = variable_get('gmap_api_key', '');
            $script = '<script type="text/javascript" ';
            drupal_set_html_head($script . 'src="http://www.google.com/jsapi?key=' . $key . '&sensor=true&language=pt-BR"></script>');

            // Add ArcGIS js API
            drupal_set_html_head($script . 'src="'. $base_url .'/sites/all/libraries/google-maps-utility-library-v3/arcgislink/src/arcgislink_compiled.js"></script>');

            // Setup Map Id
            drupal_add_js(array('gmap_arcgis' => array('id' => $map['id'])), 'setting');

            // Add custom js
            $gmap_arcgis_path = drupal_get_path('module', 'gmap_arcgis') . '/';
            drupal_add_js($gmap_arcgis_path . 'gmap_arcgis.js');
            //jquery_ui_add('ui.dialog');
            jquery_ui_add('effects.fold');
            jquery_ui_add('effects.explode');
            jquery_ui_add('effects.core');
            jquery_ui_add('ui.slider');

            $path_ui_css = drupal_get_path('module', 'jquery_ui');
            drupal_add_css($path_ui_css . '/jquery.ui/themes/default/ui.all.css');
        }
    }
}

/**
 * Set the title of the marker;
 * @param array $marker
 */
function add_title_on_marker(&$marker) {
    if (!empty($marker['text'])) {
        $dom = new DOMDocument();
        $dom->loadHTML($marker['text']);
        //$dom->preserveWhiteSpace = false;

        $divs = $dom->getElementsByTagName('div');
        if ($divs) {
            foreach ($divs AS $div) {

                if ($div->getAttribute('id') === 'nid') {

                    $x = node_load($div->nodeValue);

                    $titulo = $x->field_categoria[0]['value'] . ' ' . $x->field_preposicao[0]['value'] . ' ' . $x->title;
                    $marker['opts'] = array('title' => $titulo, 'nid' => $div->nodeValue, 'category' => 'marker_uc');
                }
            }
        }
    }
}

/**
 * Wrapper to add custom markers on map. Need to be out somewhere
 * @param array marker
 */
function __addMarker(&$map, $data) {
    $map['markers'][] = array(
        'latitude' => $data['latitude'],
        'longitude' => $data['longitude'],
        'markername' => $data['markername'],
        'offset' => $data['offset'],
        'text' => $data['body'],
        'opts' => array(
            'title' => $data['title'],
            'nid' => $data['nid'],
            'category' => $data['category'],
        ),
    );
}

/**
 * Implementation of hook_block()
 */
function gmap_arcgis_block($op='list', $delta=0, $edit=array()) {
    global $map;
    switch ($op) {
        case 'list':
            $blocks[0]['info'] = t('Protected areas list');
            return $blocks;

        case 'view':
            $blocks['subject'] = t('Protected areas list');
            $blocks['content'] = drupal_get_form('uc_form') .
                    '<div id="abrefecha" class="close_gmap_arcgis"><a href="javascript:void(0)" id="clickmapa" title="Temas">X</a></div>';

            return $blocks;

        case 'configure':

        case 'save':
    }
}

// TODO: move to isa_gmap_overlays
function gmap_arcgis_link_nota_tecnica() {
  return l(t('Sources'), 'quem-somos', array('fragment' => 'tabset-tab-3', 'external' => TRUE));
}

/**
 * Implementation of hook_menu().
 */
function gmap_arcgis_menu() {
  $items['gmap_arcgis/mapserver'] = array(
    'title'            => 'MapServer Proxy',
    'description'      => 'Caches requests to a MapServer',
    'page callback'    => 'gmap_arcgis_mapserver',
    'access arguments' => array('access content'),
    'type'             => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Menu callback.
 *
 * Caches dynamic ArcGIS requests by ignoring the timestamp pattern.
 */
function gmap_arcgis_mapserver() {
  header('Content-Type: text/plain;charset=utf-8');

  // The ArcGIS server URL.
  $base    = 'http://gis2.socioambiental.org:8399/arcgis/rest/services/';

  // The dynamic pattern we want to exclude.
  $pattern = '/_[0-9]*._callback/';

  $dynamic = FALSE;
  $request = $_SERVER['QUERY_STRING'];
  $query   = str_replace('q=gmap_arcgis/mapserver/', '', $request);
  $query   = preg_replace('/&/', '?', $query, 1);
  $dest    = $base . $query;
  $process = curl_init($dest);

  if (preg_match($pattern, $dest, $timestamp) == 1) {
    $dynamic = TRUE;
    $key     = sha1(preg_replace($pattern, '', $dest));
  }
  else {
    $key = sha1($dest);
  }

  $data = cache_get($key, 'cache_page');

  if ($data == 0) {
    curl_setopt($process, CURLOPT_TIMEOUT, 30);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);

    $return = curl_exec($process);
    curl_close($process);

    cache_set($key, $return, 'cache_page', CACHE_TEMPORARY);
  }
  else {
    $return = $dynamic == TRUE ? preg_replace($pattern, $timestamp[0], $data->data) : $data->data;

    // Cache expiration.
    if ($data->created < time() - 3600) {
      cache_clear_all($key, 'cache_page');
    }
  }

  echo $return;
  exit;
}