You are here

function asset_import_form in Asset 5.2

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

File

contrib/asset_import/asset_import.module, line 57

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
    $list = asset_directory_options();
    $form['parent'] = array(
      '#type' => 'select',
      '#title' => t('Parent Directory'),
      '#default_value' => $form_values['parent'] ? $form_values['parent'] : $_GET['dir'],
      '#options' => $list,
    );

    // Offer to create a subdirectory in the chosen asset directory
    $form['subdirectory'] = array(
      '#type' => 'textfield',
      '#title' => t('Create subdirectory'),
      '#size' => 13,
      '#description' => t('Enter the name of the subdirectory to be created in the chose Parent Directory. Don\'t add a leading or trailing slash. Leave blank to import all files in the Parent Directory.'),
    );

    // Copy or move the files? Is useful when importing files from other file modules to make a comparison
    $form['copy'] = array(
      '#type' => 'checkbox',
      '#title' => t('Copy the files instead of moving them'),
      '#description' => t('Default behavior is to move the selected files. Check this options if you wish to copy the files instead of moving them. This could be useful for testing the asset module in comparision with the file handling module(s) you are currently using.'),
    );

    // Put the files into an array for the checkboxes and gather
    // additional information like the 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');
    $fields = array(
      'filesize',
      'title',
      'author',
    );
    foreach ($fields as $field) {
      $form['files'][$field][0] = NULL;
    }
    $filelist = array(
      0 => NULL,
    );
    foreach ($files as $file) {
      $info = pathinfo($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']['author'][] = 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']['author']['#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;
}