tmgmt_ui.cart.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @file
  4. * Contains TMGMTJobItemUICart.
  5. */
  6. /**
  7. * Represents a job item cart.
  8. *
  9. * @ingroup tmgmt_ui_cart
  10. */
  11. class TMGMTJobItemUICart {
  12. /**
  13. * Singleton instance of cart.
  14. */
  15. protected static $instance;
  16. /**
  17. * Array key to store the contents of $this->cart into the $_SESSION variable.
  18. *
  19. * @var string
  20. */
  21. protected $session_key = 'tmgmt_cart';
  22. /**
  23. * An array to hold and manipulate the contents of the job item cart.
  24. *
  25. * @var array
  26. */
  27. protected $cart;
  28. /**
  29. * Set up a new TMGMTJobItemUICart instance.
  30. *
  31. * Will load the cart from the session or initialize a new one if nothing has
  32. * been stored yet.
  33. */
  34. protected function __construct() {
  35. if (!isset($_SESSION[$this->session_key])) {
  36. $_SESSION[$this->session_key] = array();
  37. }
  38. $this->cart = &$_SESSION[$this->session_key];
  39. }
  40. /**
  41. * Returns the singleton cart.
  42. *
  43. * @return TMGMTJobItemUICart
  44. * An instance of the cart.
  45. */
  46. public static function getInstance() {
  47. if (!isset(self::$instance)) {
  48. self::$instance = new self();
  49. }
  50. return self::$instance;
  51. }
  52. /**
  53. * Adds existing job items into the cart.
  54. *
  55. * @param TMGMTJobItem[] $items
  56. * Job items to be added.
  57. */
  58. public function addExistingJobItems(array $items) {
  59. foreach ($items as $item) {
  60. if (!$this->isSourceItemAdded($item->plugin, $item->item_type, $item->item_id)) {
  61. $this->cart[] = $item->tjiid;
  62. }
  63. }
  64. }
  65. /**
  66. * Creates a job item and adds it into the cart.
  67. *
  68. * @param string $plugin
  69. * The source plugin.
  70. * @param string $item_type
  71. * The source item type.
  72. * @param $item_id
  73. * The source item id.
  74. *
  75. * @return TMGMTJobItem|null
  76. * Added job item. If the item exists NULL is returned.
  77. */
  78. public function addJobItem($plugin, $item_type, $item_id) {
  79. if ($this->isSourceItemAdded($plugin, $item_type, $item_id)) {
  80. return NULL;
  81. }
  82. $job_item = tmgmt_job_item_create($plugin, $item_type, $item_id);
  83. $job_item->save();
  84. $this->cart[] = $job_item->tjiid;
  85. return $job_item;
  86. }
  87. /**
  88. * Checks if the source item has been added into the cart.
  89. *
  90. * @param string $plugin
  91. * The source plugin.
  92. * @param string $item_type
  93. * The source type.
  94. * @param int $source_id
  95. * The source id.
  96. *
  97. * @return bool
  98. * If the source item is in the cart.
  99. */
  100. public function isSourceItemAdded($plugin, $item_type, $source_id) {
  101. foreach ($this->getJobItemsFromCart() as $job_item) {
  102. if ($job_item->item_id == $source_id && $job_item->item_type == $item_type && $job_item->plugin == $plugin) {
  103. return TRUE;
  104. }
  105. }
  106. return FALSE;
  107. }
  108. /**
  109. * Remove job items from the cart.
  110. *
  111. * @param array $job_item_ids
  112. * Job items to be removed.
  113. */
  114. public function removeJobItems(array $job_item_ids) {
  115. $this->cart = array_diff($this->cart, $job_item_ids);
  116. }
  117. /**
  118. * Gets job items in the cart.
  119. *
  120. * @return TMGMTJobItem[] $items
  121. * Job items in the cart.
  122. */
  123. public function getJobItemsFromCart() {
  124. return entity_load('tmgmt_job_item', $this->cart);
  125. }
  126. /**
  127. * Gets count of items in the cart.
  128. *
  129. * @return int
  130. * Number of items in the cart.
  131. */
  132. public function count() {
  133. return count($this->cart);
  134. }
  135. /**
  136. * Remove all contents from the cart.
  137. */
  138. public function emptyCart() {
  139. $this->cart = array();
  140. }
  141. }