uc_cart.controller.inc 1.5 KB

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