You are here

uc_atctweaks.module in UC Add to Cart Tweaks 6

Same filename and directory in other branches
  1. 7 uc_atctweaks.module

Defines a product feature to tweak the add to cart form behavior.

File

uc_atctweaks.module
View source
<?php

/**
 * @file
 * Defines a product feature to tweak the add to cart form behavior.
 */

/**
 * Implementation of hook_form_alter().
 *
 * Summary of alterations:
 * 1) Alters the product feature add form to restrict multiple Add to Cart
 *      Tweak features from being added to a single product.
 * 2) Alters the add to cart form for variable priced products.
 * 3) Alter the product class form to set default add to cart tweaks.
 */
function uc_atctweaks_form_alter(&$form, &$form_state, $form_id) {

  // 1) Alter the product feature add form.
  if ($form_id == 'uc_product_feature_add_form') {

    // If an add to cart tweak has already been added to this product...
    if (db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", arg(1), 'atctweaks'))) {

      // Remove Add to Cart Tweak from the available list of features to add.
      unset($form['feature']['#options']['atctweaks']);
    }
  }

  // 2) Alters the add to cart form for variable priced products.
  if (strpos($form_id, 'uc_product_add_to_cart_form_') === 0) {
    $data = uc_atctweaks_product_load($form['nid']['#value']);
    if ($data) {
      $form['atctweaks_data'] = array(
        '#type' => 'value',
        '#value' => $data,
      );
      $form['#submit'] = array_merge(array(
        'uc_atctweaks_add_to_cart_pre_submit',
      ), $form['#submit']);
      $form['#submit'][] = 'uc_atctweaks_add_to_cart_post_submit';
    }
  }

  // 3) Alter the product class form to set default add to cart tweaks.
  if ($form_id == 'uc_product_class_form') {
    $data = variable_get('ucatc_class_def_' . $form['pcid']['#value'], array());
    if (!empty($data)) {
      $data = (object) unserialize($data);
    }
    else {
      $data = FALSE;
    }
    $form['atctweaks'] = array(
      '#type' => 'fieldset',
      '#title' => t('Default Add to Cart Tweaks'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 5,
    );
    $form['atctweaks']['default_atctweaks'] = array(
      '#type' => 'checkbox',
      '#title' => t('Check this box to add default Add to Cart Tweaks to every product of this class using these settings.'),
      '#default_value' => $data === FALSE ? FALSE : TRUE,
    );
    $form['atctweaks'] += _uc_atctweaks_feature_form($data);
    $form['#submit'][] = 'uc_atctweaks_product_class_submit';
    $form['submit']['#weight'] = 10;
  }
}

// Submit handler for the add to cart form to process after the normal handler.
function uc_atctweaks_add_to_cart_pre_submit($form, &$form_state) {
  if (!empty($form_state['values']['atctweaks_data'])) {
    $data = $form_state['values']['atctweaks_data'];

    // Empty the shopping cart if the feature specifies to do so.
    if ($data->cart_empty) {
      uc_cart_empty(uc_cart_get_id());
    }
  }
}

// Submit handler for the add to cart form to process after the normal handler.
function uc_atctweaks_add_to_cart_post_submit($form, &$form_state) {
  if (!empty($form_state['values']['atctweaks_data'])) {
    $data = $form_state['values']['atctweaks_data'];
    switch ($data->redirect) {
      case 'cart':
        $form_state['redirect'] = 'cart';
        break;
      case 'checkout':
        $form_state['redirect'] = 'cart/checkout';
        break;
      case 'none':
        $form_state['redirect'] = 'node/' . $form_state['values']['nid'];
        break;
      case 'global':
      default:
        break;
    }
  }
}

// Submit handler for the product class form for default Variable Price features.
function uc_atctweaks_product_class_submit($form, &$form_state) {
  if ($form_state['values']['default_atctweaks']) {
    $data = array(
      'cart_empty' => $form_state['values']['cart_empty'],
      'redirect' => $form_state['values']['redirect'],
    );
    variable_set('ucatc_class_def_' . $form_state['values']['pcid'], serialize($data));
  }
  else {
    variable_del('ucatc_class_def_' . $form_state['values']['pcid']);
  }
}

/**
 * Implementation of hook_nodeapi().
 *
 * Summary of alterations:
 * 1) Inserts add to cart tweak product feature on product node creation.
 */
function uc_atctweaks_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  // When a product node is created...
  if ($op == 'insert' && uc_product_is_product($node)) {
    $data = variable_get('ucatc_class_def_' . $node->type, array());

    // If the product class has default Add to Cart Tweaks...
    if ($data) {

      // Prepare the data as if it were from a form submission.
      $data = unserialize($data);
      $data['nid'] = $node->nid;
      $data['pfid'] = '';
      $form_state = array(
        'values' => $data,
      );

      // Add the feature to the product by spoofing the normal form submission.
      uc_atctweaks_feature_form_submit(array(), $form_state);
    }
  }
}

/**
 * Implementation of hook_product_feature().
 */
function uc_atctweaks_product_feature() {
  $features = array();
  $features[] = array(
    'id' => 'atctweaks',
    'title' => t('Add to cart tweaks'),
    'callback' => 'uc_atctweaks_feature_form',
    'delete' => 'uc_atctweaks_feature_delete',
    'multiple' => FALSE,
  );
  return $features;
}

// Settings form for individual Add to Cart Tweaks.
function uc_atctweaks_feature_form($form_state, $node, $feature) {
  $form = array();

  // Load the Add to Cart Tweaks specific to this product.
  $data = db_fetch_object(db_query("SELECT * FROM {uc_atctweaks_products} WHERE pfid = %d", $feature['pfid']));
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $node->nid,
  );
  $form['pfid'] = array(
    '#type' => 'value',
    '#value' => $data ? $data->pfid : '',
  );
  $form += _uc_atctweaks_feature_form($data);
  return uc_product_feature_form($form);
}
function _uc_atctweaks_feature_form($data = FALSE) {
  $form = array();
  $form['cart_empty'] = array(
    '#type' => 'checkbox',
    '#title' => t('Empty the shopping cart of all other items when this product is added to it.'),
    '#default_value' => $data ? $data->cart_empty : FALSE,
  );
  $form['redirect'] = array(
    '#type' => 'radios',
    '#title' => t('Add to cart form redirect'),
    '#options' => array(
      'global' => t('Global default'),
      'cart' => t('Cart view page'),
      'checkout' => t('Checkout form'),
      'none' => t('No redirect'),
    ),
    '#default_value' => $data ? $data->redirect : 'global',
  );
  return $form;
}
function uc_atctweaks_feature_form_submit($form, &$form_state) {

  // Build an array of Add to Cart Tweaks data from the form submission.
  $vp_data = array(
    'pfid' => $form_state['values']['pfid'],
    'cart_empty' => $form_state['values']['cart_empty'],
    'redirect' => $form_state['values']['redirect'],
  );

  // Build the product feature description.
  $tweaks_form = _uc_atctweaks_feature_form();
  $description = array(
    t('<b>Add to cart form redirect:</b> @redirect', array(
      '@redirect' => $tweaks_form['redirect']['#options'][$vp_data['redirect']],
    )),
  );
  if ($vp_data['cart_empty']) {
    $description[] = t('The cart will be emptied prior to adding this item to it.');
  }

  // Save the basic product feature data.
  $data = array(
    'pfid' => $vp_data['pfid'],
    'nid' => $form_state['values']['nid'],
    'fid' => 'atctweaks',
    'description' => implode('<br />', $description),
  );
  $form_state['redirect'] = uc_product_feature_save($data);

  // Insert or update the data in the Variable Price products table.
  if (empty($data['pfid'])) {
    $vp_data['pfid'] = db_last_insert_id('uc_product_features', 'pfid');
    $key = NULL;
  }
  else {
    $vp_data['vpid'] = db_result(db_query("SELECT vpid FROM {uc_atctweaks_products} WHERE pfid = %d", $data['pfid']));
    $key = 'vpid';
  }
  drupal_write_record('uc_atctweaks_products', $vp_data, $key);
}

// Add to Cart Tweaks product feature delete function.
function uc_atctweaks_feature_delete($feature) {
  db_query("DELETE FROM {uc_atctweaks_products} WHERE pfid = %d", $feature['pfid']);
}

// Load the product feature data for a given node.
function uc_atctweaks_product_load($nid) {
  return db_fetch_object(db_query("SELECT atc.* FROM {uc_product_features} AS pf LEFT JOIN {uc_atctweaks_products} AS atc ON pf.pfid = atc.pfid WHERE pf.fid = 'atctweaks' AND pf.nid = %d", $nid));
}