You are here

function uc_file_user_form in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 uc_file/uc_file.module \uc_file_user_form()

Form builder for per-user file download administration.

1 string reference to 'uc_file_user_form'
uc_file_user_downloads in uc_file/uc_file.pages.inc
Table builder for user downloads.

File

uc_file/uc_file.module, line 143

Code

function uc_file_user_form($form_state, $account) {
  $form['file'] = array(
    '#type' => 'fieldset',
    '#title' => t('Administration'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  // Drop out early if we don't even have any files uploaded.
  if (!db_result(db_query("SELECT COUNT(*) FROM {uc_files}"))) {
    $form['file']['file_message'] = array(
      '#value' => '<p>' . t('You must add files at the !url in order to attach them to a user.', array(
        '!url' => l(t('Ubercart file download administration page'), 'admin/store/products/files', array(
          'query' => 'destination=user/' . $account->uid . '/edit',
        )),
      )) . '</p>',
    );
    return $form;
  }

  // Table displaying current downloadable files and limits.
  $form['file']['download']['#theme'] = 'uc_file_hook_user_file_downloads';
  $form['file']['download']['file_download']['#tree'] = TRUE;
  $downloadable_files = array();
  $file_downloads = db_query("SELECT * FROM {uc_file_users} AS ufu INNER JOIN {uc_files} AS uf ON ufu.fid = uf.fid WHERE ufu.uid = %d ORDER BY uf.filename ASC", $account->uid);
  $behavior = 0;
  while ($file_download = db_fetch_object($file_downloads)) {

    // Store a flat array so we can array_diff the ones already allowed when
    // building the list of which can be attached.
    $downloadable_files[$file_download->fid] = $file_download->filename;
    $form['file']['download']['file_download'][$file_download->fid] = array(
      'fuid' => array(
        '#type' => 'value',
        '#value' => $file_download->fuid,
      ),
      'expiration' => array(
        '#type' => 'value',
        '#value' => $file_download->expiration,
      ),
      'remove' => array(
        '#type' => 'checkbox',
      ),
      'filename' => array(
        '#value' => $file_download->filename,
      ),
      'expires' => array(
        '#value' => $file_download->expiration ? format_date($file_download->expiration, 'small') : t('Never'),
      ),
      'time_polarity' => array(
        '#type' => 'select',
        '#default_value' => '+',
        '#options' => array(
          '+' => '+',
          '-' => '-',
        ),
      ),
      'time_quantity' => array(
        '#type' => 'textfield',
        '#size' => 2,
        '#maxlength' => 2,
      ),
      'time_granularity' => array(
        '#type' => 'select',
        '#default_value' => 'day',
        '#options' => array(
          'never' => t('never'),
          'day' => t('day(s)'),
          'week' => t('week(s)'),
          'month' => t('month(s)'),
          'year' => t('year(s)'),
        ),
      ),
      'downloads_in' => array(
        '#value' => $file_download->accessed,
      ),
      'download_limit' => array(
        '#type' => 'textfield',
        '#maxlength' => 3,
        '#size' => 3,
        '#default_value' => $file_download->download_limit ? $file_download->download_limit : NULL,
      ),
      'addresses_in' => array(
        '#value' => count(unserialize($file_download->addresses)),
      ),
      'address_limit' => array(
        '#type' => 'textfield',
        '#maxlength' => 2,
        '#size' => 2,
        '#default_value' => $file_download->address_limit ? $file_download->address_limit : NULL,
      ),
    );

    // Incrementally add behaviors.
    _uc_file_download_table_behavior($behavior++, $file_download->fid);

    // Store old values for comparing to see if we actually made any changes.
    $less_reading =& $form['file']['download']['file_download'][$file_download->fid];
    $less_reading['download_limit_old'] = array(
      '#type' => 'value',
      '#value' => $less_reading['download_limit']['#default_value'],
    );
    $less_reading['address_limit_old'] = array(
      '#type' => 'value',
      '#value' => $less_reading['address_limit']['#default_value'],
    );
    $less_reading['expiration_old'] = array(
      '#type' => 'value',
      '#value' => $less_reading['expiration']['#value'],
    );
  }

  // Create the list of files able to be attached to this user.
  $available_files = array();
  $files = db_query("SELECT * FROM {uc_files} ORDER BY filename ASC");
  while ($file = db_fetch_object($files)) {
    if (substr($file->filename, -1) != '/' && substr($file->filename, -1) != '\\') {
      $available_files[$file->fid] = $file->filename;
    }
  }

  // Dialog for uploading new files.
  $available_files = array_diff($available_files, $downloadable_files);
  if (count($available_files)) {
    $form['file']['file_add'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#size' => 6,
      '#title' => t('Add file'),
      '#description' => t('Select a file to add as a download. Newly added files will inherit the settings at the !url.', array(
        '!url' => l(t('Ubercart file product feature settings page'), 'admin/store/settings/products/edit/features'),
      )),
      '#options' => $available_files,
      '#tree' => TRUE,
    );
  }
  $form['file']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}