multiplechoicesfield.class.inc.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // Copyright (C) 2010-2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. namespace Combodo\iTop\Form\Field;
  19. use \Closure;
  20. use \Combodo\iTop\Form\Field\Field;
  21. /**
  22. * Description of MultipleChoicesField
  23. *
  24. * Choices = Set of items that can be picked
  25. * Values = Items that have been picked
  26. *
  27. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  28. */
  29. abstract class MultipleChoicesField extends Field
  30. {
  31. const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
  32. protected $bMultipleValuesEnabled;
  33. protected $aChoices;
  34. public function __construct($sId, Closure $onFinalizeCallback = null)
  35. {
  36. parent::__construct($sId, $onFinalizeCallback);
  37. $this->bMultipleValuesEnabled = static::DEFAULT_MULTIPLE_VALUES_ENABLED;
  38. $this->aChoices = array();
  39. $this->currentValue = array();
  40. }
  41. public function GetCurrentValue()
  42. {
  43. $value = null;
  44. if (!empty($this->currentValue))
  45. {
  46. if ($this->bMultipleValuesEnabled)
  47. {
  48. $value = $this->currentValue;
  49. }
  50. else
  51. {
  52. reset($this->currentValue);
  53. $value = current($this->currentValue);
  54. }
  55. }
  56. return $value;
  57. }
  58. /**
  59. * Sets the current value for the MultipleChoicesField.
  60. *
  61. * @param mixed $currentValue Can be either an array of values (in case of multiple values) or just a simple value
  62. * @return \Combodo\iTop\Form\Field\MultipleChoicesField
  63. */
  64. public function SetCurrentValue($currentValue)
  65. {
  66. if (is_array($currentValue))
  67. {
  68. $this->currentValue = $currentValue;
  69. }
  70. elseif (is_null($currentValue))
  71. {
  72. $this->currentValue = array();
  73. }
  74. else
  75. {
  76. $this->currentValue = array($currentValue);
  77. }
  78. return $this;
  79. }
  80. public function GetMultipleValuesEnabled()
  81. {
  82. return $this->bMultipleValuesEnabled;
  83. }
  84. public function SetMultipleValuesEnabled($bMultipleValuesEnabled)
  85. {
  86. $this->bMultipleValuesEnabled = $bMultipleValuesEnabled;
  87. return $this;
  88. }
  89. public function SetValues($aValues)
  90. {
  91. $this->currentValue = $aValues;
  92. return $this;
  93. }
  94. public function AddValue($value)
  95. {
  96. $this->currentValue = $value;
  97. return $this;
  98. }
  99. public function RemoveValue($value)
  100. {
  101. if (array_key_exists($value, $this->currentValue))
  102. {
  103. unset($this->currentValue[$sId]);
  104. }
  105. return $this;
  106. }
  107. public function IsAmongValues($value)
  108. {
  109. return in_array($value, $this->currentValue);
  110. }
  111. public function GetChoices()
  112. {
  113. return $this->aChoices;
  114. }
  115. public function SetChoices($aChoices)
  116. {
  117. $this->aChoices = $aChoices;
  118. return $this;
  119. }
  120. public function AddChoice($sId, $choice = null)
  121. {
  122. if ($choice === null)
  123. {
  124. $choice = $sId;
  125. }
  126. $this->aChoices[$sId] = $choice;
  127. return $this;
  128. }
  129. public function RemoveChoice($sId)
  130. {
  131. if (in_array($sId, $this->aChoices))
  132. {
  133. unset($this->aChoices[$sId]);
  134. }
  135. return $this;
  136. }
  137. public function Validate()
  138. {
  139. $this->SetValid(true);
  140. $this->EmptyErrorMessages();
  141. foreach ($this->GetValidators() as $oValidator)
  142. {
  143. foreach ($this->currentValue as $value)
  144. {
  145. if (!preg_match($oValidator->GetRegExp(true), $value))
  146. {
  147. $this->SetValid(false);
  148. $this->AddErrorMessage($oValidator->GetErrorMessage());
  149. }
  150. }
  151. }
  152. return $this->GetValid();
  153. }
  154. }