You are here

function asset_import_form in Asset 6

Same name in this branch
  1. 6 contrib/asset_import/asset_import.module \asset_import_form()
  2. 6 asset_import/inc/asset_import.admin.inc \asset_import_form()
Same name and namespace in other branches
  1. 5.2 contrib/asset_import/asset_import.module \asset_import_form()
  2. 5 asset_import/asset_import.module \asset_import_form()
2 string references to 'asset_import_form'
asset_import_menu in asset_import/asset_import.module
Implementation of hook_menu().
asset_import_menu in contrib/asset_import/asset_import.module
Implementation of hook_menu().

File

asset_import/inc/asset_import.admin.inc, line 2

Code

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

    // Display a list of asset directories
    include_once drupal_get_path('module', 'asset') . '/inc/asset_wizard.inc';
    $list = asset_wizard_directory_options();
    $form['parent'] = array(
      '#type' => 'select',
      '#title' => t('Parent directory'),
      '#default_value' => $form_values['parent'] ? $form_values['parent'] : $_GET['dir'],
      '#options' => asset_wizard_directory_options(),
    );

    // Put the 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) {
      $_title = db_fetch_array(db_query("SELECT n.title, n.type, f.filename FROM {node} n, {files} f WHERE f.filepath='%s' AND n.nid = f.nid LIMIT 1", $file->filename));
      if (!empty($_title)) {
        if ($_title['type'] == "image") {

          // This is an image created by the image module, we have information about the image size name
          $title = $_title['title'] . " (" . str_replace("_original", "original", $_title['filename']) . ")";
        }
        else {

          // This is a file created by a drupal module, we have some information about it
          $title = $_title['title'];
        }
      }
      else {

        // This file was not created by a drupal module but uploaded using ftp for instance, we don't have any information about it
        $title = basename($file->name);
      }
      $info = image_get_info($file->filename);
      if ($info && isset($info['extension'])) {

        // This is an image so we provide as much details as possible
        $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' => $title,
        );
        $form['files']['body'][] = array(
          '#type' => 'textfield',
          '#size' => 20,
        );
      }
      else {

        // This is not an image
        $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' => '',
        );
        $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;
}