You are here

function uc_recurring_admin_edit_form_submit in UC Recurring Payments and Subscriptions 7.2

Same name and namespace in other branches
  1. 6.2 uc_recurring.admin.inc \uc_recurring_admin_edit_form_submit()
  2. 6 uc_recurring.admin.inc \uc_recurring_admin_edit_form_submit()

@todo Please document this function.

See also

http://drupal.org/node/1354

File

./uc_recurring.admin.inc, line 345
Recurring payments administration page callbacks and form builder functions.

Code

function uc_recurring_admin_edit_form_submit($form, &$form_state) {
  global $user;
  $rfid = $form_state['values']['rfid'];
  $fee = uc_recurring_fee_user_load($rfid);
  $member = user_load($fee->uid);

  // There are different interval settings for recurring orders & products.
  $interval = isset($form_state['values']['regular_interval']) ? $form_state['values']['regular_interval'] : $form_state['values']['regular_interval_value'] . ' ' . $form_state['values']['regular_interval_unit'];
  $number_intervals = empty($form_state['values']['unlimited_intervals']) ? $form_state['values']['number_intervals'] : UC_RECURRING_UNLIMITED_INTERVALS;
  $fee_amount = $form_state['values']['fee_amount'];
  $fee_title = $form_state['values']['fee_title'];
  $fee_status = $form_state['values']['fee_status'];
  $next_charge = $fee->next_charge;
  if ($form_state['values']['reset_next_charge']) {
    $next_charge = strtotime('+' . $interval);
  }
  elseif (!empty($form_state['values']['next_charge_time'])) {
    $next_charge = strtotime($form_state['values']['next_charge_time']);
  }
  elseif (!empty($form_state['values']['next_charge_date'])) {
    extract($form_state['values']['next_charge_date']);
    $hour = date('H', $fee->next_charge);
    $min = date('i', $fee->next_charge);
    $sec = date('s', $fee->next_charge);
    $next_charge = mktime($hour, $min, $sec, $month, $day, $year);
  }
  $status = $fee->status;
  if ($fee->status != UC_RECURRING_FEE_STATUS_ACTIVE && $number_intervals != 0) {
    $status = UC_RECURRING_FEE_STATUS_ACTIVE;
  }

  // todo - replace with a drupal_write_record()
  db_update('uc_recurring_users')
    ->fields(array(
    'fee_title' => $fee_title,
    'fee_amount' => $fee_amount,
    'regular_interval' => $interval,
    'remaining_intervals' => $number_intervals,
    'next_charge' => $next_charge,
    'status' => $status,
  ))
    ->condition('rfid', $rfid)
    ->execute();
  $context = array(
    'revision' => 'themed-original',
    'location' => 'recurring-admin',
  );
  drupal_set_message(t('The changes to <a href="@user-link">@user</a> recurring fee @rfid (@title) has been saved. The next charge of !fee_amount will be on @date.', array(
    '@user' => $member->name,
    '@user-link' => url('user/' . $member->uid),
    '@rfid' => $rfid,
    '@title' => $fee_title,
    '!fee_amount' => theme('uc_price', array(
      'price' => $fee_amount,
    )),
    '@date' => format_date($next_charge),
  )));

  // Fee was already cancelled and now has been resumed, add a comment to the order.
  if ($fee->status != $status && $status == UC_RECURRING_FEE_STATUS_ACTIVE) {
    uc_order_comment_save($fee->order_id, $user->uid, t('<a href="@user-link">@user</a> resumed the recurring fee <a href="@fee-link">@fee</a>.', array(
      '@user-link' => url('user/' . $user->uid),
      '@user' => $user->name,
      '@fee-link' => url('admin/store/orders/recurring/view/fee/' . $rfid),
      '@fee' => $rfid,
    )));
  }

  // If the number of intervals was set to zero then also call cancel.
  if ($fee->status == UC_RECURRING_FEE_STATUS_ACTIVE && $number_intervals == 0) {
    uc_recurring_fee_cancel($rfid, $fee);
  }
}