/[drupal]/contributions/modules/countries_api/contrib/continents_api/continents_api.module
ViewVC logotype

Diff of /contributions/modules/countries_api/contrib/continents_api/continents_api.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Sat Jul 18 09:26:52 2009 UTC revision 1.1.2.1, Sat Jul 18 09:26:52 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    /**
5     * @file
6     * Continents API provides an API for official and up-to-date list of
7     * continents and their countries. Countries are provided in ISO 3166 alpha-2
8     * country codes.
9     * Source: http://en.wikipedia.org/wiki/List_of_countries_by_continent_(data_file)
10     */
11    
12    
13    /**
14     * Get all continents.
15     *
16     * @return
17     *   An array of (continent code, continent name) pairs.
18     */
19    function continents_api_get_continents() {
20      return array(
21        'AF' => t('Africa'),
22        'AS' => t('Asia'),
23        'EU' => t('Europe'),
24        'NA' => t('North America'),
25        'SA' => t('South America'),
26        'OC' => t('Oceania'),
27        'AN' => t('Antarctica'),
28      );
29    }
30    
31    /**
32     * Function to get a region by iso2 country name
33     *
34     * @param $continent_code
35     *   A continent code. E.g. 'EU' for Europe.
36     * @return
37     *   A list of countries in the given continent. In ISO-2 format. Use the
38     *   countries_api module to convert to other country representations.
39     *   Returns NULL in case of an invalid continent code.
40     */
41    function continents_api_get_countries($continent_code) {
42      // Validate the continent code.
43      $valid_continent_codes = array_keys(continents_api_get_continents());
44      if (!in_array($continent_code, $valid_continent_codes)) {
45        return NULL;
46      }
47    
48      // Look up the countries for the given continent.
49      $result = db_query("SELECT country FROM {continents_api_continents} WHERE continent = '%s'", $continent_code);
50      $continents = array();
51      while ($row = db_fetch_object($result)) {
52        $countries[] = $row->country;
53      }
54      return $countries;
55    }
56    
57    /**
58     * Function to import regions from CSV file
59     * TODO: provide arguments for specifying csv files
60     * TODO: Setup permissions
61     *
62     * @param $offset
63     *   Integer value for CSV row offset.
64     */
65    function continents_api_csv_import_continents($offset = 1) {
66      $handle = fopen(drupal_get_path('module', 'continents_api') .'/data/continents.csv', 'r');
67      $index = 1;
68      while (($row = fgetcsv($handle, 1024, ",")) !== FALSE) {
69        if ($index > $offset) {
70          // Create row variables.
71          $continent = ($row[0]) ? $row[0] : "";
72          $country   = ($row[1]) ? $row[1] : "";
73          db_query("INSERT INTO {continents_api_continents} (continent, country) VALUES('%s', '%s')", $continent, $country);
74        }
75        $index++;
76      }
77      fclose($handle);
78      watchdog('continents_api', 'Pre-populated continents data.');
79    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2