You are here

function uc_recurring_subscription_product_form_submit 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_submit()

File

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

Code

function uc_recurring_subscription_product_form_submit(&$form, &$form_state) {
  global $user;
  switch ($form_state['values']['op']) {
    case 'Delete':
      node_delete($form_state['values']['product_id']);
      break;
    default:

      // Save
      if (empty($form_state['values']['product_id'])) {
        $node = new stdClass();
        $node->type = variable_get('uc_recurring_subscription_product_class', 'uc_recurring_subscription');
        $node->uid = $user->uid;
        $node->status = 1;
        $new = TRUE;
        node_save($node);
      }
      else {
        $node = node_load($form_state['values']['product_id']);
        if ($node->type != variable_get('uc_recurring_subscription_product_class', 'uc_recurring_subscription')) {
          $node->type = variable_get('uc_recurring_subscription_product_class', 'uc_recurring_subscription');
        }
        $new = FALSE;
      }
      if (!empty($node)) {
        $node->title = $form_state['values']['title'];
        if (isset($node->body)) {
          $node->body['und'][0]['value'] = $form_state['values']['body'];
          $node->body['und'][0]['format'] = empty($node->body['und'][0]['format']) ? 'filtered_html' : $node->body['und'][0]['format'];
          $node->body['und'][0]['safe'] = _filter_html($node->body['und'][0]['value'], $node->body['und'][0]['format']);
        }

        //$node->list_price = $node->sell_price = $form_state['values']['recurring'][0]['amount'];

        // Product attribute.
        $attribute = uc_attribute_load(variable_get('uc_recurring_subscription_attribute', ''), $node->nid, 'product');

        // We are going to delete and recreate the product option and adjustments.
        uc_attribute_subject_delete($attribute->aid, 'product', $node->nid);
        uc_attribute_adjustments_delete(array(
          'nid' => $node->nid,
        ));

        // Create product attribute without the options.
        uc_attribute_subject_save($attribute, 'product', $node->nid);
        $subscription = uc_recurring_subscription_load($node->nid);
        if (empty($subscription)) {
          $subscription = new stdClass();
          $subscription->nid = $node->nid;
          $subscription->new = TRUE;
        }
        if (!empty($form_state['values']['access'][0])) {
          foreach ($form_state['values']['access'][0] as $access => $value) {
            $subscription->access[$access] = $value;
          }
        }
        $key = $subscription->new == TRUE ? NULL : 'nid';
        drupal_write_record('uc_recurring_subscription', $subscription, $key);

        // add/update the recurring features
        $features = array();
        if (isset($form_state['values']['recurring_intervals']['default_payment'])) {
          $default_payment = $form_state['values']['recurring_intervals']['default_payment'];
          unset($form_state['values']['recurring_intervals']['default_payment']);
        }
        foreach ($form_state['values']['recurring_intervals'] as $index => $value) {
          if (!is_numeric($index)) {
            continue;
          }
          $new_feature = FALSE;
          if (!empty($value['pfid'])) {
            $product = uc_recurring_product_fee_load($value['pfid']);
          }
          else {
            $product = new stdClass();
            $product->nid = $node->nid;
            uc_recurring_product_feature_save($product);

            // We need a new pfid
          }
          $product->fee_amount = $value['amount'];
          $product->initial_charge = $value['interval_value'] . ' ' . $value['interval_unit'];
          $product->regular_interval = $value['interval_value'] . ' ' . $value['interval_unit'];

          // If number intervals is negative, it means that it's unlimited intervals.
          $product->number_intervals = !empty($value['number_intervals']) ? $value['number_intervals'] : -1;

          // Create a model that hopefully will be unique.
          $model = 'sub-' . $node->nid . '-' . $product->pfid;

          // get option or create if it doesn't exist.
          $num = $value['interval_value'];
          $unit = $value['interval_unit'];
          $option_name = _uc_recurring_subscription_create_attribute_option($num, $unit);
          $option = _uc_recurring_subscription_attribute_option_by_name($attribute->aid, $option_name);

          // Set the product option.
          $option->nid = $node->nid;
          $option->oid = $option->oid;
          $option->ordering = $value['weight'];
          $option->price = $value['amount'];
          $attribute->options[$option->oid] = $option;

          // Set the default product options.
          if (isset($default_payment) && $index == $default_payment) {
            $attribute->default_option = $option->oid;
            $node->model = $model;
            $node->sell_price = $product->fee_amount;
          }

          // Set the product adjustments.
          $adj = new stdClass();
          $adj->nid = $node->nid;
          $adj->combination = serialize(array(
            $attribute->aid => $option->oid,
          ));
          $adj->model = $model;
          drupal_write_record('uc_product_adjustments', $adj);

          // If the model has changed we save the feature again.
          if ($model != $product->model) {
            $product->model = $model;
          }
          $features[$product->pfid] = $product;
        }

        // If there is only one option then don't make selection required.
        $attribute->required = count($form_state['values']['recurring_intervals']) <= 1 ? 0 : 1;

        // Process trial information.
        $trial_amount = $form_state['values']['trial_amount'];
        $trial_value = intval($form_state['values']['trial_interval_value']);
        $trial_unit = $form_state['values']['trial_interval_unit'];
        if ($trial_value > 0) {
          $node->sell_price = $trial_amount;
        }

        // We need to calculate the option prices adjustments.
        foreach ($attribute->options as $index => $option) {
          if ($trial_value > 0) {
            $attribute->options[$index]->price = 0;
          }
          else {
            $attribute->options[$index]->price = $option->price - $node->sell_price;
          }
        }

        // Save the product attribute and options.
        uc_attribute_subject_save($attribute, 'product', $node->nid, TRUE);

        // Save the product features.
        foreach ($features as $feature) {
          if ($trial_value > 0) {
            $feature->initial_charge = $trial_value . ' ' . $trial_unit;
          }
          $feature->nid = $node->nid;
          uc_recurring_product_feature_save($feature);
        }

        // Save the product/node.
        $node = node_submit($node);
        node_save($node);
      }
      break;
  }
  $form_state['redirect'] = 'admin/store/subscriptions';
}