You are here

function uc_file_files_form in Ubercart 5

Form builder for file products admin

1 string reference to 'uc_file_files_form'
uc_file_files_admin in uc_file/uc_file.module
Page builder for file products admin

File

uc_file/uc_file.module, line 602
Allows products to be associated with downloadable files.

Code

function uc_file_files_form($form_values = NULL) {
  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => !isset($form_values) ? 1 : $form_values['step'] + 1,
  );
  switch ($form['step']['#value']) {

    //Display File Options and File checkboxes
    case 1:
      $files = db_query("SELECT * FROM {uc_files}");
      $file_actions = array(
        'uc_file_delete' => t('Delete file(s)'),
        'uc_file_upload' => t('Upload file'),
      );

      //Check any if any hook_file_action('info', $args) are implemented
      foreach (module_implements('file_action') as $module) {
        $name = $module . '_file_action';
        $result = $name('info', NULL);
        if (is_array($result)) {
          foreach ($result as $key => $action) {
            if ($key != 'uc_file_delete' && $key != 'uc_file_upload') {
              $file_actions[$key] = $action;
            }
          }
        }
      }
      while ($file = db_fetch_object($files)) {
        $form['file_select_' . $file->fid] = array(
          '#type' => 'checkbox',
        );
      }
      $form['uc_file_action'] = array(
        '#type' => 'fieldset',
        '#title' => t('File options'),
        '#collapsible' => FALSE,
        '#collapsed' => FALSE,
      );
      $form['uc_file_action']['action'] = array(
        '#type' => 'select',
        '#title' => t('Action'),
        '#options' => $file_actions,
        '#prefix' => '<div class="duration">',
        '#suffix' => '</div>',
      );
      $form['uc_file_action']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Perform action'),
        '#prefix' => '<div class="duration">',
        '#suffix' => '</div>',
      );
      break;
    case 2:

      //Perform File Action (Upload, Delete, hooked in actions)
      $file_ids = array();
      foreach ($form_values as $name => $form_value) {
        if (strpos($name, 'file_select_') !== FALSE) {
          $file_ids[] = intval(str_replace('file_select_', '', $name));
        }
      }
      $form['file_ids'] = array(
        '#type' => 'value',
        '#value' => $file_ids,
      );
      $form['action'] = array(
        '#type' => 'value',
        '#value' => $form_values['action'],
      );

      //Switch to an action to perform
      switch ($form_values['action']) {
        case 'uc_file_delete':

          //Delete selected files
          foreach ($file_ids as $file_id) {
            $filename = db_result(db_query("SELECT filename FROM {uc_files} WHERE fid = %d", $file_id));
            $filename = substr($filename, -1) == "/" ? $filename . ' (' . t('directory') . ')' : $filename;
            $file_list[] = $filename;
          }
          $form['files'] = array(
            '#type' => 'markup',
            '#value' => theme_item_list($file_list, NULL, 'ul', array(
              'class' => 'file-name',
            )),
          );
          $form['recurse_directories'] = array(
            '#type' => 'checkbox',
            '#title' => t('Delete selected directories and their sub directories'),
          );
          $form = confirm_form($form, t('Delete the following 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('Yes'), t('No'));
          break;
        case 'uc_file_upload':

          //Upload file
          drupal_set_title(t('Upload File'));
          $max_bytes = trim(ini_get('post_max_size'));
          $directories = array(
            '' => '/',
          );
          switch (strtolower($max_bytes[strlen($max_bytes) - 1])) {
            case 'g':
              $max_bytes *= 1024;
            case 'm':
              $max_bytes *= 1024;
            case 'k':
              $max_bytes *= 1024;
          }
          $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['#attributes']['enctype'] = 'multipart/form-data';
          $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. FTP, SSH) if your file exceeds this size.', array(
              '%size' => number_format($max_bytes),
            )),
          );
          $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('Upload'),
          );
          break;
        default:

          //Check any 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_values['action'],
              'file_ids' => $file_ids,
            ));
            $form = is_array($result) ? array_merge($form, $result) : $form;
          }
          break;
      }
      break;
    default:
      break;
  }
  $form['#multistep'] = TRUE;
  $form['#redirect'] = FALSE;
  return $form;
}