You are here

function image_import_form in Image 6

Same name and namespace in other branches
  1. 5.2 contrib/image_import/image_import.module \image_import_form()
  2. 5 contrib/image_import/image_import.module \image_import_form()
  3. 7 contrib/image_import/image_import.pages.inc \image_import_form()

Form generating function for the image import page.

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.pages.inc, line 33

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-import'>settings page</a>.", array(
      '!admin-settings-import' => url('admin/settings/image/image_import'),
    )), 'error');
    return $form;
  }
  $form['#dirpath'] = $dirpath;
  $form['#node_type'] = 'image';
  $files = file_scan_directory($dirpath, '.*');
  ksort($files);

  // When the form gets too large we end up running out of memory submitting it.
  // To avoid this we use a pager and rather than setting up all the variables
  // ourself we just send in a fake query and then select the desired part of
  // the files array.
  $page_size = variable_get('image_import_page_size', 50);
  pager_query('SELECT %d', $page_size, 0, 'SELECT %d', array(
    count($files),
  ));
  $files = array_slice($files, $GLOBALS['pager_page_array'][0] * $page_size, $page_size);
  $file_metadata_modules = module_implements('file_metadata');
  if ($files) {
    if (module_exists('taxonomy')) {

      // here's a little hack to get the taxonomy controls onto our form
      $form['type'] = array(
        '#type' => 'value',
        '#value' => $form['#node_type'],
      );
      $form['#node'] = new stdClass();
      $form['#node']->type = $form['#node_type'];
      taxonomy_form_alter($form, array(), $form['#node_type'] . '_node_form');
      unset($form['type']);
      unset($form['#node']);
    }
    if (module_exists('image_gallery')) {
      $form['tree'] = array(
        '#type' => 'checkbox',
        '#title' => t('Reproduce directories structure with subgalleries'),
        '#description' => t('A subgallery will be created only if the folder contains at least an image.'),
      );
    }
    $form['delete_directories'] = array(
      '#type' => 'checkbox',
      '#title' => t('Delete empty directories after import'),
      '#description' => '',
    );

    // 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.
    $index = 0;
    foreach ($files as $file) {
      $index++;
      $filelist[$index] = substr($file->filename, strlen($dirpath) + 1);
      $problems = image_import_validate_file($file);

      // Allow other modules to supply metadata about the images being imported.
      // hook_file_metadata() may populate the $file properties 'title' and
      // 'description'.
      foreach ($file_metadata_modules as $module) {
        $function = $module . '_file_metadata';
        $function($file);
      }

      // Spit out the import form elements.
      $form['files']['import'][$index] = array(
        '#type' => 'checkbox',
        '#title' => substr($file->filename, strlen($dirpath) + 1),
      );
      $form['files']['filesize'][$index] = array(
        '#type' => 'item',
        '#value' => format_size(filesize($file->filename)),
      );
      $form['files']['dimensions'][$index] = array(
        '#type' => 'item',
        '#value' => $file->width . 'x' . $file->height,
      );
      $form['files']['title'][$index] = array(
        '#type' => 'textfield',
        '#size' => 20,
        '#default_value' => isset($file->title) ? $file->title : basename($file->name),
      );
      $form['files']['body'][$index] = array(
        '#type' => 'textfield',
        '#size' => 20,
        '#default_value' => isset($file->body) ? $file->body : basename($file->name),
      );

      // If there were problems don't let them import it
      if (count($problems)) {
        $form['files']['import'][$index]['#type'] = 'item';
        $form['files']['errors'][$index] = array(
          '#type' => 'markup',
          '#value' => '<em>' . implode(' ', $problems) . '</em>',
        );
        unset($form['files']['title'][$index]);
        unset($form['files']['body'][$index]);
      }
    }
    $form['pager'] = array(
      '#value' => theme('pager', NULL, $page_size, 0),
    );

    // Put the titles into an array.
    $form['files']['import']['#tree'] = TRUE;
    $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['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Import'),
    );
  }
  else {
    $form['none_found'] = array(
      '#type' => 'item',
      '#value' => '<em>' . t('No files were found.') . '</em>',
    );
  }
  return $form;
}