You are here

function uc_file_feature_form in Ubercart 6.2

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

Form builder for hook_product_feature.

See also

uc_file_feature_form_validate()

uc_file_feature_form_submit()

1 string reference to 'uc_file_feature_form'
uc_file_product_feature in uc_file/uc_file.module
Implements hook_product_feature().

File

uc_file/uc_file.module, line 711

Code

function uc_file_feature_form($form_state, $node, $feature) {
  $form = array();
  if (!is_dir(variable_get('uc_file_base_dir', NULL))) {
    drupal_set_message(t('A file directory needs to be configured in <a href="!url">product feature settings</a> before a file can be selected.', array(
      '!url' => url('admin/store/settings/products/edit/features'),
    )), 'error');
    return $form;
  }
  if (!db_result(db_query("SELECT COUNT(*) FROM {uc_files}"))) {
    $form['file']['file_message'] = array(
      '#value' => t('You must add files at the !url in order to attach them to a model.', array(
        '!url' => l(t('Ubercart file download administration page'), 'admin/store/products/files', array(
          'query' => 'destination=node/' . $node->nid . '/edit/features/file/add',
        )),
      )),
    );
    return $form;
  }

  // Make sure we have an up-to-date list for the autocompletion.
  uc_file_refresh();

  // Grab all the models on this product.
  $models = uc_product_get_models($node->nid);

  // Use the feature's values to fill the form, if they exist.
  if (!empty($feature)) {
    $file_product = db_fetch_object(db_query("SELECT * FROM {uc_file_products} as p LEFT JOIN {uc_files} as f ON p.fid = f.fid WHERE pfid = %d", $feature['pfid']));
    $default_feature = $feature['pfid'];
    $default_model = $file_product->model;
    $default_filename = $file_product->filename;
    $default_description = $file_product->description;
    $default_shippable = $file_product->shippable;
    $download_status = $file_product->download_limit != UC_FILE_LIMIT_SENTINEL;
    $download_value = $download_status ? $file_product->download_limit : NULL;
    $address_status = $file_product->address_limit != UC_FILE_LIMIT_SENTINEL;
    $address_value = $address_status ? $file_product->address_limit : NULL;
    $time_status = $file_product->time_granularity != UC_FILE_LIMIT_SENTINEL;
    $quantity_value = $time_status ? $file_product->time_quantity : NULL;
    $granularity_value = $time_status ? $file_product->time_granularity : 'never';
  }
  else {
    $file_product = FALSE;
    $default_feature = NULL;
    $default_model = '';
    $default_filename = '';
    $default_description = '';
    $default_shippable = $node->shippable;
    $download_status = FALSE;
    $download_value = NULL;
    $address_status = FALSE;
    $address_value = NULL;
    $time_status = FALSE;
    $quantity_value = NULL;
    $granularity_value = 'never';
  }
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $node->nid,
  );
  $form['pfid'] = array(
    '#type' => 'value',
    '#value' => $default_feature,
  );
  $form['uc_file_model'] = array(
    '#type' => 'select',
    '#title' => t('SKU'),
    '#default_value' => $default_model,
    '#description' => t('This is the SKU that will need to be purchased to obtain the file download.'),
    '#options' => $models,
  );
  $form['uc_file_filename'] = array(
    '#type' => 'textfield',
    '#title' => t('File download'),
    '#default_value' => $default_filename,
    '#autocomplete_path' => '_autocomplete_file',
    '#description' => t('The file that can be downloaded when product is purchased (enter a path relative to the %dir directory).', array(
      '%dir' => variable_get('uc_file_base_dir', NULL),
    )),
    '#maxlength' => 255,
  );
  $form['uc_file_description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#default_value' => $default_description,
    '#maxlength' => 255,
    '#description' => t('A description of the download associated with the product.'),
  );
  $form['uc_file_shippable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Shippable product'),
    '#default_value' => $default_shippable,
    '#description' => t('Check if this product model/SKU file download is also associated with a shippable product.'),
  );
  $form['uc_file_limits'] = array(
    '#type' => 'fieldset',
    '#description' => t('Use these options to override any global download limits set at the !url.', array(
      '!url' => l(t('Ubercart product settings page'), 'admin/store/settings/products/edit/features', array(
        'query' => 'destination=node/' . $node->nid . '/edit/features/file/add',
      )),
    )),
    '#collapsed' => FALSE,
    '#collapsible' => FALSE,
    '#title' => t('File limitations'),
  );
  $form['uc_file_limits']['download_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Override download limit'),
    '#default_value' => $download_status,
    '#description' => t('Override the amount of times a customer can download this file after the product has been purchased.'),
  );
  $form['uc_file_limits']['download_limit_number'] = array(
    '#type' => 'textfield',
    '#title' => t('Downloads'),
    '#default_value' => $download_value,
    '#description' => t("The number of times this file can be downloaded."),
    '#maxlength' => 4,
    '#size' => 4,
  );
  $form['uc_file_limits']['location_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Override location limit'),
    '#default_value' => $address_status,
    '#description' => t('Override the amount of locations (IP addresses) a customer can download this file from after the product has been purchased.'),
  );
  $form['uc_file_limits']['download_limit_addresses'] = array(
    '#type' => 'textfield',
    '#title' => t('IP addresses'),
    '#default_value' => $address_value,
    '#description' => t("The number of unique IPs that a file can be downloaded from."),
    '#maxlength' => 4,
    '#size' => 4,
  );
  $form['uc_file_limits']['time_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Override time limit'),
    '#default_value' => $time_status,
    '#description' => t('Override the amount of time a customer can download this file after the product has been purchased.'),
  );
  $form['uc_file_limits']['download_limit_duration_qty'] = array(
    '#type' => 'textfield',
    '#title' => t('Time'),
    '#default_value' => $quantity_value,
    '#size' => 4,
    '#maxlength' => 4,
    '#prefix' => '<div class="duration">',
    '#suffix' => '</div>',
  );
  $form['uc_file_limits']['download_limit_duration_granularity'] = array(
    '#type' => 'select',
    '#default_value' => $granularity_value,
    '#options' => array(
      'never' => t('never'),
      'day' => t('day(s)'),
      'week' => t('week(s)'),
      'month' => t('month(s)'),
      'year' => t('year(s)'),
    ),
    '#description' => t('How long after this product has been purchased until this file download expires.'),
    '#prefix' => '<div class="duration">',
    '#suffix' => '</div>',
  );
  return uc_product_feature_form($form);
}