You are here

function uc_roles_feature_form_submit in Ubercart 5

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

File

uc_roles/uc_roles.module, line 656
Grants roles upon accepted payment of products

Code

function uc_roles_feature_form_submit($form_id, $form_values) {
  $description = empty($form_values['uc_roles_model']) ? t('<strong>SKU:</strong> Any<br />') : t('<strong>SKU:</strong> !sku<br />', array(
    '!sku' => $form_values['uc_roles_model'],
  ));
  $description .= t('<strong>Role:</strong> !role_name<br />', array(
    '!role_name' => _get_role_name($form_values['uc_roles_role']),
  ));
  switch ($form_values['uc_roles_granularity']) {
    case 'never':
      $description .= t('<strong>Expiration:</strong> never<br />');
      break;
    case 'day':
      $description .= t('<strong>Expiration:</strong> !qty day(s)<br />', array(
        '!qty' => $form_values['uc_roles_qty'],
      ));
      break;
    case 'week':
      $description .= t('<strong>Expiration:</strong> !qty week(s)<br />', array(
        '!qty' => $form_values['uc_roles_qty'],
      ));
      break;
    case 'month':
      $description .= t('<strong>Expiration:</strong> !qty month(s)<br />', array(
        '!qty' => $form_values['uc_roles_qty'],
      ));
      break;
    case 'year':
      $description .= t('<strong>Expiration:</strong> !qty year(s)<br />', array(
        '!qty' => $form_values['uc_roles_qty'],
      ));
      break;
    default:
      break;
  }
  $description .= $form_values['uc_roles_shippable'] ? t('<strong>Shippable:</strong> Yes<br />') : t('<strong>Shippable:</strong> No<br />');
  $shippable = $form_values['uc_roles_shippable'] ? 1 : 0;
  $description .= $form_values['uc_roles_by_quantity'] ? t('<strong>Multiply by Quantity:</strong> Yes') : t('<strong>Multiply by Quantity:</strong> No');
  $by_quantity = $form_values['uc_roles_by_quantity'] ? 1 : 0;
  $granularity = $form_values['uc_roles_granularity'] != 'never' ? $form_values['uc_roles_granularity'] : NULL;
  $duration = $form_values['uc_roles_granularity'] != 'never' ? $form_values['uc_roles_qty'] : NULL;
  $model = !is_null($form_values['uc_roles_model']) ? $form_values['uc_roles_model'] : NULL;

  //Insert or update uc_roles_products table
  if ($form_values['pfid'] == 0) {
    $pfid = db_next_id('{uc_product_features}_pfid');
  }
  else {
    $pfid = $form_values['pfid'];
    db_query("DELETE FROM {uc_roles_products} WHERE pfid = %d", $pfid);
  }
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      db_query("INSERT INTO {uc_roles_products} (pfid, nid, model, rid, duration, granularity, by_quantity, shippable) VALUES (%d, %d, '%s', %d, %d, '%s', %d, %d)", $pfid, $form_values['nid'], $model, $form_values['uc_roles_role'], $duration, $granularity, $by_quantity, $shippable);
      break;
    case 'pgsql':
      db_query("INSERT INTO {uc_roles_products} (pfid, nid, model, rid, duration, granularity, by_quantity, shippable) VALUES (%d, %d, '%s', %d, %d, '%s', '%d', '%d')", $pfid, $form_values['nid'], $model, $form_values['uc_roles_role'], $duration, $granularity, $by_quantity ? 't' : 'f', $shippable ? 't' : 'f');
      break;
  }
  $data = array(
    'pfid' => $pfid,
    'nid' => $form_values['nid'],
    'fid' => 'role',
    'description' => $description,
  );
  return uc_product_feature_save($data);
}