Index: continents_api/continents_api.module =================================================================== --- continents_api/continents_api.module (revision 0) +++ continents_api/continents_api.module (revision 0) @@ -0,0 +1,79 @@ + t('Africa'), + 'AS' => t('Asia'), + 'EU' => t('Europe'), + 'NA' => t('North America'), + 'SA' => t('South America'), + 'OC' => t('Oceania'), + 'AN' => t('Antarctica'), + ); +} + +/** + * Function to get a region by iso2 country name + * + * @param $continent_code + * A continent code. E.g. 'EU' for Europe. + * @return + * A list of countries in the given continent. In ISO-2 format. Use the + * countries_api module to convert to other country representations. + * Returns NULL in case of an invalid continent code. + */ +function continents_api_get_countries($continent_code) { + // Validate the continent code. + $valid_continent_codes = array_keys(continents_api_get_continents()); + if (!in_array($continent_code, $valid_continent_codes)) { + return NULL; + } + + // Look up the countries for the given continent. + $result = db_query("SELECT country FROM {continents_api_continents} WHERE continent = '%s'", $continent_code); + $continents = array(); + while ($row = db_fetch_object($result)) { + $countries[] = $row->country; + } + return $countries; +} + +/** + * Function to import regions from CSV file + * TODO: provide arguments for specifying csv files + * TODO: Setup permissions + * + * @param $offset + * Integer value for CSV row offset. + */ +function continents_api_csv_import_continents($offset = 1) { + $handle = fopen(drupal_get_path('module', 'continents_api') . '/data/continents.csv', 'r'); + $index = 1; + while (($row = fgetcsv($handle, 1024, ",")) !== FALSE) { + if ($index > $offset) { + // Create row variables. + $continent = ($row[0]) ? $row[0] : ""; + $country = ($row[1]) ? $row[1] : ""; + db_query("INSERT INTO {continents_api_continents} (continent, country) VALUES('%s', '%s')", $continent, $country); + } + $index++; + } + fclose($handle); + watchdog('continents_api', 'Pre-populated continents data.'); +} Index: continents_api/continents_api.install =================================================================== --- continents_api/continents_api.install (revision 0) +++ continents_api/continents_api.install (revision 0) @@ -0,0 +1,53 @@ + 'Mapping of continent codes to country codes.', + 'fields' => array( + 'continent' => array( + 'description' => 'Continent code.', + 'type' => 'char', + 'length' => 2, + 'not null' => TRUE, + ), + 'country' => array( + 'description' => 'ISO 3166 alpha-2 country code.', + 'type' => 'char', + 'length' => 2, + 'not null' => TRUE, + ), + ), + 'primary key' => array('continent', 'country'), + ); + + return $schema; +} + +/** + * Implementation of hook_install(). + */ +function continents_api_install() { + // Create tables. + drupal_install_schema('continents_api'); + + // Include continents_api module include for initial data import. + require_once(dirname(__FILE__). '/continents_api.module'); + continents_api_csv_import_continents(1); +} + +/** + * Implementation of hook_uninstall(). + */ +function continents_api_uninstall() { + // Remove tables. + drupal_uninstall_schema('continents_api'); +} Index: continents_api/continents_api.info =================================================================== --- continents_api/continents_api.info (revision 0) +++ continents_api/continents_api.info (revision 0) @@ -0,0 +1,5 @@ +; $Id$ +name = "Continents API" +description = "This module provides an API for accessing continent data." +package = "Location" +core = 6.x Index: continents_api/data/continents.csv =================================================================== --- continents_api/data/continents.csv (revision 0) +++ continents_api/data/continents.csv (revision 0) @@ -0,0 +1,247 @@ +continent,country +AS,AF +EU,AX +EU,AL +AF,DZ +OC,AS +EU,AD +AF,AO +NA,AI +AN,AQ +NA,AG +SA,AR +AS,AM +NA,AW +OC,AU +EU,AT +AS,AZ +NA,BS +AS,BH +AS,BD +NA,BB +EU,BY +EU,BE +NA,BZ +AF,BJ +NA,BM +AS,BT +SA,BO +EU,BA +AF,BW +AN,BV +SA,BR +AS,IO +NA,VG +AS,BN +EU,BG +AF,BF +AF,BI +AS,KH +AF,CM +NA,CA +AF,CV +NA,KY +AF,CF +AF,TD +SA,CL +AS,CN +AS,CX +AS,CC +SA,CO +AF,KM +AF,CD +AF,CG +OC,CK +NA,CR +AF,CI +EU,HR +NA,CU +AS,CY +EU,CZ +EU,DK +AF,DJ +NA,DM +NA,DO +SA,EC +AF,EG +NA,SV +AF,GQ +AF,ER +EU,EE +AF,ET +EU,FO +SA,FK +OC,FJ +EU,FI +EU,FR +SA,GF +OC,PF +AN,TF +AF,GA +AF,GM +AS,GE +EU,DE +AF,GH +EU,GI +EU,GR +NA,GL +NA,GD +NA,GP +OC,GU +NA,GT +EU,GG +AF,GN +AF,GW +SA,GY +NA,HT +AN,HM +EU,VA +NA,HN +AS,HK +EU,HU +EU,IS +AS,IN +AS,ID +AS,IR +AS,IQ +EU,IE +EU,IM +AS,IL +EU,IT +NA,JM +AS,JP +EU,JE +AS,JO +AS,KZ +AF,KE +OC,KI +AS,KP +AS,KR +AS,KW +AS,KG +AS,LA +EU,LV +AS,LB +AF,LS +AF,LR +AF,LY +EU,LI +EU,LT +EU,LU +AS,MO +EU,MK +AF,MG +AF,MW +AS,MY +AS,MV +AF,ML +EU,MT +OC,MH +NA,MQ +AF,MR +AF,MU +AF,YT +NA,MX +OC,FM +EU,MD +EU,MC +AS,MN +EU,ME +NA,MS +AF,MA +AF,MZ +AS,MM +AF,NA +OC,NR +AS,NP +NA,AN +EU,NL +OC,NC +OC,NZ +NA,NI +AF,NE +AF,NG +OC,NU +OC,NF +OC,MP +EU,NO +AS,OM +AS,PK +OC,PW +AS,PS +NA,PA +OC,PG +SA,PY +SA,PE +AS,PH +OC,PN +EU,PL +EU,PT +NA,PR +AS,QA +AF,RE +EU,RO +EU,RU +AF,RW +NA,BL +AF,SH +NA,KN +NA,LC +NA,MF +NA,PM +NA,VC +OC,WS +EU,SM +AF,ST +AS,SA +AF,SN +EU,RS +AF,SC +AF,SL +AS,SG +EU,SK +EU,SI +OC,SB +AF,SO +AF,ZA +AN,GS +EU,ES +AS,LK +AF,SD +SA,SR +EU,SJ +AF,SZ +EU,SE +EU,CH +AS,SY +AS,TW +AS,TJ +AF,TZ +AS,TH +AS,TL +AF,TG +OC,TK +OC,TO +NA,TT +AF,TN +AS,TR +AS,TM +NA,TC +OC,TV +AF,UG +EU,UA +AS,AE +EU,GB +NA,US +OC,UM +NA,VI +SA,UY +AS,UZ +OC,VU +SA,VE +AS,VN +OC,WF +AF,EH +AS,YE +AF,ZM +AF,ZW