You are here

function uc_order_create_form in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_order/uc_order.module \uc_order_create_form()
  2. 6.2 uc_order/uc_order.admin.inc \uc_order_create_form()

Creates a new order and redirect to its edit screen.

See also

uc_order_create_form_create_validate()

uc_order_create_form_create_submit()

1 string reference to 'uc_order_create_form'
uc_order_menu in uc_order/uc_order.module
Implements hook_menu().

File

uc_order/uc_order.admin.inc, line 343
Order administration menu items.

Code

function uc_order_create_form($form, &$form_state) {
  $form['customer_type'] = array(
    '#type' => 'radios',
    '#options' => array(
      'search' => t('Search for an existing customer.'),
      'create' => t('Create a new customer account.'),
      'none' => t('No customer account required.'),
    ),
    '#required' => TRUE,
    '#default_value' => 'search',
    '#ajax' => array(
      'callback' => 'uc_order_create_form_customer',
      'wrapper' => 'uc-order-customer',
      'progress' => array(
        'type' => 'throbber',
      ),
    ),
  );
  $form['customer'] = array(
    '#prefix' => '<div id="uc-order-customer">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );

  // Create form elements needed for customer search.
  // Shown only when the 'Search for an existing customer.' radio is selected.
  if (!isset($form_state['values']['customer_type']) || $form_state['values']['customer_type'] == 'search') {

    // Container for customer search fields.
    $form['customer'] += array(
      '#type' => 'fieldset',
      '#title' => t('Customer search'),
      '#description' => t('Enter full or partial information in one or more of the following fields, then press the "Search" button. Search results will match all the provided information.'),
    );

    // Customer first name.
    $form['customer']['first_name'] = array(
      '#type' => 'textfield',
      '#title' => t('First name'),
      '#size' => 24,
      '#maxlength' => 32,
    );

    // Customer last name.
    $form['customer']['last_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Last name'),
      '#size' => 24,
      '#maxlength' => 32,
    );

    // Customer e-mail address.
    $form['customer']['email'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail'),
      '#size' => 24,
      '#maxlength' => 96,
    );

    // Customer username.
    $form['customer']['username'] = array(
      '#type' => 'textfield',
      '#title' => t('Username'),
      '#size' => 24,
      '#maxlength' => 96,
    );
    $form['customer']['search'] = array(
      '#type' => 'button',
      '#value' => t('Search'),
      '#ajax' => array(
        'callback' => 'uc_order_create_form_customer_search',
        'wrapper' => 'uc-order-customer-results',
        'progress' => array(
          'type' => 'throbber',
        ),
      ),
    );
    $form['customer']['uid'] = array(
      '#prefix' => '<div id="uc-order-customer-results">',
      '#suffix' => '</div>',
    );

    // Search for existing customer by e-mail address.
    if (isset($form_state['values']['customer']['email'])) {
      $query = db_select('users', 'u')
        ->distinct();
      $query
        ->leftJoin('uc_orders', 'o', 'u.uid = o.uid');
      $query
        ->fields('u', array(
        'uid',
        'name',
        'mail',
      ))
        ->fields('o', array(
        'billing_first_name',
        'billing_last_name',
      ))
        ->condition('u.uid', 0, '>')
        ->condition(db_or()
        ->isNull('o.billing_first_name')
        ->condition('o.billing_first_name', db_like(trim($form_state['values']['customer']['first_name'])) . '%', 'LIKE'))
        ->condition(db_or()
        ->isNull('o.billing_last_name')
        ->condition('o.billing_last_name', db_like(trim($form_state['values']['customer']['last_name'])) . '%', 'LIKE'))
        ->condition(db_or()
        ->condition('o.primary_email', db_like(trim($form_state['values']['customer']['email'])) . '%', 'LIKE')
        ->condition('u.mail', db_like(trim($form_state['values']['customer']['email'])) . '%', 'LIKE'))
        ->condition('u.name', db_like(trim($form_state['values']['customer']['username'])) . '%', 'LIKE')
        ->orderBy('o.created', 'DESC')
        ->range(0, $limit = 11);
      $result = $query
        ->execute();
      $options = array();
      foreach ($result as $user) {
        $name = '';
        if (!empty($user->billing_first_name) && !empty($user->billing_last_name)) {
          $name = $user->billing_first_name . ' ' . $user->billing_last_name . ' ';
        }

        // Options formated as "First Last <email@example.com> (username)".
        $options[$user->uid] = $name . '&lt;' . $user->mail . '&gt;' . ' (' . $user->name . ')';
      }
      $max = FALSE;
      if (count($options) == $limit) {
        array_pop($options);
        $max = TRUE;
      }
      if (!empty($options)) {

        // Display search results.
        $form['customer']['uid'] += array(
          '#type' => 'radios',
          '#title' => t('Select customer'),
          '#description' => $max ? t('More than !limit results found. Refine your search to find other customers.', array(
            '!limit' => $limit - 1,
          )) : '',
          '#options' => $options,
          '#default_value' => key($options),
        );
      }
      else {

        // No search results found.
        $form['customer']['uid'] += array(
          '#markup' => '<p>' . t('Search returned no results.') . '</p>',
        );
      }
    }
  }
  elseif ($form_state['values']['customer_type'] == 'create') {

    // Container for new customer information.
    $form['customer'] += array(
      '#type' => 'fieldset',
      '#title' => t('New customer details'),
    );

    // Customer e-mail address.
    $form['customer']['email'] = array(
      '#type' => 'textfield',
      '#title' => t('Customer e-mail address'),
      '#size' => 24,
      '#maxlength' => 96,
    );

    // Option to notify customer.
    $form['customer']['sendmail'] = array(
      '#type' => 'checkbox',
      '#title' => t('E-mail account details to customer.'),
    );
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create order'),
    '#validate' => array(
      'uc_order_create_form_create_validate',
    ),
    '#submit' => array(
      'uc_order_create_form_create_submit',
    ),
  );
  return $form;
}