You are here

function uc_file_feature_form in Ubercart 7.3

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

Form builder for hook_uc_product_feature.

See also

uc_file_feature_form_validate()

uc_file_feature_form_submit()

1 string reference to 'uc_file_feature_form'
uc_file_uc_product_feature in uc_file/uc_file.module
Implements hook_uc_product_feature().

File

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

Code

function uc_file_feature_form($form, &$form_state, $node, $feature) {
  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 settings</a> under the file download settings tab before a file can be selected.', array(
      '@url' => url('admin/store/settings/products'),
    )), 'warning');
    unset($form['buttons']);
    return $form;
  }

  // Rescan the file directory to populate {uc_files} with the current list
  // because files uploaded via any method other than the Upload button
  // (e.g. by FTP) won't be in {uc_files} yet.
  uc_file_refresh();
  if (!db_query_range('SELECT 1 FROM {uc_files}', 0, 1)
    ->fetchField()) {
    $form['file']['file_message'] = array(
      '#markup' => t('You must add files at the <a href="@url">Ubercart file download administration page</a> in order to attach them to a model.', array(
        '@url' => url('admin/store/products/files', array(
          'query' => array(
            'destination' => 'node/' . $node->nid . '/edit/features/file/add',
          ),
        )),
      )),
    );
    unset($form['buttons']);
    return $form;
  }

  // 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_query("SELECT * FROM {uc_file_products} p LEFT JOIN {uc_files} f ON p.fid = f.fid WHERE pfid = :pfid", array(
      ':pfid' => $feature['pfid'],
    ))
      ->fetchObject();
    $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', array(
        'query' => array(
          '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,
  );
  $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,
    '#states' => array(
      'visible' => array(
        'input[name="download_override"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['uc_file_limits']['location_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Override IP address limit'),
    '#default_value' => $address_status,
  );
  $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,
    '#states' => array(
      'visible' => array(
        'input[name="location_override"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['uc_file_limits']['time_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Override time limit'),
    '#default_value' => $time_status,
  );
  $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>',
    '#states' => array(
      'disabled' => array(
        'select[name="download_limit_duration_granularity"]' => array(
          'value' => 'never',
        ),
      ),
      'visible' => array(
        'input[name="time_override"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $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>',
    '#states' => array(
      'visible' => array(
        'input[name="time_override"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  return $form;
}