You are here

function _countries_import_bulk_import_selection_form in Countries 8

1 call to _countries_import_bulk_import_selection_form()
countries_import_bulk_import_form in modules/countries_import/countries_import.admin.inc

File

modules/countries_import/countries_import.admin.inc, line 109

Code

function _countries_import_bulk_import_selection_form($form, &$form_state) {
  $plugin = new $form_state['values']['source']();
  $lookup_key = $plugin
    ->importKey();
  $form['countries'] = array(
    '#tree' => TRUE,
    '#type' => 'container',
  );
  $form['source'] = array(
    '#type' => 'value',
    '#value' => $form_state['values']['source'],
  );
  $properties = $form_state['import_settings']['properties'];
  $properties[$lookup_key] = 1;
  $continents = countries_get_continents();
  $i18n = module_exists('countries_i18n');
  $update_i18n = $i18n && $form_state['langcode'] != language_default('language');
  foreach ($form_state['import_data'] as $key => $data) {
    $data->{$lookup_key} = $key;
    foreach (countries_core_properties() as $property => $property_label) {
      if (empty($properties[$property]) || !property_exists($data, $property)) {
        $data->{$property} = '';
      }
    }
    $country = countries_country_lookup($key, $lookup_key);
    if (!$country) {

      // We need a name to parse new countries.
      if (empty($properties['name'])) {
        continue;
      }
    }
    else {
      if ($form_state['skip_disabled'] && !$country->enabled) {
        continue;
      }
    }
    $form['countries'][$key]['skip'] = array(
      '#type' => 'checkbox',
      '#title' => t('Skip'),
      '#default_Value' => 1,
    );
    $form['countries'][$key]['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Status'),
      '#default_value' => isset($properties['enabled']) ? (bool) $data->enabled : ($country ? $country->enabled : FALSE),
    );
    $form['countries'][$key]['cid'] = array(
      '#type' => 'value',
      '#value' => $country ? $country->cid : NULL,
    );
    $form['countries'][$key]['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $data->name,
      '#required' => TRUE,
      '#maxlength' => 95,
    );
    $locked = country_is_locked($country);
    $form['countries'][$key]['iso2'] = array(
      '#type' => 'textfield',
      '#title' => t('ISO alpha-2 code'),
      '#default_value' => $data->iso2,
      '#required' => TRUE,
      '#maxlength' => 2,
    );
    $form['countries'][$key]['iso3'] = array(
      '#type' => 'textfield',
      '#title' => t('ISO alpha-3 code'),
      '#default_value' => $data->iso3,
      '#maxlength' => 3,
    );
    $form['countries'][$key]['official_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Official name'),
      '#default_value' => $data->official_name,
      '#maxlength' => 127,
    );
    $form['countries'][$key]['numcode'] = array(
      '#type' => 'textfield',
      '#title' => t('ISO numeric-3 code'),
      '#default_value' => $data->numcode,
      '#maxlength' => 5,
    );
    $default_value = isset($continents[$data->continent]) ? $data->continent : '';
    $form['countries'][$key]['continent'] = array(
      '#type' => 'select',
      '#title' => t('Continent'),
      '#options' => $continents,
      '#default_value' => $default_value,
      '#empty_value' => '',
    );
    if ($country) {
      $has_update = FALSE;
      foreach (countries_core_properties() as $property => $property_label) {
        if (empty($form['countries'][$key][$property]['#default_value'])) {
          $form['countries'][$key][$property]['#default_value'] = $country->{$property};
        }
        if (empty($properties[$property])) {
          $form['countries'][$key][$property]['#disabled'] = TRUE;
        }
        else {
          $comparison_property = $country->{$property};
          if ($update_i18n && in_array($property, array(
            'name',
            'official_name',
          ))) {
            $comparison_property = country_property($country, $property, array(
              'langcode' => $form_state['langcode'],
              'sanitize' => 0,
            ));
          }
          if ($comparison_property == $data->{$property}) {
            $form['countries'][$key][$property]['#disabled'] = TRUE;
          }
          else {
            $has_update = TRUE;
            if ($property == 'continent') {
              $form['countries'][$key][$property]['#description'] = t('Currently is "%value"', array(
                '%value' => isset($continents[$country->continent]) ? $continents[$country->continent] : $country->continent,
              ));
            }
            else {
              $form['countries'][$key][$property]['#description'] = t('Currently is "%value"', array(
                '%value' => $comparison_property,
              ));
            }
          }
        }
      }
      if (!$has_update) {
        unset($form['countries'][$key]);
      }
    }
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
  );
  $form['countries']['#theme'] = 'countries_import_bulk_import_selection_form';
  $form['countries']['#update_country_i18n'] = $update_i18n;
  return $form;
}