You are here

function uc_recurring_subscription_product_form in UC Recurring Payments and Subscriptions 7.2

Same name and namespace in other branches
  1. 6.2 modules/uc_recurring_subscription/uc_recurring_subscription.admin.inc \uc_recurring_subscription_product_form()

Form to add a new payment interval for the product.

1 string reference to 'uc_recurring_subscription_product_form'
uc_recurring_subscription_menu in modules/uc_recurring_subscription/uc_recurring_subscription.module
Implements hook_menu().

File

modules/uc_recurring_subscription/uc_recurring_subscription.admin.inc, line 113
Uc recurring subscription UI.

Code

function uc_recurring_subscription_product_form($form, $form_state, $product_id = NULL) {
  drupal_add_js(drupal_get_path('module', 'uc_recurring_subscription') . '/uc_recurring_subscription.js');
  drupal_add_css(drupal_get_path('module', 'uc_recurring_subscription') . '/uc_recurring_subscription.css');
  $form = array();
  $products = array();
  if (isset($product_id)) {
    $form['product_id'] = array(
      '#type' => 'hidden',
      '#value' => $product_id,
    );
    $node = node_load($product_id);
    $products = _uc_recurring_subscription_get_product_features($node->nid);
  }
  else {
    $node = new stdClass();
    $node->title = '';
    $node->body = '';
    $node->attributes = array();
    $node->sell_price = '0';

    //     $node->x = '';
  }
  $form['product'] = array(
    '#type' => 'fieldset',
    '#title' => 'Step One: Product Details',
  );
  $form['product']['title'] = array(
    '#type' => 'textfield',
    '#title' => 'Title',
    '#required' => TRUE,
    '#default_value' => $node->title,
  );
  if (isset($node->body)) {
    $form['product']['body'] = array(
      '#type' => 'textarea',
      '#title' => 'Description',
    );
  }
  $form['recurring'] = array(
    '#type' => 'fieldset',
    '#title' => 'Step Two: Define Subscription Details',
    '#description' => t('By adding multiple payment options, users will be given the option to choose from one of these payment options, this allows you to specify different prices and payment plans for this subscription product'),
  );
  $form['recurring']['trial'] = array(
    '#type' => 'fieldset',
    '#title' => 'Add trial',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  // We look at the default option for the trial information.
  $trial_product = new stdClass();
  $aid = variable_get('uc_recurring_subscription_attribute', '');
  if (!empty($aid) && isset($node->attributes[$aid])) {
    $default_option = $node->attributes[$aid]->default_option;
    $name = $node->attributes[$aid]->options[$default_option]->name;
    foreach ($products as $product) {
      $num = $product->regular_interval_value;
      $unit = $product->regular_interval_unit;
      if ($name == _uc_recurring_subscription_create_attribute_option($num, $unit)) {

        // This is the default product, the price and initial interval should
        // be different if there is a trial.
        if ($node->sell_price != $product->fee_amount || $product->regular_interval_value != $product->initial_charge_value || $product->regular_interval_unit != $product->initial_charge_unit) {
          $trial_product = $product;
          $form['recurring']['trial']['#collapsed'] = FALSE;
        }
        break;
      }
    }
  }
  $form['recurring']['trial']['trial_amount'] = array(
    '#type' => 'textfield',
    '#title' => 'Trial Amount',
    '#default_value' => !empty($trial_product) ? $node->sell_price : '',
    '#size' => 20,
  );
  $form['recurring']['trial']['trial_interval_value'] = array(
    '#type' => 'textfield',
    '#default_value' => !empty($trial_product) && isset($trial_product->initial_charge_value) ? $trial_product->initial_charge_value : '',
    '#size' => 2,
    '#prefix' => '<div class="subscription-interval-value">',
    '#suffix' => '</div>',
  );
  $form['recurring']['trial']['trial_interval_unit'] = array(
    '#type' => 'select',
    '#options' => array(
      'days' => t('day(s)'),
      'weeks' => t('week(s)'),
      'months' => t('month(s)'),
      'years' => t('year(s)'),
    ),
    '#default_value' => !empty($trial_product) && isset($trial_product->initial_charge_unit) ? $trial_product->initial_charge_unit : '',
    '#prefix' => '<div class="subscription-interval-period">',
    '#suffix' => '</div>',
  );
  $form['recurring']['add'] = array(
    '#type' => 'submit',
    '#value' => 'Add a payment option',
    '#submit' => array(
      'uc_recurring_subscription_add_interval',
    ),
    '#ajax' => array(
      'event' => 'click',
      'callback' => 'uc_recurring_subscription_add_interval_callback',
      'wrapper' => 'recurring_intervals',
      'method' => 'replace',
      'effect' => 'fade',
      'progress' => array(
        'type' => 'bar',
      ),
    ),
    '#prefix' => '<div style="clear:both"></div>',
  );
  if (isset($form_state['recurring_count'])) {
    $recurring_count = $form_state['recurring_count'];
  }
  else {
    $recurring_count = count($products);
  }
  $form['recurring']['recurring_intervals']['#theme'] = 'uc_recurring_subscription_item';
  if ($recurring_count > 0) {
    $delta = 0;
    $form['recurring']['recurring_intervals'] = _uc_recurring_subscription_add_interval_form($recurring_count, $products);
  }
  $form['recurring']['recurring_intervals']['#value'] = t(' ');
  $form['recurring']['recurring_intervals']['#prefix'] = '<div id="recurring_intervals">';
  $form['recurring']['recurring_intervals']['#suffix'] = '</div>';
  $form['access'] = array(
    '#type' => 'fieldset',
    '#title' => 'Step Three: Access & Permissions',
    '#description' => t('In this section you can define the access that is granted or removed on subscription creation, renewal and expiration events.'),
  );

  // If uc_roles is enabled we will use the settings from this.
  $roles = array();
  if (module_exists('uc_roles')) {
    $roles = _uc_roles_get_choices();
  }
  else {
    if (user_access('administer permissions')) {
      $roles = user_roles(TRUE);
      unset($roles[DRUPAL_AUTHENTICATED_RID]);
    }
  }
  $form['access']['role'] = array(
    '#type' => 'fieldset',
    '#title' => 'Roles',
    '#description' => t('Select the role(s) which are assigned to members who subscribe to this subscription product and then removed when the subscription expires.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  if (empty($roles)) {
    $form['access']['role']['message'] = array(
      '#value' => t('You need to first add a user role <a href="@role_link">here</a>.', array(
        '@role_link' => url('admin/user/roles'),
      )),
    );
  }
  else {
    $form['access']['role']['#theme'] = 'uc_recurring_subscription_role_items';
    $form['access']['role']['subscribe_grant'] = array(
      '#type' => 'select',
      '#options' => $roles,
      '#multiple' => TRUE,
      '#default_value' => isset($node->subscription->access) ? $node->subscription->access['subscribe_grant'] : '',
      '#attributes' => array(
        'class' => array(
          'role-option',
        ),
      ),
      '#parents' => array(
        'access',
        0,
        'subscribe_grant',
      ),
    );
    $form['access']['role']['expire_grant'] = array(
      '#type' => 'select',
      '#options' => $roles,
      '#multiple' => TRUE,
      '#default_value' => isset($node->subscription->access) ? $node->subscription->access['expire_grant'] : '',
      '#attributes' => array(
        'class' => array(
          'role-option',
        ),
      ),
      '#parents' => array(
        'access',
        0,
        'expire_grant',
      ),
    );
    $form['access']['role']['expire_revoke'] = array(
      '#type' => 'select',
      '#options' => $roles,
      '#multiple' => TRUE,
      '#default_value' => isset($node->subscription->access) ? $node->subscription->access['expire_revoke'] : '',
      '#attributes' => array(
        'class' => array(
          'role-option',
        ),
      ),
      '#parents' => array(
        'access',
        0,
        'expire_revoke',
      ),
    );
  }
  if (module_exists('og_actions')) {
    $groups = og_all_groups_options();
    if (count($groups)) {
      $form['access']['og'] = array(
        '#type' => 'fieldset',
        '#title' => 'Organic Groups',
        '#description' => t('Select which organic groups a user should be subscribed to on a new subscripion purchased or unsubscribed when the subscription expires.'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#theme' => 'uc_recurring_subscription_og_items',
      );
      $form['access']['og']['subscribe_grant_og'] = array(
        '#type' => 'select',
        '#default_value' => $node->subscription->access['subscribe_grant_og'],
        '#options' => $groups,
        '#multiple' => TRUE,
        '#parents' => array(
          'access',
          0,
          'subscribe_grant_og',
        ),
      );
      $form['access']['og']['expire_grant_og'] = array(
        '#type' => 'select',
        '#default_value' => $node->subscription->access['expire_grant_og'],
        '#options' => $groups,
        '#multiple' => TRUE,
        '#parents' => array(
          'access',
          0,
          'expire_grant_og',
        ),
      );
      $form['access']['og']['expire_revoke_og'] = array(
        '#type' => 'select',
        '#default_value' => $node->subscription->access['expire_revoke_og'],
        '#options' => $groups,
        '#multiple' => TRUE,
        '#parents' => array(
          'access',
          0,
          'expire_revoke_og',
        ),
      );
    }
  }
  if (!empty($node->nid) && $node->type != variable_get('uc_recurring_subscription_product_class', 'uc_recurring_subscription')) {
    return confirm_form($form, '', $_SERVER['HTTP_REFERER'], t('This product is not a subscription product, saving this form will convert this product to a subscription product, which could result in data being lost if you have specified custom fields.'), t('Save product and convert to a subscription'), t('Cancel'));
  }
  else {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
  }
  if (!isset($product_id)) {
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  return $form;
}