You are here

function uc_file_admin_files_form_action in Ubercart 7.3

Same name and namespace in other branches
  1. 6.2 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 220
File administration menu items.

Code

function uc_file_admin_files_form_action($form, &$form_state) {
  $file_ids = array_filter($form_state['values']['file_select']);
  $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(
        '#theme' => 'item_list',
        '#items' => $file_list,
        '#attributes' => array(
          'class' => array(
            '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(
          '#theme' => 'item_list',
          '#items' => $affected_list[FALSE],
          '#title' => t('Affected files'),
          '#attributes' => array(
            'class' => array(
              'affected-file-name',
            ),
          ),
        );
      }
      break;
    case 'uc_file_upload':

      // Calculate the maximum size of uploaded files in bytes.
      $post_max_size = ini_get('post_max_size');
      if (is_numeric($post_max_size)) {

        // Handle the case where 'post_max_size' has no suffix.
        // An explicit cast is needed because floats are not allowed.
        $max_bytes = (int) $post_max_size;
      }
      else {

        // Handle the case where 'post_max_size' has a suffix of
        // 'M', 'K', or 'G' (case insensitive).
        $max_bytes = (int) substr($post_max_size, 0, -1);
        $suffix = strtolower(substr($post_max_size, -1));
        switch ($suffix) {
          case 'k':
            $max_bytes *= 1024;
            break;
          case 'm':
            $max_bytes *= 1048576;
            break;
          case 'g':
            $max_bytes *= 1073741824;
            break;
        }
      }

      // Gather list of directories under the selected one(s).
      // '/' is always available.
      $directories = array(
        '' => '/',
      );
      $files = db_query("SELECT * FROM {uc_files}");
      foreach ($files as $file) {
        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 on the server where the file should be put. The default directory is the root of the file downloads directory.'),
        '#options' => $directories,
      );
      $form['upload'] = array(
        '#type' => 'file',
        '#title' => t('File'),
        // Deliberately concatenate the strings to preserve backwards
        // compatibility with existing translations.
        '#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. Files you upload using one of these alternate methods will be automatically detected.', array(
          '%size' => number_format($max_bytes),
        )) . ' ' . t("Note: A value of '0' means there is no size limit."),
      );
      $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 https://www.drupal.org/node/319723.
      $form['#attributes']['enctype'] = 'multipart/form-data';
      break;
    default:

      // This action isn't handled by us, so check if any
      // hook_uc_file_action('form', $args) are implemented.
      foreach (module_implements('uc_file_action') as $module) {
        $name = $module . '_uc_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;
}