summaryrefslogtreecommitdiffstats
path: root/location.install
blob: 37d98213c11fd2141957f8f2342899b180cdc4ac (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
<?php

/**
 * @file
 * Install, update and uninstall functions for the location module.
 */

/**
 * Implements of hook_uninstall().
 */
function location_uninstall() {
  // Delete variables.
  variable_del('location_default_country');
  variable_del('location_display_location');
  variable_del('location_usegmap');
  variable_del('location_locpick_macro');

  // Delete geocoder settings.
  db_delete('variable')
    ->condition('name', db_like('location_geocode_') . '%', 'LIKE')
    ->execute();

  // Delete maxnum settings.
  db_delete('variable')
    ->condition('name', db_like('location_maxnum_') . '%', 'LIKE')
    ->execute();

  cache_clear_all('variables', 'cache_bootstrap');
}

/**
 * Implements of hook_schema().
 */
function location_schema() {
  $schema['location'] = array(
    'description' => 'Locational data managed by location.module.',
    'fields' => array(
      'lid' => array(
        'description' => 'Primary Key: Unique location ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Place Name.',
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
        'not null' => TRUE,
      ),
      'street' => array(
        'description' => 'Street address, line 1.',
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
        'not null' => TRUE,
      ),
      'additional' => array(
        'description' => 'Street address, line 2.',
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
        'not null' => TRUE,
      ),
      'city' => array(
        'description' => 'City.',
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
        'not null' => TRUE,
      ),
      'province' => array(
        'description' => 'State / Province code.',
        'type' => 'varchar',
        'length' => 16,
        'default' => '',
        'not null' => TRUE,
      ),
      'postal_code' => array(
        'description' => 'Postal / ZIP code.',
        'type' => 'varchar',
        'length' => 16,
        'default' => '',
        'not null' => TRUE,
      ),
      'country' => array(
        'description' => 'Two letter ISO country code.',
        'type' => 'char',
        'length' => 2,
        'not null' => TRUE,
        'default' => '',
      ),
      'latitude' => array(
        'description' => 'Location latitude (decimal degrees).',
        'type' => 'numeric',
        'precision' => 10,
        // @todo Shouldn't these all be 7?
        'scale' => 6,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'longitude' => array(
        'description' => 'Location longitude (decimal degrees).',
        'type' => 'numeric',
        'precision' => 10,
        'scale' => 6,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'source' => array(
        'description' => 'Source of the latitude and longitude data (Geocoder, user entered, invalid, etc.)',
        'type' => 'int',
        'size' => 'tiny',
        'default' => 0,
        'not null' => TRUE,
      ),
      // @@@ Historical civicrm field that isn't applicable to location, I think..
      'is_primary' => array(
        'description' => 'Is this the primary location of an object? (unused, civicrm legacy field?).',
        'type' => 'int',
        'size' => 'tiny',
        'default' => 0,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('lid'),
  );
  $schema['location_instance'] = array(
    'description' => 'N:M join table to join locations to other tables.',
    'fields' => array(
      'nid' => array(
        'description' => 'Reference to {node}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'vid' => array(
        'description' => 'Reference to {node_revision}.vid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'Reference to {users}.uid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'genid' => array(
        'description' => 'Generic reference key.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'lid' => array(
        'description' => 'Reference to {location}.lid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
      'vid' => array('vid'),
      'uid' => array('uid'),
      'genid' => array('genid'),
      'lid' => array('lid'),
    ),
  );
  $schema['zipcodes'] = array(
    'description' => 'Location.module zipcode database.',
    'fields' => array(
      'zip' => array(
        'description' => 'Postal / ZIP code.',
        'type' => 'varchar',
        'length' => 16,
        'not null' => TRUE,
        'default' => '0',
      ),
      'city' => array(
        'description' => 'City.',
        'type' => 'varchar',
        'length' => 30,
        'not null' => TRUE,
        'default' => '',
      ),
      'state' => array(
        'description' => 'Province / State.',
        'type' => 'varchar',
        'length' => 30,
        'not null' => TRUE,
        'default' => '',
      ),
      'latitude' => array(
        'description' => 'Location latitude (decimal degrees).',
        'type' => 'numeric',
        'precision' => 10,
        'scale' => 6,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'longitude' => array(
        'description' => 'Location longitude (decimal degrees).',
        'type' => 'numeric',
        'precision' => 10,
        'scale' => 6,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      // @@@ Not used, an artifact of the original data dump format.
      'timezone' => array(
        'description' => 'Timezone (unused).',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      // @@@ Not used, an artifact of the original data dump format.
      'dst' => array(
        'description' => 'Daylight Savings Time (unused).',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'country' => array(
        'description' => 'Two letter ISO country code.',
        'type' => 'char',
        'length' => 2,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    // @@@ This pk is invalid, see issue queue.
    // 'primary key' => array('country', 'zip').
    // @@@ These need reworked.
    'indexes' => array(
      'pc' => array('country', 'zip'),
      'zip' => array('zip'),
      // @@@ No combined one?
      'latitude' => array('latitude'),
      'longitude' => array('longitude'),
      'country' => array('country'),
    ),
  );
  // Copied from system.module.
  $schema['cache_location'] = array(
    'description' => 'Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.',
    'fields' => array(
      'cid' => array(
        'description' => 'Primary Key: Unique cache ID.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'data' => array(
        'description' => 'A collection of data to cache.',
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
      ),
      'expire' => array(
        'description' => 'A Unix timestamp indicating when the cache entry should expire, or 0 for never.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'created' => array(
        'description' => 'A Unix timestamp indicating when the cache entry was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'headers' => array(
        'description' => 'Any custom HTTP headers to be added to cached data.',
        'type' => 'text',
        'not null' => FALSE,
      ),
      'serialized' => array(
        'description' => 'A flag to indicate whether content is serialized (1) or not (0).',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0
      )
    ),
    'indexes' => array('expire' => array('expire')),
    'primary key' => array('cid'),
  );

  // New location country table (needed for sorting).
  $schema['location_country'] = array(
    'description' => 'Country data managed by location.module.',
    'fields' => array(
      'code' => array(
        'description' => 'Primary Key: Two letter ISO Country Code',
        'type' => 'char',
        'length' => 2,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Full Country Name ',
        'type' => 'varchar',
        'length' => 255,
        'default' => '',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('code'),
  );

  return $schema;
}

/**
 * Legacy update 1.
 *
 * Convert tables to utf8.
 */
function location_update_1() {
}

/**
 * Legacy update 2.
 *
 * Fix a bug with the "us" entry in the "location_configured_countries" var.
 */
function location_update_2() {
  $configured_countries = variable_get('location_configured_countries', array());
  if ($configured_countries['us']) {
    $configured_countries['us'] = 'us';
    variable_set('location_configured_countries', $configured_countries);
  }

  return array();
}

/**
 * Legacy update 3.
 *
 * Allow for postgresql support by renaming the oid column, which is a reserved
 * name on postgresql.
 */
function location_update_3() {
  db_change_field(
    'location',
    'oid',
    'eid',
    array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    )
  );

  if (module_exists('views')) {
    views_invalidate_cache();
  }

  return t(
    "The schema for location module has been updated.  The update is such that you may want to re-resave any views you have that may include locations."
  );
}

/***************************************************************
 * PostgreSQL must be supported in all updates after this comment
 ***************************************************************/

/**
 * Legacy update 4.
 *
 * Add "lid" as the new location key.
 */
function location_update_4() {
  db_add_field(
    'location',
    'lid',
    array(
      'type' => 'serial',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'description' => 'Primary Key: Unique location ID.',
    )
  );

  $result = db_query("SELECT eid, type FROM {location}");
  $next_id = 0;
  foreach ($result as $row) {
    $next_id++;
    db_update('location')
      ->fields(
        array(
          'lid' => $next_id,
        )
      )
      ->condition('eid', $row->eid)
      ->condition('type', $row->type)
      ->execute();
  }

  db_drop_primary_key('location');
  db_add_primary_key('location', array('lid'));

  db_insert('sequences')
    ->fields(
      array(
        'name' => '{location}_lid',
        'id' => $next_id,
      )
    )
    ->execute();

  db_add_field(
    'location',
    'is_primary',
    array(
      'description' => 'Is this the primary location of an object? (unused, civicrm legacy field?).',
      'type' => 'int',
      'size' => 'tiny',
      'default' => 0,
      'not null' => TRUE,
    )
  );

  db_update('location')
    ->fields(
      array(
        'is_primary' => 1,
      )
    )
    ->condition('type', 'user')
    ->execute();

  foreach (node_type_get_types() as $type) {
    $new_setting = variable_get('location_' . $type->type, 0) ? 1 : 0;
    variable_del('location_' . $type->type);
    variable_set('location_maxnum_' . $type->type, $new_setting);
    variable_set('location_defaultnum_' . $type->type, $new_setting);
  }
}

/**
 * Legacy update 5.
 *
 * Postgresql support that was missing from previous update.
 *
 * This update is redundant now that we have db api functions
 * that are database independant.
 */
function location_update_5() {
}

/**
 * Legacy update 6.
 *
 * Use correct country code for Sweeden.
 */
function location_update_6() {
  db_update('location')
    ->fields(
      array(
        'country' => 'se',
      )
    )
    ->condition('country', 'sw')
    ->execute();
}

/**
 * Update 7 (Location 2.x).
 *
 * Generalize google geocoding so you don't have to enter the api key over and over.
 */
function location_update_7() {
  $services = array('google');
  $general_geocoders_in_use = array();

  $result = db_query("SELECT * FROM {variable} WHERE name REGEXP '^location_geocode_[a-z][a-z]$'");

  foreach ($result as $row) {
    $value_decoded = unserialize($row->value);
    if (!in_array($value_decoded, $services)) {
      db_update('variable')
        ->fields(
          array(
            'value' => serialize($value_decoded . '|' . substr($row->name, 17)),
          )
        )
        ->condition('name', $row->name)
        ->execute();
    }
    else {
      $general_geocoders_in_use[$value_decoded] = $value_decoded;
    }
  }

  $key = db_query(
    "SELECT value FROM {variable} WHERE name REGEXP '^location_geocode_[a-z][a-z]_google_apikey$'"
  )->fetchField();

  db_delete('variable')
    ->where("name REGEXP '^location_geocode_[a-z][a-z]_google_apikey$'")
    ->execute();

  db_insert('variable')
    ->fields(
      array(
        'name' => 'location_geocode_google_apikey',
        'value' => $key,
      )
    )
    ->execute();

  db_delete('cache')
    ->condition('cid', 'variables')
    ->execute();

  variable_set('location_general_geocoders_in_use', $general_geocoders_in_use);
}

/**
 * Location 3.x update 1.
 *
 * Add location specific cache table.
 */
function location_update_5300() {
  $schema['cache_location'] = array(
    'description' => 'Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.',
    'fields' => array(
      'cid' => array(
        'description' => 'Primary Key: Unique cache ID.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'data' => array(
        'description' => 'A collection of data to cache.',
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
      ),
      'expire' => array(
        'description' => 'A Unix timestamp indicating when the cache entry should expire, or 0 for never.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'created' => array(
        'description' => 'A Unix timestamp indicating when the cache entry was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'headers' => array(
        'description' => 'Any custom HTTP headers to be added to cached data.',
        'type' => 'text',
        'not null' => FALSE,
      ),
    ),
    'indexes' => array('expire' => array('expire')),
    'primary key' => array('cid'),
  );

  db_create_table('cache_location', $schema['cache_location']);
}

/**
 * Location 3.x update 2.
 *
 * Normalize the location table.
 * This allows:
 *   - Making the loading and saving code cleaner.
 *   - Fixing a longstanding bug with revisions.
 *   - Having the same location on multiple nodes/users/both.
 *   - Garbage collecting unused locations periodically.
 *   - Having full support for deletions.
 *   - Full revisions support.
 * Note that the location_instance table does NOT have a primary key.
 * This is on purpose. It's a N:M join table.
 */
function location_update_5301() {
  $schema['location_instance'] = array(
    'description' => 'N:M join table to join locations to other tables.',
    'fields' => array(
      'nid' => array(
        'description' => 'Reference to {node}.nid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'vid' => array(
        'description' => 'Reference to {node_revision}.vid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'Reference to {users}.uid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'genid' => array(
        'description' => 'Generic reference key.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'lid' => array(
        'description' => 'Reference to {location}.lid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
      'vid' => array('vid'),
      'uid' => array('uid'),
      'genid' => array('genid'),
      'lid' => array('lid'),
    ),
  );

  db_create_table('location_instance', $schema['location_instance']);

  // Synthesise node location data based on what we have.
  // Storage of locations was previously stored against node revision, BUT the
  // data was not properly duplicated by revision (i.e. only the latest revision
  // carried the data.)
  // Joining like this allows us to backfill all the old revisions with the current
  // data, which is not nice but better than having no data at all when viewing
  // old revisions.

  $query = db_select('node_revision', 'nr');
  $query->join('node_revision', 'nr2', 'nr.nid = nr2.nid');
  $query->join('location', 'l', "nr2.vid = l.eid AND l.type = 'node'");
  $query->addField('nr', 'nid');
  $query->addField('nr', 'vid');
  $query->addField('l', 'lid');

  db_insert('location_instance')
    ->fields(array('nid', 'vid', 'lid'))
    ->from($query)
    ->execute();

  // Users is much simpler.
  $query = db_select('location', 'l');
  $query->addField('l', 'eid');
  $query->addField('l', 'lid');
  $query->condition('type', 'user');

  db_insert('location_instance')
    ->fields(array('uid', 'lid'))
    ->from($query)
    ->execute();

  // Aug 18 2008:
  // Save everything else in genid.
  $query = db_select('location', 'l');
  $query->addExpression("CONCAT(l.type, ':', l.eid)");
  $query->addField('l', 'lid');
  $query->condition('type', 'user', '<>');
  $query->condition('type', 'node', '<>');

  db_insert('location_instance')
    ->fields(array('genid', 'lid'))
    ->from($query)
    ->execute();

  // Remove now unused columns.
  db_drop_field('location', 'type');
  db_drop_field('location', 'eid');

  // General cleanup.
  // Removed in favor of permission check.
  variable_del('location_user');

  // Variable consolidation (as part of the element based system)
  // We're doing this "raw" so we can be sure we got everything moved over,
  // INCLUDING content types that were deleted in the past.
  // This will let us do better cleanup sometime in the future.
  $data = array();
  $todelete = array();
  foreach (array(
             'name',
             'street',
             'additional',
             'city',
             'province',
             'postal_code',
             'country',
             'phone',
             'fax',
           ) as $field) {
    $result = db_query("SELECT name, value FROM {variable} WHERE name > :name", array(':name' => "location_$field%"));
    foreach ($result as $row) {
      $data[substr($row->name, strlen($field) + 10)][$field] = (string) (int) unserialize($row->value);
      $todelete[] = $row->name;
    }
  }
  foreach ($data as $type => $value) {
    // We aren't going to trust that hook_locationapi is operational.
    // So, stick with some conservative defaults.
    $value = array_merge(
      array(
        'name' => '1',
        'street' => '1',
        // Additional is left out of this list intentionally.
        'city' => '0',
        'province' => '0',
        'postal_code' => '0',
        'country' => '1',
      ),
      $value
    );
    if (!isset($value['additional'])) {
      // Initialize additional to match street.
      $value['additional'] = $value['street'];
    }
    variable_set('location_fields_' . $type, $value);
  }
  foreach ($todelete as $key) {
    variable_del($key);
  }

  // This update was retrofitted on Aug 18, 2008. Set a flag for use by
  // the next update in order to handle the case where someone has already
  // updated to EXACTLY schema revision 5301 before the retrofit took effect.
  // People who migrated past this point before that date may have the following
  // inconsistencies:
  // A) location_{field}_{type} variables were not collected for content types
  // that had been deleted in the past.
  // B) Any locations with the 'type' field set to something other than 'node'
  // or 'user' did not get entries in {location_instance}.
  variable_set('location_update_5301_retrofit', TRUE);
}

/**
 * Location 3.x update 3.
 *
 * Add genid to {location_instance}.
 */
function location_update_5302() {
  // OK, here's the deal. I retrofitted 5301 on Aug 18 2008 to integrate the genid.
  // This was needed to fix the pre location 3.x todo item regarding keeping non
  // user, non node data intact. People doing an update after Aug 18 will already
  // have the genid column in place, so it can be safely skipped.
  if (!variable_get('location_update_5301_retrofit', FALSE)) {
    db_add_field(
      'location_instance',
      'genid',
      array(
        'description' => 'Generic reference key.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      )
    );

    db_add_index('location_instance', 'genid', array('genid'));
  }
}

/**
 * Location 3.x update 4.
 *
 * Shuffle more variables around.
 */
function location_update_5303() {
  $types = array();
  $result = db_query("SELECT name FROM {variable} WHERE name LIKE 'location_display_teaser_%'");
  foreach ($result as $row) {
    $type = substr($row->name, 24);
    $types[$type]['teaser'] = variable_get('location_display_teaser_' . $type, TRUE);
    $types[$type]['full'] = variable_get('location_display_full_' . $type, TRUE);
    $types[$type]['weight'] = variable_get('location_display_weight_' . $type, 0);
    // @@@ Combine location_suppress_country and country require settings to set this up?
    $types[$type]['hide'] = array();
  }
  foreach ($types as $type => $value) {
    variable_set("location_display_$type", $value);
    variable_del("location_display_teaser_$type");
    variable_del("location_display_full_$type");
    variable_del("location_display_weight_$type");
  }
}

// @@@ Does 5303 need rerun in some circumstances?

/**
 * Drupal 6 location 3.x update.
 */
function location_update_6301() {
  $message = '';

  // Update cache table.
  db_drop_table('cache_location');

  $schema['cache_location'] = array(
    'description' => 'Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.',
    'fields' => array(
      'cid' => array(
        'description' => 'Primary Key: Unique cache ID.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'data' => array(
        'description' => 'A collection of data to cache.',
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
      ),
      'expire' => array(
        'description' => 'A Unix timestamp indicating when the cache entry should expire, or 0 for never.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'created' => array(
        'description' => 'A Unix timestamp indicating when the cache entry was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'headers' => array(
        'description' => 'Any custom HTTP headers to be added to cached data.',
        'type' => 'text',
        'not null' => FALSE
      ),
      'serialized' => array(
        'description' => 'A flag to indicate whether content is serialized (1) or not (0).',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      )
    ),
    'indexes' => array('expire' => array('expire')),
    'primary key' => array('cid'),
  );

  db_create_table('cache_location', $schema['cache_location']);

  // LID 0 causes all sorts of issues, and will break our update routine
  // unless we handle it beforehand.
  // Since we're so nice, we're gonna renumber it for the user.
  $has_rows = (bool) db_query_range('SELECT 1 FROM {location} WHERE lid = 0', 0, 1)->fetchField();
  if ($has_rows) {
    $lid = 1 + db_query('SELECT MAX(lid) FROM {location}')->fetchField();
    $message = t(
      'Note: A location with lid 0 was found in your database. It has been moved to lid %lid. You may wish to verify it manually, as lid 0 is usually a corrupt entry.',
      array('%lid' => $lid)
    );

    db_update('location')
      ->fields(
        array(
          'lid' => $lid,
        )
      )
      ->condition('lid', 0)
      ->execute();

    db_update('location_instance')
      ->fields(
        array(
          'lid' => $lid,
        )
      )
      ->condition('lid', 0)
      ->execute();
  }

  // Field changes.
  // {location}.lid -- Becomes a serial.
  db_drop_primary_key('location');
  db_change_field(
    'location',
    'lid',
    'lid',
    array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
    array('primary key' => array('lid'))
  );

  // The rest of the changes to this table were moved to update 6302 due to a bug.
  // {location_instance}
  // Fix oddly named indexes -- Was using the postgresql method for both.
  db_drop_index('location_instance', '{location_instance}_nid_idx');
  db_drop_index('location_instance', '{location_instance}_vid_idx');
  db_drop_index('location_instance', '{location_instance}_uid_idx');
  db_drop_index('location_instance', '{location_instance}_genid_idx');
  db_drop_index('location_instance', '{location_instance}_lid_idx');
  db_drop_index('location_instance', 'nid');
  db_drop_index('location_instance', 'vid');
  db_drop_index('location_instance', 'uid');
  db_drop_index('location_instance', 'genid');
  db_drop_index('location_instance', 'lid');

  // Fill in nulls.
  db_update('location_instance')
    ->fields(
      array(
        'nid' => 0,
      )
    )
    ->isNull('nid')
    ->execute();

  db_update('location_instance')
    ->fields(
      array(
        'vid' => 0,
      )
    )
    ->isNull('vid')
    ->execute();

  db_update('location_instance')
    ->fields(
      array(
        'uid' => 0,
      )
    )
    ->isNull('uid')
    ->execute();

  db_update('location_instance')
    ->fields(
      array(
        'genid' => 0,
      )
    )
    ->isNull('genid')
    ->execute();

  // {location_instance}.nid
  db_change_field(
    'location_instance',
    'nid',
    'nid',
    array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    )
  );
  // {location_instance}.vid
  db_change_field(
    'location_instance',
    'vid',
    'vid',
    array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    )
  );
  // {location_instance}.uid
  db_change_field(
    'location_instance',
    'uid',
    'uid',
    array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    )
  );
  // {location_instance}.genid
  db_change_field(
    'location_instance',
    'genid',
    'genid',
    array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    )
  );

  // {location_instance}.lid
  db_change_field(
    'location_instance',
    'lid',
    'lid',
    array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    )
  );

  // Readd indexes.
  db_add_index('location_instance', 'nid', array('nid'));
  db_add_index('location_instance', 'vid', array('vid'));
  db_add_index('location_instance', 'uid', array('uid'));
  db_add_index('location_instance', 'genid', array('genid'));
  db_add_index('location_instance', 'lid', array('lid'));

  // {zipcodes}
  // Drop primary key.
  db_drop_primary_key('zipcodes');

  if ($message) {
    $message .= '<br /><br />';
  }

  return $message . t(
    'Note: Location.module update 6301 will generate several warnings/failures regarding indexes and primary keys if you are upgrading from one of the 6.x test releases. These warnings can be safely disregarded in this case.'
  );
}

/**
 * Drupal 6 location 3.x update, part 2.
 */
function location_update_6302() {
  // OK, here's the update to fix the previous update which had a few problems
  // when upgrading from pre-rc 6.x versions.

  // The "mismatch between mysql and postgresql" was actually applicable to
  // 6.x-3.0 pre-rc1 as well, but I didn't notice because I accidentally added
  // the not null when reformatting the schema.
  db_update('location')
    ->fields(
      array(
        'is_primary' => 0,
      )
    )
    ->isNull('is_primary')
    ->execute();

  db_change_field(
    'location',
    'is_primary',
    'is_primary',
    array(
      'type' => 'int',
      'size' => 'tiny',
      'default' => 0,
      'not null' => TRUE,
    )
  );

  // Fix zipcode mismatches caused by the same problem.
  // There shouldn't be any rows like this, but it doesn't hurt to be sure.
  db_update('zipcodes')
    ->fields(
      array(
        'zip' => 0,
      )
    )
    ->isNull('zip')
    ->execute();

  // Set not null.
  db_change_field(
    'zipcodes',
    'zip',
    'zip',
    array(
      'type' => 'varchar',
      'length' => 16,
      'not null' => TRUE,
      'default' => '0',
    )
  );

  // Prepare latitude and longitude for the same.
  db_update('zipcodes')
    ->fields(
      array(
        'latitude' => 0.0,
      )
    )
    ->isNull('latitude')
    ->execute();

  db_update('zipcodes')
    ->fields(
      array(
        'longitude' => 0.0,
      )
    )
    ->isNull('longitude')
    ->execute();

  // Set not null.
  db_change_field(
    'zipcodes',
    'latitude',
    'latitude',
    array(
      'type' => 'numeric',
      'not null' => TRUE,
      'default' => 0,
      'precision' => 10,
      'scale' => 6,
    )
  );
  db_change_field(
    'zipcodes',
    'longitude',
    'longitude',
    array(
      'type' => 'numeric',
      'not null' => TRUE,
      'default' => 0,
      'precision' => 10,
      'scale' => 6,
    )
  );

  // Prepare country.
  update_sql("UPDATE {zipcodes} SET country = '' WHERE country IS NULL");
  db_update('zipcodes')
    ->fields(
      array(
        'country' => '',
      )
    )
    ->isNull('country')
    ->execute();

  // Set not null.
  db_change_field(
    'zipcodes',
    'country',
    'country',
    array(
      'type' => 'char',
      'length' => 2,
      'not null' => TRUE,
      'default' => '',
    )
  );

  // Fix up possible {location} problems from previous update that could be caused if you had NULLed fields.

  // Set defaults.
  $fields = array(
    'name' => '',
    'street' => '',
    'additional' => '',
    'city' => '',
    'province' => '',
    'postal_code' => '',
    'latitude' => 0.0,
    'longitude' => 0.0,
    'source' => 0,
  );
  foreach ($fields as $field => $value) {
    db_update('location')
      ->fields(
        array(
          $field => $value,
        )
      )
      ->isNull($field)
      ->execute();
  }

  // {location}.name -- NOT NULL.
  db_change_field(
    'location',
    'name',
    'name',
    array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    )
  );
  // {location}.street -- NOT NULL.
  db_change_field(
    'location',
    'street',
    'street',
    array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    )
  );
  // {location}.additional -- NOT NULL.
  db_change_field(
    'location',
    'additional',
    'additional',
    array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    )
  );
  // {location}.city -- NOT NULL.
  db_change_field(
    'location',
    'city',
    'city',
    array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    )
  );
  // {location}.province -- NOT NULL.
  db_change_field(
    'location',
    'province',
    'province',
    array(
      'type' => 'varchar',
      'length' => 16,
      'not null' => TRUE,
      'default' => '',
    )
  );
  // {location}.postal_code -- NOT NULL.
  db_change_field(
    'location',
    'postal_code',
    'postal_code',
    array(
      'type' => 'varchar',
      'length' => 16,
      'not null' => TRUE,
      'default' => '',
    )
  );
  // {location}.country -- NOT NULL.
  db_change_field(
    'location',
    'country',
    'country',
    array(
      'type' => 'char',
      'length' => 2,
      'not null' => TRUE,
      'default' => '',
    )
  );

  // {location}.latitude.
  db_change_field(
    'location',
    'latitude',
    'latitude',
    array(
      'type' => 'numeric',
      'precision' => 10,
      'scale' => 6,
      'not null' => TRUE,
      'default' => 0.0,
    )
  );
  // {location}.longitude.
  db_change_field(
    'location',
    'longitude',
    'longitude',
    array(
      'type' => 'numeric',
      'precision' => 10,
      'scale' => 6,
      'not null' => TRUE,
      'default' => 0.0,
    )
  );

  // {location}.source.
  db_change_field(
    'location',
    'source',
    'source',
    array(
      'type' => 'int',
      'size' => 'tiny',
      'not null' => TRUE,
      'default' => 0,
    )
  );
}

/**
 * Drupal 6 location 3.x update, part 3.
 */
function location_update_6303() {
  if (!variable_get('location_update_5304_done', FALSE)) {
    // Do the same updates as 5304.
    // Delete unused variables.
    variable_del('location_configured_countries');
    variable_del('location_garbagecollect');

    // Update province code for Italy/Forlì-Cesena.
    db_update('location')
      ->fields(
        array(
          'province' => 'FC',
        )
      )
      ->condition('country', 'it')
      ->condition('province', 'FO')
      ->execute();

    // Update province code for Italy/Pesaro e Urbino.
    db_update('location')
      ->fields(
        array(
          'province' => 'PU',
        )
      )
      ->condition('country', 'it')
      ->condition('province', 'PS')
      ->execute();

    // Do one final garbage collection by hand.
    $query = db_select('location_instance', 'li')
      ->addField('li', 'lid');
    db_delete('location')
      ->condition('lid', $query, 'NOT IN')
      ->execute();

    // Garbage collect {location_phone} by hand.
    if (db_table_exists('location_phone')) {
      $query = db_select('location', 'l')
        ->addField('l', 'lid');
      db_delete('location_phone')
        ->condition('lid', $query, 'NOT IN')
        ->execute();
    }

    // Garbage collect {location_fax} by hand.
    if (db_table_exists('location_fax')) {
      $query = db_select('location', 'l')
        ->addField('l', 'lid');
      db_delete('location_fax')
        ->condition('lid', $query, 'NOT IN')
        ->execute();
    }

    variable_del('location_update_5304_done');
  }
}

/**
 * Upgrade all of the settings variables to the new unified system.
 */
function location_update_6304() {
  // Skip this update if it was already done on the 5.x side.
  if (variable_get('location_update_5305_done', FALSE)) {
    variable_del('location_update_5305_done');

    return array();
  }

  $variables = array();
  $todelete = array();

  $base = array(
    'multiple' => array(
      'min' => 0,
      'max' => 0,
      'add' => 3,
    ),
    'form' => array(
      'weight' => 0,
      'collapsible' => TRUE,
      'collapsed' => TRUE,
      'fields' => array(),
    ),
    'display' => array(
      'weight' => 0,
      'hide' => array(),
    ),
  );

  // Pull in user settings.
  $variables['location_settings_user'] = $base;
  $tmp = & $variables['location_settings_user'];
  // Users previously could not have multiple locations, initialize with those
  // settings.
  $tmp['multiple'] = array(
    'min' => 0,
    'max' => 1,
    'add' => 3,
  );
  $tmp['form']['weight'] = variable_get('location_user_weight', 9);
  $tmp['form']['collapsible'] = variable_get('location_user_collapsible', TRUE);
  $tmp['form']['collapased'] = variable_get('location_user_collapsed', TRUE);
  $tmp['form']['fields'] = variable_get('location_user_fields', array());

  // Pull in node settings.
  $result = db_query("SELECT name FROM {variable} WHERE name LIKE 'location_maxnum_%'");
  foreach ($result as $row) {
    $type = substr($row->name, 16);
    $todelete[] = $type;

    $variables["location_settings_node_$type"] = $base;
    $tmp = & $variables["location_settings_node_$type"];

    // Old behavior was to have the first one be required.
    $tmp['multiple']['min'] = 1;
    $tmp['multiple']['max'] = variable_get("location_maxnum_$type", 0);
    $tmp['multiple']['add'] = variable_get("location_defaultnum_$type", 3);
    $tmp['form']['weight'] = variable_get("location_weight_$type", 9);
    $tmp['form']['collapsible'] = variable_get("location_collapsible_$type", TRUE);
    $tmp['form']['collapsed'] = variable_get("location_collapsed_$type", TRUE);
    $tmp['form']['fields'] = variable_get("location_fields_$type", array());
    $tmp['rss']['mode'] = variable_get("location_rss_$type", 'simple');
    $tmp['display'] = variable_get(
      "location_display_$type",
      array(
        'teaser' => TRUE,
        'full' => TRUE,
        'weight' => 0,
        'hide' => array(),
      )
    );
  }

  foreach ($variables as $name => $value) {
    variable_set($name, $value);
  }

  // Delete old node variables.
  foreach ($todelete as $key) {
    variable_del("location_weight_$key");
    variable_del("location_collapsible_$key");
    variable_del("location_collapsed_$key");
    variable_del("location_fields_$key");
    variable_del("location_rss_$key");
    variable_del("location_display_$key");
  }

  // Delete old user variables.
  variable_del('location_user_weight');
  variable_del('location_user_collapsible');
  variable_del('location_user_collapsed');
  variable_del('location_user_fields');
}

/**
 * Disabled due to some typos, moved to 6306.
 */
function location_update_6305() {
  return array();
}

/**
 * Add per-location-field weights and defaults.
 */
function location_update_6306() {
  // Skip this update if it was already done on the 5.x side.
  if (variable_get('location_update_5306_done', FALSE)) {
    variable_del('location_update_5306_done');

    return array();
  }

  $result = db_query("SELECT name FROM {variable} WHERE name LIKE 'location_settings_%'");
  foreach ($result as $row) {
    $var = variable_get($row->name, array());
    $collect = $var['form']['fields'];
    $var['form']['fields'] = array();
    foreach ($collect as $k => $v) {
      $var['form']['fields'][$k]['collect'] = $v;
    }
    // Country 3 has changed to 4 to make requirements code easier.
    if (isset($var['form']['fields']['country']['collect']) && $var['form']['fields']['country']['collect'] == 3) {
      $var['form']['fields']['country']['collect'] = 4;
    }
    // Weight and default values don't need to get set for now.
    variable_set($row->name, $var);
  }
}

/**
 * Tell users about the new location_user module and enable it.
 */
function location_update_6307() {
  $message = t(
    "Note: Location module has now split off user location support into a separate module, called <em>User Locations</em>. It has been enabled for you. If you don't want user locations, visit the <a href='!url'>modules page</a> and disable it.",
    array('!url' => url('admin/modules'))
  );

  if (module_exists('location')) {
    module_enable(array('location_user'));
  }
  else {
    $message .= '<br />' . t(
        "Note: Refusing to enable location_user.module, as location.module is not currently enabled."
      );
  }

  return $message;
}

/**
 * Tell users about the new location_node module and enable it.
 */
function location_update_6308() {
  $message = t(
    "Note: Location module has now split off node location support into a separate module, called <em>Node Locations</em>. It has been enabled for you. If you don't want node locations, visit the <a href='!url'>modules page</a> and disable it.",
    array('!url' => url('admin/modules'))
  );

  if (module_exists('location')) {
    module_enable(array('location_node'));
  }
  else {
    $message .= '<br />' . t(
        "Note: Refusing to enable location_node.module, as location.module is not currently enabled."
      );
  }

  return $message;
}

/**
 * There has been a change in the options for the views distance/proximity filter.
 *
 * The old options need to be migrated into the new.
 */
function location_update_6309() {
  $updated_views = array();

  if (module_exists('views')) {
    $views = views_get_all_views();
    foreach ($views as $view) {
      $modified = FALSE;
      // For each view check all filters of all displays for the distance field filter.
      foreach ($view->display as $did => $display) {
        if (isset($display->display_options['filters']) && !empty($display->display_options['filters'])) {
          foreach ($display->display_options['filters'] as $fid => $filter) {
            if ($filter['table'] == 'location' && $filter['field'] == 'distance') {
              // Set the origin (new option) from the type (old option).
              $origin = '';
              switch ($filter['type']) {
                case 'latlon':
                  $origin = 'static';
                  break;

                case 'postal':
                case 'postal_default':
                case 'latlon_gmap':
                  $origin = $filter['type'];
                  break;
              }
              if ($origin) {
                $view->display[$did]->display_options['filters'][$fid]['origin'] = $origin;
                unset($view->display[$did]->display_options['filters'][$fid]['type']);
                $modified = TRUE;
              }
            }
          }
        }
      }
      // Save the view if we changed any options on any displays.
      // Views caches are cleared during save so we don't have to do it.
      if ($modified) {
        $updated_views[] = $view->name;
        $view->save();
      }
    }
  }

  if (!empty($updated_views)) {
    return t(
      "Note: The 'Form type' option for the 'Location: Distance / Proximity' filter in views has been changed and is now the 'Origin' option.  The following views were using that setting and have been updated to use the new setting: %views.",
      array('%views' => implode(', ', $updated_views))
    );
  }
}

/**
 * Updates the ISO code for United Kingdom from 'uk' to 'gb'.
 *
 * In the location table, the zipcodes table, and for any variable names
 * and variable values where it is used.
 */
function location_update_7301() {

  db_update('location')
    ->fields(
      array(
        'country' => 'gb',
      )
    )
    ->condition('country', 'uk')
    ->execute();

  db_update('zipcodes')
    ->fields(
      array(
        'country' => 'gb',
      )
    )
    ->condition('country', 'uk')
    ->execute();

  $uk = variable_get('location_geocode_uk', '');
  $gb = variable_get('location_geocode_gb', '');

  if ($uk) {
    variable_set('location_geocode_gb', $uk);
  }

  $uk = variable_get('location_map_link_uk', '');
  $gb = variable_get('location_map_link_gb', '');

  if ($uk) {
    variable_set('location_map_link_gb', $uk);
  }

  cache_clear_all('*', 'cache_location', TRUE);

  return;
}

/**
 * Adds the Country code to name table "location_country".
 *
 * It is created from the included array country array.
 */
function location_update_7302() {
  // Create the new table.
  drupal_load('module', 'location');
  $schema = location_schema();
  if (!db_table_exists('location_country')) {
    db_create_table('location_country', $schema['location_country']);
  }
  else {
    db_truncate('location_country')->execute();
  }

  $countries = location_get_iso3166_list();
  foreach ($countries as $code => $name) {
    db_insert('location_country')
      ->fields(
        array(
          'code' => $code,
          'name' => $name,
        )
      )
      ->execute();
  }
}