uc_order.line_item.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @file
  4. * Contains the callbacks for the default line items for orders and
  5. * the various functions that make line items work.
  6. *
  7. * Line items are defined using hook_uc_line_item() and use a callback to
  8. * handle the different processes involved in line item
  9. * viewing/editing/calculating. The default line items are defined in
  10. * uc_order_uc_line_item() in uc_order.module.
  11. */
  12. /**
  13. * Handles the subtotal line item.
  14. */
  15. function uc_line_item_subtotal($op, $order) {
  16. switch ($op) {
  17. case 'load':
  18. $lines[] = array(
  19. 'id' => 'subtotal',
  20. 'title' => t('Subtotal'),
  21. 'amount' => uc_order_get_total($order, TRUE),
  22. );
  23. return $lines;
  24. }
  25. }
  26. /**
  27. * Handles the total line item.
  28. */
  29. function uc_line_item_total($op, $order) {
  30. switch ($op) {
  31. case 'display':
  32. $lines[] = array(
  33. 'id' => 'total',
  34. 'title' => t('Order total'),
  35. 'amount' => uc_order_get_total($order),
  36. );
  37. return $lines;
  38. }
  39. }
  40. /**
  41. * Calculates the total value of line items of types that should be calculated.
  42. */
  43. function uc_line_items_calculate($order) {
  44. $total = 0;
  45. if (is_array($order->line_items)) {
  46. foreach ($order->line_items as $item) {
  47. if (_uc_line_item_data($item['type'], 'calculated') == TRUE) {
  48. $total += $item['amount'];
  49. }
  50. }
  51. }
  52. return $total;
  53. }
  54. /**
  55. * Updates a line item.
  56. */
  57. function uc_order_update_line_item($id, $title, $amount, $data = NULL) {
  58. $fields = array(
  59. 'title' => $title,
  60. 'amount' => $amount,
  61. );
  62. if (!is_null($data)) {
  63. $fields['data'] = serialize($data);
  64. }
  65. db_update('uc_order_line_items')
  66. ->fields($fields)
  67. ->condition('line_item_id', $id)
  68. ->execute();
  69. return TRUE;
  70. }
  71. /**
  72. * Deletes a specific line item, or every line item in an order.
  73. *
  74. * @param $id
  75. * The line item ID, or order ID.
  76. * @param $order_id
  77. * If FALSE, deletes the line item with the specified ID (default).
  78. * If TRUE, deletes all line items on the order with the specified ID.
  79. */
  80. function uc_order_delete_line_item($id, $order = FALSE) {
  81. if ($order === FALSE) {
  82. db_delete('uc_order_line_items')
  83. ->condition('line_item_id', $id)
  84. ->execute();
  85. }
  86. else {
  87. db_delete('uc_order_line_items')
  88. ->condition('order_id', $id)
  89. ->execute();
  90. }
  91. return TRUE;
  92. }
  93. /**
  94. * Adds a line item to an order.
  95. */
  96. function uc_order_line_item_add($order_id, $type, $title, $amount, $weight = NULL, $data = NULL) {
  97. if (is_null($weight)) {
  98. $weight = _uc_line_item_data($type, 'weight');
  99. }
  100. $line_item = array(
  101. 'order_id' => $order_id,
  102. 'type' => $type,
  103. 'title' => $title,
  104. 'amount' => $amount,
  105. 'weight' => $weight,
  106. 'data' => $data,
  107. );
  108. drupal_write_record('uc_order_line_items', $line_item);
  109. return $line_item;
  110. }
  111. /**
  112. * Builds a list of line items defined in the enabled modules.
  113. */
  114. function _uc_line_item_list($action = NULL) {
  115. static $items = array();
  116. if (count($items) > 0 && $action !== 'rebuild') {
  117. return $items;
  118. }
  119. $items = array();
  120. foreach (module_invoke_all('uc_line_item') as $id => $item) {
  121. // Preserve backward compatibility for methods with no key specified.
  122. if (is_numeric($id)) {
  123. $id = $item['id'];
  124. }
  125. // Set defaults.
  126. $item += array(
  127. 'id' => $id,
  128. 'enabled' => TRUE,
  129. 'weight' => 1,
  130. );
  131. // Merge in any settings.
  132. $items[$id] = array_merge($item, array(
  133. 'enabled' => variable_get('uc_li_' . $id . '_enabled', $item['enabled']),
  134. 'weight' => variable_get('uc_li_' . $id . '_weight', $item['weight']),
  135. ));
  136. }
  137. drupal_alter('uc_line_item_data', $items);
  138. uasort($items, 'uc_weight_sort');
  139. return $items;
  140. }
  141. /**
  142. * Returns data from a line item by ID and the array key.
  143. */
  144. function _uc_line_item_data($item_id, $key) {
  145. $items = _uc_line_item_list();
  146. return isset($items[$item_id][$key]) ? $items[$item_id][$key] : NULL;
  147. }