You are here

uc_extra_fields_pane.module in Extra Fields Checkout Pane 6

Same filename and directory in other branches
  1. 6.2 uc_extra_fields_pane.module
  2. 7 uc_extra_fields_pane.module

File

uc_extra_fields_pane.module
View source
<?php

/* 
 * Module: uc_extra_fields_pane.module
 * 
 * This module extends Ubercart checkout panes, allowing creation
 * of custom extra address fields.  
 *  
 * Developed by blackice78 - Maurizio Ganovelli - drupal@ganovelli.it 
 *
 */

/*
 * implementation of hook_menu()
 */
function uc_extra_fields_pane_menu() {
  $items['admin/store/settings/checkout/edit/extrafields'] = array(
    'title' => 'Extra address fields',
    'description' => 'Add more address fields.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_extra_fields_pane_form',
    ),
    'access arguments' => array(
      'administer store',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 5,
  );
  $items['admin/store/settings/extrafields/%uc_extra_fields/delete'] = array(
    'title' => 'Delete field',
    'description' => 'Delete an address field.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_extra_fields_pane_field_delete',
      4,
    ),
    'access arguments' => array(
      'administer store',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/store/settings/extrafields/%uc_extra_fields/edit'] = array(
    'title' => 'Modify field',
    'description' => 'Edit extra address field.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_extra_fields_pane_field_edit',
      4,
    ),
    'access arguments' => array(
      'administer store',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/store/settings/checkout/edit/extrapanes'] = array(
    'title' => 'Edit extra panes information',
    'description' => 'Edit extra panes information',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'uc_extra_fields_pane_panes_edit',
    ),
    'access arguments' => array(
      'administer store',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 6,
  );
  return $items;
}

/*
 * Loader function for menù system
 */
function uc_extra_fields_pane_panes_edit_submit($form_id, &$form_state) {
  variable_set('uc_extra_fields_pane_billing_pane_title', $form_state['values']['billing-pane-title']);
  variable_set('uc_extra_fields_pane_delivery_pane_title', $form_state['values']['delivery-pane-title']);
  variable_set('uc_extra_fields_pane_billing_pane_description', $form_state['values']['billing-pane-description']);
  variable_set('uc_extra_fields_pane_delivery_pane_description', $form_state['values']['delivery-pane-description']);
  drupal_set_message(t('Titles saved.'));
}
function uc_extra_fields_pane_panes_edit(&$form_state) {
  $form = array();
  $form['billing-pane-title'] = array(
    '#title' => t('Extra billing pane title'),
    '#description' => t('Enter a string to display as extra billing pane title'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => variable_get('uc_extra_fields_pane_billing_pane_title', 'Additional billing information'),
  );
  $form['billing-pane-description'] = array(
    '#title' => t('Extra billing pane description'),
    '#description' => t('Enter a string to display as extra billing pane description'),
    '#type' => 'textarea',
    '#default_value' => variable_get('uc_extra_fields_pane_billing_pane_description', 'Please fill in additional billing information below.'),
  );
  $form['delivery-pane-title'] = array(
    '#title' => t('Extra delivery pane title'),
    '#description' => t('Enter a string to display as extra delivery pane title'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => variable_get('uc_extra_fields_pane_delivery_pane_title', 'Additional delivery information'),
  );
  $form['delivery-pane-description'] = array(
    '#title' => t('Extra delivery pane description'),
    '#description' => t('Enter a string to display as extra delivery pane description'),
    '#type' => 'textarea',
    '#default_value' => variable_get('uc_extra_fields_pane_delivery_pane_description', 'Please fill in additional delivery information below.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/*
 * Loader function for menù system
 */
function uc_extra_fields_load($fid) {
  return db_fetch_object(db_query('select * from {uc_extra_fields} where field_id=%d', $fid));
}

/*
 * implementation of hook_checkout_pane();
 * Create two additional panes for billing and delivery extra information
 */
function uc_extra_fields_pane_checkout_pane() {
  $panes[] = array(
    'id' => 'extra_billing',
    'callback' => 'uc_extra_fields_pane_billing',
    'title' => t(variable_get('uc_extra_fields_pane_billing_pane_title', 'Additional billing information')),
    'desc' => t(variable_get('uc_extra_fields_pane_billing_pane_title', 'Additional billing information')),
    'weight' => 7,
  );
  $panes[] = array(
    'id' => 'extra_delivery',
    'callback' => 'uc_extra_fields_pane_delivery',
    'title' => t(variable_get('uc_extra_fields_pane_delivery_pane_title', 'Additional delivery information')),
    'desc' => t(variable_get('uc_extra_fields_pane_delivery_pane_title', 'Additional delivery information')),
    'weight' => 8,
  );
  return $panes;
}

/*
 *
 * uc_extra_fields_pane_form form validation (called after new field insert)
 *
 */
function uc_extra_fields_pane_form_validate($form, &$form_state) {
  $field = $form_state['values']['ucxf']['add_one_more_field'];

  // No label.
  if (!$field['label']) {
    form_set_error('ucxf][add_one_more_field][label', t('Add new field: you need to provide a label.'));
  }

  // No field name.
  if (!$field['field_name']) {
    form_set_error('ucxf][add_one_more_field][field_name', t('Add new field: you need to provide a field name.'));
  }
  else {
    $field_name = $field['field_name'];

    // Add the 'uc_extra_' prefix.
    if (substr($field_name, 0, 8) != 'uc_extra_') {
      $field_name = 'ucxf_' . $field_name;
      form_set_value($form['ucxf']['add_one_more_field']['field_name'], $field_name, $form_state);
    }

    // Invalid field name.
    if (!preg_match('!^ucxf_[a-z0-9_]+$!', $field_name)) {
      form_set_error('ucxf][add_one_more_field][field_name', t('Add new field: the field name %field_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array(
        '%field_name' => $field_name,
      )));
    }

    // considering prefix uc_extra_ and suffix _billing/_delivery : no more than 23 characters (32 max for a db field)
    if (strlen($field_name) > 23) {
      form_set_error('ucxf][add_one_more_field][field_name', t('Add new field: the field name %field_name is too long. The name is limited to 23 characters, including the \'ucxf_\' prefix.', array(
        '%field_name' => $field_name,
      )));
    }

    // Field name already exists.
    $count = db_result(db_query("select count(*) from {uc_extra_fields} where field_db_name='%s'", $field_name));
    if ((int) $count > 0) {
      form_set_error('ucxf][add_one_more_field][field_name', t('Add new field: the field name %field_name already exists.', array(
        '%field_name' => $field_name,
      )));
    }
  }
}

/*
 * uc_extra_fields_pane_form submit function: insert new field into db
 *
 */
function uc_extra_fields_pane_form_submit($form, &$form_state) {
  $post_data = $form_state['values']['ucxf']['add_one_more_field'];
  $field = new stdClass();
  $field->field_name = $post_data['label'];
  $field->field_description = $post_data['description'];
  $field->field_db_name = $post_data['field_name'];
  $field->field_required = $post_data['required'];
  if (drupal_write_record('uc_extra_fields', $field) != FALSE) {
    $billing_field_value_name = $field->field_db_name . '_billing';
    $delivery_field_value_name = $field->field_db_name . '_delivery';
    $created = array();
    $spec = array(
      'type' => 'varchar',
      'length' => '255',
      'not null' => FALSE,
    );
    db_add_field($created, 'uc_extra_fields_values', $billing_field_value_name, $spec);
    db_add_field($created, 'uc_extra_fields_values', $delivery_field_value_name, $spec);
    $success = TRUE;
    foreach ($created as $check) {
      if (!$check['success']) {
        $success = FALSE;
      }
    }
    if ($success) {
      drupal_set_message(t('Field saved'));
    }
    else {
      drupal_set_message(t('Error creating new field'));
    }
  }
  else {
    drupal_set_message(t('Error creating new field'));
  }
}

/*
 * return a confirm delete form for the passed field id
 *
 */
function uc_extra_fields_pane_field_delete(&$form_state, $field) {
  return confirm_form(array(
    'field' => array(
      '#type' => 'value',
      '#value' => $field->field_id,
    ),
  ), t('Are you sure you want to remove the field "%field"?', array(
    '%field' => $field->field_name,
  )), 'admin/store/settings/checkout/edit/extrafields', t('This action cannot be undone.'), t('Remove'), t('Cancel'));
}
function uc_extra_fields_pane_field_delete_submit($form, &$form_state) {
  $fields = uc_extra_fields_pane_load_fields_from_db();
  $field_name = "";
  if (count($fields)) {
    foreach ($fields as $field) {
      if ($field['id'] == $form_state['values']['field']) {
        $field_db_name = $field['field'];
        break;
      }
    }
  }
  db_query("delete from {uc_extra_fields} where field_id=%d", $form_state['values']['field']);
  $deleted = array();
  db_drop_field($deleted, 'uc_extra_fields_values', $field_db_name . '_billing');
  db_drop_field($deleted, 'uc_extra_fields_values', $field_db_name . '_delivery');
  $success = TRUE;
  foreach ($deleted as $check) {
    if (!$check['success']) {
      $success = FALSE;
    }
  }
  if ($success) {
    drupal_set_message(t('Field deleted'));
  }
  else {
    drupal_set_message(t('Error deleting field'));
  }
  $form_state['redirect'] = 'admin/store/settings/checkout/edit/extrafields';
}

/*
 *  return existent fields list and the "add new" field form 
 *
 */
function _uc_extra_fields_pane_summarize($fields) {
  $output = array();
  if (is_array($fields['#summary arguments'][0]) && count($fields['#summary arguments'][0])) {
    foreach ($fields['#summary arguments'][0] as $field) {
      $output[] = t($field['title']) . ': ' . t($field['description']);
    }
  }
  else {
    $output = t('No field defined.');
  }
  return $output;
}
function uc_extra_fields_pane_form($form_state) {
  $fields = uc_extra_fields_pane_load_fields_from_db();
  $page_content .= "";
  $form['ucxf'] = array(
    '#tree' => TRUE,
    '#summary callback' => '_uc_extra_fields_pane_summarize',
    '#summary arguments' => array(
      $fields,
    ),
  );
  if (count($fields)) {
    $headers = array();
    $headers[] = array(
      'data' => t('Label'),
    );
    $headers[] = array(
      'data' => t('Field name'),
    );
    $headers[] = array(
      'data' => t('Required'),
    );
    $headers[] = array(
      'data' => t('Description'),
    );
    $headers[] = array(
      'data' => t('Actions'),
    );
    $rows = array();
    foreach ($fields as $field) {
      $content = array();
      $content[] = array(
        'data' => $field['title'],
      );
      $content[] = array(
        'data' => $field['field'],
      );
      $content[] = array(
        'data' => $field['required'] == 1 ? t('Yes') : t('No'),
      );
      $content[] = array(
        'data' => $field['description'],
      );
      $content[] = array(
        'data' => l(t('delete'), 'admin/store/settings/extrafields/' . $field['id'] . '/delete') . ' | ' . l(t('edit'), 'admin/store/settings/extrafields/' . $field['id'] . '/edit'),
      );
      $rows[] = $content;
    }
    $page_content = theme_table($headers, $rows);
    $form['ucxf']['current-fields'] = array(
      '#type' => 'fieldset',
      '#title' => t('Manage fields'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['ucxf']['current-fields']['table'] = array(
      '#value' => $page_content,
    );
  }
  $form['ucxf']['add_one_more_field'] = array(
    '#type' => 'fieldset',
    '#title' => t('Create new field'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['ucxf']['add_one_more_field']['label'] = array(
    '#title' => t('Label'),
    '#type' => 'textfield',
    '#size' => 15,
    '#description' => t('Label shown to customers in checkout pages.'),
  );
  $form['ucxf']['add_one_more_field']['field_name'] = array(
    '#title' => t('Field name'),
    '#type' => 'textfield',
    '#size' => 15,
    '#description' => t('Database field name. It must contain only lower chars a-z, digits 0-9 and _. Max allowed lenght is 23 characters.'),
  );
  $form['ucxf']['add_one_more_field']['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textarea',
    '#rows' => 3,
    '#description' => t('Insert a description to tell customers how to fill this field.'),
  );
  $form['ucxf']['add_one_more_field']['required'] = array(
    '#title' => t('Field required'),
    '#type' => 'checkbox',
    '#description' => t('Check this item is field is mandatory.'),
  );
  $form['ucxf']['add_one_more_field']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}
function uc_extra_fields_pane_field_edit_submit($form, &$form_state) {
  $post_data = $form_state['values'];
  $field = new stdClass();
  $field->field_id = (int) arg(4);
  $field->field_name = $post_data['label'];
  $field->field_description = $post_data['description'];
  $field->field_required = $post_data['required'];
  if (drupal_write_record('uc_extra_fields', $field, 'field_id') != FALSE) {
    drupal_set_message(t('Field updated'));
  }
  else {
    drupal_set_message(t('Error saving field.'));
  }
  $form_state['redirect'] = 'admin/store/settings/checkout/edit/extrafields';
}
function uc_extra_fields_pane_field_edit(&$form_state, $field) {
  $form = array();
  $form['label'] = array(
    '#title' => t('Label'),
    '#type' => 'textfield',
    '#size' => 15,
    '#description' => t('Label shown to customers in checkout pages.'),
    '#required' => TRUE,
    '#default_value' => $field->field_name,
  );
  $form['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textarea',
    '#rows' => 3,
    '#description' => t('Insert a description to tell customers how to fill this field.'),
    '#default_value' => $field->field_description,
  );
  $form['required'] = array(
    '#title' => t('Field required'),
    '#type' => 'checkbox',
    '#description' => t('Check this item is field is mandatory.'),
    '#default_value' => $field->field_required,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}
function uc_extra_fields_pane_load_fields_from_db() {
  static $uc_extra_fields_pane_fields;

  // caching results
  if ($uc_extra_fields_pane_fields == NULL) {
    $uc_extra_fields_pane_fields = array();
  }
  else {
    return $uc_extra_fields_pane_fields;
  }
  $db_results = db_query("select * from {uc_extra_fields}");
  while ($db_result = db_fetch_array($db_results)) {
    $uc_extra_fields_pane_fields[$db_result['field_db_name']] = array(
      'id' => $db_result['field_id'],
      'field' => $db_result['field_db_name'],
      'title' => $db_result['field_name'],
      'description' => $db_result['field_description'],
      'required' => $db_result['field_required'],
    );
  }
  return $uc_extra_fields_pane_fields;
}

/**
 * billing information.
 */
function uc_extra_fields_pane_billing($op, &$arg1 = NULL, $arg2 = NULL) {
  switch ($op) {
    case 'settings':
      return;
    case 'view':
      $description = t(variable_get('uc_extra_fields_pane_billing_pane_description', 'Please fill in additional billing information below.'));
      $fields = uc_extra_fields_pane_load_fields_from_db();
      $contents = array();

      // dynamically generate form elements
      if (count($fields)) {
        foreach ($fields as $field) {
          $contents['extra_fields'][$field['field'] . '_billing'] = array(
            '#type' => 'textfield',
            '#title' => t($field['title']),
            '#description' => t($field['description']),
            '#size' => 32,
            '#maxlength' => 255,
            '#required' => $field['required'],
            '#default_value' => isset($arg1->extra_fields[$field['field'] . '_billing']) ? $arg1->extra_fields[$field['field'] . '_billing'] : NULL,
          );
        }
      }
      return array(
        'description' => $description,
        'contents' => $contents,
        'theme' => 'uc_extra_fields_pane',
      );
    case 'process':

      // initialize and fill array
      if ($arg1->extra_fields == NULL) {
        $arg1->extra_fields = array();
      }
      $arg1->extra_fields = array_merge($arg1->extra_fields, $arg2['extra_fields']);
      return TRUE;
    case 'review':
      $fields = uc_extra_fields_pane_load_fields_from_db();
      if (count($fields)) {
        foreach ($fields as $field) {
          $review[] = array(
            'title' => $field['title'],
            'data' => $arg1->extra_fields[$field['field'] . '_billing'],
          );
        }
      }
      return $review;
  }
}

/**
 * delivery information.
 */
function uc_extra_fields_pane_delivery($op, &$arg1 = NULL, $arg2 = NULL) {
  switch ($op) {
    case 'settings':
      return;
    case 'view':
      $description = t(variable_get('uc_extra_fields_pane_delivery_pane_description', 'Please fill in additional delivery information below.'));
      $fields = uc_extra_fields_pane_load_fields_from_db();
      $contents = array();

      // dynamically generate form elements
      if (count($fields)) {
        foreach ($fields as $field) {
          $contents['extra_fields'][$field['field'] . '_delivery'] = array(
            '#type' => 'textfield',
            '#title' => t($field['title']),
            '#description' => t($field['description']),
            '#size' => 32,
            '#maxlength' => 255,
            '#required' => $field['required'],
            '#default_value' => isset($arg1->extra_fields[$field['field'] . '_delivery']) ? $arg1->extra_fields[$field['field'] . '_delivery'] : NULL,
          );
        }
      }
      return array(
        'description' => $description,
        'contents' => $contents,
        'theme' => 'uc_extra_fields_pane',
      );
    case 'process':

      // initialize and fill array
      if ($arg1->extra_fields == NULL) {
        $arg1->extra_fields = array();
      }
      $arg1->extra_fields = array_merge($arg1->extra_fields, $arg2['extra_fields']);
      return TRUE;
    case 'review':
      $fields = uc_extra_fields_pane_load_fields_from_db();
      if (count($fields)) {
        foreach ($fields as $field) {
          $review[] = array(
            'title' => $field['title'],
            'data' => $arg1->extra_fields[$field['field'] . '_delivery'],
          );
        }
      }
      return $review;
  }
}
function uc_extra_fields_pane_order($op, &$arg1, $arg2) {
  switch ($op) {
    case 'new':

      // do nothing, extra field cannot be handled here
      break;
    case 'save':
      if (is_array($arg1->extra_fields) && count($arg1->extra_fields)) {
        $fields = uc_extra_fields_pane_load_fields_from_db();
        $sql_field_names = array();
        $sql_field_values = array();
        foreach ($arg1->extra_fields as $key => $value) {
          $sql_field_names[] = $key;
          $sql_field_values[] = $value;
        }
        $sql_field_values[] = $arg1->order_id;
        db_query("update {uc_extra_fields_values} set " . implode("='%s',", $sql_field_names) . "='%s' where order_id=%d", $sql_field_values);
        if (!db_affected_rows()) {
          $placeholders = "";
          for ($i = 0; $i < count($sql_field_names); $i++) {
            $placeholders .= "'%s',";
          }
          db_query("insert into {uc_extra_fields_values}(" . implode(',', $sql_field_names) . ",order_id) values (" . $placeholders . "%d)", $sql_field_values);
        }
      }
      break;
    case 'load':
      $arg1->extra_fields = array();
      $values = db_fetch_array(db_query("select * from {uc_extra_fields_values} where order_id=%d", $arg1->order_id));
      if ($values && count($values)) {
        foreach ($values as $key => $value) {
          if ($value && $key != 'order_id') {
            $arg1->extra_fields[$key] = $value;
          }
        }
      }
      break;
  }
}
function uc_extra_fields_pane_order_pane() {
  $panes[] = array(
    'id' => 'extra_fields',
    'callback' => 'uc_extra_fields_pane_order_handler',
    'title' => t('Additional information'),
    'desc' => t('Additional information'),
    'class' => 'pos-left',
    'weight' => 1,
    'show' => array(
      'view',
      'edit',
      'customer',
    ),
  );
  return $panes;
}
function uc_extra_fields_pane_order_handler($op, $arg1) {
  switch ($op) {
    case 'edit-theme':
      $output = '<table class="order-edit-table">
                 <tr><th>' . $arg1['extra_fields_billing']['#title'] . '</th></tr>';
      foreach (element_children($arg1['extra_fields_billing']) as $field) {
        $title = $arg1['extra_fields_billing'][$field]['#title'];
        unset($arg1['extra_fields_billing'][$field]['#title']);
        unset($arg1['extra_fields_billing'][$field]['#description']);
        $output .= '<tr><td><strong>' . $title . '</strong>: ' . drupal_render($arg1['extra_fields_billing'][$field]) . '</td></tr>';
      }
      $output .= '</table>';
      $output .= '<table class="order-edit-table">
                  <tr><th>' . $arg1['extra_fields_delivery']['#title'] . '</th></tr>';
      foreach (element_children($arg1['extra_fields_delivery']) as $field) {
        $title = $arg1['extra_fields_delivery'][$field]['#title'];
        unset($arg1['extra_fields_delivery'][$field]['#title']);
        unset($arg1['extra_fields_delivery'][$field]['#description']);
        $output .= '<tr><td><strong>' . $title . '</strong>: ' . drupal_render($arg1['extra_fields_delivery'][$field]) . '</td></tr>';
      }
      $output .= '</table>';
      return $output;
    case 'edit-process':
      $fields = uc_extra_fields_pane_load_fields_from_db();
      $changes = array();
      $changes['extra_fields'] = array();

      // dynamically generate form elements
      if (count($fields)) {
        foreach ($fields as $field) {
          if (isset($arg1[$field['field'] . '_billing'])) {
            $changes['extra_fields'][$field['field'] . '_billing'] = $arg1[$field['field'] . '_billing'];
          }
          if (isset($arg1[$field['field'] . '_delivery'])) {
            $changes['extra_fields'][$field['field'] . '_delivery'] = $arg1[$field['field'] . '_delivery'];
          }
        }
      }
      return $changes;
    case 'edit-form':
      $form = array();
      $billing_enabled = variable_get('uc_pane_extra_billing_enabled', TRUE);
      $delivery_enabled = variable_get('uc_pane_extra_delivery_enabled', TRUE);
      if (!$delivery_enabled && !$billing_enabled) {
        return $form;
      }
      if ($billing_enabled) {
        $form['extra_fields_billing'] = array(
          '#type' => 'fieldset',
          '#title' => t(variable_get('uc_extra_fields_pane_billing_pane_title', 'Additional billing information')),
          '#collapsible' => FALSE,
          '#collapsed' => FALSE,
        );
      }
      if ($delivery_enabled) {
        $form['extra_fields_delivery'] = array(
          '#type' => 'fieldset',
          '#title' => t(variable_get('uc_extra_fields_pane_delivery_pane_title', 'Additional delivery information')),
          '#collapsible' => FALSE,
          '#collapsed' => FALSE,
        );
      }
      $fields = uc_extra_fields_pane_load_fields_from_db();

      // dynamically generate form elements
      if (count($fields)) {
        foreach ($fields as $field) {
          if ($delivery_enabled) {
            $form['extra_fields_delivery'][$field['field'] . '_delivery'] = array(
              '#type' => 'textfield',
              '#title' => t($field['title']),
              '#description' => t($field['description']),
              '#size' => 32,
              '#maxlength' => 255,
              '#required' => $field['required'],
              '#default_value' => isset($arg1->extra_fields[$field['field'] . '_delivery']) ? $arg1->extra_fields[$field['field'] . '_delivery'] : NULL,
            );
          }
          if ($billing_enabled) {
            $form['extra_fields_billing'][$field['field'] . '_billing'] = array(
              '#type' => 'textfield',
              '#title' => t($field['title']),
              '#description' => t($field['description']),
              '#size' => 32,
              '#maxlength' => 255,
              '#required' => $field['required'],
              '#default_value' => isset($arg1->extra_fields[$field['field'] . '_billing']) ? $arg1->extra_fields[$field['field'] . '_billing'] : NULL,
            );
          }
        }
      }
      return $form;

    // customer or administrator order detail page.
    case 'customer':
    case 'view':
      $fields = uc_extra_fields_pane_load_fields_from_db();
      $output = "";
      $values = db_fetch_array(db_query("select * from {uc_extra_fields_values} where order_id=%d", $arg1->order_id));
      $billing_fields = array();
      $delivery_fields = array();
      if (count($fields)) {
        foreach ($fields as $field) {

          // warning: user input --> check_plain
          if (isset($values[$field['field'] . '_billing'])) {
            $billing_fields[] = t($field['title']) . ': ' . check_plain($values[$field['field'] . '_billing']) . '<br />';
          }
          if (isset($values[$field['field'] . '_delivery'])) {
            $delivery_fields[] = t($field['title']) . ': ' . check_plain($values[$field['field'] . '_delivery']) . '<br />';
          }
        }
      }
      if (count($billing_fields)) {
        $output = '<strong><small>' . t(variable_get('uc_extra_fields_pane_billing_pane_title', 'Additional billing information')) . '</small></strong><br />' . implode('<br />', $billing_fields);
      }
      if (count($delivery_fields)) {
        $output .= '<strong><small>' . t(variable_get('uc_extra_fields_pane_delivery_pane_title', 'Additional delivery information')) . '</small></strong><br />' . implode('<br />', $delivery_fields);
      }
      return $output;
  }
}

/*
 *  TOKEN INTEGRATION
 *
 */

/**
 * Implementation of hook_token_values().
 * 
 *   
 */
function uc_extra_fields_pane_token_values($type, $object = NULL) {
  $values = array();
  switch ($type) {
    case 'order':
      $order = $object;
      $results = db_query("select field_db_name,field_name from {uc_extra_fields}");
      while ($result = db_fetch_array($results)) {
        $values['extra-billing-' . str_replace('ucxf_', '', $result['field_db_name'])] = isset($order->extra_fields[$result['field_db_name'] . '_billing']) ? $order->extra_fields[$result['field_db_name'] . '_billing'] : '';
        $values['extra-shipping-' . str_replace('ucxf_', '', $result['field_db_name'])] = isset($order->extra_fields[$result['field_db_name'] . '_delivery']) ? $order->extra_fields[$result['field_db_name'] . '_delivery'] : '';
      }
      break;
  }
  return $values;
}

/**
 * Implementation of hook_token_list().
 * 
 *   
 */
function uc_extra_fields_pane_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'order' || $type == 'ubercart' || $type == 'all') {
    $results = db_query("select field_db_name,field_name from {uc_extra_fields}");
    while ($result = db_fetch_array($results)) {
      $tokens['order']['extra-billing-' . str_replace('ucxf_', '', $result['field_db_name'])] = t(variable_get('uc_extra_fields_pane_billing_pane_title', 'Additional billing information')) . ': ' . t($result['field_name']);
      $tokens['order']['extra-shipping-' . str_replace('ucxf_', '', $result['field_db_name'])] = t(variable_get('uc_extra_fields_pane_delivery_pane_title', 'Additional delivery information')) . ': ' . t($result['field_name']);
    }
  }
  return $tokens;
}

/*
 * THEMING
 */

/**
 * Implementation of hook_theme().
 */
function uc_extra_fields_pane_theme() {
  return array(
    'uc_extra_fields_pane' => array(
      'arguments' => array(
        'form' => NULL,
      ),
    ),
  );
}

/**
 * Theme the extra fields delivery/billing forms in tables.
 *
 * @ingroup themeable
 * @see
 *   uc_extra_fields_pane_delivery()
 *   uc_extra_fields_pane_billing()
 */
function theme_uc_extra_fields_pane($form) {
  $req = '<span class="form-required">*</span>';
  $output = '<div class="address-pane-table"><table>';
  foreach (element_children($form['extra_fields']) as $field) {
    $title = $form['extra_fields'][$field]['#title'] . ':';
    unset($form['extra_fields'][$field]['#title']);
    $output .= '<tr><td class="field-label">';
    if ($form['extra_fields'][$field]['#required']) {
      $output .= $req;
    }
    $output .= $title . '</td><td>' . drupal_render($form['extra_fields'][$field]) . '</td></tr>';
  }
  $output .= '</table></div>';
  foreach (element_children($form) as $element) {
    $output .= drupal_render($form[$element]);
  }
  return $output;
}