home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (uc_order.js)

This example Drupal source code file (uc_order.js) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

contents, customer-select, edit, false, function, hide_product_throbber, if, php, post_vars, return, show_product_throbber, type, url, var

The uc_order.js Drupal example source code

// $Id: uc_order.js,v 1.9.2.9 2009/09/21 14:34:46 islandusurper Exp $

/**
 * @file
 * Handles asynchronous requests for order editing forms.
 */

var customer_select = '';
var add_product_browser = '';
var order_save_holds = 0;

/**
 * Add the double click behavior to the order table at admin/store/orders.
 */
Drupal.behaviors.ucOrderClick = function(context) {
  $('.uc-orders-table tr.odd, .uc-orders-table tr.even:not(.ucOrderClick-processed)', context).addClass('ucOrderClick-processed').each(
    function() {
      $(this).dblclick(
        function() {
          var url = Drupal.settings.ucURL.adminOrders + this.id.substring(6);
          window.location = url;
        }
      );
    }
  );
}

/**
 * Add the submit behavior to the order form
 */
Drupal.behaviors.ucOrderSubmit = function(context) {
  $('#uc-order-edit-form:not(.ucOrderSubmit-processed)', context).addClass('ucOrderSubmit-processed').submit(
    function() {
      $('#products-selector').empty().removeClass();
      $('#delivery_address_select').empty().removeClass();
      $('#billing_address_select').empty().removeClass();
      $('#customer-select').empty().removeClass();
    }
  );
}

$(document).ready(
  function() {
    if (order_save_holds == 0) {
      release_held_buttons();
    }

    $('.uc-orders-table tr.odd, .uc-orders-table tr.even').each(
      function() {
        $(this).dblclick(
          function() {
            var url = Drupal.settings.ucURL.adminOrders + this.id.substring(6);
            window.location = url;
          }
        );
      }
    );

    $('#uc-order-edit-form').submit(
      function() {
        $('#products-selector').empty().removeClass();
        $('#delivery_address_select').empty().removeClass();
        $('#billing_address_select').empty().removeClass();
        $('#customer-select').empty().removeClass();
      }
    );
  }
);

/**
 * Copy the shipping data on the order edit screen to the corresponding billing
 * fields if they exist.
 */
function uc_order_copy_shipping_to_billing() {
  if ($('#edit-delivery-zone').html() != $('#edit-billing-zone').html()) {
    $('#edit-billing-zone').empty().append($('#edit-delivery-zone').children().clone());
  }

  $('#uc-order-edit-form input, select, textarea').each(
    function() {
      if (this.id.substring(0, 13) == 'edit-delivery') {
        $('#edit-billing' + this.id.substring(13)).val($(this).val());
      }
    }
  );
}

/**
 * Load the address book div on the order edit screen.
 */
function load_address_select(uid, div, address_type) {
  var options = {
    'uid'  : uid,
    'type' : address_type,
    'func' : "apply_address('" + address_type + "', this.value);"
  };

  $.post(Drupal.settings.ucURL.adminOrders + 'address_book', options,
         function (contents) {
           $(div).empty().addClass('address-select-box').append(contents);
         }
  );
}

/**
 * Apply the selected address to the appropriate fields in the order edit form.
 */
function apply_address(type, address_str) {
  eval('var address = ' + address_str + ';');
  $('#edit-' + type + '-first-name').val(address['first_name']);
  $('#edit-' + type + '-last-name').val(address['last_name']);
  $('#edit-' + type + '-phone').val(address['phone']);
  $('#edit-' + type + '-company').val(address['company']);
  $('#edit-' + type + '-street1').val(address['street1']);
  $('#edit-' + type + '-street2').val(address['street2']);
  $('#edit-' + type + '-city').val(address['city']);
  $('#edit-' + type + '-postal-code').val(address['postal_code']);

  if ($('#edit-' + type + '-country').val() != address['country']) {
    $('#edit-' + type + '-country').val(address['country']);
    try {
      uc_update_zone_select('edit-' + type + '-country', address['zone']);
    }
    catch (err) {}
  }

  $('#edit-' + type + '-zone').val(address['zone']);
}

/**
 * Close the address book div.
 */
function close_address_select(div) {
  $(div).empty().removeClass('address-select-box');
  return false;
}

/**
 * Load the customer select div on the order edit screen.
 */
function load_customer_search() {
  if (customer_select == 'search' && $('#customer-select #edit-back').val() == null) {
    return close_customer_select();
  }

  $.post(Drupal.settings.ucURL.adminOrders + 'customer', {},
         function (contents) {
           $('#customer-select').empty().addClass('customer-select-box').append(contents);
           $('#customer-select #edit-first-name').val($('#edit-billing-first-name').val());
           $('#customer-select #edit-last-name').val($('#edit-billing-last-name').val());
           customer_select = 'search';
         }
  );

  return false;
}

/**
 * Display the results of the customer search.
 */
function load_customer_search_results() {
  var first_name = $('#customer-select #edit-first-name').val();
  var last_name = $('#customer-select #edit-last-name').val();
  var email = $('#customer-select #edit-email').val();

  if (first_name == '') {
    first_name = '0';
  }
  if (last_name == '') {
    last_name = '0';
  }
  if (email == '') {
    email = '0';
  }

  $.post(Drupal.settings.ucURL.adminOrders + 'customer/search/' + encodeURIComponent(first_name) + '/' + encodeURIComponent(last_name) + '/' + encodeURIComponent(email),
         { },
         function (contents) {
           $('#customer-select').empty().append(contents);
         }
  );
  return false;
}

/**
 * Set customer values from search selection.
 */
function select_customer_search() {
  var data = $('#edit-cust-select').val();
  $('#edit-uid').val(data.substr(0, data.indexOf(':')));
  $('#edit-uid-text').val(data.substr(0, data.indexOf(':')));
  $('#edit-primary-email').val(data.substr(data.indexOf(':') + 1));
  $('#edit-primary-email-text').val(data.substr(data.indexOf(':') + 1));
  try {
    $('#edit-submit-changes').get(0).click();
  }
  catch (err) {
  }
  return close_customer_select();
}

/**
 * Display the new customer form.
 */
function load_new_customer_form() {
  if (customer_select == 'new') {
    return close_customer_select();
  }

  $.post(Drupal.settings.ucURL.adminOrders + 'customer/new', {},
         function (contents) {
           $('#customer-select').empty().addClass('customer-select-box').append(contents);
           customer_select = 'new';
         }
  );
  return false;
}

/**
 * Validate the customer's email address.
 */
function check_new_customer_address() {
  var options = {
    'email' : $('#customer-select #edit-email').val(),
    'sendmail' : $('#customer-select #edit-sendmail').attr('checked')
  };
  $.post(Drupal.settings.ucURL.adminOrders + 'customer/new/check/' + encodeURIComponent(options['email']), options,
         function (contents) {
           $('#customer-select').empty().append(contents);
         }
  );
  return false;
}

/**
 * Load existing customer as new order's customer.
 */
function select_existing_customer(uid, email) {
  $('#edit-uid').val(uid);
  $('#edit-uid-text').val(uid);
  $('#edit-primary-email').val(email);
  $('#edit-primary-email-text').val(email);
  try {
    $('#edit-submit-changes').click();
  }
  catch (err) {
  }
  return close_customer_select();
}

/**
 * Hide the customer selection form.
 */
function close_customer_select() {
  $('#customer-select').empty().removeClass('customer-select-box');
  customer_select = '';
  return false;
}

/**
 * Load the products div on the order edit screen.
 */
function uc_order_load_product_edit_div(order_id) {
  $(document).ready(
    function() {
      add_order_save_hold();

      show_product_throbber();

      $.post(Drupal.settings.ucURL.adminOrders + order_id + '/products',
             { action: 'view' },
             function(contents) {
               if (contents != '') {
                 $('#products-container').empty().append(contents);
               }
               remove_order_save_hold();
               hide_product_throbber();
             });
    }
  );
}

/**
 * Load the product selection form.
 */
function load_product_select(order_id, search) {
  if (search == true) {
    options = {'search' : $('#edit-product-search').val()};
  }
  else {
    options = { };
  }

  show_product_throbber();

  $.post(Drupal.settings.ucURL.adminOrders + order_id + '/product_select', options,
         function (contents) {
           $('#products-selector').empty().addClass('product-select-box2').append(contents);
           hide_product_throbber();
         }
  );

  return false;
}

/**
 * Deprecated?
 */
function select_product() {
  add_product_form();
  return false;
}

/**
 * Hide product selection form.
 */
function close_product_select() {
  $('#products-selector').empty().removeClass('product-select-box2');
  return false;
}

/**
 * Load the quantity and other extra product fields.
 */
function add_product_form() {
  add_product_browser = $('#products-selector').html();

  show_product_throbber();

  if (parseInt($('#edit-unid').val()) > 0) {
    $.post(Drupal.settings.ucURL.adminOrders + $('#edit-order-id').val() + '/add_product/' + $('#edit-unid').val(), { },
           function(contents) {
             $('#products-selector').empty().append(contents);
             hide_product_throbber();
           }
    );
  }
}

/**
 * Add the selected product to the order.
 */
function add_product_to_order(order_id, node_id) {
  var post_vars = fetch_product_data();
  post_vars['action'] = 'add';
  post_vars['nid'] = node_id;
  post_vars['qty'] = $('#edit-add-qty').val();

  $('#uc-order-add-product-form :input').not(':radio:not(:checked), :checkbox:not(:checked)').each(
    function() {
      post_vars[$(this).attr('name')] = $(this).val();
    }
  );

  show_product_throbber();

  $.post(Drupal.settings.ucURL.adminOrders + order_id + '/products', post_vars,
         function(contents) {
           if (contents != '') {
             $('#products-container').empty().append(contents);
           }
           hide_product_throbber();
         }
  );

  $('#add-product-button').click();

  return false;
}

/**
 * Gather all of the products' data fields into one array.
 */
function fetch_product_data() {
  var pdata = { };

  $('.order-pane-table :input').each(
    function() {
      pdata[$(this).attr('name')] = $(this).val();
    }
  );
  $('.order-pane-table ~ :input').each(
    function() {
      pdata[$(this).attr('name')] = $(this).val();
    }
  );

  return pdata;
}

/**
 * Button to create a new row of empty data fields.
 */
function add_blank_line_button(order_id) {
  var post_vars = fetch_product_data();
  post_vars['action'] = 'add_blank';

  show_product_throbber();

  $.post(Drupal.settings.ucURL.adminOrders + order_id + '/products',
         post_vars,
         function(contents) {
           if (contents != '') {
             $('#products-container').empty().append(contents);
           }
           hide_product_throbber();
         }
  );
}

/**
 * Button to remove product from the order.
 */
function remove_product_button(message, opid) {
  if (confirm(message)) {
    var post_vars = fetch_product_data();
    post_vars['action'] = 'remove';
    post_vars['opid'] = opid;

    show_product_throbber();

    $.post(Drupal.settings.ucURL.adminOrders + $('#edit-order-id').val() + '/products',
           post_vars,
           function(contents) {
             if (contents != '') {
               $('#products-container').empty().append(contents);
             }
             hide_product_throbber();
           }
    );
  }
}

/**
 * Prevent mistakes by confirming deletion.
 */
function confirm_line_item_delete(message, img_id) {
  if (confirm(message)) {
    var li_id = img_id.substring(3);
    $('#edit-li-delete-id').val(li_id);
    $('#uc-order-edit-form #edit-submit-changes').get(0).click();
  }
}

/**
 * Disable order submit button while parts of the page are still loading.
 */
function add_order_save_hold() {
  order_save_holds++;
  $('#uc-order-edit-form input.save-button').attr('disabled', 'disabled');
}

/**
 * Remove a hold and enable the save buttons when all holds are gone!
 */
function remove_order_save_hold() {
  order_save_holds--;

  if (order_save_holds == 0) {
    release_held_buttons();
  }
}

/**
 * Remove the disable attribute on any input item with the save-button class.
 */
function release_held_buttons() {
  $('#uc-order-edit-form input.save-button').removeAttr('disabled');
}

/**
 * User feedback that something is happening.
 */
function show_product_throbber() {
  $('#product-div-throbber').attr('style', 'background-image: url(' + Drupal.settings.basePath + 'misc/throbber.gif); background-repeat: no-repeat; background-position: 100% -20px;').html('    ');
}

/**
 * Done loading forms.
 */
function hide_product_throbber() {
  $('#product-div-throbber').removeAttr('style').empty();
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal uc_order.js source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.