location.install 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the location module.
  5. */
  6. /**
  7. * Implements of hook_uninstall().
  8. */
  9. function location_uninstall() {
  10. // Delete variables.
  11. variable_del('location_default_country');
  12. variable_del('location_display_location');
  13. variable_del('location_usegmap');
  14. variable_del('location_locpick_macro');
  15. // Delete geocoder settings.
  16. db_delete('variable')
  17. ->condition('name', db_like('location_geocode_') . '%', 'LIKE')
  18. ->execute();
  19. // Delete maxnum settings.
  20. db_delete('variable')
  21. ->condition('name', db_like('location_maxnum_') . '%', 'LIKE')
  22. ->execute();
  23. cache_clear_all('variables', 'cache_bootstrap');
  24. }
  25. /**
  26. * Implements of hook_schema().
  27. */
  28. function location_schema() {
  29. $schema['location'] = array(
  30. 'description' => 'Locational data managed by location.module.',
  31. 'fields' => array(
  32. 'lid' => array(
  33. 'description' => 'Primary Key: Unique location ID.',
  34. 'type' => 'serial',
  35. 'unsigned' => TRUE,
  36. 'not null' => TRUE,
  37. ),
  38. 'name' => array(
  39. 'description' => 'Place Name.',
  40. 'type' => 'varchar',
  41. 'length' => 255,
  42. 'default' => '',
  43. 'not null' => TRUE,
  44. ),
  45. 'street' => array(
  46. 'description' => 'Street address, line 1.',
  47. 'type' => 'varchar',
  48. 'length' => 255,
  49. 'default' => '',
  50. 'not null' => TRUE,
  51. ),
  52. 'additional' => array(
  53. 'description' => 'Street address, line 2.',
  54. 'type' => 'varchar',
  55. 'length' => 255,
  56. 'default' => '',
  57. 'not null' => TRUE,
  58. ),
  59. 'city' => array(
  60. 'description' => 'City.',
  61. 'type' => 'varchar',
  62. 'length' => 255,
  63. 'default' => '',
  64. 'not null' => TRUE,
  65. ),
  66. 'province' => array(
  67. 'description' => 'State / Province code.',
  68. 'type' => 'varchar',
  69. 'length' => 16,
  70. 'default' => '',
  71. 'not null' => TRUE,
  72. ),
  73. 'postal_code' => array(
  74. 'description' => 'Postal / ZIP code.',
  75. 'type' => 'varchar',
  76. 'length' => 16,
  77. 'default' => '',
  78. 'not null' => TRUE,
  79. ),
  80. 'country' => array(
  81. 'description' => 'Two letter ISO country code.',
  82. 'type' => 'char',
  83. 'length' => 2,
  84. 'not null' => TRUE,
  85. 'default' => '',
  86. ),
  87. 'latitude' => array(
  88. 'description' => 'Location latitude (decimal degrees).',
  89. 'type' => 'numeric',
  90. 'precision' => 10,
  91. // @todo Shouldn't these all be 7?
  92. 'scale' => 6,
  93. 'not null' => TRUE,
  94. 'default' => 0.0,
  95. ),
  96. 'longitude' => array(
  97. 'description' => 'Location longitude (decimal degrees).',
  98. 'type' => 'numeric',
  99. 'precision' => 10,
  100. 'scale' => 6,
  101. 'not null' => TRUE,
  102. 'default' => 0.0,
  103. ),
  104. 'source' => array(
  105. 'description' => 'Source of the latitude and longitude data (Geocoder, user entered, invalid, etc.)',
  106. 'type' => 'int',
  107. 'size' => 'tiny',
  108. 'default' => 0,
  109. 'not null' => TRUE,
  110. ),
  111. // @@@ Historical civicrm field that isn't applicable to location, I think..
  112. 'is_primary' => array(
  113. 'description' => 'Is this the primary location of an object? (unused, civicrm legacy field?).',
  114. 'type' => 'int',
  115. 'size' => 'tiny',
  116. 'default' => 0,
  117. 'not null' => TRUE,
  118. ),
  119. ),
  120. 'primary key' => array('lid'),
  121. );
  122. $schema['location_instance'] = array(
  123. 'description' => 'N:M join table to join locations to other tables.',
  124. 'fields' => array(
  125. 'nid' => array(
  126. 'description' => 'Reference to {node}.nid.',
  127. 'type' => 'int',
  128. 'unsigned' => TRUE,
  129. 'not null' => TRUE,
  130. 'default' => 0,
  131. ),
  132. 'vid' => array(
  133. 'description' => 'Reference to {node_revision}.vid.',
  134. 'type' => 'int',
  135. 'unsigned' => TRUE,
  136. 'not null' => TRUE,
  137. 'default' => 0,
  138. ),
  139. 'uid' => array(
  140. 'description' => 'Reference to {users}.uid.',
  141. 'type' => 'int',
  142. 'unsigned' => TRUE,
  143. 'not null' => TRUE,
  144. 'default' => 0,
  145. ),
  146. 'genid' => array(
  147. 'description' => 'Generic reference key.',
  148. 'type' => 'varchar',
  149. 'length' => 255,
  150. 'not null' => TRUE,
  151. 'default' => '',
  152. ),
  153. 'lid' => array(
  154. 'description' => 'Reference to {location}.lid.',
  155. 'type' => 'int',
  156. 'unsigned' => TRUE,
  157. 'not null' => TRUE,
  158. 'default' => 0,
  159. ),
  160. ),
  161. 'indexes' => array(
  162. 'nid' => array('nid'),
  163. 'vid' => array('vid'),
  164. 'uid' => array('uid'),
  165. 'genid' => array('genid'),
  166. 'lid' => array('lid'),
  167. ),
  168. );
  169. $schema['zipcodes'] = array(
  170. 'description' => 'Location.module zipcode database.',
  171. 'fields' => array(
  172. 'zip' => array(
  173. 'description' => 'Postal / ZIP code.',
  174. 'type' => 'varchar',
  175. 'length' => 16,
  176. 'not null' => TRUE,
  177. 'default' => '0',
  178. ),
  179. 'city' => array(
  180. 'description' => 'City.',
  181. 'type' => 'varchar',
  182. 'length' => 30,
  183. 'not null' => TRUE,
  184. 'default' => '',
  185. ),
  186. 'state' => array(
  187. 'description' => 'Province / State.',
  188. 'type' => 'varchar',
  189. 'length' => 30,
  190. 'not null' => TRUE,
  191. 'default' => '',
  192. ),
  193. 'latitude' => array(
  194. 'description' => 'Location latitude (decimal degrees).',
  195. 'type' => 'numeric',
  196. 'precision' => 10,
  197. 'scale' => 6,
  198. 'not null' => TRUE,
  199. 'default' => 0.0,
  200. ),
  201. 'longitude' => array(
  202. 'description' => 'Location longitude (decimal degrees).',
  203. 'type' => 'numeric',
  204. 'precision' => 10,
  205. 'scale' => 6,
  206. 'not null' => TRUE,
  207. 'default' => 0.0,
  208. ),
  209. // @@@ Not used, an artifact of the original data dump format.
  210. 'timezone' => array(
  211. 'description' => 'Timezone (unused).',
  212. 'type' => 'int',
  213. 'size' => 'tiny',
  214. 'not null' => TRUE,
  215. 'default' => 0,
  216. ),
  217. // @@@ Not used, an artifact of the original data dump format.
  218. 'dst' => array(
  219. 'description' => 'Daylight Savings Time (unused).',
  220. 'type' => 'int',
  221. 'size' => 'tiny',
  222. 'not null' => TRUE,
  223. 'default' => 0,
  224. ),
  225. 'country' => array(
  226. 'description' => 'Two letter ISO country code.',
  227. 'type' => 'char',
  228. 'length' => 2,
  229. 'not null' => TRUE,
  230. 'default' => '',
  231. ),
  232. ),
  233. // @@@ This pk is invalid, see issue queue.
  234. // 'primary key' => array('country', 'zip').
  235. // @@@ These need reworked.
  236. 'indexes' => array(
  237. 'pc' => array('country', 'zip'),
  238. 'zip' => array('zip'),
  239. // @@@ No combined one?
  240. 'latitude' => array('latitude'),
  241. 'longitude' => array('longitude'),
  242. 'country' => array('country'),
  243. ),
  244. );
  245. // Copied from system.module.
  246. $schema['cache_location'] = array(
  247. 'description' => 'Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.',
  248. 'fields' => array(
  249. 'cid' => array(
  250. 'description' => 'Primary Key: Unique cache ID.',
  251. 'type' => 'varchar',
  252. 'length' => 255,
  253. 'not null' => TRUE,
  254. 'default' => '',
  255. ),
  256. 'data' => array(
  257. 'description' => 'A collection of data to cache.',
  258. 'type' => 'blob',
  259. 'not null' => FALSE,
  260. 'size' => 'big',
  261. ),
  262. 'expire' => array(
  263. 'description' => 'A Unix timestamp indicating when the cache entry should expire, or 0 for never.',
  264. 'type' => 'int',
  265. 'not null' => TRUE,
  266. 'default' => 0,
  267. ),
  268. 'created' => array(
  269. 'description' => 'A Unix timestamp indicating when the cache entry was created.',
  270. 'type' => 'int',
  271. 'not null' => TRUE,
  272. 'default' => 0,
  273. ),
  274. 'headers' => array(
  275. 'description' => 'Any custom HTTP headers to be added to cached data.',
  276. 'type' => 'text',
  277. 'not null' => FALSE,
  278. ),
  279. 'serialized' => array(
  280. 'description' => 'A flag to indicate whether content is serialized (1) or not (0).',
  281. 'type' => 'int',
  282. 'size' => 'small',
  283. 'not null' => TRUE,
  284. 'default' => 0
  285. )
  286. ),
  287. 'indexes' => array('expire' => array('expire')),
  288. 'primary key' => array('cid'),
  289. );
  290. // New location country table (needed for sorting).
  291. $schema['location_country'] = array(
  292. 'description' => 'Country data managed by location.module.',
  293. 'fields' => array(
  294. 'code' => array(
  295. 'description' => 'Primary Key: Two letter ISO Country Code',
  296. 'type' => 'char',
  297. 'length' => 2,
  298. 'not null' => TRUE,
  299. ),
  300. 'name' => array(
  301. 'description' => 'Full Country Name ',
  302. 'type' => 'varchar',
  303. 'length' => 255,
  304. 'default' => '',
  305. 'not null' => TRUE,
  306. ),
  307. ),
  308. 'primary key' => array('code'),
  309. );
  310. return $schema;
  311. }
  312. /**
  313. * Legacy update 1.
  314. *
  315. * Convert tables to utf8.
  316. */
  317. function location_update_1() {
  318. }
  319. /**
  320. * Legacy update 2.
  321. *
  322. * Fix a bug with the "us" entry in the "location_configured_countries" var.
  323. */
  324. function location_update_2() {
  325. $configured_countries = variable_get('location_configured_countries', array());
  326. if ($configured_countries['us']) {
  327. $configured_countries['us'] = 'us';
  328. variable_set('location_configured_countries', $configured_countries);
  329. }
  330. return array();
  331. }
  332. /**
  333. * Legacy update 3.
  334. *
  335. * Allow for postgresql support by renaming the oid column, which is a reserved
  336. * name on postgresql.
  337. */
  338. function location_update_3() {
  339. db_change_field(
  340. 'location',
  341. 'oid',
  342. 'eid',
  343. array(
  344. 'type' => 'int',
  345. 'unsigned' => TRUE,
  346. 'not null' => TRUE,
  347. 'default' => 0,
  348. )
  349. );
  350. if (module_exists('views')) {
  351. views_invalidate_cache();
  352. }
  353. return t(
  354. "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."
  355. );
  356. }
  357. /***************************************************************
  358. * PostgreSQL must be supported in all updates after this comment
  359. ***************************************************************/
  360. /**
  361. * Legacy update 4.
  362. *
  363. * Add "lid" as the new location key.
  364. */
  365. function location_update_4() {
  366. db_add_field(
  367. 'location',
  368. 'lid',
  369. array(
  370. 'type' => 'serial',
  371. 'unsigned' => TRUE,
  372. 'not null' => TRUE,
  373. 'description' => 'Primary Key: Unique location ID.',
  374. )
  375. );
  376. $result = db_query("SELECT eid, type FROM {location}");
  377. $next_id = 0;
  378. foreach ($result as $row) {
  379. $next_id++;
  380. db_update('location')
  381. ->fields(
  382. array(
  383. 'lid' => $next_id,
  384. )
  385. )
  386. ->condition('eid', $row->eid)
  387. ->condition('type', $row->type)
  388. ->execute();
  389. }
  390. db_drop_primary_key('location');
  391. db_add_primary_key('location', array('lid'));
  392. db_insert('sequences')
  393. ->fields(
  394. array(
  395. 'name' => '{location}_lid',
  396. 'id' => $next_id,
  397. )
  398. )
  399. ->execute();
  400. db_add_field(
  401. 'location',
  402. 'is_primary',
  403. array(
  404. 'description' => 'Is this the primary location of an object? (unused, civicrm legacy field?).',
  405. 'type' => 'int',
  406. 'size' => 'tiny',
  407. 'default' => 0,
  408. 'not null' => TRUE,
  409. )
  410. );
  411. db_update('location')
  412. ->fields(
  413. array(
  414. 'is_primary' => 1,
  415. )
  416. )
  417. ->condition('type', 'user')
  418. ->execute();
  419. foreach (node_type_get_types() as $type) {
  420. $new_setting = variable_get('location_' . $type->type, 0) ? 1 : 0;
  421. variable_del('location_' . $type->type);
  422. variable_set('location_maxnum_' . $type->type, $new_setting);
  423. variable_set('location_defaultnum_' . $type->type, $new_setting);
  424. }
  425. }
  426. /**
  427. * Legacy update 5.
  428. *
  429. * Postgresql support that was missing from previous update.
  430. *
  431. * This update is redundant now that we have db api functions
  432. * that are database independant.
  433. */
  434. function location_update_5() {
  435. }
  436. /**
  437. * Legacy update 6.
  438. *
  439. * Use correct country code for Sweeden.
  440. */
  441. function location_update_6() {
  442. db_update('location')
  443. ->fields(
  444. array(
  445. 'country' => 'se',
  446. )
  447. )
  448. ->condition('country', 'sw')
  449. ->execute();
  450. }
  451. /**
  452. * Update 7 (Location 2.x).
  453. *
  454. * Generalize google geocoding so you don't have to enter the api key over and over.
  455. */
  456. function location_update_7() {
  457. $services = array('google');
  458. $general_geocoders_in_use = array();
  459. $result = db_query("SELECT * FROM {variable} WHERE name REGEXP '^location_geocode_[a-z][a-z]$'");
  460. foreach ($result as $row) {
  461. $value_decoded = unserialize($row->value);
  462. if (!in_array($value_decoded, $services)) {
  463. db_update('variable')
  464. ->fields(
  465. array(
  466. 'value' => serialize($value_decoded . '|' . substr($row->name, 17)),
  467. )
  468. )
  469. ->condition('name', $row->name)
  470. ->execute();
  471. }
  472. else {
  473. $general_geocoders_in_use[$value_decoded] = $value_decoded;
  474. }
  475. }
  476. $key = db_query(
  477. "SELECT value FROM {variable} WHERE name REGEXP '^location_geocode_[a-z][a-z]_google_apikey$'"
  478. )->fetchField();
  479. db_delete('variable')
  480. ->where("name REGEXP '^location_geocode_[a-z][a-z]_google_apikey$'")
  481. ->execute();
  482. db_insert('variable')
  483. ->fields(
  484. array(
  485. 'name' => 'location_geocode_google_apikey',
  486. 'value' => $key,
  487. )
  488. )
  489. ->execute();
  490. db_delete('cache')
  491. ->condition('cid', 'variables')
  492. ->execute();
  493. variable_set('location_general_geocoders_in_use', $general_geocoders_in_use);
  494. }
  495. /**
  496. * Location 3.x update 1.
  497. *
  498. * Add location specific cache table.
  499. */
  500. function location_update_5300() {
  501. $schema['cache_location'] = array(
  502. 'description' => 'Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.',
  503. 'fields' => array(
  504. 'cid' => array(
  505. 'description' => 'Primary Key: Unique cache ID.',
  506. 'type' => 'varchar',
  507. 'length' => 255,
  508. 'not null' => TRUE,
  509. 'default' => '',
  510. ),
  511. 'data' => array(
  512. 'description' => 'A collection of data to cache.',
  513. 'type' => 'blob',
  514. 'not null' => FALSE,
  515. 'size' => 'big',
  516. ),
  517. 'expire' => array(
  518. 'description' => 'A Unix timestamp indicating when the cache entry should expire, or 0 for never.',
  519. 'type' => 'int',
  520. 'not null' => TRUE,
  521. 'default' => 0,
  522. ),
  523. 'created' => array(
  524. 'description' => 'A Unix timestamp indicating when the cache entry was created.',
  525. 'type' => 'int',
  526. 'not null' => TRUE,
  527. 'default' => 0,
  528. ),
  529. 'headers' => array(
  530. 'description' => 'Any custom HTTP headers to be added to cached data.',
  531. 'type' => 'text',
  532. 'not null' => FALSE,
  533. ),
  534. ),
  535. 'indexes' => array('expire' => array('expire')),
  536. 'primary key' => array('cid'),
  537. );
  538. db_create_table('cache_location', $schema['cache_location']);
  539. }
  540. /**
  541. * Location 3.x update 2.
  542. *
  543. * Normalize the location table.
  544. * This allows:
  545. * - Making the loading and saving code cleaner.
  546. * - Fixing a longstanding bug with revisions.
  547. * - Having the same location on multiple nodes/users/both.
  548. * - Garbage collecting unused locations periodically.
  549. * - Having full support for deletions.
  550. * - Full revisions support.
  551. * Note that the location_instance table does NOT have a primary key.
  552. * This is on purpose. It's a N:M join table.
  553. */
  554. function location_update_5301() {
  555. $schema['location_instance'] = array(
  556. 'description' => 'N:M join table to join locations to other tables.',
  557. 'fields' => array(
  558. 'nid' => array(
  559. 'description' => 'Reference to {node}.nid.',
  560. 'type' => 'int',
  561. 'unsigned' => TRUE,
  562. 'not null' => TRUE,
  563. 'default' => 0,
  564. ),
  565. 'vid' => array(
  566. 'description' => 'Reference to {node_revision}.vid.',
  567. 'type' => 'int',
  568. 'unsigned' => TRUE,
  569. 'not null' => TRUE,
  570. 'default' => 0,
  571. ),
  572. 'uid' => array(
  573. 'description' => 'Reference to {users}.uid.',
  574. 'type' => 'int',
  575. 'unsigned' => TRUE,
  576. 'not null' => TRUE,
  577. 'default' => 0,
  578. ),
  579. 'genid' => array(
  580. 'description' => 'Generic reference key.',
  581. 'type' => 'varchar',
  582. 'length' => 255,
  583. 'not null' => TRUE,
  584. 'default' => '',
  585. ),
  586. 'lid' => array(
  587. 'description' => 'Reference to {location}.lid.',
  588. 'type' => 'int',
  589. 'unsigned' => TRUE,
  590. 'not null' => TRUE,
  591. 'default' => 0,
  592. ),
  593. ),
  594. 'indexes' => array(
  595. 'nid' => array('nid'),
  596. 'vid' => array('vid'),
  597. 'uid' => array('uid'),
  598. 'genid' => array('genid'),
  599. 'lid' => array('lid'),
  600. ),
  601. );
  602. db_create_table('location_instance', $schema['location_instance']);
  603. // Synthesise node location data based on what we have.
  604. // Storage of locations was previously stored against node revision, BUT the
  605. // data was not properly duplicated by revision (i.e. only the latest revision
  606. // carried the data.)
  607. // Joining like this allows us to backfill all the old revisions with the current
  608. // data, which is not nice but better than having no data at all when viewing
  609. // old revisions.
  610. $query = db_select('node_revision', 'nr');
  611. $query->join('node_revision', 'nr2', 'nr.nid = nr2.nid');
  612. $query->join('location', 'l', "nr2.vid = l.eid AND l.type = 'node'");
  613. $query->addField('nr', 'nid');
  614. $query->addField('nr', 'vid');
  615. $query->addField('l', 'lid');
  616. db_insert('location_instance')
  617. ->fields(array('nid', 'vid', 'lid'))
  618. ->from($query)
  619. ->execute();
  620. // Users is much simpler.
  621. $query = db_select('location', 'l');
  622. $query->addField('l', 'eid');
  623. $query->addField('l', 'lid');
  624. $query->condition('type', 'user');
  625. db_insert('location_instance')
  626. ->fields(array('uid', 'lid'))
  627. ->from($query)
  628. ->execute();
  629. // Aug 18 2008:
  630. // Save everything else in genid.
  631. $query = db_select('location', 'l');
  632. $query->addExpression("CONCAT(l.type, ':', l.eid)");
  633. $query->addField('l', 'lid');
  634. $query->condition('type', 'user', '<>');
  635. $query->condition('type', 'node', '<>');
  636. db_insert('location_instance')
  637. ->fields(array('genid', 'lid'))
  638. ->from($query)
  639. ->execute();
  640. // Remove now unused columns.
  641. db_drop_field('location', 'type');
  642. db_drop_field('location', 'eid');
  643. // General cleanup.
  644. // Removed in favor of permission check.
  645. variable_del('location_user');
  646. // Variable consolidation (as part of the element based system)
  647. // We're doing this "raw" so we can be sure we got everything moved over,
  648. // INCLUDING content types that were deleted in the past.
  649. // This will let us do better cleanup sometime in the future.
  650. $data = array();
  651. $todelete = array();
  652. foreach (array(
  653. 'name',
  654. 'street',
  655. 'additional',
  656. 'city',
  657. 'province',
  658. 'postal_code',
  659. 'country',
  660. 'phone',
  661. 'fax',
  662. ) as $field) {
  663. $result = db_query("SELECT name, value FROM {variable} WHERE name > :name", array(':name' => "location_$field%"));
  664. foreach ($result as $row) {
  665. $data[substr($row->name, strlen($field) + 10)][$field] = (string) (int) unserialize($row->value);
  666. $todelete[] = $row->name;
  667. }
  668. }
  669. foreach ($data as $type => $value) {
  670. // We aren't going to trust that hook_locationapi is operational.
  671. // So, stick with some conservative defaults.
  672. $value = array_merge(
  673. array(
  674. 'name' => '1',
  675. 'street' => '1',
  676. // Additional is left out of this list intentionally.
  677. 'city' => '0',
  678. 'province' => '0',
  679. 'postal_code' => '0',
  680. 'country' => '1',
  681. ),
  682. $value
  683. );
  684. if (!isset($value['additional'])) {
  685. // Initialize additional to match street.
  686. $value['additional'] = $value['street'];
  687. }
  688. variable_set('location_fields_' . $type, $value);
  689. }
  690. foreach ($todelete as $key) {
  691. variable_del($key);
  692. }
  693. // This update was retrofitted on Aug 18, 2008. Set a flag for use by
  694. // the next update in order to handle the case where someone has already
  695. // updated to EXACTLY schema revision 5301 before the retrofit took effect.
  696. // People who migrated past this point before that date may have the following
  697. // inconsistencies:
  698. // A) location_{field}_{type} variables were not collected for content types
  699. // that had been deleted in the past.
  700. // B) Any locations with the 'type' field set to something other than 'node'
  701. // or 'user' did not get entries in {location_instance}.
  702. variable_set('location_update_5301_retrofit', TRUE);
  703. }
  704. /**
  705. * Location 3.x update 3.
  706. *
  707. * Add genid to {location_instance}.
  708. */
  709. function location_update_5302() {
  710. // OK, here's the deal. I retrofitted 5301 on Aug 18 2008 to integrate the genid.
  711. // This was needed to fix the pre location 3.x todo item regarding keeping non
  712. // user, non node data intact. People doing an update after Aug 18 will already
  713. // have the genid column in place, so it can be safely skipped.
  714. if (!variable_get('location_update_5301_retrofit', FALSE)) {
  715. db_add_field(
  716. 'location_instance',
  717. 'genid',
  718. array(
  719. 'description' => 'Generic reference key.',
  720. 'type' => 'varchar',
  721. 'length' => 255,
  722. 'not null' => TRUE,
  723. 'default' => '',
  724. )
  725. );
  726. db_add_index('location_instance', 'genid', array('genid'));
  727. }
  728. }
  729. /**
  730. * Location 3.x update 4.
  731. *
  732. * Shuffle more variables around.
  733. */
  734. function location_update_5303() {
  735. $types = array();
  736. $result = db_query("SELECT name FROM {variable} WHERE name LIKE 'location_display_teaser_%'");
  737. foreach ($result as $row) {
  738. $type = substr($row->name, 24);
  739. $types[$type]['teaser'] = variable_get('location_display_teaser_' . $type, TRUE);
  740. $types[$type]['full'] = variable_get('location_display_full_' . $type, TRUE);
  741. $types[$type]['weight'] = variable_get('location_display_weight_' . $type, 0);
  742. // @@@ Combine location_suppress_country and country require settings to set this up?
  743. $types[$type]['hide'] = array();
  744. }
  745. foreach ($types as $type => $value) {
  746. variable_set("location_display_$type", $value);
  747. variable_del("location_display_teaser_$type");
  748. variable_del("location_display_full_$type");
  749. variable_del("location_display_weight_$type");
  750. }
  751. }
  752. // @@@ Does 5303 need rerun in some circumstances?
  753. /**
  754. * Drupal 6 location 3.x update.
  755. */
  756. function location_update_6301() {
  757. $message = '';
  758. // Update cache table.
  759. db_drop_table('cache_location');
  760. $schema['cache_location'] = array(
  761. 'description' => 'Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.',
  762. 'fields' => array(
  763. 'cid' => array(
  764. 'description' => 'Primary Key: Unique cache ID.',
  765. 'type' => 'varchar',
  766. 'length' => 255,
  767. 'not null' => TRUE,
  768. 'default' => '',
  769. ),
  770. 'data' => array(
  771. 'description' => 'A collection of data to cache.',
  772. 'type' => 'blob',
  773. 'not null' => FALSE,
  774. 'size' => 'big',
  775. ),
  776. 'expire' => array(
  777. 'description' => 'A Unix timestamp indicating when the cache entry should expire, or 0 for never.',
  778. 'type' => 'int',
  779. 'not null' => TRUE,
  780. 'default' => 0,
  781. ),
  782. 'created' => array(
  783. 'description' => 'A Unix timestamp indicating when the cache entry was created.',
  784. 'type' => 'int',
  785. 'not null' => TRUE,
  786. 'default' => 0,
  787. ),
  788. 'headers' => array(
  789. 'description' => 'Any custom HTTP headers to be added to cached data.',
  790. 'type' => 'text',
  791. 'not null' => FALSE
  792. ),
  793. 'serialized' => array(
  794. 'description' => 'A flag to indicate whether content is serialized (1) or not (0).',
  795. 'type' => 'int',
  796. 'size' => 'small',
  797. 'not null' => TRUE,
  798. 'default' => 0,
  799. )
  800. ),
  801. 'indexes' => array('expire' => array('expire')),
  802. 'primary key' => array('cid'),
  803. );
  804. db_create_table('cache_location', $schema['cache_location']);
  805. // LID 0 causes all sorts of issues, and will break our update routine
  806. // unless we handle it beforehand.
  807. // Since we're so nice, we're gonna renumber it for the user.
  808. $has_rows = (bool) db_query_range('SELECT 1 FROM {location} WHERE lid = 0', 0, 1)->fetchField();
  809. if ($has_rows) {
  810. $lid = 1 + db_query('SELECT MAX(lid) FROM {location}')->fetchField();
  811. $message = t(
  812. '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.',
  813. array('%lid' => $lid)
  814. );
  815. db_update('location')
  816. ->fields(
  817. array(
  818. 'lid' => $lid,
  819. )
  820. )
  821. ->condition('lid', 0)
  822. ->execute();
  823. db_update('location_instance')
  824. ->fields(
  825. array(
  826. 'lid' => $lid,
  827. )
  828. )
  829. ->condition('lid', 0)
  830. ->execute();
  831. }
  832. // Field changes.
  833. // {location}.lid -- Becomes a serial.
  834. db_drop_primary_key('location');
  835. db_change_field(
  836. 'location',
  837. 'lid',
  838. 'lid',
  839. array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
  840. array('primary key' => array('lid'))
  841. );
  842. // The rest of the changes to this table were moved to update 6302 due to a bug.
  843. // {location_instance}
  844. // Fix oddly named indexes -- Was using the postgresql method for both.
  845. db_drop_index('location_instance', '{location_instance}_nid_idx');
  846. db_drop_index('location_instance', '{location_instance}_vid_idx');
  847. db_drop_index('location_instance', '{location_instance}_uid_idx');
  848. db_drop_index('location_instance', '{location_instance}_genid_idx');
  849. db_drop_index('location_instance', '{location_instance}_lid_idx');
  850. db_drop_index('location_instance', 'nid');
  851. db_drop_index('location_instance', 'vid');
  852. db_drop_index('location_instance', 'uid');
  853. db_drop_index('location_instance', 'genid');
  854. db_drop_index('location_instance', 'lid');
  855. // Fill in nulls.
  856. db_update('location_instance')
  857. ->fields(
  858. array(
  859. 'nid' => 0,
  860. )
  861. )
  862. ->isNull('nid')
  863. ->execute();
  864. db_update('location_instance')
  865. ->fields(
  866. array(
  867. 'vid' => 0,
  868. )
  869. )
  870. ->isNull('vid')
  871. ->execute();
  872. db_update('location_instance')
  873. ->fields(
  874. array(
  875. 'uid' => 0,
  876. )
  877. )
  878. ->isNull('uid')
  879. ->execute();
  880. db_update('location_instance')
  881. ->fields(
  882. array(
  883. 'genid' => 0,
  884. )
  885. )
  886. ->isNull('genid')
  887. ->execute();
  888. // {location_instance}.nid
  889. db_change_field(
  890. 'location_instance',
  891. 'nid',
  892. 'nid',
  893. array(
  894. 'type' => 'int',
  895. 'unsigned' => TRUE,
  896. 'not null' => TRUE,
  897. 'default' => 0,
  898. )
  899. );
  900. // {location_instance}.vid
  901. db_change_field(
  902. 'location_instance',
  903. 'vid',
  904. 'vid',
  905. array(
  906. 'type' => 'int',
  907. 'unsigned' => TRUE,
  908. 'not null' => TRUE,
  909. 'default' => 0,
  910. )
  911. );
  912. // {location_instance}.uid
  913. db_change_field(
  914. 'location_instance',
  915. 'uid',
  916. 'uid',
  917. array(
  918. 'type' => 'int',
  919. 'unsigned' => TRUE,
  920. 'not null' => TRUE,
  921. 'default' => 0,
  922. )
  923. );
  924. // {location_instance}.genid
  925. db_change_field(
  926. 'location_instance',
  927. 'genid',
  928. 'genid',
  929. array(
  930. 'type' => 'varchar',
  931. 'length' => 255,
  932. 'not null' => TRUE,
  933. 'default' => '',
  934. )
  935. );
  936. // {location_instance}.lid
  937. db_change_field(
  938. 'location_instance',
  939. 'lid',
  940. 'lid',
  941. array(
  942. 'type' => 'int',
  943. 'unsigned' => TRUE,
  944. 'not null' => TRUE,
  945. 'default' => 0,
  946. )
  947. );
  948. // Readd indexes.
  949. db_add_index('location_instance', 'nid', array('nid'));
  950. db_add_index('location_instance', 'vid', array('vid'));
  951. db_add_index('location_instance', 'uid', array('uid'));
  952. db_add_index('location_instance', 'genid', array('genid'));
  953. db_add_index('location_instance', 'lid', array('lid'));
  954. // {zipcodes}
  955. // Drop primary key.
  956. db_drop_primary_key('zipcodes');
  957. if ($message) {
  958. $message .= '<br /><br />';
  959. }
  960. return $message . t(
  961. '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.'
  962. );
  963. }
  964. /**
  965. * Drupal 6 location 3.x update, part 2.
  966. */
  967. function location_update_6302() {
  968. // OK, here's the update to fix the previous update which had a few problems
  969. // when upgrading from pre-rc 6.x versions.
  970. // The "mismatch between mysql and postgresql" was actually applicable to
  971. // 6.x-3.0 pre-rc1 as well, but I didn't notice because I accidentally added
  972. // the not null when reformatting the schema.
  973. db_update('location')
  974. ->fields(
  975. array(
  976. 'is_primary' => 0,
  977. )
  978. )
  979. ->isNull('is_primary')
  980. ->execute();
  981. db_change_field(
  982. 'location',
  983. 'is_primary',
  984. 'is_primary',
  985. array(
  986. 'type' => 'int',
  987. 'size' => 'tiny',
  988. 'default' => 0,
  989. 'not null' => TRUE,
  990. )
  991. );
  992. // Fix zipcode mismatches caused by the same problem.
  993. // There shouldn't be any rows like this, but it doesn't hurt to be sure.
  994. db_update('zipcodes')
  995. ->fields(
  996. array(
  997. 'zip' => 0,
  998. )
  999. )
  1000. ->isNull('zip')
  1001. ->execute();
  1002. // Set not null.
  1003. db_change_field(
  1004. 'zipcodes',
  1005. 'zip',
  1006. 'zip',
  1007. array(
  1008. 'type' => 'varchar',
  1009. 'length' => 16,
  1010. 'not null' => TRUE,
  1011. 'default' => '0',
  1012. )
  1013. );
  1014. // Prepare latitude and longitude for the same.
  1015. db_update('zipcodes')
  1016. ->fields(
  1017. array(
  1018. 'latitude' => 0.0,
  1019. )
  1020. )
  1021. ->isNull('latitude')
  1022. ->execute();
  1023. db_update('zipcodes')
  1024. ->fields(
  1025. array(
  1026. 'longitude' => 0.0,
  1027. )
  1028. )
  1029. ->isNull('longitude')
  1030. ->execute();
  1031. // Set not null.
  1032. db_change_field(
  1033. 'zipcodes',
  1034. 'latitude',
  1035. 'latitude',
  1036. array(
  1037. 'type' => 'numeric',
  1038. 'not null' => TRUE,
  1039. 'default' => 0,
  1040. 'precision' => 10,
  1041. 'scale' => 6,
  1042. )
  1043. );
  1044. db_change_field(
  1045. 'zipcodes',
  1046. 'longitude',
  1047. 'longitude',
  1048. array(
  1049. 'type' => 'numeric',
  1050. 'not null' => TRUE,
  1051. 'default' => 0,
  1052. 'precision' => 10,
  1053. 'scale' => 6,
  1054. )
  1055. );
  1056. // Prepare country.
  1057. update_sql("UPDATE {zipcodes} SET country = '' WHERE country IS NULL");
  1058. db_update('zipcodes')
  1059. ->fields(
  1060. array(
  1061. 'country' => '',
  1062. )
  1063. )
  1064. ->isNull('country')
  1065. ->execute();
  1066. // Set not null.
  1067. db_change_field(
  1068. 'zipcodes',
  1069. 'country',
  1070. 'country',
  1071. array(
  1072. 'type' => 'char',
  1073. 'length' => 2,
  1074. 'not null' => TRUE,
  1075. 'default' => '',
  1076. )
  1077. );
  1078. // Fix up possible {location} problems from previous update that could be caused if you had NULLed fields.
  1079. // Set defaults.
  1080. $fields = array(
  1081. 'name' => '',
  1082. 'street' => '',
  1083. 'additional' => '',
  1084. 'city' => '',
  1085. 'province' => '',
  1086. 'postal_code' => '',
  1087. 'latitude' => 0.0,
  1088. 'longitude' => 0.0,
  1089. 'source' => 0,
  1090. );
  1091. foreach ($fields as $field => $value) {
  1092. db_update('location')
  1093. ->fields(
  1094. array(
  1095. $field => $value,
  1096. )
  1097. )
  1098. ->isNull($field)
  1099. ->execute();
  1100. }
  1101. // {location}.name -- NOT NULL.
  1102. db_change_field(
  1103. 'location',
  1104. 'name',
  1105. 'name',
  1106. array(
  1107. 'type' => 'varchar',
  1108. 'length' => 255,
  1109. 'not null' => TRUE,
  1110. 'default' => '',
  1111. )
  1112. );
  1113. // {location}.street -- NOT NULL.
  1114. db_change_field(
  1115. 'location',
  1116. 'street',
  1117. 'street',
  1118. array(
  1119. 'type' => 'varchar',
  1120. 'length' => 255,
  1121. 'not null' => TRUE,
  1122. 'default' => '',
  1123. )
  1124. );
  1125. // {location}.additional -- NOT NULL.
  1126. db_change_field(
  1127. 'location',
  1128. 'additional',
  1129. 'additional',
  1130. array(
  1131. 'type' => 'varchar',
  1132. 'length' => 255,
  1133. 'not null' => TRUE,
  1134. 'default' => '',
  1135. )
  1136. );
  1137. // {location}.city -- NOT NULL.
  1138. db_change_field(
  1139. 'location',
  1140. 'city',
  1141. 'city',
  1142. array(
  1143. 'type' => 'varchar',
  1144. 'length' => 255,
  1145. 'not null' => TRUE,
  1146. 'default' => '',
  1147. )
  1148. );
  1149. // {location}.province -- NOT NULL.
  1150. db_change_field(
  1151. 'location',
  1152. 'province',
  1153. 'province',
  1154. array(
  1155. 'type' => 'varchar',
  1156. 'length' => 16,
  1157. 'not null' => TRUE,
  1158. 'default' => '',
  1159. )
  1160. );
  1161. // {location}.postal_code -- NOT NULL.
  1162. db_change_field(
  1163. 'location',
  1164. 'postal_code',
  1165. 'postal_code',
  1166. array(
  1167. 'type' => 'varchar',
  1168. 'length' => 16,
  1169. 'not null' => TRUE,
  1170. 'default' => '',
  1171. )
  1172. );
  1173. // {location}.country -- NOT NULL.
  1174. db_change_field(
  1175. 'location',
  1176. 'country',
  1177. 'country',
  1178. array(
  1179. 'type' => 'char',
  1180. 'length' => 2,
  1181. 'not null' => TRUE,
  1182. 'default' => '',
  1183. )
  1184. );
  1185. // {location}.latitude.
  1186. db_change_field(
  1187. 'location',
  1188. 'latitude',
  1189. 'latitude',
  1190. array(
  1191. 'type' => 'numeric',
  1192. 'precision' => 10,
  1193. 'scale' => 6,
  1194. 'not null' => TRUE,
  1195. 'default' => 0.0,
  1196. )
  1197. );
  1198. // {location}.longitude.
  1199. db_change_field(
  1200. 'location',
  1201. 'longitude',
  1202. 'longitude',
  1203. array(
  1204. 'type' => 'numeric',
  1205. 'precision' => 10,
  1206. 'scale' => 6,
  1207. 'not null' => TRUE,
  1208. 'default' => 0.0,
  1209. )
  1210. );
  1211. // {location}.source.
  1212. db_change_field(
  1213. 'location',
  1214. 'source',
  1215. 'source',
  1216. array(
  1217. 'type' => 'int',
  1218. 'size' => 'tiny',
  1219. 'not null' => TRUE,
  1220. 'default' => 0,
  1221. )
  1222. );
  1223. }
  1224. /**
  1225. * Drupal 6 location 3.x update, part 3.
  1226. */
  1227. function location_update_6303() {
  1228. if (!variable_get('location_update_5304_done', FALSE)) {
  1229. // Do the same updates as 5304.
  1230. // Delete unused variables.
  1231. variable_del('location_configured_countries');
  1232. variable_del('location_garbagecollect');
  1233. // Update province code for Italy/Forlì-Cesena.
  1234. db_update('location')
  1235. ->fields(
  1236. array(
  1237. 'province' => 'FC',
  1238. )
  1239. )
  1240. ->condition('country', 'it')
  1241. ->condition('province', 'FO')
  1242. ->execute();
  1243. // Update province code for Italy/Pesaro e Urbino.
  1244. db_update('location')
  1245. ->fields(
  1246. array(
  1247. 'province' => 'PU',
  1248. )
  1249. )
  1250. ->condition('country', 'it')
  1251. ->condition('province', 'PS')
  1252. ->execute();
  1253. // Do one final garbage collection by hand.
  1254. $query = db_select('location_instance', 'li')
  1255. ->addField('li', 'lid');
  1256. db_delete('location')
  1257. ->condition('lid', $query, 'NOT IN')
  1258. ->execute();
  1259. // Garbage collect {location_phone} by hand.
  1260. if (db_table_exists('location_phone')) {
  1261. $query = db_select('location', 'l')
  1262. ->addField('l', 'lid');
  1263. db_delete('location_phone')
  1264. ->condition('lid', $query, 'NOT IN')
  1265. ->execute();
  1266. }
  1267. // Garbage collect {location_fax} by hand.
  1268. if (db_table_exists('location_fax')) {
  1269. $query = db_select('location', 'l')
  1270. ->addField('l', 'lid');
  1271. db_delete('location_fax')
  1272. ->condition('lid', $query, 'NOT IN')
  1273. ->execute();
  1274. }
  1275. variable_del('location_update_5304_done');
  1276. }
  1277. }
  1278. /**
  1279. * Upgrade all of the settings variables to the new unified system.
  1280. */
  1281. function location_update_6304() {
  1282. // Skip this update if it was already done on the 5.x side.
  1283. if (variable_get('location_update_5305_done', FALSE)) {
  1284. variable_del('location_update_5305_done');
  1285. return array();
  1286. }
  1287. $variables = array();
  1288. $todelete = array();
  1289. $base = array(
  1290. 'multiple' => array(
  1291. 'min' => 0,
  1292. 'max' => 0,
  1293. 'add' => 3,
  1294. ),
  1295. 'form' => array(
  1296. 'weight' => 0,
  1297. 'collapsible' => TRUE,
  1298. 'collapsed' => TRUE,
  1299. 'fields' => array(),
  1300. ),
  1301. 'display' => array(
  1302. 'weight' => 0,
  1303. 'hide' => array(),
  1304. ),
  1305. );
  1306. // Pull in user settings.
  1307. $variables['location_settings_user'] = $base;
  1308. $tmp = & $variables['location_settings_user'];
  1309. // Users previously could not have multiple locations, initialize with those
  1310. // settings.
  1311. $tmp['multiple'] = array(
  1312. 'min' => 0,
  1313. 'max' => 1,
  1314. 'add' => 3,
  1315. );
  1316. $tmp['form']['weight'] = variable_get('location_user_weight', 9);
  1317. $tmp['form']['collapsible'] = variable_get('location_user_collapsible', TRUE);
  1318. $tmp['form']['collapased'] = variable_get('location_user_collapsed', TRUE);
  1319. $tmp['form']['fields'] = variable_get('location_user_fields', array());
  1320. // Pull in node settings.
  1321. $result = db_query("SELECT name FROM {variable} WHERE name LIKE 'location_maxnum_%'");
  1322. foreach ($result as $row) {
  1323. $type = substr($row->name, 16);
  1324. $todelete[] = $type;
  1325. $variables["location_settings_node_$type"] = $base;
  1326. $tmp = & $variables["location_settings_node_$type"];
  1327. // Old behavior was to have the first one be required.
  1328. $tmp['multiple']['min'] = 1;
  1329. $tmp['multiple']['max'] = variable_get("location_maxnum_$type", 0);
  1330. $tmp['multiple']['add'] = variable_get("location_defaultnum_$type", 3);
  1331. $tmp['form']['weight'] = variable_get("location_weight_$type", 9);
  1332. $tmp['form']['collapsible'] = variable_get("location_collapsible_$type", TRUE);
  1333. $tmp['form']['collapsed'] = variable_get("location_collapsed_$type", TRUE);
  1334. $tmp['form']['fields'] = variable_get("location_fields_$type", array());
  1335. $tmp['rss']['mode'] = variable_get("location_rss_$type", 'simple');
  1336. $tmp['display'] = variable_get(
  1337. "location_display_$type",
  1338. array(
  1339. 'teaser' => TRUE,
  1340. 'full' => TRUE,
  1341. 'weight' => 0,
  1342. 'hide' => array(),
  1343. )
  1344. );
  1345. }
  1346. foreach ($variables as $name => $value) {
  1347. variable_set($name, $value);
  1348. }
  1349. // Delete old node variables.
  1350. foreach ($todelete as $key) {
  1351. variable_del("location_weight_$key");
  1352. variable_del("location_collapsible_$key");
  1353. variable_del("location_collapsed_$key");
  1354. variable_del("location_fields_$key");
  1355. variable_del("location_rss_$key");
  1356. variable_del("location_display_$key");
  1357. }
  1358. // Delete old user variables.
  1359. variable_del('location_user_weight');
  1360. variable_del('location_user_collapsible');
  1361. variable_del('location_user_collapsed');
  1362. variable_del('location_user_fields');
  1363. }
  1364. /**
  1365. * Disabled due to some typos, moved to 6306.
  1366. */
  1367. function location_update_6305() {
  1368. return array();
  1369. }
  1370. /**
  1371. * Add per-location-field weights and defaults.
  1372. */
  1373. function location_update_6306() {
  1374. // Skip this update if it was already done on the 5.x side.
  1375. if (variable_get('location_update_5306_done', FALSE)) {
  1376. variable_del('location_update_5306_done');
  1377. return array();
  1378. }
  1379. $result = db_query("SELECT name FROM {variable} WHERE name LIKE 'location_settings_%'");
  1380. foreach ($result as $row) {
  1381. $var = variable_get($row->name, array());
  1382. $collect = $var['form']['fields'];
  1383. $var['form']['fields'] = array();
  1384. foreach ($collect as $k => $v) {
  1385. $var['form']['fields'][$k]['collect'] = $v;
  1386. }
  1387. // Country 3 has changed to 4 to make requirements code easier.
  1388. if (isset($var['form']['fields']['country']['collect']) && $var['form']['fields']['country']['collect'] == 3) {
  1389. $var['form']['fields']['country']['collect'] = 4;
  1390. }
  1391. // Weight and default values don't need to get set for now.
  1392. variable_set($row->name, $var);
  1393. }
  1394. }
  1395. /**
  1396. * Tell users about the new location_user module and enable it.
  1397. */
  1398. function location_update_6307() {
  1399. $message = t(
  1400. "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.",
  1401. array('!url' => url('admin/modules'))
  1402. );
  1403. if (module_exists('location')) {
  1404. module_enable(array('location_user'));
  1405. }
  1406. else {
  1407. $message .= '<br />' . t(
  1408. "Note: Refusing to enable location_user.module, as location.module is not currently enabled."
  1409. );
  1410. }
  1411. return $message;
  1412. }
  1413. /**
  1414. * Tell users about the new location_node module and enable it.
  1415. */
  1416. function location_update_6308() {
  1417. $message = t(
  1418. "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.",
  1419. array('!url' => url('admin/modules'))
  1420. );
  1421. if (module_exists('location')) {
  1422. module_enable(array('location_node'));
  1423. }
  1424. else {
  1425. $message .= '<br />' . t(
  1426. "Note: Refusing to enable location_node.module, as location.module is not currently enabled."
  1427. );
  1428. }
  1429. return $message;
  1430. }
  1431. /**
  1432. * There has been a change in the options for the views distance/proximity filter.
  1433. *
  1434. * The old options need to be migrated into the new.
  1435. */
  1436. function location_update_6309() {
  1437. $updated_views = array();
  1438. if (module_exists('views')) {
  1439. $views = views_get_all_views();
  1440. foreach ($views as $view) {
  1441. $modified = FALSE;
  1442. // For each view check all filters of all displays for the distance field filter.
  1443. foreach ($view->display as $did => $display) {
  1444. if (isset($display->display_options['filters']) && !empty($display->display_options['filters'])) {
  1445. foreach ($display->display_options['filters'] as $fid => $filter) {
  1446. if ($filter['table'] == 'location' && $filter['field'] == 'distance') {
  1447. // Set the origin (new option) from the type (old option).
  1448. $origin = '';
  1449. switch ($filter['type']) {
  1450. case 'latlon':
  1451. $origin = 'static';
  1452. break;
  1453. case 'postal':
  1454. case 'postal_default':
  1455. case 'latlon_gmap':
  1456. $origin = $filter['type'];
  1457. break;
  1458. }
  1459. if ($origin) {
  1460. $view->display[$did]->display_options['filters'][$fid]['origin'] = $origin;
  1461. unset($view->display[$did]->display_options['filters'][$fid]['type']);
  1462. $modified = TRUE;
  1463. }
  1464. }
  1465. }
  1466. }
  1467. }
  1468. // Save the view if we changed any options on any displays.
  1469. // Views caches are cleared during save so we don't have to do it.
  1470. if ($modified) {
  1471. $updated_views[] = $view->name;
  1472. $view->save();
  1473. }
  1474. }
  1475. }
  1476. if (!empty($updated_views)) {
  1477. return t(
  1478. "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.",
  1479. array('%views' => implode(', ', $updated_views))
  1480. );
  1481. }
  1482. }
  1483. /**
  1484. * Updates the ISO code for United Kingdom from 'uk' to 'gb'.
  1485. *
  1486. * In the location table, the zipcodes table, and for any variable names
  1487. * and variable values where it is used.
  1488. */
  1489. function location_update_7301() {
  1490. db_update('location')
  1491. ->fields(
  1492. array(
  1493. 'country' => 'gb',
  1494. )
  1495. )
  1496. ->condition('country', 'uk')
  1497. ->execute();
  1498. db_update('zipcodes')
  1499. ->fields(
  1500. array(
  1501. 'country' => 'gb',
  1502. )
  1503. )
  1504. ->condition('country', 'uk')
  1505. ->execute();
  1506. $uk = variable_get('location_geocode_uk', '');
  1507. $gb = variable_get('location_geocode_gb', '');
  1508. if ($uk) {
  1509. variable_set('location_geocode_gb', $uk);
  1510. }
  1511. $uk = variable_get('location_map_link_uk', '');
  1512. $gb = variable_get('location_map_link_gb', '');
  1513. if ($uk) {
  1514. variable_set('location_map_link_gb', $uk);
  1515. }
  1516. cache_clear_all('*', 'cache_location', TRUE);
  1517. return;
  1518. }
  1519. /**
  1520. * Adds the Country code to name table "location_country".
  1521. *
  1522. * It is created from the included array country array.
  1523. */
  1524. function location_update_7302() {
  1525. // Create the new table.
  1526. drupal_load('module', 'location');
  1527. $schema = location_schema();
  1528. if (!db_table_exists('location_country')) {
  1529. db_create_table('location_country', $schema['location_country']);
  1530. }
  1531. else {
  1532. db_truncate('location_country')->execute();
  1533. }
  1534. $countries = location_get_iso3166_list();
  1535. foreach ($countries as $code => $name) {
  1536. db_insert('location_country')
  1537. ->fields(
  1538. array(
  1539. 'code' => $code,
  1540. 'name' => $name,
  1541. )
  1542. )
  1543. ->execute();
  1544. }
  1545. }