You are here

function uc_roles_feature_form in Ubercart 5

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

Form builder for hook_product_feature

1 string reference to 'uc_roles_feature_form'
uc_roles_product_feature in uc_roles/uc_roles.module
Implementation of hook_product_feature().

File

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

Code

function uc_roles_feature_form($node, $feature) {
  uc_add_js('$(document).ready(function() { if ($("#edit-uc-roles-granularity").val() == "never") {$("#edit-uc-roles-qty").attr("disabled", "disabled").val("");} });', 'inline');
  $models = array(
    NULL => t('Any'),
    $node->model => $node->model,
  );

  //Check if product adjustments exist and add models
  if (module_exists('uc_attribute')) {
    $adjustments = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = %d", $node->nid);
    while ($adjustment = db_fetch_object($adjustments)) {
      if (!in_array($adjustment->model, $models)) {
        $models[$adjustment->model] = $adjustment->model;
      }
    }
  }

  //Check if editing or adding to set default values
  if (!empty($feature)) {
    $product_role = db_fetch_object(db_query("SELECT * FROM {uc_roles_products} WHERE pfid = %d", $feature['pfid']));
    $default_model = $product_role->model;
    $default_role = $product_role->rid;
    $default_qty = $product_role->duration;
    $default_granularity = $product_role->granularity;
    $default_shippable = $product_role->shippable;
    $default_by_quantity = $product_role->by_quantity;
    $form['pfid'] = array(
      '#type' => 'value',
      '#value' => $feature['pfid'],
    );
  }
  else {
    $default_model = 0;
    $default_role = variable_get('uc_roles_default_role', NULL);
    $default_qty = variable_get('uc_roles_default_granularity', 'never') == 'never' ? NULL : variable_get('uc_roles_default_length', NULL);
    $default_granularity = variable_get('uc_roles_default_granularity', 'never');
    $default_shippable = $node->shippable;
    $default_by_quantity = 1;
  }
  $form['title'] = array(
    '#type' => 'markup',
    '#value' => '<h2>' . t('Role assignment') . '</h2>',
  );
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $node->nid,
  );
  $form['uc_roles_model'] = array(
    '#type' => 'select',
    '#title' => t('Model/SKU'),
    '#default_value' => $default_model,
    '#description' => t('This is the model/SKU of the product that will grant the role.'),
    '#options' => $models,
  );
  $form['uc_roles_role'] = array(
    '#type' => 'select',
    '#title' => t('Role'),
    '#default_value' => $default_role,
    '#description' => t('This is the role the customer will receive after purchasing the product.'),
    '#options' => _get_role_choices(),
  );
  $form['uc_roles_qty'] = array(
    '#type' => 'textfield',
    '#title' => t('Time until expiration'),
    '#default_value' => $default_qty,
    '#size' => 4,
    '#maxlength' => 4,
    '#prefix' => '<div class="expiration">',
    '#suffix' => '</div>',
  );
  $form['uc_roles_granularity'] = array(
    '#type' => 'select',
    '#options' => array(
      'never' => t('never'),
      'day' => t('day(s)'),
      'week' => t('week(s)'),
      'month' => t('month(s)'),
      'year' => t('year(s)'),
    ),
    '#default_value' => $default_granularity,
    '#attributes' => array(
      //Javascript to disable qty on never select
      'onchange' => 'if (this.value == "never") {$("#edit-uc-roles-qty").attr("disabled", "disabled").val("");} else {$("#edit-uc-roles-qty").removeAttr("disabled");}',
    ),
    '#description' => t('This will set how long the specified role will last until it expires.'),
    '#prefix' => '<div class="expiration">',
    '#suffix' => '</div>',
  );
  $form['uc_roles_shippable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Shippable product'),
    '#default_value' => $default_shippable,
    '#description' => t('Check if this product model/SKU that uses role assignment is associated with a shippable product.'),
  );
  $form['uc_roles_by_quantity'] = array(
    '#type' => 'checkbox',
    '#title' => t('Multiply by quantity'),
    '#default_value' => $default_by_quantity,
    '#description' => t('Check if the role duration should be multiplied by the quantity purchased.'),
  );
  return uc_product_feature_form($form);
}