You are here

function uc_file_feature_form_submit in Ubercart 7.3

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

Form submission handler for uc_file_feature_form().

See also

uc_file_feature_form()

uc_file_feature_form_submit()

File

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

Code

function uc_file_feature_form_submit($form, &$form_state) {
  global $user;

  // Build the file_product object from the form values.
  $file = uc_file_get_by_name($form_state['values']['uc_file_filename']);
  $file_product = array(
    'fid' => $file->fid,
    'filename' => $file->filename,
    'pfid' => $form_state['values']['pfid'],
    'model' => $form_state['values']['uc_file_model'],
    'description' => $form_state['values']['uc_file_description'],
    'shippable' => $form_state['values']['uc_file_shippable'],
    // Local limitations... set them if there's an override.
    'download_limit' => $form_state['values']['download_override'] ? $form_state['values']['download_limit_number'] : UC_FILE_LIMIT_SENTINEL,
    'address_limit' => $form_state['values']['location_override'] ? $form_state['values']['download_limit_addresses'] : UC_FILE_LIMIT_SENTINEL,
    'time_granularity' => $form_state['values']['time_override'] ? $form_state['values']['download_limit_duration_granularity'] : UC_FILE_LIMIT_SENTINEL,
    'time_quantity' => $form_state['values']['time_override'] ? $form_state['values']['download_limit_duration_qty'] : UC_FILE_LIMIT_SENTINEL,
  );

  // Build product feature descriptions.
  $description = t('<strong>SKU:</strong> !sku<br />', array(
    '!sku' => empty($file_product['model']) ? 'Any' : $file_product['model'],
  ));
  if (is_dir(variable_get('uc_file_base_dir', NULL) . "/" . $file_product['filename'])) {
    $description .= t('<strong>Directory:</strong> !dir<br />', array(
      '!dir' => $file_product['filename'],
    ));
  }
  else {
    $description .= t('<strong>File:</strong> !file<br />', array(
      '!file' => basename($file_product['filename']),
    ));
  }
  $description .= $file_product['shippable'] ? t('<strong>Shippable:</strong> Yes') : t('<strong>Shippable:</strong> No');
  $data = array(
    'pfid' => $file_product['pfid'],
    'nid' => $form_state['values']['nid'],
    'fid' => 'file',
    'description' => $description,
  );
  $form_state['redirect'] = uc_product_feature_save($data);

  // Updating the 'pfid' on $file_product and on $form_state for future use.
  $form_state['values']['pfid'] = $file_product['pfid'] = $data['pfid'];

  // Insert or update uc_file_product table.
  $key = array();
  if ($fpid = _uc_file_get_fpid($file_product['pfid'])) {
    $key = 'fpid';
    $file_product['fpid'] = $fpid;
  }
  drupal_write_record('uc_file_products', $file_product, $key);
}