You are here

function uc_roles_feature_form in Ubercart 7.3

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

Form builder for hook_uc_product_feature().

See also

uc_roles_feature_form_validate()

uc_roles_feature_form_submit()

1 string reference to 'uc_roles_feature_form'
uc_roles_uc_product_feature in uc_roles/uc_roles.module
Implements hook_uc_product_feature().

File

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

Code

function uc_roles_feature_form($form, &$form_state, $node, $feature) {
  $models = uc_product_get_models($node->nid);

  // Check if editing or adding to set default values.
  if (!empty($feature)) {
    $product_role = db_query("SELECT * FROM {uc_roles_products} WHERE pfid = :pfid", array(
      ':pfid' => $feature['pfid'],
    ))
      ->fetchObject();
    $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;
    if ($product_role->end_time) {
      $end_time = array(
        'day' => date('j', $product_role->end_time),
        'month' => date('n', $product_role->end_time),
        'year' => date('Y', $product_role->end_time),
      );
      $default_end_type = 'abs';
    }
    else {
      $temp = _uc_roles_get_expiration($default_qty, $default_granularity);
      $end_time = array(
        'day' => date('j', $temp),
        'month' => date('n', $temp),
        'year' => date('Y', $temp),
      );
      $default_end_type = 'rel';
    }
    $form['pfid'] = array(
      '#type' => 'value',
      '#value' => $feature['pfid'],
    );
    $form['rpid'] = array(
      '#type' => 'value',
      '#value' => $product_role->rpid,
    );
    $default_end_override = $product_role->end_override;
  }
  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 = variable_get('uc_roles_default_by_quantity', FALSE);
    $end_time = variable_get('uc_roles_default_end_time', array(
      'day' => date('j'),
      'month' => date('n'),
      'year' => date('Y'),
    ));
    $default_end_type = variable_get('uc_roles_default_end_expiration', 'rel');
    $default_end_override = FALSE;
  }
  $roles = _uc_roles_get_choices();
  if (!count($roles)) {

    // No actions can be done. Remove submit buttons.
    unset($form['buttons']);
    $form['no_roles'] = array(
      '#markup' => t('You need to <a href="!url">create new roles</a> before any can be added as product features.', array(
        '!url' => url('admin/people/permissions/roles', array(
          'query' => array(
            'destination' => 'admin/store/settings/products',
          ),
        )),
      )),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    );
    return $form;
  }
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $node->nid,
  );
  $form['uc_roles_model'] = array(
    '#type' => 'select',
    '#title' => t('SKU'),
    '#default_value' => $default_model,
    '#description' => t('This is the 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' => $roles,
  );
  $form['uc_roles_shippable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Shippable product'),
    '#default_value' => $default_shippable,
    '#description' => t('Check if this product SKU that uses role assignment is associated with a shippable product.'),
  );
  $form['end_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Override the <a href="!url">default role expiration</a>.', array(
      '!url' => url('admin/store/settings/products'),
    )),
    '#default_value' => $default_end_override,
  );
  $form['role_lifetime'] = array(
    '#type' => 'fieldset',
    '#title' => t('Role expiration'),
    '#states' => array(
      'visible' => array(
        'input[name="end_override"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['role_lifetime']['expiration'] = array(
    '#type' => 'select',
    '#title' => t('Expiration type'),
    '#options' => array(
      'rel' => t('Relative to purchase date'),
      'abs' => t('Fixed date'),
    ),
    '#default_value' => $default_end_type,
  );
  $form['role_lifetime']['uc_roles_expire_relative_duration'] = array(
    '#type' => 'textfield',
    '#default_value' => $default_qty,
    '#size' => 4,
    '#maxlength' => 4,
    '#prefix' => '<div class="expiration">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        'select[name="expiration"]' => array(
          'value' => 'rel',
        ),
      ),
      'invisible' => array(
        'select[name="uc_roles_expire_relative_granularity"]' => array(
          'value' => 'never',
        ),
      ),
    ),
  );
  $form['role_lifetime']['uc_roles_expire_relative_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,
    '#description' => t('From the time the role was purchased.'),
    '#prefix' => '<div class="expiration">',
    '#suffix' => '</div>',
    '#states' => array(
      'visible' => array(
        'select[name="expiration"]' => array(
          'value' => 'rel',
        ),
      ),
    ),
  );
  $form['role_lifetime']['absolute'] = array(
    '#type' => 'container',
    '#states' => array(
      'visible' => array(
        'select[name="expiration"]' => array(
          'value' => 'abs',
        ),
      ),
    ),
  );
  $form['role_lifetime']['absolute']['uc_roles_expire_absolute'] = array(
    '#type' => 'date',
    '#description' => t('Expire the role at the beginning of this day.'),
  );
  if ($end_time) {
    $form['role_lifetime']['absolute']['uc_roles_expire_absolute']['#default_value'] = $end_time;
  }
  $form['role_lifetime']['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 $form;
}