You are here

function uc_file_admin_files_form_action in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 uc_file/uc_file.admin.inc \uc_file_admin_files_form_action()

Performs file action (upload, delete, hooked in actions).

See also

uc_file_admin_files_form()

uc_file_admin_files_form_action_validate()

uc_file_admin_files_form_action_submit()

1 call to uc_file_admin_files_form_action()
uc_file_admin_files_form in uc_file/uc_file.admin.inc
Form builder for file products admin.

File

uc_file/uc_file.admin.inc, line 236
File administration menu items.

Code

function uc_file_admin_files_form_action($form_state) {
  $file_ids = array();
  foreach ((array) $form_state['values']['file_select'] as $fid => $value) {
    $value = $value['check'];
    if ($value) {
      $file_ids[] = $fid;
    }
  }
  $form['file_ids'] = array(
    '#type' => 'value',
    '#value' => $file_ids,
  );
  $form['action'] = array(
    '#type' => 'value',
    '#value' => $form_state['values']['uc_file_action']['action'],
  );
  $file_ids = _uc_file_sort_names(_uc_file_get_dir_file_ids($file_ids, FALSE));
  switch ($form_state['values']['uc_file_action']['action']) {
    case 'uc_file_delete':
      $affected_list = _uc_file_build_js_file_display($file_ids);
      $has_directory = FALSE;
      foreach ($file_ids as $file_id) {

        // Gather a list of user-selected filenames.
        $file = uc_file_get_by_id($file_id);
        $filename = $file->filename;
        $file_list[] = substr($filename, -1) == "/" ? $filename . ' (' . t('directory') . ')' : $filename;

        // Determine if there are any directories in this list.
        $path = uc_file_qualify_file($filename);
        if (is_dir($path)) {
          $has_directory = TRUE;
        }
      }

      // Base files/dirs the user selected.
      $form['selected_files'] = array(
        '#type' => 'markup',
        '#value' => theme('item_list', $file_list, NULL, 'ul', array(
          'class' => 'selected-file-name',
        )),
      );
      $form = confirm_form($form, t('Delete file(s)'), 'admin/store/products/files', t('Deleting a file will remove all its associated file downloads and product features. Removing a directory will remove any files it contains and their associated file downloads and product features.'), t('Delete affected files'), t('Cancel'));

      // Don't even show the recursion checkbox unless we have any directories.
      if ($has_directory && $affected_list[TRUE] !== FALSE) {
        $form['recurse_directories'] = array(
          '#type' => 'checkbox',
          '#title' => t('Delete selected directories and their sub directories'),
        );

        // Default to FALSE... although we have the JS behavior to update with the state
        // of the checkbox on load, this should improve the experience of users who
        // don't have JS enabled over not defaulting to any info.
        $form['affected_files'] = array(
          '#type' => 'markup',
          '#value' => theme('item_list', $affected_list[FALSE], 'Affected file(s)', 'ul', array(
            'class' => 'affected-file-name',
          )),
        );
      }
      break;
    case 'uc_file_upload':

      // Upload file
      // Calculate the max size of uploaded files, in bytes.
      $max_bytes = trim(ini_get('post_max_size'));
      switch (strtolower($max_bytes[strlen($max_bytes) - 1])) {
        case 'g':
          $max_bytes *= 1024;
        case 'm':
          $max_bytes *= 1024;
        case 'k':
          $max_bytes *= 1024;
      }

      // Gather list of directories under the selected one(s).
      // '/' is always available.
      $directories = array(
        '' => '/',
      );
      $files = db_query("SELECT * FROM {uc_files}");
      while ($file = db_fetch_object($files)) {
        if (is_dir(variable_get('uc_file_base_dir', NULL) . "/" . $file->filename)) {
          $directories[$file->filename] = $file->filename;
        }
      }
      $form['upload_dir'] = array(
        '#type' => 'select',
        '#title' => t('Directory'),
        '#description' => t('The directory to upload the file to. The default directory is the root of the file downloads directory.'),
        '#options' => $directories,
      );
      $form['upload'] = array(
        '#type' => 'file',
        '#title' => t('File'),
        '#description' => t('The maximum file size that can be uploaded is %size bytes. You will need to use a different method to upload the file to the directory (e.g. (S)FTP, SCP) if your file exceeds this size.<br />The module will automatically detect files you upload using an alternate method.', array(
          '%size' => number_format($max_bytes),
        )),
      );
      $form['#attributes']['class'] = 'foo';
      $form = confirm_form($form, t('Upload file'), 'admin/store/products/files', '', t('Upload file'), t('Cancel'));

      // Must add this after confirm_form, as it runs over $form['#attributes'].
      // Issue logged at d#319723
      $form['#attributes']['enctype'] = 'multipart/form-data';
      break;
    default:

      // This action isn't handled by us, so check if any hook_file_action('form', $args) are implemented.
      foreach (module_implements('file_action') as $module) {
        $name = $module . '_file_action';
        $result = $name('form', array(
          'action' => $form_state['values']['uc_file_action']['action'],
          'file_ids' => $file_ids,
        ));
        $form = is_array($result) ? array_merge($form, $result) : $form;
      }
      break;
  }
  return $form;
}