You are here

function commerce_order_order_form in Commerce Core 7

Form callback: create or edit an order.

Parameters

$order: The order object to edit through the form.

1 string reference to 'commerce_order_order_form'
commerce_order_ui_forms in modules/order/commerce_order_ui.module
Implements hook_forms().

File

modules/order/includes/commerce_order.forms.inc, line 15
Forms for creating / editing and deleting orders.

Code

function commerce_order_order_form($form, &$form_state, $order) {

  // Ensure this include file is loaded when the form is rebuilt from the cache.
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'commerce_order') . '/includes/commerce_order.forms.inc';

  // Ensure the owner name is accessible if the uid is set.
  if (!empty($order->uid) && ($owner = user_load($order->uid))) {
    $order->name = $owner->name;
    if (empty($order->mail)) {
      $order->mail = $owner->mail;
    }
  }

  // Add the field related form elements.
  $form_state['commerce_order'] = $order;
  field_attach_form('commerce_order', $order, $form, $form_state);

  // Hide the order total field from direct editing.
  $form['commerce_order_total']['#access'] = FALSE;
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  );

  // Build an array of order status options grouped by order state.
  $options = array();
  foreach (commerce_order_state_get_title() as $name => $title) {
    foreach (commerce_order_statuses(array(
      'state' => $name,
    )) as $order_status) {

      // Only include enabled statues and the current order status.
      if (!empty($order_status['status']) || $order->status == $order_status['name']) {
        $options[check_plain($title)][$order_status['name']] = check_plain($order_status['title']);
      }
    }
  }

  // Add a section to update the status and leave a log message.
  $form['order_status'] = array(
    '#type' => 'fieldset',
    '#title' => t('Order status'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'commerce_order') . '/commerce_order.js',
        array(
          'type' => 'setting',
          'data' => array(
            'status_titles' => commerce_order_status_get_title(),
          ),
        ),
      ),
    ),
    '#weight' => 20,
  );
  $form['order_status']['status'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#options' => $options,
    '#default_value' => $order->status,
  );
  $form['order_status']['status_original'] = array(
    '#type' => 'hidden',
    '#value' => $order->status,
    '#attributes' => array(
      'id' => 'edit-status-original',
    ),
  );
  $form['order_status']['log'] = array(
    '#type' => 'textarea',
    '#title' => t('Update log message'),
    '#description' => t('Provide an explanation of the changes you are making. This will provide a meaningful audit trail for updates to this order.'),
    '#rows' => 4,
  );

  // Add the user account and e-mail fields.
  $form['user'] = array(
    '#type' => 'fieldset',
    '#title' => t('User information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#access' => user_access('administer commerce_order entities'),
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'commerce_order') . '/commerce_order.js',
        array(
          'type' => 'setting',
          'data' => array(
            'anonymous' => variable_get('anonymous', t('Anonymous')),
          ),
        ),
      ),
    ),
    '#weight' => 30,
  );
  $form['user']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Owned by'),
    '#description' => t('Leave blank for %anonymous.', array(
      '%anonymous' => variable_get('anonymous', t('Anonymous')),
    )),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => !empty($order->name) ? $order->name : '',
    '#weight' => -1,
  );
  $form['user']['mail'] = array(
    '#type' => 'textfield',
    '#title' => t('Order contact e-mail'),
    '#description' => t('Defaults to the owner account e-mail address if left blank. Used for order communication.'),
    '#default_value' => $order->mail,
  );

  // Add a log checkbox and timestamp field to a history tab.
  $created_timestamp = empty($order->created) ? REQUEST_TIME : $order->created;
  $placed_timestamp = empty($order->placed) ? REQUEST_TIME : $order->placed;
  $form['order_history'] = array(
    '#type' => 'fieldset',
    '#title' => t('Order history', array(), array(
      'context' => 'a drupal commerce order',
    )),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'commerce_order') . '/commerce_order.js',
      ),
    ),
    '#weight' => 40,
  );
  $form['order_history']['revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision on update'),
    '#description' => t('If an update log message is entered, a revision will be created even if this is unchecked.'),
    '#default_value' => variable_get('commerce_order_auto_revision', TRUE),
    '#access' => user_access('administer commerce_order entities'),
  );
  $form['order_history']['date_created'] = array(
    '#type' => 'textfield',
    '#title' => t('Created on'),
    '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array(
      '%time' => format_date($created_timestamp, 'custom', 'Y-m-d H:i:s O'),
      '%timezone' => format_date($created_timestamp, 'custom', 'O'),
    )),
    '#maxlength' => 25,
    '#default_value' => !empty($order->created) ? format_date($order->created, 'custom', 'Y-m-d H:i:s O') : '',
  );
  $form['order_history']['date_placed'] = array(
    '#type' => 'textfield',
    '#title' => t('Placed on'),
    '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC.', array(
      '%time' => format_date($placed_timestamp, 'custom', 'Y-m-d H:i:s O'),
      '%timezone' => format_date($placed_timestamp, 'custom', 'O'),
    )),
    '#maxlength' => 25,
    '#default_value' => !empty($order->placed) ? format_date($order->placed, 'custom', 'Y-m-d H:i:s O') : '',
  );
  $form['order_history']['created'] = array(
    '#type' => 'hidden',
    '#value' => !empty($order->created) ? format_date($order->created, 'short') : '',
    '#attributes' => array(
      'id' => 'edit-created',
    ),
  );
  $form['order_history']['changed'] = array(
    '#type' => 'hidden',
    '#value' => !empty($order->changed) ? format_date($order->changed, 'short') : '',
    '#attributes' => array(
      'id' => 'edit-changed',
    ),
  );
  $form['order_history']['placed'] = array(
    '#type' => 'hidden',
    '#value' => !empty($order->placed) ? format_date($order->placed, 'short') : '',
    '#attributes' => array(
      'id' => 'edit-placed',
    ),
  );

  // We add the form's #submit array to this button along with the actual submit
  // handler to preserve any submit handlers added by a form callback_wrapper.
  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save order', array(), array(
      'context' => 'a drupal commerce order',
    )),
    '#submit' => array_merge($submit, array(
      'commerce_order_order_form_submit',
    )),
    '#weight' => 40,
  );

  // We append the validate handler to #validate in case a form callback_wrapper
  // is used to add validate handlers earlier.
  $form['#validate'][] = 'commerce_order_order_form_validate';
  return $form;
}