PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/code/base/SilvercartShoppingCartPositionNotice.php

https://bitbucket.org/silvercart/silvercart/
PHP | 131 lines | 49 code | 6 blank | 76 comment | 3 complexity | 927dc8ef0c88523a9a2204afb9be6afb MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011 pixeltricks GmbH
  4. *
  5. * This file is part of SilverCart.
  6. *
  7. * SilverCart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SilverCart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Silvercart
  21. * @subpackage Base
  22. */
  23. /**
  24. * Contains a couple of static methods for shopping cart related notices for the
  25. * customer. They are manages via the session.
  26. *
  27. * @package Silvercart
  28. * @subpackage Base
  29. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  30. * @copyright 2011 pixeltricks GmbH
  31. * @since 07.08.2011
  32. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  33. */
  34. class SilvercartShoppingCartPositionNotice extends SilvercartNotice {
  35. /**
  36. * Holds an array with possible notices that are selected with a $code
  37. * A notice can have a type: hint, error, warning
  38. *
  39. * @param string $code Code to identify the notice text
  40. *
  41. * @return array|false the translated notice and a notice type
  42. *
  43. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  44. * @since 7.8.2011
  45. */
  46. public static function getNoticeText($code) {
  47. $notices = array(
  48. 'adjusted' => array(
  49. 'text' => _t('SilvercartShoppingCartPosition.QUANTITY_ADJUSTED_MESSAGE'),
  50. 'type' => 'hint'
  51. ),
  52. 'remaining' => array(
  53. 'text' => _t('SilvercartShoppingCartPosition.REMAINING_QUANTITY_ADDED_MESSAGE'),
  54. 'type' => 'hint'
  55. ),
  56. 'maxQuantityReached' => array(
  57. 'text' => _t('SilvercartShoppingCartPosition.MAX_QUANTITY_REACHED_MESSAGE'),
  58. 'type' => 'hint'
  59. ),
  60. );
  61. if (array_key_exists($code, $notices)) {
  62. return $notices[$code]['text'];
  63. }
  64. return false;
  65. }
  66. /**
  67. * adds a notice to a position
  68. *
  69. * @param integer $positionID object id of the position
  70. * @param string $code message identifier
  71. *
  72. * @return void
  73. *
  74. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  75. * @since 7.8.2011
  76. */
  77. public static function setNotice($positionID, $code) {
  78. $notice = array(
  79. 'codes' => array($code)
  80. );
  81. //merge existing notices for the position
  82. if (Session::get("position".$positionID)) {
  83. $existingPositionCodes = Session::get("position".$positionID);
  84. $codes = array_merge($existingPositionCodes['codes'], $notice['codes']);
  85. $notice['codes'] = array_unique($codes);
  86. }
  87. Session::set("position".$positionID, $notice);
  88. Session::save();
  89. }
  90. /**
  91. * deletes only one specific position notice.
  92. *
  93. * @param integer $positionID the positions id
  94. * @param string $code the code to identify the message
  95. *
  96. * @return boolean Was the notice unset?
  97. *
  98. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  99. * @since 7.8.2011
  100. */
  101. public static function unsetNotice($positionID, $code) {
  102. $notices = Session::get("position".$positionID);
  103. if (array_key_exists('codes', $notices)) {
  104. unset ($notices[$code]);
  105. Session::set("position".$positionID, $notices);
  106. Session::save();
  107. return true;
  108. }
  109. return false;
  110. }
  111. /**
  112. * deletes all notices of a position.
  113. *
  114. * @param integer $positionID the positions id
  115. *
  116. * @return void
  117. *
  118. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  119. * @since 7.8.2011
  120. */
  121. public static function unsetNotices($positionID) {
  122. Session::clear("position".$positionID);
  123. Session::save();
  124. }
  125. }