You are here

function uc_recurring_subscription_product_form in UC Recurring Payments and Subscriptions 6.2

Same name and namespace in other branches
  1. 7.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
Implementation of hook_menu().

File

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

Code

function uc_recurring_subscription_product_form($form_state, $product_id = NULL) {
  drupal_add_js(drupal_get_path('module', 'uc_recurring_subscription') . '/uc_recurring_subscription.js', 'module');
  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);
  }
  $form['product'] = array(
    '#type' => 'fieldset',
    '#title' => 'Step One: Product Details',
  );
  $form['product']['title'] = array(
    '#type' => 'textfield',
    '#title' => 'Title',
    '#required' => TRUE,
    '#default_value' => $node->title,
  );
  $form['product']['body'] = array(
    '#type' => 'textarea',
    '#title' => 'Description',
    '#default_value' => $node->body,
  );
  $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.
  $aid = variable_get('uc_recurring_subscription_attribute', '');
  if (!empty($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' => $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' => $trial_product->initial_charge_unit,
    '#prefix' => '<div class="subscription-interval-period">',
    '#suffix' => '</div>',
  );
  $form['recurring']['add'] = array(
    '#type' => 'button',
    '#value' => 'Add a payment option',
    '#ahah' => array(
      'event' => 'click',
      'path' => 'subscriptions/ahah/add_interval',
      'wrapper' => 'recurring_intervals',
      'method' => 'replace',
      'effect' => 'fade',
      'progress' => array(
        'type' => 'bar',
      ),
    ),
    '#prefix' => '<div style="clear:both"></div>',
  );
  $form['recurring']['recurring_intervals'] = array(
    '#value' => t(' '),
    '#prefix' => '<div id="recurring_intervals">',
    '#suffix' => '</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;
    foreach ($products as $product) {
      $form['recurring']['recurring_intervals'][$delta] = _uc_recurring_subscription_add_interval_form($delta, $product);
      $default_options[$delta] = '';
      if (isset($product) && $product->option->default_option) {
        $default_option = $delta;
      }
      $delta++;
    }
  }
  $form['recurring']['recurring_intervals']['default_payment'] = array(
    '#type' => 'radios',
    '#options' => $default_options,
    '#default_value' => $default_option,
  );
  $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' => $node->subscription->access['subscribe_grant'],
      '#attributes' => array(
        'class' => 'role-option',
      ),
      '#parents' => array(
        'access',
        0,
        'subscribe_grant',
      ),
    );
    $form['access']['role']['expire_grant'] = array(
      '#type' => 'select',
      '#options' => $roles,
      '#multiple' => TRUE,
      '#default_value' => $node->subscription->access['expire_grant'],
      '#attributes' => array(
        'class' => 'role-option',
      ),
      '#parents' => array(
        'access',
        0,
        'expire_grant',
      ),
    );
    $form['access']['role']['expire_revoke'] = array(
      '#type' => 'select',
      '#options' => $roles,
      '#multiple' => TRUE,
      '#default_value' => $node->subscription->access['expire_revoke'],
      '#attributes' => array(
        'class' => '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',
        ),
      );
    }
  }

  /*
    $form['email'] = array(
      '#type' => 'fieldset',
      '#title' => 'Step Four: Email/Notifications',
      '#description' => t('Setup emails to be sent on various events.'),
    );
    $form['email']['#theme'] = 'uc_recurring_subscription_email_options';
    $form['email']['notication_event'] = array(
      '#type' => 'select',
      '#title' => t('Add new email on event'),
      '#options' => array(),
    );
    $form['email']['add_notification'] = array('#type' => 'submit', '#value' => t('Add email'));
  */
  if (!empty($node->nid) && $node->type != variable_get('uc_recurring_subscription_product_class', 'uc_recurring_subscription')) {
    return confirm_form($form, '', uc_referer_uri(), 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;
}