selectfield.class.inc.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 \Dict;
  21. use \Combodo\iTop\Form\Field\MultipleChoicesField;
  22. /**
  23. * Description of SelectField
  24. *
  25. * @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
  26. */
  27. class SelectField extends MultipleChoicesField
  28. {
  29. const DEFAULT_MULTIPLE_VALUES_ENABLED = false;
  30. const DEFAULT_NULL_CHOICE_LABEL = 'UI:SelectOne';
  31. const DEFAULT_STARTS_WITH_NULL_CHOICE = true;
  32. protected $bStartsWithNullChoice;
  33. public function __construct($sId, Closure $onFinalizeCallback = null)
  34. {
  35. parent::__construct($sId, $onFinalizeCallback);
  36. $this->bStartsWithNullChoice = static::DEFAULT_STARTS_WITH_NULL_CHOICE;
  37. }
  38. /**
  39. * Returns if the select starts with a dummy choice before its choices.
  40. * This can be useful when you want to force the user to explicitly select a choice.
  41. *
  42. * @return boolean
  43. */
  44. public function GetStartsWithNullChoice()
  45. {
  46. return $this->bStartsWithNullChoice;
  47. }
  48. public function SetStartsWithNullChoice($bStartsWithNullChoice)
  49. {
  50. $this->bStartsWithNullChoice = $bStartsWithNullChoice;
  51. return $this;
  52. }
  53. /**
  54. * Returns the field choices with null choice first
  55. *
  56. * @return array
  57. */
  58. public function GetChoices()
  59. {
  60. $aChoices = parent::GetChoices();
  61. if ($this->bStartsWithNullChoice && !array_key_exists(null, $aChoices))
  62. {
  63. $aChoices = array(null => Dict::S(static::DEFAULT_NULL_CHOICE_LABEL)) + $aChoices;
  64. }
  65. return $aChoices;
  66. }
  67. /**
  68. * Overloads the method to prevent changing this property.
  69. *
  70. * @param boolean $bMultipleValuesEnabled
  71. * @return \Combodo\iTop\Form\Field\SelectField
  72. */
  73. public function SetMultipleValuesEnabled($bMultipleValuesEnabled)
  74. {
  75. // We don't allow changing this value
  76. return $this;
  77. }
  78. }