You are here

function weather_data_installation in Weather 7

Helper function for installation and upgrades.

This function inserts data into the weather_icao table.

The data file lists one table entry per line. Fields are separated by tab stops. In order to be better readable by humans, the fields are filled with spaces. Those superfluous space characters are removed before inserting the value in the table. The fields are listed in the file with the following order:

ICAO code 4 characters. Tab stop Latitude 10 characters, positive values for north, negative for south. Tab stop Longitude 11 characters, positive values for east, negative for west. Tab stop Country 35 characters. Tab stop Name Variable length, not padded with spaces.

5 calls to weather_data_installation()
weather_install in ./weather.install
Implement hook_install().
weather_update_7101 in ./weather.install
Implement hook_update_N().
weather_update_7102 in ./weather.install
Implement hook_update_N().
weather_update_7104 in ./weather.install
Implement hook_update_N().
weather_update_7105 in ./weather.install
Implement hook_update_N().

File

./weather.install, line 50
Install, update and uninstall functions for the weather module.

Code

function weather_data_installation() {

  // Delete all entries from the table, if any.
  db_delete('weather_icao')
    ->execute();

  // Prepare the SQL query.
  $query = db_insert('weather_icao')
    ->fields(array(
    'icao',
    'country',
    'name',
    'latitude',
    'longitude',
  ));

  // Read the data file and append values to query.
  $data = fopen(drupal_get_path('module', 'weather') . '/weather_data.txt', 'r');
  while (!feof($data)) {
    $line = fgets($data);
    $fields = explode("\t", $line);

    // If we get to the last line, there won't be more than one field.
    if (isset($fields[1])) {
      $query
        ->values(array(
        'icao' => $fields[0],
        'country' => trim($fields[3]),
        'name' => trim($fields[4]),
        'latitude' => $fields[1],
        'longitude' => $fields[2],
      ));
    }
  }
  fclose($data);
  $query
    ->execute();
}