Source for file MultiSelectBox.php

Documentation is available at MultiSelectBox.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Forms
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Select box control that allows multiple item selection.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Forms
  20. 20:  */
  21. 21: class MultiSelectBox extends SelectBox
  22. 22: {
  23. 23:  
  24. 24:  
  25. 25:     /**
  26. 26:      * Returns selected keys.
  27. 27:      * @return array 
  28. 28:      */
  29. 29:     public function getValue()
  30. 30:     {
  31. 31:         $allowed array_keys($this->allowed);
  32. 32:         if ($this->isFirstSkipped()) {
  33. 33:             unset($allowed[0]);
  34. 34:         }
  35. 35:         return array_intersect($this->getRawValue()$allowed);
  36. 36:     }
  37. 37:  
  38. 38:  
  39. 39:  
  40. 40:     /**
  41. 41:      * Returns selected keys (not checked).
  42. 42:      * @return array 
  43. 43:      */
  44. 44:     public function getRawValue()
  45. 45:     {
  46. 46:         if (is_scalar($this->value)) {
  47. 47:             $value array($this->value);
  48. 48:  
  49. 49:         elseif (!is_array($this->value)) {
  50. 50:             $value array();
  51. 51:  
  52. 52:         else {
  53. 53:             $value $this->value;
  54. 54:         }
  55. 55:  
  56. 56:         $res array();
  57. 57:         foreach ($value as $val{
  58. 58:             if (is_scalar($val)) {
  59. 59:                 $res[$val;
  60. 60:             }
  61. 61:         }
  62. 62:         return $res;
  63. 63:     }
  64. 64:  
  65. 65:  
  66. 66:  
  67. 67:     /**
  68. 68:      * Returns selected values.
  69. 69:      * @return array 
  70. 70:      */
  71. 71:     public function getSelectedItem()
  72. 72:     {
  73. 73:         if (!$this->useKeys{
  74. 74:             return $this->getValue();
  75. 75:  
  76. 76:         else {
  77. 77:             $res array();
  78. 78:             foreach ($this->getValue(as $value{
  79. 79:                 $res[$value$this->allowed[$value];
  80. 80:             }
  81. 81:             return $res;
  82. 82:         }
  83. 83:     }
  84. 84:  
  85. 85:  
  86. 86:  
  87. 87:     /**
  88. 88:      * Returns name of control within a Form & INamingContainer scope.
  89. 89:      * @return string 
  90. 90:      */
  91. 91:     public function getHtmlName()
  92. 92:     {
  93. 93:         return parent::getHtmlName('[]';
  94. 94:     }
  95. 95:  
  96. 96:  
  97. 97:  
  98. 98:     /**
  99. 99:      * Generates control's HTML element.
  100. 100:      * @return Html 
  101. 101:      */
  102. 102:     public function getControl()
  103. 103:     {
  104. 104:         $control parent::getControl();
  105. 105:         $control->multiple TRUE;
  106. 106:         return $control;
  107. 107:     }
  108. 108: