summaryrefslogtreecommitdiffstats
path: root/supported/location.de.inc
blob: 8f65dcb24958ebeb87a41b9b3bb2161bf746b429 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php

/**
 * @file
 * Deutschland.
 */

/**
 * Returns an associative array of states/territories.
 */
function location_province_list_de() {
  return array(
    'BB' => 'Brandenburg',
    'BE' => 'Berlin',
    'BW' => 'Baden-Württemberg',
    'BY' => 'Bayern',
    'HB' => 'Bremen',
    'HE' => 'Hessen',
    'HH' => 'Hamburg',
    'MV' => 'Mecklenburg-Vorpommern',
    'NI' => 'Niedersachsen',
    'NW' => 'Nordrhein-Westfalen',
    'RP' => 'Rheinland-Pfalz',
    'SH' => 'Schleswig-Holstein',
    'SL' => 'Saarland',
    'SN' => 'Sachsen',
    'ST' => 'Sachsen-Anhalt',
    'TH' => 'Thüringen'
  );
}

/**
 * Map link.
 *
 * @param array $location
 *   Is an associative array that represents a full location where
 *        'street'       => the street portions of the location
 *        'supplemental' => additional street portion of the location
 *        'province'     => the province, state, or territory
 *        'country'      => lower-cased two-letter ISO code (REQUIRED)
 *
 * @param array $hide
 *   Is associative array that represents a full location in the same way that
 *       parameter $location_b does.
 *
 * @returns string
 *   A link to driving directions
 */
function location_map_link_de($location = array(), $hide = array()) {
  $map_links = array();
  // For now, just call the suchen (tinfo) function.  May want to make this configurable on some level
  // in order to maintain freedom of choice so users and site-admins don't have to be slaves
  // to tinfo!.... not that I have anything personal against tinfo!.
  if ($link = _location_map_link_de_suchen($location)) {
    $map_links['suchen'] = $link;
  }

  return $map_links;
}

/**
 * Map link.
 */
function _location_map_link_de_suchen($location = array()) {
  $get_query = '?';
  $get_query .= 'where=';
  $query_parts = array();
  if (isset($location['street'])) {
    $query_parts[] = $location['street'];
  }
  if ($location['postal_code'] != '') {
    $query_parts[] = $location['postal_code'];
  }
  if ($location['city'] != '') {
    $query_parts[] = $location['city'];
  }
  $get_query .= urlencode(implode(', ', $query_parts));

  return ('http://www.suchen.de/lokalmap' . $get_query);
}

/**
 * Providers.
 */
function location_map_link_de_providers() {
  return array(
    'suchen' => array(
      'name' => 'suchen.de (T-Info)',
      'url' => 'http://www.suchen.de/',
      'tos' => 'http://www.suchen.de/agb',
    ),
    'google' => array(
      'name' => t('Google Maps'),
      'url' => 'http://maps.google.com',
      'tos' => 'http://www.google.com/help/terms_local.html',
    ),
  );
}

/**
 * Default providers.
 */
function location_map_link_de_default_providers() {
  return array('google');
}

/**
 * Directions link.
 *
 * For now, assume site-admin wants driving directions linked to tinfo!
 * Maybe later, we can add some kind of country-specific settings page that allows the site-admin to
 * decide which site to link to for driving directions.
 *
 * @param array $location_a
 *   Is an associative array that represents a full location where
 *        'street'       => the street portions of the location
 *        'supplemental' => additional street portion of the location
 *        'province'     => the province, state, or territory
 *        'country'      => lower-cased two-letter ISO code (REQUIRED)
 *
 * @param array $location_b
 *   Is associative array that represents a full location in the same way that
 *       parameter $location_b does.
 *
 * @returns string
 *   A link to driving directions
 */
function location_driving_directions_link_de($location_a, $location_b) {
  return _location_driving_directions_link_de_suchen($location_a, $location_b);
}

/**
 * Directions link.
 *
 * @param array $location_a
 *   Is an associative array that represents a full location where
 *        'street'       => the street portions of the location
 *        'supplemental' => additional street portion of the location
 *        'province'     => the province, state, or territory
 *        'country'      => lower-cased two-letter ISO code (REQUIRED)
 *
 * @param array $location_b
 *   Is associative array that represents a full location in the same way that
 *       parameter $location_b does.
 *
 * @return string
 *   URL with HTTP GET variables
 *    Depending on how full the locationes are, the URL will either point to the driving directions
 *    on tinfo! or, if only partial locationes are provided, a URL that points to the *form* for
 *    tinfo! driving directions where the form is filled with whatever fields have been provided
 *    for the partial location(es).
 */
function _location_driving_directions_link_de_suchen($location_a, $location_b) {

  foreach ($location_a as $field => $value) {
    $location_a[$field] = trim($value);
  }

  foreach ($location_b as $field => $value) {
    $location_b[$field] = trim($value);
  }

  if ($location_a['country'] == 'de' and $location_b['country'] == 'de') {
    $get_query = '?';

    // VON.
    $query_parts = array();
    if (isset($location_a['street'])) {
      $query_parts[] = $location_a['street'];
    }
    if ($location_a['postal_code'] != '') {
      $query_parts[] = $location_a['postal_code'];
    }
    if ($location_a['city'] != '') {
      $query_parts[] = $location_a['city'];
    }
    $get_query .= 'route_from=' . urlencode(implode(', ', $query_parts));

    // NACH.
    $query_parts = array();
    if (isset($location_b['street'])) {
      $query_parts[] = $location_b['street'];
    }
    if ($location_b['postal_code'] != '') {
      $query_parts[] = $location_b['postal_code'];
    }
    if ($location_b['city'] != '') {
      $query_parts[] = $location_b['city'];
    }
    $get_query .= '&amp;route_to=' . urlencode(implode(', ', $query_parts));

    return ('http://www.suchen.de/route' . $get_query);
  }
}

/**
 * Google link.
 */
function location_map_link_de_google($location = array()) {
  $query_params = array();

  foreach (array('street', 'postal_code', 'city', 'country') as $field) {
    if (isset($location[$field])) {
      $query_params[] = $location[$field];
    }
  }

  if (count($query_params)) {
    return ('http://maps.google.com?q=' . urlencode(implode(', ', $query_params)));
  }
  else {
    return NULL;
  }
}

/**
 * Theme callback.
 */
function theme_location_de($location = array(), $hide = array()) {
  $output = '';
  if (count($location)) {
    $output .= "\n";
    $output .= '<div class="location vcard"><div class="adr">' . "\n";
    if (!empty($location['name']) && !in_array('name', $hide)) {
      $output .= '<div class="fn">' . $location['name'] . '</div>';
    }

    if (!empty($location['street']) && !in_array('street', $hide)) {
      $output .= '<div class="street-address">' . $location['street'];
      if (!empty($location['additional']) && !in_array('street', $hide)) {
        $output .= ' ' . $location['additional'];
      }
      $output .= '</div>';
    }

    if ((!empty($location['city']) && !in_array('city', $hide)) ||
      (!empty($location['postal_codet']) && !in_array('postal_code', $hide))
    ) {

      $city_postal = array();

      if (!empty($location['postal_code']) && !in_array('postal_code', $hide)) {
        $city_postal[] = '<span class="postal-code">' . $location['postal_code'] . '</span>';
      }

      if (!empty($location['city']) && !in_array('city', $hide)) {
        $city_postal[] = '<span class="locality">' . $location['city'] . '</span>';
      }

      $output .= '<div>' . implode(' ', $city_postal) . '</div>';
    }

    if (!in_array('country', $hide)) {
      $output .= '<div class="country-name">' . t('Germany') . '</div>';
    }

    if (location_has_coordinates($location)) {
      $output .= '<div class="geo"><abbr class="latitude" title="' . $location['latitude'] . '" /><abbr class="longitude" title="' . $location['latitude'] . '" /></div>';
    }

    $output .= '</div></div>';

  }

  return $output;
}

/**
 * Returns a lat/lon pair of the approximate center of the given postal code in the given country.
 *
 * @param array $location
 *   An associative array $location where only postal code and country are necessary, but can have the keys:
 *     'street'       => the street portion of the location
 *     'supplemental' => additional street portion of the location
 *     'province'     => the province, state, or territory
 *     'country'      => lower-cased two-letter ISO code (REQUIRED)
 *     'postal_code'  => the international postal code for this location (REQUIRED)
 *
 * @return array
 *   An associative array where
 *      'lat' => approximate latitude of the center of the postal code's area
 *      'lon' => approximate longitude of the center of the postal code's area
 */
function location_get_postalcode_data_de($location = array()) {
  $dash_index = strpos($location['postal_code'], '-');
  // First we strip slash off if we're dealing with a 9-digit US zipcode.
  if ($dash_index === FALSE) {
    $location['postal_code'] = substr($location['postal_code'], 0, $dash_index);
  }

  // Now we pad the thing and query.
  $row = db_query(
    "SELECT * FROM {zipcodes} where country = :country AND zip = :zip",
    array(
      ':country' => $location['country'],
      ':zip' => str_pad($location['postal_code'], 5, "0", STR_PAD_LEFT),
    )
  )->fetchObject();
  if ($row) {
    return array(
      'lat' => $row->latitude,
      'lon' => $row->longitude,
      'city' => $row->city,
      'province' => $row->state,
      'country' => $row->country,
    );
  }
  else {
    return NULL;
  }
}

/**
 * Returns minimum and maximum latitude and longitude needed to create a bounding box.
 */
function location_bounds_de() {
  return array(
    'minlng' => 5.87225,
    'minlat' => 47.2249,
    'maxlng' => 15.04765,
    'maxlat' => 55.0839,
  );
}