You are here

function uc_file_feature_form_submit in Ubercart 5

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

File

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

Code

function uc_file_feature_form_submit($form_id, $form_values) {
  global $user;
  $fid = db_result(db_query("SELECT fid FROM {uc_files} WHERE filename = '%s'", $form_values['uc_file_filename']));

  //Build product feature descriptions
  $description = empty($form_values['uc_file_model']) ? t('<strong>SKU:</strong> Any<br />') : t('<strong>SKU:</strong> !sku<br />', array(
    '!sku' => $form_values['uc_file_model'],
  ));
  if (is_dir(variable_get('uc_file_base_dir', NULL) . "/" . $form_values['uc_file_filename'])) {
    $description .= t('<strong>Directory:</strong> !dir<br />', array(
      '!dir' => $form_values['uc_file_filename'],
    ));
  }
  else {
    $description .= t('<strong>File:</strong> !file<br />', array(
      '!file' => basename($form_values['uc_file_filename']),
    ));
  }
  $shippable = $form_values['uc_file_shippable'] ? 1 : 0;
  $description .= $shippable ? t('<strong>Shippable:</strong> Yes') : t('<strong>Shippable:</strong> No');

  //Insert or update uc_file_product table
  if (empty($form_values['pfid'])) {
    $pfid = db_next_id('{uc_product_features}_pfid');
  }
  else {
    $pfid = $form_values['pfid'];
    db_query("DELETE FROM {uc_file_products} WHERE pfid = %d", $pfid);
  }
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      db_query("INSERT INTO {uc_file_products} (pfid, fid, model, description, shippable) VALUES (%d, %d, '%s', '%s', %d)", $pfid, $fid, $form_values['uc_file_model'], $form_values['uc_file_description'], $shippable);
      break;
    case 'pgsql':
      db_query("INSERT INTO {uc_file_products} (pfid, fid, model, description, shippable) VALUES (%d, %d, '%s', '%s', '%d')", $pfid, $fid, $form_values['uc_file_model'], $form_values['uc_file_description'], $shippable);
      break;
  }
  $data = array(
    'pfid' => $pfid,
    'nid' => $form_values['nid'],
    'fid' => 'file',
    'description' => $description,
  );
  return uc_product_feature_save($data);
}