You are here

function countries_admin_import_form in Countries 7.2

Same name and namespace in other branches
  1. 7 countries.admin.inc \countries_admin_import_form()

Menu callback to update the database from the CSV file.

1 string reference to 'countries_admin_import_form'
countries_menu in ./countries.module
Implements hook_menu().

File

./countries.admin.inc, line 273
Admin page callbacks for the Countries module.

Code

function countries_admin_import_form($form, &$form_state) {
  $results = countries_csv_updates();
  if (count($results['inserts'])) {
    $form_state['inserts'] = $results['inserts'];
    $form['inserts'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Select the countries to import'),
    );
    foreach ($results['inserts'] as $iso2 => $country) {
      $t_options = array(
        '%name' => $country->name,
        '%continent' => $country->continent,
        '%official_name' => $country->official_name,
        '%iso2' => $country->iso2,
        '%iso3' => $country->iso3,
        '%numcode' => theme('countries_number', array(
          'country' => $country,
        )),
        '%enabled' => $country->enabled ? t('Enabled') : t('Disabled'),
      );
      $form['inserts']['#options'][$iso2] = t('New country %name, %continent (%official_name): %iso2, %iso3, %numcode, %enabled', $t_options);
      $form['inserts']['#default_value'][$iso2] = $iso2;
    }
  }
  else {
    $form['inserts'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="form-item">' . t('There are no new records to import.') . '</div>',
    );
  }
  if (count($results['updates'])) {
    $form_state['updates'] = $results['updates'];
    $form['updates'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Select the countries and their properties to update'),
    );
    foreach ($results['updates'] as $iso2 => $changes) {
      $country = country_load($iso2);
      foreach ($changes as $key => $values) {
        $t_options = array(
          '%name' => $country->name,
          '%iso2' => $country->iso2,
          '%code' => $key,
          '%new' => $values['new'],
          '%old' => $values['old'],
        );
        if ($key == 'enabled') {
          if ($values['new']) {
            $form['updates']['#options'][$iso2 . '-' . $key] = t('%name (%iso2): Enable this country', $t_options);
          }
          else {
            $form['updates']['#options'][$iso2 . '-' . $key] = t('%name (%iso2): Disable this country', $t_options);
          }
        }
        else {
          $form['updates']['#options'][$iso2 . '-' . $key] = t('%name (%iso2): Change %code from "%old" to "%new"', $t_options);
        }
        $form['updates']['#default_value'][$iso2 . '-' . $key] = $iso2 . '-' . $key;
      }
    }
  }
  else {
    $form['updates'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="form-item">' . t('There are no updates to import.') . '</div>',
    );
  }
  if (!empty($results['skipped'])) {
    $items = array();
    foreach ($results['skipped'] as $iso => $errors) {
      foreach ($errors as $error) {
        $items[] = t('@iso: !error', array(
          '@iso' => $iso,
          '!error' => $error,
        ));
      }
    }
    $form['skipped'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="form-item">' . t('The following errors were found. These records have been skipped: !errors', array(
        '!errors' => theme('item_list', array(
          'items' => $items,
        )),
      )) . '</div>',
    );
  }
  if (!empty($form_state['inserts']) || !empty($form_state['updates'])) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Import'),
    );
  }
  return $form;
}