You are here

function uc_recurring_feature_form_submit in UC Recurring Payments and Subscriptions 6

File

./uc_recurring.module, line 558
Allows you to add a recurring fee to a product/SKU to handle subscription type services.

Code

function uc_recurring_feature_form_submit($form, &$form_state) {

  // Use the form specified pfid if available.
  if (!empty($form_state['values']['pfid'])) {
    $pfid = $form_state['values']['pfid'];
  }

  // Build the recurring fee's data array.
  $fee = array(
    'pfid' => $pfid,
    'model' => $form_state['values']['model'],
    'fee_amount' => $form_state['values']['fee_amount'],
    'initial_charge' => $form_state['values']['initial_charge_value'] . ' ' . $form_state['values']['initial_charge_unit'],
    'regular_interval' => $form_state['values']['regular_interval_value'] . ' ' . $form_state['values']['regular_interval_unit'],
    'number_intervals' => intval($form_state['values']['number_intervals']),
  );
  $context = array(
    'revision' => 'formatted-original',
    'location' => 'recurring-feature-submit',
  );
  $args = array(
    '@product' => empty($fee['model']) ? t('this product') : t('product @model', array(
      '@model' => $fee['model'],
    )),
    '!amount' => uc_price($fee['fee_amount'], $context),
    '!initial' => $fee['initial_charge'],
    '!regular' => $fee['regular_interval'],
    '!intervals' => t('!num times', array(
      '!num' => $fee['number_intervals'] - 1,
    )),
  );

  // Build the feature's data array.
  $data = array(
    'pfid' => $pfid,
    'nid' => $form_state['values']['nid'],
    'fid' => 'recurring',
    'description' => t('When @product is purchased, add a fee for !amount charged first after !initial and every !regular after that !intervals.', $args),
  );

  // Save the product feature and store the returned URL as our redirect.
  $form_state['redirect'] = uc_product_feature_save($data);
  if (empty($pfid)) {
    $fee['pfid'] = db_last_insert_id('uc_product_features', 'pfid');
  }
  uc_recurring_fee_save('product', $fee);
}