You are here

function countries_admin_overview in Countries 7

Same name and namespace in other branches
  1. 8 countries.admin.inc \countries_admin_overview()
  2. 7.2 countries.admin.inc \countries_admin_overview()

Menu callback; Displays a list of all countries.

1 string reference to 'countries_admin_overview'
countries_menu in ./countries.module
Implement hook_menu().

File

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

Code

function countries_admin_overview() {
  $header = array();
  $header[] = array(
    'data' => t('Name'),
    'field' => 'c.name',
    'sort' => 'asc',
  );
  $header[] = array(
    'data' => t('ISO2'),
    'field' => 'c.iso2',
  );
  $columns = variable_get('countries_admin_overview_columns', array(
    'iso3' => t('ISO3'),
    'numcode' => t('Number code'),
    'continent' => t('Continent'),
    'official_name' => t('Official name'),
  ));
  foreach ($columns as $key => $title) {
    $header[] = array(
      'data' => $title,
      'field' => 'c.' . $key,
    );
  }
  $header[] = array(
    'data' => t('Status'),
    'field' => 'c.enabled',
  );
  $header[] = array(
    'data' => t('Operations'),
  );
  $query = db_select('countries_country', 'c')
    ->extend('PagerDefault')
    ->extend('TableSort');
  $result = $query
    ->fields('c')
    ->orderByHeader($header)
    ->limit(50)
    ->execute();
  $destination = drupal_get_destination();
  $continents = countries_get_continents();
  include_once DRUPAL_ROOT . '/includes/iso.inc';
  $core_countries = _country_get_predefined_list();
  $rows = array();
  foreach ($result as $country) {
    $row = array();
    $row[] = l($country->name, 'admin/config/regional/countries/' . $country->iso2, array(
      'query' => $destination,
    ));
    $row[] = $country->iso2;
    foreach ($columns as $key => $title) {
      switch ($key) {
        case 'continent':
          $row[] = isset($continents[$country->continent]) ? $continents[$country->continent] : t('Unknown');
          break;
        case 'numcode':
          $row[] = theme('countries_number', array(
            'country' => $country,
          ));
          break;
        default:
          $row[] = check_plain($country->{$key});
          break;
      }
    }
    $row[] = $country->enabled ? t('Enabled') : t('Disabled');
    $operations = l(t('edit'), 'admin/config/regional/countries/' . $country->iso2, array(
      'query' => $destination,
    ));
    if (module_exists('countries_regions')) {
      $count = countries_regions_count($country);
      $operations .= '   ' . l(t('!count regions', array(
        '!count' => $count,
      )), 'admin/config/regional/countries/' . $country->iso2 . '/regions', array(
        'query' => $destination,
      ));
    }
    if (!array_key_exists($country->iso2, $core_countries)) {
      $operations .= '   ' . l(t('delete'), 'admin/config/regional/countries/' . $country->iso2 . '/delete', array(
        'query' => $destination,
      ));
    }
    $row[] = $operations;
    $rows[] = $row;
  }
  if (empty($rows)) {
    $rows[] = array(
      array(
        'data' => t('No countries are available.'),
        'colspan' => count($header),
      ),
    );
  }
  $build['countries_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  );
  $build['countries_pager'] = array(
    '#theme' => 'pager',
  );
  return $build;
}