Advertisement
Guest User

uc_order.order_pane.inc

a guest
Aug 7th, 2012
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.55 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * This file contains the callbacks for the default order panes supplied with
  6.  * Ubercart and their corresponding helper functions.
  7.  *
  8.  * Order panes are defined using hook_uc_order_pane() and use a callback to
  9.  * handle the different processes involved in order viewing/editing. The
  10.  * default order panes are defined in uc_order_order_pane() in uc_order.module.
  11.  */
  12.  
  13. /**
  14.  * Handles the "Ship to" order pane.
  15.  */
  16. function uc_order_pane_ship_to($op, $order, &$form = NULL, &$form_state = NULL) {
  17.   switch ($op) {
  18.     case 'customer':
  19.       if (!uc_order_is_shippable($order)) {
  20.         return;
  21.       }
  22.     case 'view':
  23.       $build = array('#markup' => uc_order_address($order, 'delivery') . '<br />' . check_plain($order->delivery_phone));
  24.       return $build;
  25.  
  26.     case 'edit-form':
  27.       $form['ship_to'] = array(
  28.         '#type' => 'uc_address',
  29.         '#default_value' => $order,
  30.         '#required' => FALSE,
  31.         '#attributes' => array('class' => array('uc-store-address-field')),
  32.         '#key_prefix' => 'delivery',
  33.       );
  34.       return $form;
  35.  
  36.     case 'edit-theme':
  37.       $output = '<div class="order-pane-icons">';
  38.       $output .= ' <img src="' . base_path() . drupal_get_path('module', 'uc_store')
  39.                 . '/images/address_book.gif" alt="' . t('Select from address book.') . '" '
  40.                 . 'title="' . t('Select from address book.') . '" onclick="load_address_select(' . $form['order_uid']['#value'] . ', \'#delivery_address_select\', \'delivery\');" />';
  41.       $output .= ' <img src="' . base_path() . drupal_get_path('module', 'uc_store')
  42.                  . '/images/copy.gif" alt="' . t('Copy billing information.') . '" title="'
  43.                  . t('Copy billing information.') . '" onclick="uc_order_copy_billing_to_shipping();" />';
  44.       $output .= '</div>';
  45.       $output .= '<div id="delivery_address_select"></div>';
  46.       return $output . drupal_render($form['ship_to']);
  47.  
  48.     case 'edit-process':
  49.       foreach ($form_state['values'] as $key => $value) {
  50.         if (substr($key, 0, 9) == 'delivery_') {
  51.           if (uc_address_field_enabled(substr($key, 9))) {
  52.             $changes[$key] = $value;
  53.           }
  54.         }
  55.       }
  56.       return $changes;
  57.   }
  58. }
  59.  
  60. /**
  61.  * Handles the "Bill to" order pane.
  62.  */
  63. function uc_order_pane_bill_to($op, $order, &$form = NULL, &$form_state = NULL) {
  64.   switch ($op) {
  65.     case 'view':
  66.     case 'customer':
  67.       $build = array('#markup' => uc_order_address($order, 'billing') . '<br />' . check_plain($order->billing_phone));
  68.       return $build;
  69.  
  70.     case 'edit-form':
  71.       $form['bill_to'] = array(
  72.         '#type' => 'uc_address',
  73.         '#default_value' => $order,
  74.         '#required' => FALSE,
  75.         '#attributes' => array('class' => array('uc-store-address-field')),
  76.         '#key_prefix' => 'billing',
  77.       );
  78.       return $form;
  79.  
  80.     case 'edit-theme':
  81.       $output = '<div class="order-pane-icons">';
  82.       $output .= ' <img src="' . base_path() . drupal_get_path('module', 'uc_store')
  83.                 . '/images/address_book.gif" alt="' . t('Select from address book.') . '" '
  84.                 . 'title="' . t('Select from address book.') . '" onclick="load_address_select(' . $form['order_uid']['#value'] . ', \'#billing_address_select\', \'billing\');" />';
  85.       $output .= ' <img src="' . base_path() . drupal_get_path('module', 'uc_store')
  86.                . '/images/copy.gif" alt="' . t('Copy shipping information.') . '" title="'
  87.                . t('Copy shipping information.') . '" onclick="uc_order_copy_shipping_to_billing();" />';
  88.       $output .= '</div>';
  89.       $output .= '<div id="billing_address_select"></div>';
  90.       return $output . drupal_render($form['bill_to']);
  91.  
  92.     case 'edit-process':
  93.       foreach ($form_state['values'] as $key => $value) {
  94.         if (substr($key, 0, 8) == 'billing_') {
  95.           if (uc_address_field_enabled(substr($key, 8))) {
  96.             $changes[$key] = $value;
  97.           }
  98.         }
  99.       }
  100.       return $changes;
  101.   }
  102. }
  103.  
  104. /**
  105.  * Handles the "Customer Info" order pane.
  106.  */
  107. function uc_order_pane_customer($op, $order, &$form = NULL, &$form_state = NULL) {
  108.   switch ($op) {
  109.     case 'view':
  110.       $build['uid'] = array('#markup' => t('Customer number: !user_link', array('!user_link' => $order->uid ? l($order->uid, 'user/' . $order->uid) : '0')));
  111.       $build['primary_email'] = array('#markup' => '<br />' . t('Primary e-mail:') . '<br />' . check_plain($order->primary_email));
  112.  
  113.       return $build;
  114.  
  115.     case 'edit-form':
  116.       $form['customer'] = array();
  117.  
  118.       $form['customer']['uid'] = array(
  119.         '#type' => 'hidden',
  120.         '#default_value' => $order->uid,
  121.       );
  122.       $form['customer']['text']['uid_text'] = array(
  123.         '#type' => 'textfield',
  124.         '#title' => t('Customer number'),
  125.         '#default_value' => $order->uid,
  126.         '#maxlength' => 10,
  127.         '#size' => 10,
  128.         '#disabled' => TRUE,
  129.       );
  130.       $form['customer']['primary_email'] = array(
  131.         '#type' => 'hidden',
  132.         '#default_value' => $order->primary_email,
  133.       );
  134.       $form['customer']['text']['primary_email_text'] = array(
  135.         '#type' => 'textfield',
  136.         '#title' => t('Primary e-mail'),
  137.         '#default_value' => $order->primary_email,
  138.         '#maxlength' => 64,
  139.         '#size' => 32,
  140.         '#disabled' => TRUE,
  141.       );
  142.       return $form;
  143.  
  144.     case 'edit-theme':
  145.       $output = '<div class="order-pane-icons">';
  146.       $output .= ' <img src="' . base_path() . drupal_get_path('module', 'uc_store')
  147.                 . '/images/order_view.gif" alt="' . t('Search for an existing customer.') . '" '
  148.                 . 'title="' . t('Search for an existing customer.') . '" onclick="load_customer_search();" />';
  149.       $output .= ' <img src="' . base_path() . drupal_get_path('module', 'uc_store')
  150.                 . '/images/menu_customers_small.gif" alt="' . t('Create a new customer.') . '" '
  151.                 . 'title="' . t('Create a new customer.') . '" onclick="load_new_customer_form();" />';
  152.       $output .= '</div>';
  153.       $output .= '<div id="customer-select"></div><table class="order-edit-table">';
  154.       foreach (element_children($form['customer']['text']) as $field) {
  155.         $title = $form['customer']['text'][$field]['#title'];
  156.         $form['customer']['text'][$field]['#title'] = NULL;
  157.         $output .= '<tr><td class="oet-label">' . $title . ':</td><td>'
  158.                  . drupal_render($form['customer']['text'][$field]) . '</td></tr>';
  159.       }
  160.       $output .= '</table>' . drupal_render($form['customer']['primary_email'])
  161.                . drupal_render($form['customer']['uid']);
  162.       return $output;
  163.  
  164.     case 'edit-process':
  165.       $changes['uid'] = $form_state['values']['uid'];
  166.       $changes['primary_email'] = $form_state['values']['primary_email'];
  167.       return $changes;
  168.   }
  169. }
  170.  
  171. /**
  172.  * Handles the "Products" order pane.
  173.  */
  174. function uc_order_pane_products($op, $order, &$form = NULL, &$form_state = NULL) {
  175.   switch ($op) {
  176.     case 'view':
  177.       return tapir_get_table('uc_op_products_view_table', $order);
  178.  
  179.     case 'customer':
  180.       return tapir_get_table('uc_op_products_customer_table', $order);
  181.  
  182.     case 'edit-form':
  183.       $form['add_product_button'] = array(
  184.         '#type' => 'submit',
  185.         '#value' => t('Add product'),
  186.         '#submit' => array('uc_order_pane_products_select'),
  187.         '#ajax' => array(
  188.           'callback' => 'uc_order_pane_products_ajax_callback',
  189.           'wrapper' => 'product-controls',
  190.         ),
  191.       );
  192.       $form['add_blank_line_button'] = array(
  193.         '#type' => 'submit',
  194.         '#value' => t('Add blank line'),
  195.         '#submit' => array('uc_order_edit_products_add_blank'),
  196.         '#ajax' => array(
  197.           'callback' => 'uc_order_pane_products_ajax_callback',
  198.           'wrapper' => 'product-controls',
  199.         ),
  200.       );
  201.  
  202.       $form['product_controls'] = array(
  203.         '#tree' => TRUE,
  204.         '#prefix' => '<div id="product-controls">',
  205.         '#suffix' => '</div>',
  206.       );
  207.  
  208.       $controls = array();
  209.  
  210.       if (isset($form_state['products_action'])) {
  211.         switch ($form_state['products_action']) {
  212.           case 'select':
  213.             $controls = uc_order_product_select_form($form['product_controls'], $form_state, $order);
  214.             break;
  215.           case 'add_product':
  216.             $controls = uc_order_add_product_form($form['product_controls'], $form_state, $order, $form_state['node']);
  217.             break;
  218.         }
  219.       }
  220.  
  221.       $form['product_controls'] += $controls;
  222.  
  223.       $form += uc_order_edit_products_form($form, $form_state, $order->products);
  224.  
  225.       return $form;
  226.  
  227.     case 'edit-theme':
  228.       $output = drupal_render($form['add_product_button']);
  229.       $output .= drupal_render($form['add_blank_line_button']);
  230.       $output .= drupal_render($form['product_controls']);
  231.       $output .= drupal_render($form['products']);
  232.  
  233.       return $output;
  234.  
  235.     case 'edit-process':
  236.       if (isset($form_state['values']['products'])) {
  237.         foreach ($form_state['values']['products'] as $key => $product) {
  238.           $product['data'] = unserialize($product['data']);
  239.           uc_order_product_save($order->order_id, (object) $product);
  240.         }
  241.       }
  242.  
  243.       break;
  244.   }
  245. }
  246.  
  247. /**
  248.  * Form to choose a product to add to the order.
  249.  *
  250.  * @ingroup forms
  251.  */
  252. function uc_order_product_select_form($form, &$form_state, $order) {
  253.   $options = $form_state['product_select_options'];
  254.   $ajax = array(
  255.     'callback' => 'uc_order_pane_products_ajax_callback',
  256.     'wrapper' => 'product-controls',
  257.   );
  258.  
  259.   $form['nid'] = array(
  260.     '#type' => 'select',
  261.     '#options' => $options,
  262.     '#size' => 7,
  263.     '#ajax' => $ajax + array(
  264.       'event' => 'dblclick',
  265.       'trigger_as' => array(
  266.         'name' => 'op',
  267.         'value' => t('Select'),
  268.       ),
  269.     ),
  270.   );
  271.   $form['product_search'] = array(
  272.     '#type' => 'textfield',
  273.     '#title' => t('Search by name or model/SKU (* is the wildcard)'),
  274.   );
  275.  
  276.   $form['actions'] = array('#type' => 'actions');
  277.   $form['actions']['select'] = array(
  278.     '#type' => 'submit',
  279.     '#value' => t('Select'),
  280.     '#validate' => array('uc_order_product_select_form_validate'),
  281.     '#submit' => array('uc_order_pane_products_add'),
  282.     '#ajax' => $ajax,
  283.     '#weight' => 0,
  284.   );
  285.   $form['actions']['search'] = array(
  286.     '#type' => 'submit',
  287.     '#value' => t('Search'),
  288.     '#submit' => array('uc_order_pane_products_select'),
  289.     '#ajax' => $ajax,
  290.     '#weight' => 1,
  291.   );
  292.   $form['actions']['close'] = array(
  293.     '#type' => 'submit',
  294.     '#value' => t('Close'),
  295.     '#submit' => array('uc_order_pane_products_close'),
  296.     '#ajax' => $ajax,
  297.     '#weight' => 2,
  298.   );
  299.  
  300.   return $form;
  301. }
  302.  
  303. /**
  304.  * Validation handler for uc_order_product_select_form().
  305.  */
  306. function uc_order_product_select_form_validate($form, &$form_state) {
  307.   if (empty($form_state['values']['product_controls']['nid'])) {
  308.     form_set_error('product_controls][nid', t('Please select a product.'));
  309.   }
  310. }
  311.  
  312. /**
  313.  * Sets the quantity and attributes of a product added to the order.
  314.  *
  315.  * @see uc_order_add_product_form()
  316.  * @ingroup forms
  317.  */
  318. function uc_order_add_product_form($form, &$form_state, $order, $node) {
  319.    $data = array();
  320.   if (isset($form_state['values']['product_controls']['qty'])) {
  321.     $data += module_invoke_all('uc_add_to_cart_data', $form_state['values']['product_controls']);
  322.   }
  323.   if (!empty($node->data) && is_array($node->data)) {
  324.     $data += $node->data;
  325.   }
  326.   $node = uc_product_load_variant(intval($form_state['values']['product_controls']['nid']), $data);
  327.   $form['title'] = array(
  328.     '#markup' => '<h3>' . check_plain($node->title) . '</h3>',
  329.   );
  330.   $form['nid'] = array(
  331.     '#type' => 'hidden',
  332.     '#value' => $node->nid,
  333.   );
  334.   $form['qty'] = array(
  335.     '#type' => 'uc_quantity',
  336.     '#title' => theme('uc_qty_label'),
  337.     '#default_value' => 1,
  338.   );
  339.   $form['actions'] = array('#type' => 'actions');
  340.   $form['actions']['submit'] = array(
  341.     '#type' => 'submit',
  342.     '#value' => t('Add to order'),
  343.     '#submit' => array('uc_order_edit_products_add'),
  344.     '#ajax' =>  array(
  345.       'callback' => 'uc_order_pane_products_ajax_callback',
  346.       'wrapper' => 'product-controls',
  347.     ),
  348.   );
  349.   $form['actions']['cancel'] = array(
  350.     '#type' => 'submit',
  351.     '#value' => t('Cancel'),
  352.     '#submit' => array('uc_order_pane_products_select'),
  353.     '#ajax' =>  array(
  354.       'callback' => 'uc_order_pane_products_ajax_callback',
  355.       'wrapper' => 'product-controls',
  356.     ),
  357.     '#limit_validation_errors' => array(),
  358.   );
  359.   $form['node'] = array(
  360.     '#type' => 'value',
  361.     '#value' => $node,
  362.   );
  363.  
  364.   uc_form_alter($form, $form_state, __FUNCTION__);
  365.  
  366.   return $form;
  367. }
  368.  
  369. /**
  370.  * Form to allow ordered products' data to be changed.
  371.  *
  372.  * @see uc_op_products_edit_table()
  373.  * @see theme_uc_order_edit_products_form()
  374.  */
  375. function uc_order_edit_products_form($form, &$form_state, $products) {
  376.   if (($product_count = count($products)) > 0) {
  377.     $form['products'] = tapir_get_table('uc_op_products_edit_table');
  378.     for ($i=0, $product = reset($products); $i < $product_count; $i++, $product = next($products)) {
  379.       $form['products'][$i]['remove'] = array(
  380.         '#type' => 'image_button',
  381.         '#title' => t('Remove this product.'),
  382.         '#src' => drupal_get_path('module', 'uc_store') . '/images/error.gif',
  383.         '#button_type' => 'remove',
  384.         '#submit' => array('uc_order_edit_products_remove', 'uc_order_edit_form_submit'),
  385.         '#return_value' => $product->order_product_id,
  386.       );
  387.       $form['products'][$i]['order_product_id'] = array(
  388.         '#type' => 'hidden',
  389.         '#value' => $product->order_product_id,
  390.       );
  391.       $form['products'][$i]['nid'] = array(
  392.         '#type' => 'hidden',
  393.         '#value' => $product->nid,
  394.       );
  395.       $form['products'][$i]['qty'] = array(
  396.         '#type' => 'uc_quantity',
  397.         '#title' => theme('uc_qty_label'),
  398.         '#title_display' => 'invisible',
  399.         '#default_value' => $product->qty,
  400.       );
  401.       $form['products'][$i]['title'] = array(
  402.         '#type' => 'textfield',
  403.         '#title' => t('Title'),
  404.         '#title_display' => 'invisible',
  405.         '#default_value' => $product->title,
  406.         '#size' => 30,
  407.         '#maxlength' => 255,
  408.       );
  409.       $form['products'][$i]['model'] = array(
  410.         '#type' => 'textfield',
  411.         '#title' => t('SKU'),
  412.         '#title_display' => 'invisible',
  413.         '#default_value' => $product->model,
  414.         '#size' => 6,
  415.       );
  416.       $form['products'][$i]['weight'] = array(
  417.         '#type' => 'textfield',
  418.         '#title' => t('Weight'),
  419.         '#title_display' => 'invisible',
  420.         '#default_value' => $product->weight,
  421.         '#size' => 3,
  422.       );
  423.       $units = array(
  424.         'lb' => t('Pounds'),
  425.         'kg' => t('Kilograms'),
  426.         'oz' => t('Ounces'),
  427.         'g'  => t('Grams'),
  428.       );
  429.       $form['products'][$i]['weight_units'] = array(
  430.         '#type' => 'select',
  431.         '#title' => t('Units'),
  432.         '#title_display' => 'invisible',
  433.         '#default_value' => $product->weight_units,
  434.         '#options' => $units,
  435.       );
  436.       $form['products'][$i]['cost'] = array(
  437.         '#type' => 'uc_price',
  438.         '#title' => t('Cost'),
  439.         '#title_display' => 'invisible',
  440.         '#default_value' => $product->cost,
  441.         '#size' => 5,
  442.       );
  443.       $form['products'][$i]['price'] = array(
  444.         '#type' => 'uc_price',
  445.         '#title' => t('Price'),
  446.         '#title_display' => 'invisible',
  447.         '#default_value' => $product->price,
  448.         '#size' => 5,
  449.       );
  450.       $form['products'][$i]['data'] = array(
  451.         '#type' => 'hidden',
  452.         '#value' => serialize($product->data),
  453.       );
  454.     }
  455.   }
  456.   else {
  457.     $form['products'] = array(
  458.       '#markup' => t('This order contains no products.'),
  459.       '#prefix' => '<div id="order-edit-products">',
  460.       '#suffix' => '</div>',
  461.     );
  462.   }
  463.  
  464.   return $form;
  465. }
  466.  
  467. /**
  468.  * Sets the order pane to show the product selection form.
  469.  */
  470. function uc_order_pane_products_select($form, &$form_state) {
  471.   $types = uc_product_types();
  472.   $options = array();
  473.  
  474.   if (!empty($form_state['values']['product_controls']['product_search'])) {
  475.     $search = strtolower(str_replace('*', '%', $form_state['values']['product_controls']['product_search']));
  476.     $search_args = array(
  477.       ':types' => $types,
  478.       ':title' => $search,
  479.       ':model' => $search,
  480.     );
  481.  
  482.     $result = db_query("SELECT n.nid, n.title FROM {node} AS n LEFT JOIN "
  483.       . "{uc_products} AS p ON n.nid = p.nid WHERE n.type IN "
  484.       . "(:types) AND (n.title LIKE :title OR p.model LIKE :model)"
  485.       . " ORDER BY n.title", $search_args);
  486.   }
  487.   else {
  488.     $result = db_query("SELECT nid, title FROM {node} WHERE type IN (:types) ORDER BY title", array(':types' => $types));
  489.   }
  490.   foreach ($result as $row) {
  491.     $options[$row->nid] = $row->title;
  492.   }
  493.  
  494.   if (count($options) == 0) {
  495.     $options[0] = t('No products found.');
  496.   }
  497.  
  498.   $form_state['products_action'] = 'select';
  499.   $form_state['product_select_options'] = $options;
  500.   unset($form_state['refresh_products']);
  501.   $form_state['rebuild'] = TRUE;
  502. }
  503.  
  504. /**
  505.  * Sets the order pane to show the add product to order form.
  506.  */
  507. function uc_order_pane_products_add($form, &$form_state) {
  508.   $form_state['products_action'] = 'add_product';
  509.   $form_state['node'] = node_load($form_state['values']['product_controls']['nid']);
  510.   unset($form_state['refresh_products']);
  511.   $form_state['rebuild'] = TRUE;
  512. }
  513.  
  514. /**
  515.  * Hides the form to add another product to the order.
  516.  */
  517. function uc_order_pane_products_close($form, &$form_state) {
  518.   unset($form_state['products_action']);
  519.   unset($form_state['refresh_products']);
  520.   unset($form_state['product_select_options']);
  521.   $form_state['rebuild'] = TRUE;
  522. }
  523.  
  524. /**
  525.  * Form submit callback: add a blank line product to an order.
  526.  */
  527. function uc_order_edit_products_add_blank($form, &$form_state) {
  528.   $form_state['refresh_products'] = TRUE;
  529.   $form_state['rebuild'] = TRUE;
  530.  
  531.   $order = $form_state['build_info']['args'][0];
  532.  
  533.   $product = new stdClass();
  534.   $product->qty = 1;
  535.   $product->order_id = $order->order_id;
  536.   drupal_write_record('uc_order_products', $product);
  537.  
  538.   $order->products[] = $product;
  539.  
  540.   uc_order_log_changes($order->order_id, array('add' => t('Added new product line to order.')));
  541. }
  542.  
  543. /**
  544.  * Form submit callback: add a product to an order.
  545.  */
  546. function uc_order_edit_products_add($form, &$form_state) {
  547.   $form_state['products_action'] = 'products_select';
  548.   $form_state['refresh_products'] = TRUE;
  549.   $form_state['rebuild'] = TRUE;
  550.   $order = $form_state['build_info']['args'][0];
  551.  
  552.   $data = module_invoke_all('uc_add_to_cart_data', $form_state['values']['product_controls']);
  553.   $product = uc_product_load_variant(intval($form_state['values']['product_controls']['nid']), $data);
  554.   $product->qty = isset($form_state['values']['product_controls']['qty']) ? $form_state['values']['product_controls']['qty'] : $product->default_qty;
  555.  
  556.   drupal_alter('uc_order_product', $product, $order);
  557.   uc_order_product_save($order->order_id, $product);
  558.   $order->products[] = $product;
  559.  
  560.   uc_order_log_changes($order->order_id, array('add' => t('Added (@qty) @title to order.', array('@qty' => $product->qty, '@title' => $product->title))));
  561.  
  562.   // Decrement stock.
  563.   if (module_exists('uc_stock')) {
  564.     uc_stock_adjust_product_stock($product, 0, $order);
  565.   }
  566.  
  567.   // Add this product to the form values for accurate tax calculations.
  568.   $form_state['values']['products'][] = (array) $product;
  569. }
  570.  
  571. /**
  572.  * Form submit callback: remove a product from an order.
  573.  */
  574. function uc_order_edit_products_remove($form, &$form_state) {
  575.   $form_state['refresh_products'] = TRUE;
  576.  
  577.   $order_product_id = intval($form_state['triggering_element']['#return_value']);
  578.  
  579.   uc_order_product_delete($order_product_id);
  580.  
  581.   $order = $form_state['build_info']['args'][0];
  582.   $matches = array();
  583.   preg_match('/products\[(\d+)\]/', $form_state['triggering_element']['#name'], $matches);
  584.   $key = $matches[1];
  585.  
  586.   unset($order->products[$key]);
  587.   $order->products = array_values($order->products);
  588. }
  589.  
  590. /**
  591.  * AJAX callback to render the order product controls.
  592.  */
  593. function uc_order_pane_products_ajax_callback($form, &$form_state) {
  594.   $commands[] = ajax_command_replace('#product-controls', drupal_render($form['product_controls']));
  595.   $commands[] = ajax_command_prepend('#product-controls', theme('status_messages'));
  596.  
  597.   if (isset($form_state['refresh_products']) && $form_state['refresh_products']) {
  598.     $commands[] = ajax_command_replace('#order-edit-products', drupal_render($form['products']));
  599.     $commands[] = ajax_command_replace('#order-line-items', drupal_render($form['line_items']));
  600.     $commands[] = ajax_command_prepend('#order-edit-products', theme('status_messages'));
  601.   }
  602.  
  603.   return array('#type' => 'ajax', '#commands' => $commands);
  604. }
  605.  
  606. /**
  607.  * Handles the "Line Items" order pane.
  608.  */
  609. function uc_order_pane_line_items($op, $order, &$form = NULL, &$form_state = NULL) {
  610.   switch ($op) {
  611.     case 'view':
  612.     case 'customer':
  613.       $line_items = $order->line_items;
  614.       $items = _uc_line_item_list();
  615.       foreach ($items as $item) {
  616.         if (isset($item['display_only']) && $item['display_only'] == TRUE) {
  617.           $result = $item['callback']('display', $order);
  618.           if (is_array($result)) {
  619.             foreach ($result as $line) {
  620.               $line_items[] = array(
  621.                 'title' => $line['title'],
  622.                 'amount' => $line['amount'],
  623.                 'weight' => $item['weight']
  624.               );
  625.             }
  626.           }
  627.         }
  628.       }
  629.       usort($line_items, 'uc_weight_sort');
  630.  
  631.       $build['line_items'] = array(
  632.         '#prefix' => '<table class="line-item-table">',
  633.         '#suffix' => '</table>',
  634.       );
  635.       foreach ($line_items as $item) {
  636.         $table_row = array(
  637.           '#prefix' => '<tr>',
  638.           '#suffix' => '</tr>',
  639.         );
  640.  
  641.         $table_row['title'] = array(
  642.           '#markup' => check_plain($item['title']),
  643.           '#prefix' => '<td class="li-title">',
  644.           '#suffix' => '</td>',
  645.         );
  646.  
  647.         $table_row['amount'] = array(
  648.           '#theme' => 'uc_price',
  649.           '#price' => $item['amount'],
  650.           '#prefix' => '<td class="li-amount">',
  651.           '#suffix' => '</td>',
  652.         );
  653.  
  654.         $build['line_items'][] = $table_row;
  655.       }
  656.  
  657.       return $build;
  658.  
  659.     case 'edit-form':
  660.       $options = array();
  661.       $items = _uc_line_item_list();
  662.       $line_items = $order->line_items;
  663.       foreach ($items as $item) {
  664.         if (isset($item['add_list']) && $item['add_list'] === TRUE) {
  665.           $options[$item['id']] = check_plain($item['title']);
  666.         }
  667.         if (isset($item['display_only']) && $item['display_only'] == TRUE) {
  668.           $result = $item['callback']('display', $order);
  669.           if (is_array($result)) {
  670.             foreach ($result as $line) {
  671.               $line_items[] = array(
  672.                 'line_item_id' => $line['id'],
  673.                 'title' => $line['title'],
  674.                 'amount' => $line['amount'],
  675.                 'weight' => $item['weight'],
  676.               );
  677.             }
  678.           }
  679.         }
  680.       }
  681.       usort($line_items, 'uc_weight_sort');
  682.  
  683.       $form['add_line_item'] = array('#type' => 'container');
  684.  
  685.       $form['add_line_item']['li_type_select'] = array(
  686.         '#type' => 'select',
  687.         '#title' => t('Add a line item'),
  688.         '#options' => $options,
  689.       );
  690.       $form['add_line_item']['submit'] = array(
  691.         '#type' => 'submit',
  692.         '#value' => t('Add line'),
  693.         '#submit' => array('uc_order_pane_line_items_submit', 'uc_order_pane_line_items_add'),
  694.         '#ajax' => array(
  695.           'callback' => 'uc_order_pane_line_items_update',
  696.         ),
  697.       );
  698.       $form['line_items'] = array(
  699.         '#tree' => TRUE,
  700.         '#theme' => 'uc_order_pane_line_items',
  701.         '#prefix' => '<div id="order-line-items">',
  702.         '#suffix' => '</div>',
  703.       );
  704.  
  705.       foreach ($line_items as $item) {
  706.         $form['line_items'][$item['line_item_id']]['li_id'] = array(
  707.           '#type' => 'hidden',
  708.           '#value' => $item['line_item_id'],
  709.         );
  710.         if (isset($item['type']) && _uc_line_item_data($item['type'], 'stored') == TRUE) {
  711.           $form['line_items'][$item['line_item_id']]['remove'] = array(
  712.             '#type' => 'image_button',
  713.             '#title' => t('Remove line item.'),
  714.             '#src' => drupal_get_path('module', 'uc_store') . '/images/error.gif',
  715.             '#button_type' => 'remove',
  716.             '#submit' => array('uc_order_pane_line_items_submit', 'uc_order_pane_line_items_remove'),
  717.             '#ajax' => array(
  718.               'callback' => 'uc_order_pane_line_items_update',
  719.             ),
  720.             '#return_value' => $item['line_item_id'],
  721.           );
  722.           $form['line_items'][$item['line_item_id']]['title'] = array(
  723.             '#type' => 'textfield',
  724.             '#title' => t('Title'),
  725.             '#default_value' => $item['title'],
  726.             '#size' => 40,
  727.             '#maxlength' => 128,
  728.           );
  729.           $form['line_items'][$item['line_item_id']]['amount'] = array(
  730.             '#type' => 'uc_price',
  731.             '#title' => t('Amount'),
  732.             '#default_value' => $item['amount'],
  733.             '#size' => 6,
  734.             '#allow_negative' => TRUE,
  735.           );
  736.         }
  737.         else {
  738.           $form['line_items'][$item['line_item_id']]['title'] = array(
  739.             '#markup' => check_plain($item['title']),
  740.           );
  741.           $form['line_items'][$item['line_item_id']]['amount'] = array(
  742.             '#theme' => 'uc_price',
  743.             '#price' => $item['amount'],
  744.           );
  745.         }
  746.       }
  747.       return $form;
  748.  
  749.     case 'edit-theme':
  750.       return drupal_render($form['add_line_item'])
  751.            . drupal_render($form['line_items']);
  752.  
  753.     case 'edit-process':
  754.       uc_order_pane_line_items_submit($form, $form_state);
  755.       return;
  756.   }
  757. }
  758.  
  759. /**
  760.  * @ingroup themeable
  761.  */
  762. function theme_uc_order_pane_line_items($variables) {
  763.   $form = $variables['form'];
  764.  
  765.   $output = '<table class="line-item-table">';
  766.   foreach (element_children($form) as $field) {
  767.     $form[$field]['title']['#title'] = '';
  768.     $form[$field]['amount']['#title'] = '';
  769.     $output .= '<tr><td class="li-title">'
  770.       . drupal_render($form[$field]['li_id'])
  771.       . drupal_render($form[$field]['remove'])
  772.       . drupal_render($form[$field]['title'])
  773.       . ':</td><td class="li-amount">'
  774.       . drupal_render($form[$field]['amount'])
  775.       . '</td></tr>';
  776.   }
  777.   $output .= '</table>' . drupal_render_children($form);
  778.  
  779.   return $output;
  780. }
  781.  
  782. /**
  783.  * Form submit callback: Update line items titles and amounts in an order.
  784.  */
  785. function uc_order_pane_line_items_submit($form, &$form_state) {
  786.   $values = $form_state['values'];
  787.  
  788.   if (is_array($values['line_items'])) {
  789.     foreach ($values['line_items'] as $line) {
  790.       if (is_numeric($line['li_id']) && intval($line['li_id']) > 0 && isset($line['title']) && isset($line['amount'])) {
  791.         uc_order_update_line_item($line['li_id'], $line['title'], $line['amount']);
  792.       }
  793.     }
  794.   }
  795. }
  796.  
  797. /**
  798.  * Order pane submit callback: Add a line item to an order.
  799.  */
  800. function uc_order_pane_line_items_add($form, &$form_state) {
  801.   $order = &$form_state['order'];
  802.   $type = $form_state['values']['li_type_select'];
  803.  
  804.   uc_order_line_item_add($order->order_id, $type, _uc_line_item_data($type, 'title'), 0);
  805.   $order->line_items = uc_order_load_line_items($order);
  806.  
  807.   $form_state['rebuild'] = TRUE;
  808. }
  809.  
  810. /**
  811.  * Order pane submit callback: Remove a line item from an order.
  812.  */
  813. function uc_order_pane_line_items_remove($form, &$form_state) {
  814.   $order = &$form_state['order'];
  815.   $line_item_id = intval($form_state['triggering_element']['#return_value']);
  816.  
  817.   uc_order_delete_line_item($line_item_id);
  818.   $order->line_items = uc_order_load_line_items($order);
  819.  
  820.   $form_state['rebuild'] = TRUE;
  821. }
  822.  
  823. /**
  824.  * AJAX callback to render the line items.
  825.  */
  826. function uc_order_pane_line_items_update($form, &$form_state) {
  827.   $commands[] = ajax_command_replace('#order-line-items', drupal_render($form['line_items']));
  828.   $commands[] = ajax_command_prepend('#order-line-items', theme('status_messages'));
  829.  
  830.   return array('#type' => 'ajax', '#commands' => $commands);
  831. }
  832.  
  833. /**
  834.  * Handles the "Order Comments" order pane.
  835.  */
  836. function uc_order_pane_order_comments($op, $order, &$form = NULL, &$form_state = NULL) {
  837.   switch ($op) {
  838.     case 'view':
  839.       $comments = uc_order_comments_load($order->order_id);
  840.       return tapir_get_table('uc_op_order_comments_view_table', $comments);
  841.  
  842.     case 'customer':
  843.       $comments = uc_order_comments_load($order->order_id);
  844.       $header = array(t('Date'), t('Status'), t('Message'));
  845.       $rows[] = array(
  846.         array('data' => format_date($order->created, 'uc_store'), 'class' => array('date')),
  847.         array('data' => '-', 'class' => array('status')),
  848.         array('data' => t('Order created.'), 'class' => array('message')),
  849.       );
  850.       if (count($comments) > 0) {
  851.         foreach ($comments as $comment) {
  852.           $rows[] = array(
  853.             array('data' => format_date($comment->created, 'uc_store'), 'class' => array('date')),
  854.             array('data' => $comment->title, 'class' => array('status')),
  855.             array('data' => check_plain($comment->message), 'class' => array('message')),
  856.           );
  857.         }
  858.       }
  859.       $build = array(
  860.         '#theme' => 'table',
  861.         '#header' => $header,
  862.         '#rows' => $rows,
  863.         '#attributes' => array('class' => array('uc-order-comments')),
  864.       );
  865.  
  866.       return $build;
  867.   }
  868. }
  869.  
  870. /**
  871.  * Handles the "Admin Comments" order pane.
  872.  */
  873. function uc_order_pane_admin_comments($op, $order, &$form = NULL, &$form_state = NULL) {
  874.   global $user;
  875.  
  876.   switch ($op) {
  877.     case 'view':
  878.       $comments = uc_order_comments_load($order->order_id, TRUE);
  879.       return tapir_get_table('uc_op_admin_comments_view_table', $comments);
  880.  
  881.     case 'edit-form':
  882.       $form['admin_comment_field'] = array(
  883.         '#type' => 'fieldset',
  884.         '#title' => t('Add an admin comment'),
  885.         '#collapsible' => TRUE,
  886.         '#collapsed' => TRUE,
  887.       );
  888.       $form['admin_comment_field']['admin_comment'] = array(
  889.         '#type' => 'textarea',
  890.         '#description' => t('Admin comments are only seen by store administrators.'),
  891.       );
  892.       return $form;
  893.  
  894.     case 'edit-theme':
  895.       $comments = uc_order_comments_load($form['order_id']['#value'], TRUE);
  896.       if (is_array($comments) && count($comments) > 0) {
  897.         foreach ($comments as $comment) {
  898.           $items[] = '[' . theme('uc_uid', array('uid' => $comment->uid)) . '] ' . filter_xss_admin($comment->message);
  899.         }
  900.       }
  901.       else {
  902.         $items = array(t('No admin comments have been entered for this order.'));
  903.       }
  904.       $output = theme('item_list', array('items' => $items)) . drupal_render($form['admin_comment_field']);
  905.       return $output;
  906.  
  907.     case 'edit-process':
  908.       if (!empty($form_state['values']['admin_comment'])) {
  909.         uc_order_comment_save($form_state['values']['order_id'], $user->uid, $form_state['values']['admin_comment']);
  910.       }
  911.       return;
  912.   }
  913. }
  914.  
  915. /**
  916.  * Handles the "Update" order pane.
  917.  */
  918. function uc_order_pane_update($op, $order, &$form = NULL, &$form_state = NULL) {
  919.   switch ($op) {
  920.     case 'view':
  921.       return drupal_get_form('uc_order_view_update_form', $order);
  922.   }
  923. }
  924.  
  925. /**
  926.  * Form to save order comments and update the order status.
  927.  *
  928.  * @see uc_order_view_update_form_submit()
  929.  * @ingroup forms
  930.  */
  931. function uc_order_view_update_form($form, &$form_state, $order) {
  932.   $form['order_comment_field'] = array(
  933.     '#type' => 'fieldset',
  934.     '#title' => t('Add an order comment'),
  935.     '#collapsible' => TRUE,
  936.     '#collapsed' => TRUE,
  937.   );
  938.   $form['order_comment_field']['order_comment'] = array(
  939.     '#type' => 'textarea',
  940.     '#description' => t('Order comments are used primarily to communicate with the customer.'),
  941.   );
  942.  
  943.   $form['admin_comment_field'] = array(
  944.     '#type' => 'fieldset',
  945.     '#title' => t('Add an admin comment'),
  946.     '#collapsible' => TRUE,
  947.     '#collapsed' => TRUE,
  948.   );
  949.   $form['admin_comment_field']['admin_comment'] = array(
  950.     '#type' => 'textarea',
  951.     '#description' => t('Admin comments are only seen by store administrators.'),
  952.   );
  953.  
  954.   $form['current_status'] = array(
  955.     '#type' => 'hidden',
  956.     '#value' => $order->order_status,
  957.   );
  958.  
  959.   $form['order_id'] = array(
  960.     '#type' => 'hidden',
  961.     '#value' => $order->order_id,
  962.   );
  963.  
  964.   $form['controls'] = array(
  965.     '#type' => 'container',
  966.     '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
  967.     '#weight' => 10,
  968.   );
  969.  
  970.   foreach (uc_order_status_list() as $status) {
  971.     $options[$status['id']] = $status['title'];
  972.   }
  973.   $form['controls']['status'] = array(
  974.     '#type' => 'select',
  975.     '#title' => t('Order status'),
  976.     '#default_value' => $order->order_status,
  977.     '#options' => $options,
  978.   );
  979.  
  980.   $form['controls']['notify'] = array(
  981.     '#type' => 'checkbox',
  982.     '#title' => t('Send e-mail notification on update.'),
  983.   );
  984.  
  985.   $form['controls']['actions'] = array('#type' => 'actions');
  986.   $form['controls']['actions']['submit'] = array(
  987.     '#type' => 'submit',
  988.     '#value' => t('Update'),
  989.   );
  990.  
  991.   return $form;
  992. }
  993.  
  994. /**
  995.  * Form submit handler for uc_order_view_update_form().
  996.  *
  997.  * @see uc_order_view_update_form()
  998.  */
  999. function uc_order_view_update_form_submit($form, &$form_state) {
  1000.   global $user;
  1001.  
  1002.   if (!empty($form_state['values']['order_comment'])) {
  1003.     uc_order_comment_save($form_state['values']['order_id'], $user->uid, $form_state['values']['order_comment'], 'order', $form_state['values']['status'], $form_state['values']['notify']);
  1004.   }
  1005.  
  1006.   if (!empty($form_state['values']['admin_comment'])) {
  1007.     uc_order_comment_save($form_state['values']['order_id'], $user->uid, $form_state['values']['admin_comment']);
  1008.   }
  1009.  
  1010.   if ($form_state['values']['status'] != $form_state['values']['current_status']) {
  1011.     if (uc_order_update_status($form_state['values']['order_id'], $form_state['values']['status'])) {
  1012.       if (empty($form_state['values']['order_comment'])) {
  1013.         uc_order_comment_save($form_state['values']['order_id'], $user->uid, '-', 'order', $form_state['values']['status'], $form_state['values']['notify']);
  1014.       }
  1015.     }
  1016.   }
  1017.  
  1018.   // Let Rules send email if requested.
  1019.   if ($form_state['values']['notify']) {
  1020.     $order = uc_order_load($form_state['values']['order_id']);
  1021.     rules_invoke_event('uc_order_status_email_update', $order);
  1022.   }
  1023.  
  1024.   drupal_set_message(t('Order updated.'));
  1025. }
  1026.  
  1027. /**
  1028.  * Builds the order view products table.
  1029.  *
  1030.  * !FIXME Refactor to use uc_order_product_view.
  1031.  */
  1032. function uc_op_products_view_table($order) {
  1033.   $table = array(
  1034.     '#type' => 'tapir_table',
  1035.     '#attributes' => array('class' => array('order-pane-table')),
  1036.   );
  1037.  
  1038.   $table['#columns']['qty'] = array(
  1039.     'cell' => array(
  1040.       'data' => theme('uc_qty_label'),
  1041.       'class' => array('qty'),
  1042.     ),
  1043.     'weight' => 0,
  1044.   );
  1045.   $table['#columns']['product'] = array(
  1046.     'cell' => array(
  1047.       'data' => t('Product'),
  1048.       'class' => array('product'),
  1049.     ),
  1050.     'weight' => 1,
  1051.   );
  1052.   $table['#columns']['model'] = array(
  1053.     'cell' => array(
  1054.       'data' => t('SKU'),
  1055.       'class' => array('sku'),
  1056.     ),
  1057.     'weight' => 2,
  1058.   );
  1059.   if (user_access('administer products')) {
  1060.     $table['#columns']['cost'] = array(
  1061.       'cell' => array(
  1062.         'data' => t('Cost'),
  1063.         'class' => array('cost'),
  1064.       ),
  1065.       'weight' => 3,
  1066.     );
  1067.   }
  1068.   $table['#columns']['price'] = array(
  1069.     'cell' => array(
  1070.       'data' => t('Price'),
  1071.       'class' => array('price'),
  1072.     ),
  1073.     'weight' => 4,
  1074.   );
  1075.   $table['#columns']['total'] = array(
  1076.     'cell' => array(
  1077.       'data' => t('Total'),
  1078.       'class' => array('total'),
  1079.     ),
  1080.     'weight' => 5,
  1081.   );
  1082.  
  1083.   if (!empty($order->products)) {
  1084.     $build = entity_view('uc_order_product', $order->products);
  1085.     $table['#rows'] = $build['uc_order_product'];
  1086.   }
  1087.   else {
  1088.     $table['#rows'][]['product'] = array(
  1089.       '#markup' => t('This order contains no products.'),
  1090.       '#cell_attributes' => array('colspan' => 'full'),
  1091.     );
  1092.   }
  1093.  
  1094.   return $table;
  1095. }
  1096.  
  1097. /**
  1098.  * Builds the order customer's view products table.
  1099.  *
  1100.  * !FIXME Refactor to use uc_order_product_view.
  1101.  */
  1102. function uc_op_products_customer_table($order) {
  1103.   $table = array(
  1104.     '#type' => 'tapir_table',
  1105.     '#attributes' => array('class' => array('order-pane-table')),
  1106.   );
  1107.  
  1108.   $table['#columns']['qty'] = array(
  1109.     'cell' => array(
  1110.       'data' => theme('uc_qty_label'),
  1111.       'class' => array('qty'),
  1112.     ),
  1113.     'weight' => 0,
  1114.   );
  1115.   $table['#columns']['product'] = array(
  1116.     'cell' => array(
  1117.       'data' => t('Product'),
  1118.       'class' => array('product'),
  1119.     ),
  1120.     'weight' => 1,
  1121.   );
  1122.   $table['#columns']['model'] = array(
  1123.     'cell' => array(
  1124.       'data' => t('SKU'),
  1125.       'class' => array('sku'),
  1126.     ),
  1127.     'weight' => 2,
  1128.   );
  1129.   if (user_access('administer products')) {
  1130.     $table['#columns']['cost'] = array(
  1131.       'cell' => array(
  1132.         'data' => t('Cost'),
  1133.         'class' => array('cost'),
  1134.       ),
  1135.       'weight' => 3,
  1136.     );
  1137.   }
  1138.   $table['#columns']['price'] = array(
  1139.     'cell' => array(
  1140.       'data' => t('Price'),
  1141.       'class' => array('price'),
  1142.     ),
  1143.     'weight' => 4,
  1144.   );
  1145.   $table['#columns']['total'] = array(
  1146.     'cell' => array(
  1147.       'data' => t('Total'),
  1148.       'class' => array('total'),
  1149.     ),
  1150.     'weight' => 5,
  1151.   );
  1152.  
  1153.   if (!empty($order->products)) {
  1154.     $build = entity_view('uc_order_product', $order->products);
  1155.     $table['#rows'] = $build['uc_order_product'];
  1156.   }
  1157.   else {
  1158.     $table['#rows'][]['product'] = array(
  1159.       '#markup' => t('This order contains no products.'),
  1160.       '#cell_attributes' => array('colspan' => 'full'),
  1161.     );
  1162.   }
  1163.  
  1164.   return $table;
  1165. }
  1166.  
  1167. /**
  1168.  * TAPIr table for products pane on the order edit page.
  1169.  */
  1170. function uc_op_products_edit_table() {
  1171.   $table = array(
  1172.     '#type' => 'tapir_table',
  1173.     '#tree' => TRUE,
  1174.     '#attributes' => array('id' => 'order-edit-products', 'class' => array('order-pane-table')),
  1175.   );
  1176.  
  1177.   $table['#columns']['remove'] = array(
  1178.     'cell' => t('Remove'),
  1179.     'weight' => 0,
  1180.   );
  1181.   $table['#columns']['qty'] = array(
  1182.     'cell' => theme('uc_qty_label'),
  1183.     'weight' => 1,
  1184.   );
  1185.   $table['#columns']['title'] = array(
  1186.     'cell' => t('Name'),
  1187.     'weight' => 2,
  1188.   );
  1189.   $table['#columns']['model'] = array(
  1190.     'cell' => t('SKU'),
  1191.     'weight' => 3,
  1192.   );
  1193.   $table['#columns']['weight'] = array(
  1194.     'cell' => t('Weight'),
  1195.     'weight' => 4,
  1196.   );
  1197.   $table['#columns']['weight_units'] = array(
  1198.     'cell' => t('Units'),
  1199.     'weight' => 5,
  1200.   );
  1201.   $table['#columns']['cost'] = array(
  1202.     'cell' => t('Cost'),
  1203.     'weight' => 6,
  1204.   );
  1205.   $table['#columns']['price'] = array(
  1206.     'cell' => t('Price'),
  1207.     'weight' => 7,
  1208.   );
  1209.  
  1210.   return $table;
  1211. }
  1212.  
  1213. /**
  1214.  * Builds the order comments table.
  1215.  */
  1216. function uc_op_order_comments_view_table($comments) {
  1217.   $table = array(
  1218.     '#type' => 'tapir_table',
  1219.     '#attributes' => array('class' => array('order-pane-table')),
  1220.   );
  1221.  
  1222.   $table['#columns']['date'] = array(
  1223.     'cell' => array('data' => t('Date'), 'class' => array('text-center')),
  1224.     'weight' => 0,
  1225.   );
  1226.   $table['#columns']['user'] = array(
  1227.     'cell' => t('User'),
  1228.     'weight' => 1,
  1229.   );
  1230.   $table['#columns']['notified'] = array(
  1231.     'cell' => t('Notified'),
  1232.     'weight' => 2,
  1233.   );
  1234.   $table['#columns']['status'] = array(
  1235.     'cell' => array('data' => t('Status'), 'class' => array('text-center')),
  1236.     'weight' => 3,
  1237.   );
  1238.   $table['#columns']['comment'] = array(
  1239.     'cell' => array('data' => t('Comment'), 'width' => '80%'),
  1240.     'weight' => 4,
  1241.   );
  1242.  
  1243.   if (is_array($comments) && !empty($comments)) {
  1244.     foreach ($comments as $comment) {
  1245.       $data = array();
  1246.       $data['date'] = array(
  1247.         '#markup' => format_date($comment->created, 'short'),
  1248.         '#cell_attributes' => array('align' => 'center'),
  1249.       );
  1250.       $data['user'] = array(
  1251.         '#markup' => theme('uc_uid', array('uid' => $comment->uid)),
  1252.         '#cell_attributes' => array('align' => 'center'),
  1253.       );
  1254.       $icon = $comment->notified ? 'true-icon.gif' : 'false-icon.gif';
  1255.       $data['notified'] = array(
  1256.         '#markup' => theme('image', array('path' => drupal_get_path('module', 'uc_order') . '/images/' . $icon)),
  1257.         '#cell_attributes' => array('align' => 'center'),
  1258.       );
  1259.       $data['status'] = array(
  1260.         '#markup' => $comment->title,
  1261.         '#cell_attributes' => array('align' => 'center'),
  1262.       );
  1263.       $data['comment'] = array(
  1264.         '#markup' => check_plain($comment->message),
  1265.       );
  1266.       $table['#rows'][] = $data;
  1267.     }
  1268.   }
  1269.   else {
  1270.     $data['comment'] = array(
  1271.       '#markup' => t('This order has no comments associated with it.'),
  1272.       '#cell_attributes' => array('colspan' => 'full'),
  1273.     );
  1274.     $table['#rows'][] = $data;
  1275.   }
  1276.  
  1277.   return $table;
  1278. }
  1279.  
  1280. /**
  1281.  * Builds the order admin comments table.
  1282.  */
  1283. function uc_op_admin_comments_view_table($comments) {
  1284.   $table = array(
  1285.     '#type' => 'tapir_table',
  1286.     '#attributes' => array('class' => array('order-pane-table')),
  1287.   );
  1288.  
  1289.   $table['#columns']['date'] = array(
  1290.     'cell' => array('data' => t('Date'), 'class' => array('text-center')),
  1291.     'weight' => 0,
  1292.   );
  1293.   $table['#columns']['user'] = array(
  1294.     'cell' => array('data' => t('User'), 'class' => array('text-center')),
  1295.     'weight' => 1,
  1296.   );
  1297.   $table['#columns']['comment'] = array(
  1298.     'cell' => array('data' => t('Comment'), 'width' => '80%'),
  1299.     'weight' => 2,
  1300.   );
  1301.  
  1302.   if (is_array($comments) && !empty($comments)) {
  1303.     foreach ($comments as $comment) {
  1304.       $data = array();
  1305.       $data['date'] = array(
  1306.         '#markup' => format_date($comment->created, 'short'),
  1307.         '#cell_attributes' => array('align' => 'center', 'valign' => 'top'),
  1308.       );
  1309.       $data['user'] = array(
  1310.         '#markup' => theme('uc_uid', array('uid' => $comment->uid)),
  1311.         '#cell_attributes' => array('align' => 'center', 'valign' => 'top'),
  1312.       );
  1313.       $data['comment'] = array(
  1314.         '#markup' => filter_xss_admin($comment->message),
  1315.         '#cell_attributes' => array('valign' => 'top'),
  1316.       );
  1317.       $table['#rows'][] = $data;
  1318.     }
  1319.   }
  1320.   else {
  1321.     $data['comment'] = array(
  1322.       '#markup' => t('This order has no admin comments associated with it.'),
  1323.       '#cell_attributes' => array('colspan' => 'full'),
  1324.     );
  1325.     $table['#rows'][] = $data;
  1326.   }
  1327.  
  1328.   return $table;
  1329. }
  1330.  
  1331. /**
  1332.  * Builds a list of order panes defined in the enabled modules.
  1333.  */
  1334. function _uc_order_pane_list($view = 'view') {
  1335.   static $panes = array();
  1336.  
  1337.   if (count($panes) > 0) {
  1338.     return $panes;
  1339.   }
  1340.  
  1341.   foreach (module_invoke_all('uc_order_pane') as $id => $pane) {
  1342.     // Preserve backward compatibility for panes with no key specified.
  1343.     if (is_numeric($id)) {
  1344.       $id = $pane['id'];
  1345.     }
  1346.  
  1347.     // Set defaults.
  1348.     $pane += array(
  1349.       'id' => $id,
  1350.       'enabled' => TRUE,
  1351.       'weight' => 0,
  1352.     );
  1353.  
  1354.     $pane['enabled'] = variable_get('uc_order_pane_' . $id . '_enabled', $pane['enabled']);
  1355.     $pane['weight'] = variable_get('uc_order_pane_' . $id . '_weight_' . $view, $pane['weight']);
  1356.  
  1357.     $panes[$id] = $pane;
  1358.   }
  1359.  
  1360.   // Allow other modules to alter the defaults.
  1361.   drupal_alter('uc_order_pane', $panes);
  1362.  
  1363.   uasort($panes, 'uc_weight_sort');
  1364.  
  1365.   return $panes;
  1366. }
  1367.  
  1368. /**
  1369.  * Returns data from an order pane by pane ID and the array key.
  1370.  */
  1371. function _uc_order_pane_data($pane_id, $key) {
  1372.   $panes = _uc_order_pane_list();
  1373.   return $panes[$pane_id][$key];
  1374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement