uc_order.controller.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @file
  4. * Contains controller classes for uc_order and uc_order_product entities.
  5. */
  6. /**
  7. * Controller class for uc_order entity.
  8. */
  9. class UcOrderController extends DrupalDefaultEntityController {
  10. function attachLoad(&$orders, $revision_id = FALSE) {
  11. foreach ($orders as &$order) {
  12. $order->data = unserialize($order->data);
  13. $efq = new EntityFieldQuery();
  14. $result = $efq->entityCondition('entity_type', 'uc_order_product')
  15. ->propertyCondition('order_id', $order->order_id)
  16. ->propertyOrderBy('order_product_id', 'ASC')
  17. ->execute();
  18. if (!empty($result['uc_order_product'])) {
  19. $order->products = uc_order_product_load_multiple(array_keys($result['uc_order_product']), TRUE);
  20. foreach ($order->products as $product) {
  21. $product->order = $order;
  22. $product->order_uid = $order->uid;
  23. }
  24. }
  25. else {
  26. $order->products = array();
  27. }
  28. uc_order_module_invoke('load', $order, NULL);
  29. // Load line items... has to be last after everything has been loaded.
  30. $order->line_items = uc_order_load_line_items($order);
  31. $fields = array();
  32. // Make sure the total still matches up...
  33. if (($total = uc_order_get_total($order)) !== $order->order_total) {
  34. $fields['order_total'] = $total;
  35. $order->order_total = $total;
  36. }
  37. if (($count = uc_order_get_product_count($order)) !== $order->product_count) {
  38. $fields['product_count'] = $count;
  39. $order->product_count = $count;
  40. }
  41. if (count($fields)) {
  42. $query = db_update('uc_orders')
  43. ->fields($fields)
  44. ->condition('order_id', $order->order_id)
  45. ->execute();
  46. }
  47. }
  48. parent::attachLoad($orders, $revision_id);
  49. }
  50. }
  51. /**
  52. * Controller class for the uc_order_product entity.
  53. */
  54. class UcOrderProductController extends EntityAPIController {
  55. /**
  56. * Overrides EntityAPIController::buildContent().
  57. */
  58. public function buildContent($product, $view_mode = 'full', $langcode = NULL, $content = array()) {
  59. $content['qty'] = array(
  60. '#theme' => 'uc_qty',
  61. '#qty' => $product->qty,
  62. '#cell_attributes' => array('class' => array('qty')),
  63. );
  64. $node = node_load($product->nid);
  65. $title = node_access('view', $node) ? l($product->title, 'node/' . $node->nid) : check_plain($product->title);
  66. $content['product'] = array(
  67. '#markup' => $title . uc_product_get_description($product),
  68. '#cell_attributes' => array('class' => array('product')),
  69. );
  70. $content['model'] = array(
  71. '#markup' => check_plain($product->model),
  72. '#cell_attributes' => array('class' => array('sku')),
  73. );
  74. if (user_access('administer products')) {
  75. $content['cost'] = array(
  76. '#theme' => 'uc_price',
  77. '#price' => $product->cost,
  78. '#cell_attributes' => array('class' => array('cost')),
  79. );
  80. }
  81. $content['price'] = array(
  82. '#theme' => 'uc_price',
  83. '#price' => $product->price,
  84. '#suffixes' => array(),
  85. '#cell_attributes' => array('class' => array('price')),
  86. );
  87. $content['total'] = array(
  88. '#theme' => 'uc_price',
  89. '#price' => $product->price * $product->qty,
  90. '#suffixes' => array(),
  91. '#cell_attributes' => array('class' => array('total')),
  92. );
  93. return parent::buildContent($product, $view_mode, $langcode, $content);
  94. }
  95. /**
  96. * Overrides EntityAPIController::save().
  97. */
  98. public function save($product, DatabaseTransaction $transaction = NULL) {
  99. // Product kits, particularly, shouldn't actually be added to an order,
  100. // but instead they cause other products to be added.
  101. if (isset($product->skip_save) && $product->skip_save == TRUE) {
  102. return;
  103. }
  104. if (empty($product->weight_units)) {
  105. if (empty($product->nid)) {
  106. $product->weight_units = variable_get('uc_weight_unit', 'lb');
  107. }
  108. else {
  109. $units = db_query("SELECT weight_units FROM {node} n JOIN {uc_products} p ON n.vid = p.vid WHERE n.nid = :nid", array(':nid' => $product->nid))->fetchField();
  110. $product->weight_units = empty($units) ? variable_get('uc_weight_unit', 'lb') : $units;
  111. }
  112. }
  113. return parent::save($product, $transaction);
  114. }
  115. }