You are here

function image_import_form in Image 5

Same name and namespace in other branches
  1. 5.2 contrib/image_import/image_import.module \image_import_form()
  2. 6 contrib/image_import/image_import.pages.inc \image_import_form()
  3. 7 contrib/image_import/image_import.pages.inc \image_import_form()
1 string reference to 'image_import_form'
image_import_menu in contrib/image_import/image_import.module
Implementation of hook_menu().

File

contrib/image_import/image_import.module, line 55

Code

function image_import_form() {
  $form = array();
  $dirpath = variable_get('image_import_path', '');
  if (!file_check_directory($dirpath)) {
    drupal_set_message(t("You need to configure the import directory on the image import module's <a href='!admin-settings-image_import'>settings page</a>.", array(
      '!admin-settings-image_import' => url('admin/settings/image_import'),
    )), 'error');
    return $form;
  }
  $files = file_scan_directory($dirpath, '.*');
  ksort($files);
  if ($files) {
    if (module_exists('taxonomy')) {

      // here's a little hack to get the taxonmy controls onto our form
      $form['type'] = array(
        '#type' => 'value',
        '#value' => 'image',
      );
      $form['#node'] = new stdClass();
      $form['#node']->type = 'image';
      taxonomy_form_alter('image_node_form', $form);
      unset($form['type']);
      unset($form['#node']);
    }

    // Put the image files into an array for the checkboxes and gather
    // additional information like dimensions and filesizes. Make sure that
    // there's no 0th element, because a checkbox with a zero value is seen as
    // unchecked and won't be imported.
    $fields = array(
      'filesize',
      'dimensions',
      'title',
      'body',
    );
    foreach ($fields as $field) {
      $form['files'][$field][0] = NULL;
    }
    $filelist = array(
      0 => NULL,
    );
    foreach ($files as $file) {
      $info = image_get_info($file->filename);
      if ($info && isset($info['extension'])) {
        $filelist[] = substr($file->filename, strlen($dirpath) + 1);
        $form['files']['filesize'][] = array(
          '#type' => 'item',
          '#value' => format_size(filesize($file->filename)),
        );
        $form['files']['dimensions'][] = array(
          '#type' => 'item',
          '#value' => $info['width'] . 'x' . $info['height'],
        );
        $form['files']['title'][] = array(
          '#type' => 'textfield',
          '#size' => 20,
          '#default_value' => basename($file->name),
        );
        $form['files']['body'][] = array(
          '#type' => 'textfield',
          '#size' => 20,
        );
      }
    }

    // Remove our 0 elements.
    unset($filelist[0]);
    foreach ($fields as $field) {
      $form['files'][$field][0] = NULL;
    }

    // Put the titles into an array.
    $form['files']['title']['#tree'] = TRUE;
    $form['files']['body']['#tree'] = TRUE;

    // Store a copy of the list into a form value so we can compare it to what
    // they submit and not have to worry about files being added or removed from
    // the filesystem.
    $form['file_list'] = array(
      '#type' => 'value',
      '#value' => $filelist,
    );
    $form['import_file'] = array(
      '#type' => 'checkboxes',
      '#options' => $filelist,
    );
    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Import'),
    );
  }
  else {
    $form['import_file'] = array(
      '#type' => 'item',
      '#value' => t('No files were found'),
    );
  }
  return $form;
}