location.module 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. <?php
  2. /**
  3. * @file
  4. * Location module main routines.
  5. * An implementation of a universal API for location manipulation. Provides functions for
  6. * postal_code proximity searching, deep-linking into online mapping services. Currently,
  7. * some options are configured through an interface provided by location.module.
  8. */
  9. define('LOCATION_PATH', drupal_get_path('module', 'location'));
  10. define('LOCATION_LATLON_UNDEFINED', 0);
  11. define('LOCATION_LATLON_USER_SUBMITTED', 1);
  12. define('LOCATION_LATLON_GEOCODED_APPROX', 2);
  13. define('LOCATION_LATLON_GEOCODED_EXACT', 3);
  14. define('LOCATION_LATLON_JIT_GEOCODING', 4); // Force regeocoding immediately.
  15. define('LOCATION_USER_DONT_COLLECT', 0);
  16. define('LOCATION_USER_COLLECT', 1);
  17. include_once(DRUPAL_ROOT . '/' . LOCATION_PATH . '/location.inc');
  18. /**
  19. * Implements hook_menu().
  20. */
  21. function location_menu() {
  22. $items = array();
  23. $items['location/autocomplete'] = array(
  24. 'access arguments' => array('access content'),
  25. 'page callback' => '_location_autocomplete',
  26. 'type' => MENU_CALLBACK,
  27. );
  28. $items['admin/config/content/location'] = array(
  29. 'title' => 'Location',
  30. 'description' => 'Settings for Location module',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('location_admin_settings'),
  33. 'file' => 'location.admin.inc',
  34. 'access arguments' => array('administer site configuration'),
  35. );
  36. $items['admin/config/content/location/main'] = array(
  37. 'title' => 'Main settings',
  38. 'type' => MENU_DEFAULT_LOCAL_TASK,
  39. );
  40. $items['admin/config/content/location/maplinking'] = array(
  41. 'title' => 'Map links',
  42. 'page callback' => 'drupal_get_form',
  43. 'page arguments' => array('location_map_link_options_form'),
  44. 'access arguments' => array('administer site configuration'),
  45. 'file' => 'location.admin.inc',
  46. 'type' => MENU_LOCAL_TASK,
  47. 'weight' => 1,
  48. );
  49. $items['admin/config/content/location/geocoding'] = array(
  50. 'title' => 'Geocoding options',
  51. 'page callback' => 'drupal_get_form',
  52. 'page arguments' => array('location_geocoding_options_form'),
  53. 'access arguments' => array('administer site configuration'),
  54. 'file' => 'location.admin.inc',
  55. 'type' => MENU_LOCAL_TASK,
  56. 'weight' => 2,
  57. );
  58. $items['admin/config/content/location/geocoding/%/%'] = array(
  59. 'page callback' => 'location_geocoding_parameters_page',
  60. 'page arguments' => array(5, 6),
  61. 'access arguments' => array('administer site configuration'),
  62. 'type' => MENU_CALLBACK,
  63. );
  64. $items['admin/config/content/location/util'] = array(
  65. 'title' => 'Location utilities',
  66. 'page callback' => 'drupal_get_form',
  67. 'page arguments' => array('location_util_form'),
  68. 'access arguments' => array('administer site configuration'),
  69. 'file' => 'location.admin.inc',
  70. 'type' => MENU_LOCAL_TASK,
  71. 'weight' => 3,
  72. );
  73. return $items;
  74. }
  75. /**
  76. *
  77. */
  78. function location_api_variant() {
  79. return 2;
  80. }
  81. /**
  82. * Implements hook_permission().
  83. */
  84. function location_permission() {
  85. return array(
  86. 'view location directory' => array(
  87. 'title' => t('View location directory'),
  88. ),
  89. 'view node location table' => array(
  90. 'title' => t('View node location table'),
  91. ),
  92. 'view user location table' => array(
  93. 'title' => t('View user location table'),
  94. ),
  95. 'submit latitude/longitude' => array(
  96. 'title' => t('Submit latitude/longitude'),
  97. ),
  98. );
  99. }
  100. /**
  101. * Implements hook_help().
  102. *
  103. * @TODO: check/fix this: admin/content/configure/types (still use %? still same url?)
  104. */
  105. function location_help($path, $arg) {
  106. switch ($path) {
  107. case 'admin/help#location':
  108. $output = '<p>'. t('The location module allows you to associate a geographic location with content and users. Users can do proximity searches by postal code. This is useful for organizing communities that have a geographic presence.') .'</p>';
  109. $output .= '<p>'. t('To administer locative information for content, use the content type administration page. To support most location enabled features, you will need to install the country specific include file. To support postal code proximity searches for a particular country, you will need a database dump of postal code data for that country. As of June 2007 only U.S. and German postal codes are supported.') .'</p>';
  110. $output .= t('<p>You can</p>
  111. <ul>
  112. <li>administer locative information at <a href="@admin-node-configure-types">Administer &gt;&gt; Content management &gt;&gt; Content types</a> to configure a type and see the locative information.</li>
  113. <li>administer location at <a href="@admin-settings-location">Administer &gt;&gt; Site configuration &gt;&gt; Location</a>.</li>
  114. <li>use a database dump for a U.S. and/or German postal codes table that can be found at <a href="@external-http-cvs-drupal-org">zipcode database</a>.</li>
  115. ', array('@admin-node-configure-types' => url('admin/content/types'), '@admin-settings-location' => url('admin/config/content/location'), '@external-http-cvs-drupal-org' => 'http://cvs.drupal.org/viewcvs/drupal/contributions/modules/location/database/')) .'</ul>';
  116. $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@location">Location page</a>.', array('@location' => 'http://www.drupal.org/handbook/modules/location/')) .'</p>';
  117. return $output;
  118. }
  119. }
  120. /**
  121. * Implements hook_element_info().
  122. */
  123. function location_element_info() {
  124. return array(
  125. 'location_element' => array(
  126. '#input' => TRUE,
  127. '#process' => array('_location_process_location'),
  128. '#tree' => TRUE,
  129. '#location_settings' => array(),
  130. '#required' => FALSE,
  131. '#attributes' => array('class' => array('location')),
  132. // Element level validation.
  133. '#element_validate' => array('location_element_validate'),
  134. ),
  135. 'location_settings' => array(
  136. '#input' => TRUE,
  137. '#process' => array('_location_process_location_settings'),
  138. '#collapsible' => TRUE,
  139. '#collapsed' => TRUE,
  140. '#tree' => TRUE,
  141. ),
  142. );
  143. }
  144. /**
  145. * Theme function to fixup location elements.
  146. * @ingroup themable
  147. */
  148. function theme_location_element($variables) {
  149. $element = $variables['element'];
  150. // Prevent spurious "Array" from appearing.
  151. unset($element['#value']);
  152. return theme('fieldset', array('element' => $element));
  153. }
  154. /**
  155. * Implements hook_theme().
  156. */
  157. function location_theme() {
  158. return array(
  159. 'location_settings' => array(
  160. 'render element' => 'element',
  161. ),
  162. 'locations' => array(
  163. 'template' => 'locations',
  164. 'variables' => array(
  165. 'locations' => NULL,
  166. 'hide' => array(),
  167. ),
  168. ),
  169. 'location' => array(
  170. 'template' => 'location',
  171. 'variables' => array(
  172. 'location' => NULL,
  173. 'hide' => array(),
  174. ),
  175. ),
  176. 'location_latitude_dms' => array(
  177. 'variables' => array('latitude'),
  178. ),
  179. 'location_longitude_dms' => array(
  180. 'variables' => array('longitude'),
  181. ),
  182. 'location_map_link_options' => array(
  183. 'render element' => 'form',
  184. 'file' => 'location.admin.inc',
  185. ),
  186. 'location_geocoding_options' => array(
  187. 'render element' => 'form',
  188. 'file' => 'location.admin.inc',
  189. ),
  190. 'location_element' => array(
  191. 'render element' => 'element',
  192. ),
  193. 'location_distance' => array(
  194. 'template' => 'location_distance',
  195. 'variables' => array(
  196. 'distance' => 0,
  197. 'units' => 'km',
  198. ),
  199. ),
  200. );
  201. }
  202. /**
  203. * Implements hook_views_api().
  204. */
  205. function location_views_api() {
  206. return array(
  207. 'api' => 3,
  208. //'path' => drupal_get_path('module', 'location') .'/includes',
  209. );
  210. }
  211. /**
  212. * Implements hook_ctools_plugin_directory().
  213. */
  214. function location_ctools_plugin_directory($module, $plugin) {
  215. if ($module == 'ctools' && !empty($plugin)) {
  216. return 'plugins/' . $plugin;
  217. }
  218. }
  219. /**
  220. * Process a location element.
  221. */
  222. function _location_process_location($element, $form_state) {
  223. // this is TRUE if we are processing a form that already contains values, such as during an AJAX call
  224. drupal_add_css(drupal_get_path('module', 'location') . '/location.css');
  225. $element['#tree'] = TRUE;
  226. if (!isset($element['#title'])) {
  227. $element['#title'] = t('Location');
  228. }
  229. if (empty($element['#location_settings'])) {
  230. $element['#location_settings'] = array();
  231. }
  232. if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
  233. $element['#default_value'] = array();
  234. }
  235. $element['location_settings'] = array(
  236. '#type' => 'value',
  237. '#value' => $element['#location_settings'],
  238. );
  239. // Ensure this isn't accidentally used later.
  240. unset($element['#location_settings']);
  241. // Make a reference to the settings.
  242. $settings =& $element['location_settings']['#value'];
  243. if (isset($element['#default_value']['lid']) && $element['#default_value']['lid']) {
  244. // Keep track of the old LID.
  245. $element['lid'] = array(
  246. '#type' => 'value',
  247. '#value' => $element['#default_value']['lid'],
  248. );
  249. }
  250. // Fill in missing defaults, etc.
  251. location_normalize_settings($settings, $element['#required']);
  252. $defaults = location_empty_location($settings);
  253. if (isset($element['lid']['#value']) && $element['lid']['#value']) {
  254. $defaults = location_load_location($element['lid']['#value']);
  255. }
  256. $fsettings =& $settings['form']['fields'];
  257. // $settings -> $settings['form']['fields']
  258. // $defaults is not necessarily what we want.
  259. // If #default_value was already specified, we want to use that, because
  260. // otherwise we will lose our values on preview!
  261. $fdefaults = $defaults;
  262. foreach ($element['#default_value'] as $k => $v) {
  263. $fdefaults[$k] = $v;
  264. }
  265. $fields = location_field_names();
  266. foreach ($fields as $field => $title) {
  267. if (!isset($element[$field])) {
  268. // @@@ Permission check hook?
  269. if ($fsettings[$field]['collect'] != 0) {
  270. $fsettings[$field]['#parents'] = $element['#parents'];
  271. $element[$field] = location_invoke_locationapi($fdefaults[$field], 'field_expand', $field, $fsettings[$field], $fdefaults);
  272. $element[$field]['#weight'] = (int) $fsettings[$field]['weight'];
  273. }
  274. }
  275. // If State/Province is using the select widget, update the element's options.
  276. if ($field == 'province' && $fsettings[$field]['widget'] == 'select') {
  277. // We are building the element for the first time
  278. if (!isset($element['value']['country'])) {
  279. $country = $fdefaults['country'];
  280. }
  281. else {
  282. $country = $element['#value']['country'];
  283. }
  284. $provinces = location_get_provinces($country);
  285. // The submit handler expects to find the full province name, not the
  286. // abbreviation. The select options should reflect this expectation.
  287. $element[$field]['#options'] = array('' => t('Please select'), 'xx' => t('NOT LISTED')) + $provinces;
  288. }
  289. }
  290. // Only include 'Street Additional' if 'Street' is 'allowed' or 'required'
  291. if (!isset($element['street'])) {
  292. unset($element['additional']);
  293. }
  294. // @@@ Split into submit and view permissions?
  295. if (user_access('submit latitude/longitude') && $fsettings['locpick']['collect']) {
  296. $element['locpick'] = array('#weight' => $fsettings['locpick']['weight']);
  297. if (location_has_coordinates($defaults, FALSE)) {
  298. $element['locpick']['current'] = array(
  299. '#type' => 'fieldset',
  300. '#title' => t('Current coordinates'),
  301. '#attributes' => array('class' => array('location-current-coordinates-fieldset')),
  302. );
  303. $element['locpick']['current']['current_latitude'] = array(
  304. '#type' => 'item',
  305. '#title' => t('Latitude'),
  306. '#markup' => $defaults['latitude'],
  307. );
  308. $element['locpick']['current']['current_longitude'] = array(
  309. '#type' => 'item',
  310. '#title' => t('Longitude'),
  311. '#markup' => $defaults['longitude'],
  312. );
  313. $source = t('Unknown');
  314. switch ($defaults['source']) {
  315. case LOCATION_LATLON_USER_SUBMITTED:
  316. $source = t('User-submitted');
  317. break;
  318. case LOCATION_LATLON_GEOCODED_APPROX:
  319. $source = t('Geocoded (Postal code level)');
  320. break;
  321. case LOCATION_LATLON_GEOCODED_EXACT:
  322. $source = t('Geocoded (Exact)');
  323. }
  324. $element['locpick']['current']['current_source'] = array(
  325. '#type' => 'item',
  326. '#title' => t('Source'),
  327. '#markup' => $source,
  328. );
  329. }
  330. $element['locpick']['user_latitude'] = array(
  331. '#type' => 'textfield',
  332. '#title' => t('Latitude'),
  333. '#default_value' => isset($element['#default_value']['locpick']['user_latitude']) ? $element['#default_value']['locpick']['user_latitude'] : '',
  334. '#size' => 16,
  335. '#attributes' => array('class' => array('container-inline')),
  336. '#maxlength' => 20,
  337. '#required' => $fsettings['locpick']['collect'] == 2,
  338. );
  339. $element['locpick']['user_longitude'] = array(
  340. '#type' => 'textfield',
  341. '#title' => t('Longitude'),
  342. '#default_value' => isset($element['#default_value']['locpick']['user_longitude']) ? $element['#default_value']['locpick']['user_longitude'] : '',
  343. '#size' => 16,
  344. '#maxlength' => 20,
  345. '#required' => $fsettings['locpick']['collect'] == 2,
  346. );
  347. $element['locpick']['instructions'] = array(
  348. '#type' => 'markup',
  349. '#weight' => 1,
  350. '#prefix' => '<div class=\'description\'>',
  351. '#markup' => '<br /><br />' . t('If you wish to supply your own latitude and longitude, you may enter them above. If you leave these fields blank, the system will attempt to determine a latitude and longitude for you from the entered address. To have the system recalculate your location from the address, for example if you change the address, delete the values for these fields.'),
  352. '#suffix' => '</div>',
  353. );
  354. if (function_exists('gmap_get_auto_mapid') && variable_get('location_usegmap', FALSE)) {
  355. $mapid = gmap_get_auto_mapid();
  356. $map = array_merge(gmap_defaults(), gmap_parse_macro(variable_get('location_locpick_macro', '[gmap]')));
  357. $map['id'] = $mapid;
  358. $map['points'] = array();
  359. $map['pointsOverlays'] = array();
  360. $map['lines'] = array();
  361. $map['behavior']['locpick'] = TRUE;
  362. $map['behavior']['collapsehack'] = TRUE;
  363. // Use previous coordinates to center the map.
  364. if (location_has_coordinates($defaults, FALSE)) {
  365. $map['latitude'] = (float)$defaults['latitude'];
  366. $map['longitude'] = (float)$defaults['longitude'];
  367. $map['markers'][] = array(
  368. 'latitude' => $defaults['latitude'],
  369. 'longitude' => $defaults['longitude'],
  370. 'markername' => 'small gray', // @@@ Settable?
  371. 'offset' => 0,
  372. 'opts' => array(
  373. 'clickable' => FALSE,
  374. ),
  375. );
  376. }
  377. $element['locpick']['user_latitude']['#map'] = $mapid;
  378. gmap_widget_setup($element['locpick']['user_latitude'], 'locpick_latitude');
  379. $element['locpick']['user_longitude']['#map'] = $mapid;
  380. gmap_widget_setup($element['locpick']['user_longitude'], 'locpick_longitude');
  381. $element['locpick']['map'] = array(
  382. '#type' => 'gmap',
  383. '#weight' => -1,
  384. '#gmap_settings' => $map,
  385. );
  386. $element['locpick']['map_instructions'] = array(
  387. '#type' => 'markup',
  388. '#weight' => 2,
  389. '#prefix' => '<div class=\'description\'>',
  390. '#markup' => t('You may set the location by clicking on the map, or dragging the location marker. To clear the location and cause it to be recalculated, click on the marker.'),
  391. '#suffix' => '</div>',
  392. );
  393. }
  394. }
  395. if (isset($defaults['lid']) && !empty($defaults['lid'])) {
  396. $element['delete_location'] = array(
  397. '#type' => 'checkbox',
  398. '#title' => t('Delete'),
  399. '#default_value' => isset($fdefaults['delete_location']) ? $fdefaults['delete_location'] : FALSE,
  400. '#description' => t('Check this box to delete this location.'),
  401. );
  402. }
  403. $element += element_info('fieldset');
  404. drupal_alter('location_element', $element);
  405. return $element;
  406. }
  407. function _location_process_location_settings(&$element) {
  408. // Set a value for the fieldset that doesn't interfere with rendering and doesn't generate a warning.
  409. $element['#tree'] = TRUE;
  410. $element['#theme'] = 'location_settings';
  411. if (!isset($element['#title'])) {
  412. $element['#title'] = t('Location Fields');
  413. }
  414. if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
  415. $element['#default_value'] = array();
  416. }
  417. // Force #tree on.
  418. $element['#tree'] = TRUE;
  419. $defaults = $element['#default_value'];
  420. if (!isset($defaults) || !is_array($defaults)) {
  421. $defaults = array();
  422. }
  423. $temp = location_invoke_locationapi($element, 'defaults');
  424. foreach ($temp as $k => $v) {
  425. if (!isset($defaults[$k])) {
  426. $defaults[$k] = array();
  427. }
  428. $defaults[$k] = array_merge($v, $defaults[$k]);
  429. }
  430. $fields = location_field_names();
  431. // Options for fields.
  432. $options = array(
  433. 0 => t('Do not collect'),
  434. 1 => t('Allow'),
  435. 2 => t('Require'),
  436. 4 => t('Force Default'), // Need to consider the new "defaults" when saving.
  437. );
  438. foreach ($fields as $field => $title) {
  439. $element[$field] = array(
  440. '#type' => 'fieldset',
  441. '#tree' => TRUE,
  442. );
  443. $element[$field]['name'] = array(
  444. '#type' => 'item',
  445. '#markup' => $title,
  446. );
  447. $element[$field]['collect'] = array(
  448. '#type' => 'select',
  449. '#default_value' => $defaults[$field]['collect'],
  450. '#options' => $options,
  451. );
  452. $dummy = array();
  453. $widgets = location_invoke_locationapi($dummy, 'widget', $field);
  454. if (!empty($widgets)) {
  455. $element[$field]['widget'] = array(
  456. '#type' => 'select',
  457. '#default_value' => $defaults[$field]['widget'],
  458. '#options' => $widgets,
  459. );
  460. }
  461. $temp = $defaults[$field]['default'];
  462. $element[$field]['default'] = location_invoke_locationapi($temp, 'field_expand', $field, 1, $defaults);
  463. $defaults[$field]['default'] = $temp;
  464. $element[$field]['weight'] = array(
  465. '#type' => 'weight',
  466. '#delta' => 100,
  467. '#default_value' => $defaults[$field]['weight'],
  468. );
  469. }
  470. // 'Street Additional' field should depend on 'Street' setting.
  471. // It should never be required and should only display when the street field is 'allowed' or 'required'
  472. // unset($element['additional']);
  473. // @@@ Alter here?
  474. return $element;
  475. }
  476. function theme_location_settings($variables) {
  477. $element = $variables['element'];
  478. $rows = array();
  479. $header = array(
  480. array(
  481. 'data' => t('Name'),
  482. 'colspan' => 2,
  483. ),
  484. t('Collect'),
  485. t('Widget'),
  486. t('Default'),
  487. t('Weight'),
  488. );
  489. // Force country required.
  490. $element['country']['default']['#required'] = TRUE;
  491. unset($element['country']['collect']['#options'][0]);
  492. foreach (element_children($element) as $key) {
  493. $element[$key]['weight']['#attributes']['class'] = array('location-settings-weight');
  494. unset($element[$key]['default']['#title']);
  495. $row = array();
  496. $row[] = array('data' => '', 'class' => array('location-settings-drag'));
  497. $row[] = drupal_render($element[$key]['name']);
  498. $row[] = drupal_render($element[$key]['collect']);
  499. $row[] = !empty($element[$key]['widget']) ? drupal_render($element[$key]['widget']) : '';
  500. $row[] = drupal_render($element[$key]['default']);
  501. $row[] = array('data' => drupal_render($element[$key]['weight']), 'class' => array('delta-order'));
  502. $rows[] = array(
  503. '#weight' => (int)$element[$key]['weight']['#value'],
  504. 'data' => $row,
  505. 'class' => array('draggable'),
  506. );
  507. }
  508. uasort($rows, 'element_sort');
  509. foreach ($rows as $k => $v) {
  510. unset($rows[$k]['#weight']);
  511. }
  512. drupal_add_tabledrag('location-settings-table', 'order', 'sibling', 'location-settings-weight');
  513. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'location-settings-table')));
  514. //return theme('form_element', array('element' => $element));
  515. return $output;
  516. }
  517. function location_field_names($all = FALSE) {
  518. $fields = &drupal_static(__FUNCTION__ . '_fields', array());
  519. $allfields = &drupal_static(__FUNCTION__ . '_allfields', array());
  520. if ($all) {
  521. if (empty($allfields)) {
  522. $dummy = array();
  523. $allfields = location_invoke_locationapi($dummy, 'fields');
  524. $virtual = location_invoke_locationapi($dummy, 'virtual fields');
  525. $allfields += $virtual;
  526. }
  527. return $allfields;
  528. }
  529. else {
  530. if (empty($fields)) {
  531. $dummy = array();
  532. $fields = location_invoke_locationapi($dummy, 'fields');
  533. }
  534. return $fields;
  535. }
  536. }
  537. /**
  538. * Implements hook_locationapi().
  539. */
  540. function location_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  541. switch ($op) {
  542. case 'fields':
  543. return array('name' => t('Location name'), 'street' => t('Street location'), 'additional' => t('Additional'), 'city' => t('City'), 'province' => t('State/Province'), 'postal_code' => t('Postal code'), 'country' => t('Country'), 'locpick' => t('Coordinate Chooser'));
  544. case 'widget':
  545. switch ($a3) {
  546. case 'province':
  547. return array(
  548. 'autocomplete' => 'Autocomplete',
  549. 'select' => 'Dropdown',
  550. );
  551. default:
  552. return array();
  553. }
  554. case 'virtual fields':
  555. return array('province_name' => t('Province name'), 'country_name' => t('Country name'), 'map_link' => t('Map link'), 'coords' => t('Coordinates'));
  556. case 'defaults':
  557. return array(
  558. 'lid' => array('default' => FALSE),
  559. 'name' => array('default' => '', 'collect' => 1, 'weight' => 2),
  560. 'street' => array('default' => '', 'collect' => 1, 'weight' => 4),
  561. 'additional' => array('default' => '', 'collect' => 1, 'weight' => 6),
  562. 'city' => array('default' => '', 'collect' => 0, 'weight' => 8),
  563. 'province' => array('default' => '', 'collect' => 0, 'weight' => 10, 'widget' => 'autocomplete'),
  564. 'postal_code' => array('default' => '', 'collect' => 0, 'weight' => 12),
  565. 'country' => array('default' => variable_get('location_default_country', 'us'), 'collect' => 1, 'weight' => 14), // @@@ Fix weight?
  566. 'locpick' => array('default' => FALSE, 'collect' => 1, 'weight' => 20, 'nodiff' => TRUE),
  567. 'latitude' => array('default' => 0),
  568. 'longitude' => array('default' => 0),
  569. 'source' => array('default' => LOCATION_LATLON_UNDEFINED),
  570. 'is_primary' => array('default' => 0), // @@@
  571. 'delete_location' => array('default' => FALSE, 'nodiff' => TRUE),
  572. );
  573. case 'validate':
  574. if (!empty($obj['country'])) {
  575. if (!empty($obj['province']) && $obj['province'] != 'xx') {
  576. $provinces = location_get_provinces($obj['country']);
  577. $found = FALSE;
  578. $p = strtoupper($obj['province']);
  579. foreach ($provinces as $k => $v) {
  580. if ($p == strtoupper($k) || $p == strtoupper($v)) {
  581. $found = TRUE;
  582. break;
  583. }
  584. }
  585. if (!$found) {
  586. form_error($a3['province'], t('The specified province was not found in the specified country.'));
  587. }
  588. }
  589. }
  590. if (!empty($obj['locpick']) && is_array($obj['locpick'])) {
  591. // Can't specify just latitude or just longitude.
  592. if (_location_floats_are_equal($obj['locpick']['user_latitude'], 0) xor _location_floats_are_equal($obj['locpick']['user_longitude'], 0)) {
  593. $ref = &$a3['locpick']['user_latitude'];
  594. if (_location_floats_are_equal($obj['locpick']['user_longitude'], 0)) {
  595. $ref = &$a3['locpick']['user_longitude'];
  596. }
  597. form_error($ref, t('You must fill out both latitude and longitude or you must leave them both blank.'));
  598. }
  599. }
  600. break;
  601. case 'field_expand':
  602. if (is_array($a4)) {
  603. $settings = $a4;
  604. }
  605. else {
  606. // On this $op, $a4 is now expected to be an array,
  607. // but we make an exception for backwards compatibility.
  608. $settings = array('default' => NULL, 'weight' => NULL, 'collect' => $a4, 'widget' => NULL);
  609. }
  610. switch ($a3) {
  611. case 'name':
  612. return array(
  613. '#type' => 'textfield',
  614. '#title' => t('Location name'),
  615. '#default_value' => $obj,
  616. '#size' => 64,
  617. '#maxlength' => 255,
  618. '#description' => t('e.g. a place of business, venue, meeting point'),
  619. '#attributes' => NULL,
  620. '#required' => ($settings['collect'] == 2),
  621. );
  622. case 'street':
  623. return array(
  624. '#type' => 'textfield',
  625. '#title' => t('Street'),
  626. '#default_value' => $obj,
  627. '#size' => 64,
  628. '#maxlength' => 255,
  629. '#required' => ($settings['collect'] == 2),
  630. );
  631. // Additional is linked to street.
  632. case 'additional':
  633. return array(
  634. '#type' => 'textfield',
  635. '#title' => t('Additional'),
  636. '#default_value' => $obj,
  637. '#size' => 64,
  638. '#maxlength' => 255,
  639. // Required is forced OFF because this is technically part of street.
  640. );
  641. case 'city':
  642. return array(
  643. '#type' => 'textfield',
  644. '#title' => t('City'),
  645. '#default_value' => $obj,
  646. '#size' => 64,
  647. '#maxlength' => 255,
  648. '#description' => NULL,
  649. '#attributes' => NULL,
  650. '#required' => ($settings['collect'] == 2),
  651. );
  652. case 'province':
  653. if (isset($a5['country']) && is_string($a5['country'])) {
  654. $country = $a5['country'];
  655. }
  656. elseif (isset($a5['country']['default']) && is_string($a5['country']['default'])) {
  657. $country = $a5['country']['default'];
  658. }
  659. else {
  660. $country = variable_get('site_default_country', 'us');
  661. }
  662. switch ($settings['widget']) {
  663. case 'select':
  664. // Options are defined once during hook_element implementation.
  665. // @see _location_process_location
  666. // $options = array_merge(array('' => t('Please select'), 'xx' => t('NOT LISTED')), location_get_provinces($country));
  667. if (!empty($settings['#parents'])) {
  668. $wrapper_suffix = '-'. implode('-', $settings['#parents']);
  669. }
  670. else {
  671. $wrapper_suffix = '';
  672. }
  673. return array(
  674. '#type' => 'select',
  675. '#title' => t('State/Province'),
  676. '#default_value' => $obj,
  677. '#description' => NULL,
  678. '#required' => ($settings['collect'] == 2),
  679. '#attributes' => array('class' => array('location_dropdown_province')),
  680. '#prefix' => '<div id="location-dropdown-province-wrapper' . $wrapper_suffix . '">',
  681. '#suffix' => '</div>',
  682. );
  683. case 'autocomplete':
  684. default:
  685. drupal_add_js(drupal_get_path('module', 'location') . '/location_autocomplete.js');
  686. return array(
  687. '#type' => 'textfield',
  688. '#title' => t('State/Province'),
  689. '#autocomplete_path' => 'location/autocomplete/' . $country,
  690. '#default_value' => $obj,
  691. //'#default_value' => variable_get('location_use_province_abbreviation', 1) ? $obj : location_province_name($country, $obj),
  692. '#size' => 64,
  693. '#maxlength' => 64,
  694. '#description' => NULL,
  695. '#attributes' => array('class' => array('location_auto_province')),
  696. '#required' => ($settings['collect'] == 2),
  697. );
  698. }
  699. case 'country':
  700. // Force default.
  701. if ($settings['collect'] == 4) {
  702. return array(
  703. '#type' => 'value',
  704. '#value' => $obj,
  705. );
  706. }
  707. else {
  708. $options = array_merge(array('' => t('Please select'), 'xx' => t('NOT LISTED')), location_get_iso3166_list());
  709. if (!empty($settings['#parents'])) {
  710. $wrapper_suffix = '-' . implode('-', $settings['#parents']);
  711. }
  712. else {
  713. $settings['#parents'] = array();
  714. $wrapper_suffix = '';
  715. }
  716. return array(
  717. '#type' => 'select',
  718. '#title' => t('Country'),
  719. '#default_value' => $obj,
  720. '#options' => $options,
  721. '#description' => NULL,
  722. '#required' => ($settings['collect'] == 2),
  723. // Used by province autocompletion js.
  724. '#attributes' => array('class' => array('location_auto_country')),
  725. '#ajax' => array(
  726. 'callback' => '_location_country_ajax_callback',
  727. 'path' => 'system/ajax/'. implode('/', $settings['#parents']),
  728. 'wrapper' => 'location-dropdown-province-wrapper' . $wrapper_suffix,
  729. 'effect' => 'fade',
  730. ),
  731. );
  732. }
  733. break;
  734. case 'postal_code':
  735. return array(
  736. '#type' => 'textfield',
  737. '#title' => t('Postal code'),
  738. '#default_value' => $obj,
  739. '#size' => 16,
  740. '#maxlength' => 16,
  741. '#required' => ($settings['collect'] == 2),
  742. );
  743. }
  744. break;
  745. case 'isunchanged':
  746. switch ($a3) {
  747. case 'lid':
  748. // Consider 0, NULL, and FALSE to be equivilent.
  749. if (empty($obj[$a3]) && empty($a4)) {
  750. return TRUE;
  751. }
  752. break;
  753. case 'latitude':
  754. case 'longitude':
  755. if (_location_floats_are_equal($obj[$a3], $a4)) {
  756. return TRUE;
  757. }
  758. break;
  759. case 'country':
  760. // Consider ' ' and '' to be equivilent, due to us storing country
  761. // as char(2) in the database.
  762. if (trim($obj[$a3]) == trim($a4)) {
  763. return TRUE;
  764. }
  765. break;
  766. case 'province_name':
  767. case 'country_name':
  768. case 'map_link':
  769. case 'coords':
  770. case 'locpick':
  771. case 'delete_location':
  772. // Always considered unchanged.
  773. return TRUE;
  774. }
  775. break;
  776. }
  777. }
  778. function location_geocoding_parameters_page($country_iso, $service) {
  779. drupal_set_title(t('Configure parameters for %service geocoding', array('%service' => $service)), PASS_THROUGH);
  780. $breadcrumbs = drupal_get_breadcrumb();
  781. $breadcrumbs[] = l('location', 'admin/config/content/location');
  782. $breadcrumbs[] = l('geocoding', 'admin/config/content/location/geocoding');
  783. $countries = location_get_iso3166_list();
  784. $breadcrumbs[] = l($countries[$country_iso], 'admin/config/content/location/geocoding', array('fragment' => $country_iso));
  785. drupal_set_breadcrumb($breadcrumbs);
  786. return drupal_get_form('location_geocoding_parameters_form', $country_iso, $service);
  787. }
  788. function location_geocoding_parameters_form($form, &$form_state, $country_iso, $service) {
  789. location_load_country($country_iso);
  790. $geocode_settings_form_function_specific = 'location_geocode_' . $country_iso . '_' . $service . '_settings';
  791. $geocode_settings_form_function_general = $service . '_geocode_settings';
  792. if (function_exists($geocode_settings_form_function_specific)) {
  793. return system_settings_form($geocode_settings_form_function_specific());
  794. }
  795. location_load_geocoder($service);
  796. if (function_exists($geocode_settings_form_function_general)) {
  797. return system_settings_form($geocode_settings_form_function_general());
  798. }
  799. else {
  800. return system_settings_form(array(
  801. '#type' => 'markup',
  802. '#markup' => t('No configuration parameters are necessary, or a form to take such paramters has not been implemented.')
  803. ));
  804. }
  805. }
  806. /**
  807. * Load associated locations.
  808. *
  809. * @param $id The identifier to match. (An integer.)
  810. * @param $key The search key for {location_instance} (usually vid or uid.)
  811. * @return An array of loaded locations.
  812. */
  813. function location_load_locations($id, $key = 'vid') {
  814. if (empty($id)) {
  815. // If the id is 0 or '' (or false), force returning early.
  816. // Otherwise, this could accidentally load a huge amount of data
  817. // by accident. 0 and '' are reserved for "not applicable."
  818. return array();
  819. }
  820. $query = db_select('location_instance', 'l');
  821. $lid_field = $query->addField('l', 'lid');
  822. $query->condition($key, $id);
  823. $result = $query->execute();
  824. $locations = array();
  825. foreach ($result as $lid) {
  826. $locations[] = location_load_location($lid->{$lid_field});
  827. }
  828. return $locations;
  829. }
  830. /**
  831. * Save associated locations.
  832. *
  833. * @param $locations The associated locations.
  834. * You can pass an empty array to remove all location references associated
  835. * with the given criteria. This is useful if you are about to delete an object,
  836. * and need Location to clean up any locations that are no longer referenced.
  837. *
  838. * @param $criteria An array of instance criteria to save as.
  839. * Example: array('genid' => 'my_custom_1111')
  840. */
  841. function location_save_locations(&$locations, $criteria) {
  842. if (isset($locations) && is_array($locations) && !empty($criteria) && is_array($criteria)) {
  843. foreach (array_keys($locations) as $key) {
  844. location_save($locations[$key], TRUE, $criteria);
  845. }
  846. // Find affected lids.
  847. $query = db_select('location_instance', 'l');
  848. $lid_field = $query->addField('l', 'lid');
  849. foreach ($criteria as $key => $value) {
  850. $query->condition($key, $value);
  851. }
  852. $oldlids = $query->execute()->fetchCol();
  853. // Delete current set of instances.
  854. $query = db_delete('location_instance');
  855. foreach ($criteria as $key => $value) {
  856. $query->condition($key, $value);
  857. }
  858. $query->execute();
  859. $newlids = array();
  860. foreach ($locations as $location) {
  861. // Don't save "empty" locations.
  862. // location_save() explicitly returns FALSE for empty locations,
  863. // so it should be ok to rely on the data type.
  864. if ($location['lid'] !== FALSE) {
  865. $newlids[] = $location['lid'];
  866. $instance = array(
  867. 'nid' => 0,
  868. 'vid' => 0,
  869. 'uid' => 0,
  870. 'genid' => '',
  871. 'lid' => $location['lid'],
  872. );
  873. foreach ($criteria as $key => $value) {
  874. $instance[$key] = $value;
  875. }
  876. db_insert('location_instance')
  877. ->fields($instance)
  878. ->execute();
  879. }
  880. }
  881. // Check anything that dropped a reference during this operation.
  882. foreach (array_diff($oldlids, $newlids) as $check) {
  883. // An instance may have been deleted. Check reference count.
  884. $count = db_query('SELECT COUNT(*) FROM {location_instance} WHERE lid = :lid', array(':lid' => $check))->fetchField();
  885. if ($count !== FALSE && $count == 0) {
  886. watchdog('location', 'Deleting unreferenced location with LID %lid.', array('%lid' => $check));
  887. $location = array('lid' => $check);
  888. location_invoke_locationapi($location, 'delete');
  889. db_delete('location')
  890. ->condition('lid', $location['lid'])
  891. ->execute();
  892. }
  893. }
  894. }
  895. }
  896. /**
  897. * Load a single location by lid.
  898. *
  899. * @param $lid Location ID to load.
  900. * @return A location array.
  901. */
  902. function location_load_location($lid) {
  903. $location = db_query('SELECT * FROM {location} WHERE lid = :lid', array(':lid' => $lid))->fetchAssoc();
  904. // @@@ Just thought of this, but I am not certain it is a good idea...
  905. if (empty($location)) {
  906. $location = array('lid' => $lid);
  907. }
  908. if (isset($location['source']) && $location['source'] == LOCATION_LATLON_USER_SUBMITTED) {
  909. // Set up location chooser or lat/lon fields from the stored location.
  910. $location['locpick'] = array(
  911. 'user_latitude' => $location['latitude'],
  912. 'user_longitude' => $location['longitude'],
  913. );
  914. }
  915. // JIT Geocoding
  916. // Geocodes during load, useful with bulk imports.
  917. if (isset($location['source']) && $location['source'] == LOCATION_LATLON_JIT_GEOCODING) {
  918. if (variable_get('location_jit_geocoding', FALSE)) {
  919. _location_geo_logic($location, array('street' => 1), array());
  920. db_update('location')
  921. ->fields(array(
  922. 'latitude' => $location['latitude'],
  923. 'longitude' => $location['longitude'],
  924. 'source' => $location['source'],
  925. ))
  926. ->condition('lid', $location['lid'])
  927. ->execute();
  928. }
  929. }
  930. $location['province_name'] = '';
  931. $location['country_name'] = '';
  932. if (!empty($location['country'])) {
  933. $location['country_name'] = location_country_name($location['country']);
  934. if (!empty($location['province'])) {
  935. $location['province_name'] = location_province_name($location['country'], $location['province']);
  936. }
  937. }
  938. $location = array_merge($location, location_invoke_locationapi($location, 'load', $lid));
  939. return $location;
  940. }
  941. /**
  942. * Create a list of states from a given country.
  943. *
  944. * @param $country
  945. * String. The country code
  946. * @param $string
  947. * String (optional). The state name typed by user
  948. * @return
  949. * Javascript array. List of states
  950. */
  951. function _location_autocomplete($country, $string = '') {
  952. $counter = 0;
  953. $string = strtolower($string);
  954. $string = '/^' . preg_quote($string) . '/';
  955. $matches = array();
  956. if (strpos($country, ',') !== FALSE) {
  957. // Multiple countries specified.
  958. $provinces = array();
  959. $country = explode(',', $country);
  960. foreach ($country as $c) {
  961. $provinces = $provinces + location_get_provinces($c);
  962. }
  963. }
  964. else {
  965. $provinces = location_get_provinces($country);
  966. }
  967. if (!empty($provinces)) {
  968. while (list($code, $name) = each($provinces)) {
  969. if ($counter < 5) {
  970. if (preg_match($string, strtolower($name))) {
  971. $matches[$name] = $name;
  972. ++$counter;
  973. }
  974. }
  975. }
  976. }
  977. drupal_json_output($matches);
  978. }
  979. /**
  980. * AJAX callback for the Country select form, for cases where the province list
  981. * is also a select element and its options need to be updated. Uses the D7 Ajax
  982. * Framework to do the select list updating.
  983. *
  984. * All we do here is select the element of the form that will be rebuilt.
  985. *
  986. */
  987. function _location_country_ajax_callback($form, $form_state) {
  988. // The isset() checks, ideally, wouldn't ever have to happen because, ideally,
  989. // this code would never get called, because, ideally, we wouldn't add an
  990. // ajax call to the country field. Unfortunately, however, there's no easy
  991. // way to check whether or not the province is being collected when putting
  992. // together the country form element in location_locationapi() when that
  993. // function is called with $op == 'field_expand'
  994. if (arg(2) == 'locations' && isset($form['locations'][arg(3)]['province'])) {
  995. return $form['locations'][arg(3)]['province'];
  996. }
  997. elseif (isset($form['locations'][arg(2)][arg(3)][arg(4)])) {
  998. return $form[arg(2)][arg(3)][arg(4)]['province'];
  999. }
  1000. }
  1001. /**
  1002. * Epsilon test.
  1003. * Helper function for seeing if two floats are equal. We could use other functions, but all
  1004. * of them belong to libraries that do not come standard with PHP out of the box.
  1005. */
  1006. function _location_floats_are_equal($x, $y) {
  1007. $x = floatval($x);
  1008. $y = floatval($y);
  1009. return (abs(max($x, $y) - min($x, $y)) < pow(10, -6));
  1010. }
  1011. /**
  1012. * Check whether a location has coordinates or not.
  1013. *
  1014. * @param $location The location to check.
  1015. * @param $canonical Is this a location that is fully saved?
  1016. * If set to TRUE, only the source will be checked.
  1017. */
  1018. function location_has_coordinates($location, $canonical = FALSE) {
  1019. // Locations that have been fully saved have an up to date source.
  1020. if ($canonical) {
  1021. return ($location['source'] != LOCATION_LATLON_UNDEFINED);
  1022. }
  1023. // Otherwise, we need to do the full checks.
  1024. // If latitude or longitude are empty / missing
  1025. if (empty($location['latitude']) || empty($location['longitude'])) {
  1026. return FALSE;
  1027. }
  1028. // If the latitude or longitude are zeroed (Although it could be a good idea to relax this slightly sometimes)
  1029. if (_location_floats_are_equal($location['latitude'], 0.0) || _location_floats_are_equal($location['longitude'], 0.0)) {
  1030. return FALSE;
  1031. }
  1032. return TRUE;
  1033. }
  1034. /**
  1035. * Invoke a hook_locationapi() operation on all modules.
  1036. *
  1037. * @param &$location A location object.
  1038. * @param $op A string containing the name of the locationapi operation.
  1039. * @param $a3, $a4, $a5 Arguments to pass on to the hook.
  1040. * @return The returned value of the invoked hooks.
  1041. */
  1042. function location_invoke_locationapi(&$location, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  1043. $return = array();
  1044. foreach (module_implements('locationapi') as $name) {
  1045. $function = $name . '_locationapi';
  1046. $result = $function($location, $op, $a3, $a4, $a5);
  1047. if (isset($result) && is_array($result)) {
  1048. $return = array_merge($return, $result);
  1049. }
  1050. elseif (isset($result)) {
  1051. $return[] = $result;
  1052. }
  1053. }
  1054. return $return;
  1055. }
  1056. /**
  1057. * Apply locpick twiddling to a location.
  1058. * This is needed before saving and comparison.
  1059. */
  1060. function _location_patch_locpick(&$location) {
  1061. $inhibit_geocode = FALSE;
  1062. if (!empty($location['locpick'])) {
  1063. $location['locpick']['user_latitude'] = trim($location['locpick']['user_latitude']);
  1064. $location['locpick']['user_longitude'] = trim($location['locpick']['user_longitude']);
  1065. }
  1066. // If the user location was set, convert it into lat / lon.
  1067. if (!empty($location['locpick']['user_latitude']) && !empty($location['locpick']['user_longitude'])) {
  1068. $location['source'] = LOCATION_LATLON_USER_SUBMITTED;
  1069. $location['latitude'] = $location['locpick']['user_latitude'];
  1070. $location['longitude'] = $location['locpick']['user_longitude'];
  1071. $inhibit_geocode = TRUE;
  1072. }
  1073. return $inhibit_geocode;
  1074. }
  1075. /**
  1076. * Save a location.
  1077. *
  1078. * This is the central function for saving a location.
  1079. * @param $location Location array to save.
  1080. * @param $cow Copy-on-write, i.e. whether or not to assign a new lid if something changes.
  1081. * @param $criteria Instance criteria. If the only instances known by location match
  1082. * the criteria, the lid will be reused, regardless of $cow status. If no criteria
  1083. * is provided, there will be no attempt to reuse lids.
  1084. * @return The lid of the saved location, or FALSE if the location is considered "empty."
  1085. */
  1086. function location_save(&$location, $cow = TRUE, $criteria = array()) {
  1087. // Quick settings fixup.
  1088. if (!isset($location['location_settings'])) {
  1089. $location['location_settings'] = array();
  1090. }
  1091. location_normalize_settings($location['location_settings']);
  1092. $inhibit_geocode = FALSE;
  1093. if (isset($location['inhibit_geocode']) && $location['inhibit_geocode']) {
  1094. // Workaround for people importing / generating locations.
  1095. // Allows things like location_generate.module to work properly.
  1096. $inhibit_geocode = TRUE;
  1097. unset($location['inhibit_geocode']);
  1098. }
  1099. if (isset($location['delete_location']) && $location['delete_location']) {
  1100. // Location is being deleted.
  1101. // Consider it empty and return early.
  1102. $location['lid'] = FALSE;
  1103. return FALSE;
  1104. }
  1105. // If there's already a lid, we're editing an old location. Load it in.
  1106. $oldloc = location_empty_location($location['location_settings']);
  1107. if (isset($location['lid']) && !empty($location['lid'])) {
  1108. $oldloc = (array)location_load_location($location['lid']);
  1109. }
  1110. if (_location_patch_locpick($location)) {
  1111. $inhibit_geocode = TRUE;
  1112. }
  1113. // Pull in fields that hold data currently not editable directly by the user.
  1114. $location = array_merge($oldloc, $location);
  1115. // Note: If the user clears all the fields, the location can still
  1116. // be non-empty if the user didn't have access to everything..
  1117. $filled = array();
  1118. if (location_is_empty($location, $filled)) {
  1119. // This location was empty, we don't need to continue.
  1120. $location['lid'] = FALSE;
  1121. return FALSE;
  1122. }
  1123. $changed = array();
  1124. if (!location_calc_difference($oldloc, $location, $changed)) {
  1125. // We didn't actually need to save anything.
  1126. if (!empty($location['lid'])) {
  1127. return $location['lid'];
  1128. }
  1129. else {
  1130. // Unfilled location (@@@ Then how did we get here?)
  1131. $location['lid'] = FALSE;
  1132. return FALSE;
  1133. }
  1134. }
  1135. // Perform geocoding logic, coordinate normalization, etc.
  1136. _location_geo_logic($location, $changed, $filled, $inhibit_geocode);
  1137. // If we are in COW mode, we *probabaly* need to make a new lid.
  1138. if ($cow) {
  1139. if (isset($location['lid']) && $location['lid']) {
  1140. if (!empty($criteria)) {
  1141. // Check for other instances.
  1142. // See #306171 for more information.
  1143. $query = db_select('location_instance', 'l');
  1144. foreach ($criteria as $key => $value) {
  1145. $query->condition($key, $value);
  1146. }
  1147. $associated = $query->countQuery()->execute()->fetchField();
  1148. $all = db_query("SELECT COUNT(*) FROM {location_instance} WHERE lid = :lid", array(':lid' => $location['lid']))->fetchField();
  1149. if ($associated != $all) {
  1150. // If there were a different number of instances than instances matching the criteria,
  1151. // we need a new LID.
  1152. unset($location['lid']);
  1153. }
  1154. }
  1155. else {
  1156. // Criteria was not provided, we need a new LID.
  1157. unset($location['lid']);
  1158. }
  1159. }
  1160. }
  1161. if (!empty($location['lid'])) {
  1162. watchdog('location', 'Conserving lid %lid due to uniqueness.', array('%lid' => $location['lid']));
  1163. // Using a merge query instead of drupal_write_record to prevent failing to save
  1164. // if the lid happens to not exist in {location}, which has been seen in the wild.
  1165. // Tested in mysql, sqlite, and postgresql.
  1166. $fields = array();
  1167. foreach (array('name', 'street', 'additional', 'city', 'province', 'postal_code', 'country', 'latitude', 'longitude', 'source') as $key) {
  1168. if (isset($location[$key])) {
  1169. $fields[$key] = $location[$key];
  1170. }
  1171. }
  1172. db_merge('location')
  1173. ->key(array('lid' => $location['lid']))
  1174. ->fields($fields)
  1175. ->execute();
  1176. }
  1177. else {
  1178. unset($location['lid']);
  1179. drupal_write_record('location', $location);
  1180. }
  1181. location_invoke_locationapi($location, 'save');
  1182. return $location['lid'];
  1183. }
  1184. /**
  1185. * Computes the differences between two locations.
  1186. * @param $oldloc Original location.
  1187. * @param $newloc New location.
  1188. * @param &$changes Array of changes.
  1189. * The keys are field names, and the values are boolean FALSE and TRUE.
  1190. * @return Whether or not there were any changes.
  1191. */
  1192. function location_calc_difference($oldloc, $newloc, &$changes) {
  1193. location_strip($oldloc);
  1194. location_strip($newloc);
  1195. $location_changed = FALSE;
  1196. foreach ($newloc as $k => $v) {
  1197. if (!isset($oldloc[$k])) {
  1198. // Field missing from old location, automatic save.
  1199. $changes[$k] = TRUE;
  1200. $location_changed = TRUE;
  1201. continue;
  1202. }
  1203. elseif ($oldloc[$k] === $v) {
  1204. $changes[$k] = FALSE;
  1205. // Exact match, no change.
  1206. continue;
  1207. }
  1208. // It wasn't equal, but perhaps it was equivilent?
  1209. $results = location_invoke_locationapi($newloc, 'isunchanged', $k, $oldloc[$k]);
  1210. $waschanged = TRUE; // First, assume changed.
  1211. foreach ($results as $r) {
  1212. if ($r) {
  1213. $waschanged = FALSE;
  1214. $changes[$k] = FALSE;
  1215. }
  1216. }
  1217. if ($waschanged) {
  1218. // Nobody okayed this difference.
  1219. $changes[$k] = TRUE;
  1220. $location_changed = TRUE;
  1221. }
  1222. }
  1223. if (!$location_changed) {
  1224. return FALSE;
  1225. }
  1226. return TRUE;
  1227. }
  1228. /**
  1229. * Checks if a location is empty, and sets up an array of filled fields.
  1230. * @param $location The location to check.
  1231. * @param $filled An array (Will contain the list of filled fields upon return.)
  1232. *
  1233. * @return TRUE if the location is empty, FALSE otherwise.
  1234. */
  1235. function location_is_empty($location, &$filled) {
  1236. // Special case: Consider an empty array to be empty.
  1237. if (empty($location)) {
  1238. return TRUE;
  1239. }
  1240. // Special case: Consider a location with the "delete" checkbox checked to
  1241. // be empty.
  1242. if (isset($location['delete_location']) && $location['delete_location']) {
  1243. return TRUE;
  1244. }
  1245. // Patch locpick at this point.
  1246. // Otherwise, changing locpick only will not show a difference.
  1247. _location_patch_locpick($location);
  1248. $settings = isset($location['location_settings']) ? $location['location_settings'] : array();
  1249. $emptyloc = location_empty_location($settings);
  1250. return !location_calc_difference($emptyloc, $location, $filled);
  1251. }
  1252. /**
  1253. * Returns an empty location object based on the given settings.
  1254. */
  1255. function location_empty_location($settings) {
  1256. $location = array();
  1257. $defaults = location_invoke_locationapi($location, 'defaults');
  1258. if (isset($settings['form']['fields'])) {
  1259. foreach ($settings['form']['fields'] as $k => $v) {
  1260. if (isset($defaults[$k])) {
  1261. $defaults[$k] = array_merge($defaults[$k], $v);
  1262. }
  1263. }
  1264. }
  1265. foreach ($defaults as $k => $v) {
  1266. if (isset($v['default'])) {
  1267. $location[$k] = $v['default'];
  1268. }
  1269. }
  1270. return $location;
  1271. }
  1272. /**
  1273. * Strip junk out of a location.
  1274. */
  1275. function location_strip(&$location) {
  1276. $tmp = &drupal_static(__FUNCTION__);
  1277. if (!isset($tmp)) {
  1278. $tmp = array();
  1279. $defaults = location_invoke_locationapi($location, 'defaults');
  1280. foreach ($defaults as $k => $v) {
  1281. if (!isset($v['nodiff'])) {
  1282. $tmp[$k] = TRUE;
  1283. }
  1284. }
  1285. }
  1286. foreach ($location as $k => $v) {
  1287. if (!isset($tmp[$k])) {
  1288. unset($location[$k]);
  1289. }
  1290. }
  1291. }
  1292. /**
  1293. * Adjust a settings array.
  1294. * This will add any missing pieces and will set up requirements.
  1295. */
  1296. function location_normalize_settings(&$settings, $required = TRUE) {
  1297. if (!isset($settings['form'])) {
  1298. $settings['form'] = array();
  1299. }
  1300. if (!isset($settings['form']['fields'])) {
  1301. $settings['form']['fields'] = array();
  1302. }
  1303. // Merge defaults in.
  1304. $dummy = array();
  1305. $ds = location_invoke_locationapi($dummy, 'defaults');
  1306. foreach ($ds as $k => $v) {
  1307. if (!isset($settings['form']['fields'][$k])) {
  1308. $settings['form']['fields'][$k] = array();
  1309. }
  1310. $settings['form']['fields'][$k] = array_merge($v, $settings['form']['fields'][$k]);
  1311. }
  1312. // Adjust collection settings if the entire location is "optional."
  1313. if (!$required) {
  1314. // Relax non-required settings.
  1315. foreach ($settings['form']['fields'] as $k => $v) {
  1316. if (isset($v['collect'])) {
  1317. if ($v['collect'] == 2) {
  1318. // Required -> Optional.
  1319. $settings['form']['fields'][$k]['collect'] = 1;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. }
  1325. /**
  1326. * Perform geocoding logic, etc., prior to storing in the database.
  1327. */
  1328. function _location_geo_logic(&$location, $changed, $filled, $inhibit_geocode = FALSE) {
  1329. if (!$inhibit_geocode) {
  1330. // Have any of the fields possibly affecting geocoding changed?
  1331. // Or, was the location previously user submitted but is no longer?
  1332. if ( !empty($changed['street']) || !empty($changed['additional']) ||
  1333. !empty($changed['city']) || !empty($changed['province']) ||
  1334. !empty($changed['country']) || !empty($changed['postal_code']) ||
  1335. $location['source'] == LOCATION_LATLON_USER_SUBMITTED ) {
  1336. // Attempt exact geocoding.
  1337. if ($data = location_latlon_exact($location)) {
  1338. $location['source'] = LOCATION_LATLON_GEOCODED_EXACT;
  1339. // @@@ How about an accuracy field here?
  1340. $location['latitude'] = $data['lat'];
  1341. $location['longitude'] = $data['lon'];
  1342. // @@@ How about address normalization?
  1343. }
  1344. // Attempt inexact geocoding against a local postcode database
  1345. elseif ($data = location_get_postalcode_data($location)) {
  1346. $location['source'] = LOCATION_LATLON_GEOCODED_APPROX;
  1347. $location['latitude'] = $data['lat'];
  1348. $location['longitude'] = $data['lon'];
  1349. }
  1350. else {
  1351. $location['source'] = LOCATION_LATLON_UNDEFINED;
  1352. $location['latitude'] = 0;
  1353. $location['longitude'] = 0;
  1354. }
  1355. }
  1356. }
  1357. // Normalize coordinates.
  1358. while ($location['latitude'] > 90) {
  1359. $location['latitude'] -= 180;
  1360. }
  1361. while ($location['latitude'] < -90) {
  1362. $location['latitude'] += 180;
  1363. }
  1364. while ($location['longitude'] > 180) {
  1365. $location['longitude'] -= 360;
  1366. }
  1367. while ($location['longitude'] < -180) {
  1368. $location['longitude'] += 360;
  1369. }
  1370. // If city and/or province weren't set, see if we can fill them in with
  1371. // postal data OR if the city and/or province aren't configured to be
  1372. // collected through the form, set them to whatever the postal code data
  1373. // says they should be.
  1374. if (!empty($location['postal_code'])) {
  1375. if ( empty($location['city']) ||
  1376. empty($location['province']) ||
  1377. empty($location['location_settings']['form']['fields']['city']['collect']) ||
  1378. empty($location['location_settings']['form']['fields']['province']['collect'])
  1379. ) {
  1380. if ($data = location_get_postalcode_data($location)) {
  1381. $location['city'] = (empty($location['city']) || empty($location['location_settings']['form']['fields']['city']['collect'])) ? $data['city'] : $location['city'];
  1382. $location['province'] = (empty($location['province']) || empty($location['location_settings']['form']['fields']['province']['collect'])) ? $data['province'] : $location['province'];
  1383. }
  1384. }
  1385. }
  1386. // Normalize province.
  1387. // Note: Validation is performed elsewhere. We assume that the province
  1388. // specified matches either the short or long form of a province.
  1389. if (!empty($location['province']) && !empty($location['country'])) {
  1390. $location['province'] = location_province_code($location['country'], $location['province']);
  1391. }
  1392. // @@@ Now would be a GREAT time to hook.
  1393. }
  1394. /**
  1395. * Perform validation against a location fieldset.
  1396. */
  1397. function location_element_validate($element, &$form_state) {
  1398. // @@@ TODO -- future API change -- Send $form_state as param 0 so implementations can set values.
  1399. location_invoke_locationapi($element['#value'], 'validate', $element);
  1400. }
  1401. /**
  1402. * Convert decimal degrees to degrees,minutes,seconds.
  1403. */
  1404. function location_dd_to_dms($coord) {
  1405. $negative = ($coord < 0) ? TRUE : FALSE;
  1406. $coord = abs($coord);
  1407. $degrees = floor($coord);
  1408. $coord -= $degrees;
  1409. $coord *= 60;
  1410. $minutes = floor($coord);
  1411. $coord -= $minutes;
  1412. $coord *= 60;
  1413. $seconds = round($coord, 6);
  1414. return array($degrees, $minutes, $seconds, $negative);
  1415. }
  1416. /**
  1417. * Display a coordinate.
  1418. */
  1419. function theme_location_latitude_dms($variables) {
  1420. $latitude = $variables['latitude'];
  1421. $output = '';
  1422. list($degrees, $minutes, $seconds, $negative) = location_dd_to_dms($latitude);
  1423. $output .= "${degrees}° ${minutes}' ${seconds}\" ";
  1424. if (!$negative) {
  1425. $output .= 'N';
  1426. }
  1427. else {
  1428. $output .= 'S';
  1429. }
  1430. return $output;
  1431. }
  1432. function theme_location_longitude_dms($variables) {
  1433. $longitude = $variables['longitude'];
  1434. $output = '';
  1435. list($degrees, $minutes, $seconds, $negative) = location_dd_to_dms($longitude);
  1436. $output .= "${degrees}° ${minutes}' ${seconds}\" ";
  1437. if (!$negative) {
  1438. $output .= 'E';
  1439. }
  1440. else {
  1441. $output .= 'W';
  1442. }
  1443. return $output;
  1444. }
  1445. /**
  1446. * Implements hook_token_values().
  1447. */
  1448. function location_token_values($type, $object = NULL) {
  1449. require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'location') . '/location.token.inc');
  1450. return _location_token_values($type, $object);
  1451. }
  1452. /**
  1453. * Implements hook_token_list().
  1454. */
  1455. function location_token_list($type = 'all') {
  1456. require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'location') . '/location.token.inc');
  1457. return _location_token_list($type);
  1458. }
  1459. /**
  1460. * Theme preprocess function for a location.
  1461. */
  1462. function template_preprocess_location(&$variables) {
  1463. $location = $variables['location'];
  1464. // This will get taken back out if map links are hidden.
  1465. $location['map_link'] = TRUE;
  1466. if (is_array($variables['hide'])) {
  1467. foreach($variables['hide'] as $key) {
  1468. unset($location[$key]);
  1469. // Special case for coords.
  1470. if ($key == 'coords') {
  1471. unset($location['latitude']);
  1472. unset($location['longitude']);
  1473. }
  1474. }
  1475. }
  1476. $fields = location_field_names(TRUE);
  1477. if (is_array($fields)) {
  1478. foreach ($fields as $key => $value) {
  1479. $variables[$key] = '';
  1480. // Arrays can't be converted, ignore them.
  1481. if (!empty($location[$key]) && !is_array($location[$key])) {
  1482. $variables[$key] = check_plain($location[$key]);
  1483. }
  1484. }
  1485. }
  1486. // Map link.
  1487. $variables['map_link'] = '';
  1488. if (!empty($location['map_link'])) {
  1489. // Do not use $location for generating the map link, since it will
  1490. // not contain the country if that field is hidden.
  1491. $variables['map_link'] = location_map_link($variables['location']);
  1492. }
  1493. // Theme latitude and longitude as d/m/s.
  1494. $variables['latitude'] = '';
  1495. $variables['latitude_dms'] = '';
  1496. if (!empty($location['latitude'])) {
  1497. $variables['latitude'] = check_plain($location['latitude']);
  1498. $variables['latitude_dms'] = theme('location_latitude_dms', array('latitude' => $location['latitude']));
  1499. }
  1500. $variables['longitude'] = '';
  1501. $variables['longitude_dms'] = '';
  1502. if (!empty($location['longitude'])) {
  1503. $variables['longitude'] = check_plain($location['longitude']);
  1504. $variables['longitude_dms'] = theme('location_longitude_dms', array('longitude' => $location['longitude']));
  1505. }
  1506. // Add a country-specific template suggestion.
  1507. if (!empty($location['country']) && location_standardize_country_code($location['country'])) {
  1508. // $location['country'] is normalized in the previous line.
  1509. $variables['theme_hook_suggestions'][] = 'location__' . $location['country'];
  1510. }
  1511. // Display either the code or the full name for the province.
  1512. if (!isset($location['province'])) {
  1513. $location['province'] = '';
  1514. }
  1515. if (!isset($location['province_name'])) {
  1516. $location['province_name'] = '';
  1517. }
  1518. $variables['province_print'] = variable_get('location_use_province_abbreviation', 1) ? $location['province'] : $location['province_name'];
  1519. }
  1520. /**
  1521. * Theme preprocess function for location_distance.
  1522. */
  1523. function template_preprocess_location_distance(&$variables) {
  1524. $units = $variables['units'];
  1525. unset($variables['units']);
  1526. if ($units == 'km') {
  1527. $variables['shortunit'] = 'km';
  1528. $variables['longunit'] = 'kilometer(s)';
  1529. }
  1530. if ($units == 'mi') {
  1531. $variables['shortunit'] = 'mi';
  1532. $variables['longunit'] = 'mile(s)';
  1533. }
  1534. $variables['distance'] = (float)$variables['distance'];
  1535. }
  1536. /**
  1537. * Theme preprocess function for theming a group of locations.
  1538. */
  1539. function template_preprocess_locations(&$variables) {
  1540. if (isset($variables['locations']) && is_array($variables['locations'])) {
  1541. $locs = $variables['locations'];
  1542. }
  1543. else {
  1544. // The locations weren't valid -- Use an empty array instead to avoid warnings.
  1545. $locs = array();
  1546. }
  1547. $variables['locations'] = array();
  1548. $variables['rawlocs'] = $locs;
  1549. foreach ($locs as $location) {
  1550. $variables['locations'][] = theme('location', array('location' => $location, 'hide' => $variables['hide']));
  1551. }
  1552. }
  1553. /**
  1554. * Get a form element for configuring location for an object.
  1555. */
  1556. function location_settings($old = FALSE) {
  1557. if (empty($old)) {
  1558. $old = array();
  1559. }
  1560. $form = array(
  1561. '#type' => 'fieldset',
  1562. '#title' => t('Locative information'),
  1563. '#tree' => TRUE,
  1564. );
  1565. $form['multiple'] = array(
  1566. '#type' => 'fieldset',
  1567. '#title' => t('Number of locations'),
  1568. '#tree' => TRUE,
  1569. '#weight' => 2,
  1570. );
  1571. $form['multiple']['min'] = array(
  1572. '#type' => 'select',
  1573. '#title' => t('Minimum number of locations'),
  1574. '#options' => drupal_map_assoc(range(0, 100)),
  1575. '#default_value' => isset($old['multiple']['min']) ? $old['multiple']['min'] : 0,
  1576. '#description' => t('The number of locations that are required to be filled in.'),
  1577. );
  1578. $form['multiple']['max'] = array(
  1579. '#type' => 'select',
  1580. '#title' => t('Maximum number of locations'),
  1581. '#options' => drupal_map_assoc(range(0, 100)),
  1582. '#default_value' => isset($old['multiple']['max']) ? $old['multiple']['max'] : 1,
  1583. '#description' => t('The maximum number of locations that can be associated.'),
  1584. );
  1585. // @@@ Dynamic location adding via ahah?
  1586. $form['multiple']['add'] = array(
  1587. '#type' => 'select',
  1588. '#title' => t('Number of locations that can be added at once'),
  1589. '#options' => drupal_map_assoc(range(0, 100)),
  1590. '#default_value' => isset($old['multiple']['add']) ? $old['multiple']['add'] : 1,
  1591. '#description' => t('The number of empty location forms to show when editing.'),
  1592. );
  1593. // Thought: What about prefilled names and fixed locations that way?
  1594. // Then again, CCK would be cleaner.
  1595. $form['form'] = array(
  1596. '#type' => 'fieldset',
  1597. '#title' => t('Collection settings'),
  1598. '#tree' => TRUE,
  1599. '#weight' => 4,
  1600. );
  1601. $form['form']['weight'] = array(
  1602. '#type' => 'weight',
  1603. '#title' => t('Location form weight'),
  1604. '#default_value' => isset($old['form']['weight']) ? $old['form']['weight'] : 0,
  1605. '#description' => t('Weight of the location box in the add / edit form. Lower values will be displayed higher in the form.'),
  1606. );
  1607. $form['form']['collapsible'] = array(
  1608. '#type' => 'checkbox',
  1609. '#title' => t('Collapsible'),
  1610. '#default_value' => isset($old['form']['collapsible']) ? $old['form']['collapsible'] : TRUE,
  1611. '#description' => t('Make the location box collapsible.'),
  1612. );
  1613. $form['form']['collapsed'] = array(
  1614. '#type' => 'checkbox',
  1615. '#title' => t('Collapsed'),
  1616. '#default_value' => isset($old['form']['collapsed']) ? $old['form']['collapsed'] : TRUE,
  1617. '#description' => t('Display the location box collapsed.'),
  1618. );
  1619. $form['form']['fields'] = array(
  1620. '#type' => 'location_settings',
  1621. '#default_value' => isset($old['form']['fields']) ? $old['form']['fields'] : array(),
  1622. );
  1623. $form['display'] = array(
  1624. '#type' => 'fieldset',
  1625. '#title' => t('Display Settings'),
  1626. // '#description' => t('Here, you can change how locative data appears in nodes when viewed.'),
  1627. '#tree' => TRUE,
  1628. '#weight' => 6,
  1629. );
  1630. $form['display']['weight'] = array(
  1631. '#type' => 'weight',
  1632. '#title' => t('Display Weight'),
  1633. '#default_value' => isset($old['display']['weight']) ? $old['display']['weight'] : 0,
  1634. );
  1635. $fields = location_field_names(TRUE);
  1636. $form['display']['hide'] = array(
  1637. '#type' => 'checkboxes',
  1638. '#title' => t('Hide fields from display'),
  1639. '#default_value' => isset($old['display']['hide']) ? $old['display']['hide'] : array(),
  1640. '#options' => $fields,
  1641. );
  1642. return $form;
  1643. }
  1644. /**
  1645. * Get form elements for editing locations on an object.
  1646. */
  1647. function location_form($settings, $locations) {
  1648. if (!isset($settings['multiple']['max']) || $settings['multiple']['max'] == 0) {
  1649. // Location not enabled for this object type.
  1650. // Bail out early.
  1651. return array();
  1652. }
  1653. // Generate location fieldsets.
  1654. $numloc = count($locations);
  1655. // Show up to 'add' number of additional forms, in addition to the preexisting
  1656. // locations. (Less if we'll hit 'max' first.)
  1657. $numforms = min($numloc + $settings['multiple']['add'], $settings['multiple']['max']);
  1658. $form = array(
  1659. '#type' => 'fieldset',
  1660. '#title' => format_plural($numforms, 'Location', 'Locations'),
  1661. '#tree' => TRUE,
  1662. '#attributes' => array('class' => array('locations')),
  1663. '#weight' => $settings['form']['weight'],
  1664. '#collapsible' => $settings['form']['collapsible'],
  1665. '#collapsed' => $settings['form']['collapsed'],
  1666. );
  1667. for ($i = 0; $i < $numforms; $i++) {
  1668. $required = FALSE;
  1669. // Check if this is a required location.
  1670. if ($i < $settings['multiple']['min']) {
  1671. $required = TRUE;
  1672. }
  1673. $form[$i] = array(
  1674. '#type' => 'location_element',
  1675. '#has_garbage_value' => TRUE,
  1676. '#value' => '',
  1677. '#title' => t('Location #%number', array('%number' => $i + 1)),
  1678. '#default_value' => isset($locations[$i]) ? $locations[$i] : NULL,
  1679. '#location_settings' => $settings,
  1680. '#required' => $required,
  1681. );
  1682. }
  1683. // Tidy up the form in the single location case.
  1684. if ($numforms == 1) {
  1685. $form[0]['#title'] = t('Location');
  1686. // If the user had configured the form for a single location, inherit
  1687. // the collapsible / collapsed settings.
  1688. $form[0]['#collapsible'] = $form['#collapsible'];
  1689. $form[0]['#collapsed'] = $form['#collapsed'];
  1690. }
  1691. return $form;
  1692. }
  1693. function location_display($settings, $locations) {
  1694. if (!isset($settings['display']['hide'])) {
  1695. // We weren't configured properly, bail.
  1696. return array();
  1697. }
  1698. $hide = array_keys(array_filter($settings['display']['hide']));
  1699. // Show all locations
  1700. return array(
  1701. '#type' => 'markup',
  1702. '#theme' => 'locations',
  1703. '#locations' => $locations,
  1704. '#hide' => $hide,
  1705. '#weight' => $settings['display']['weight'],
  1706. );
  1707. }
  1708. function location_rss_item($location, $mode = 'simple') {
  1709. require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'location') . '/location.georss.inc');
  1710. return _location_rss_item($location, $mode);
  1711. }
  1712. /**
  1713. * Returns a list of accuracy codes as defined by the Google Maps API.
  1714. * See http://code.google.com/apis/maps/documentation/reference.html#GGeoAddressAccuracy.
  1715. */
  1716. function location_google_geocode_accuracy_codes() {
  1717. return array(
  1718. 0 => t('Unknown location'),
  1719. 1 => t('Country level accuracy'),
  1720. 2 => t('Region (state, province, prefecture, etc.) level accuracy'),
  1721. 3 => t('Sub-region (county, municipality, etc.) level accuracy'),
  1722. 4 => t('Town (city, village) level accuracy'),
  1723. 5 => t('Post code (zip code) level accuracy'),
  1724. 6 => t('Street level accuracy'),
  1725. 7 => t('Intersection level accuracy'),
  1726. 8 => t('Address level accuracy'),
  1727. 9 => t('Premise (building name, property name, shopping center, etc.) level accuracy'),
  1728. );
  1729. }