uc_cart.controller.inc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * @file
  4. * Contains the controller for uc_cart_item entities.
  5. */
  6. class UcCartItemController extends EntityAPIController {
  7. /**
  8. * Overrides EntityAPIController::attachLoad().
  9. */
  10. public function attachLoad(&$items, $revision_id = FALSE) {
  11. foreach ($items as &$item) {
  12. $product = uc_product_load_variant($item->nid, $item->data);
  13. // Merge in fields from the product.
  14. foreach ($product as $key => $value) {
  15. $item->$key = $value;
  16. }
  17. $item->module = $item->data['module'];
  18. }
  19. parent::attachLoad($items, $revision_id);
  20. }
  21. /**
  22. * Saves a cart item entity.
  23. *
  24. * Cart items are deleted if saved with a quantity of zero.
  25. */
  26. public function save($item, DatabaseTransaction $transaction = NULL) {
  27. if ($item->qty < 1) {
  28. if (isset($item->cart_item_id)) {
  29. parent::delete(array($item->cart_item_id), $transaction);
  30. }
  31. }
  32. else {
  33. $item->changed = REQUEST_TIME;
  34. parent::save($item, $transaction);
  35. }
  36. }
  37. /**
  38. * Overrides EntityAPIController::buildContent().
  39. */
  40. public function buildContent($product, $view_mode = 'full', $langcode = NULL, $content = array()) {
  41. $content += module_invoke($product->data['module'], 'uc_cart_display', $product);
  42. if (!empty($content)) {
  43. $content['cart_item_id'] = array(
  44. '#type' => 'hidden',
  45. '#value' => isset($product->cart_item_id) ? $product->cart_item_id : 0,
  46. );
  47. }
  48. return parent::buildContent($product, $view_mode, $langcode, $content);
  49. }
  50. }