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 (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../../Forms/Controls/SelectBox.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Select box control that allows multiple item selection.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Forms
  33. 33:  */
  34. 34: class MultiSelectBox extends SelectBox
  35. 35: {
  36. 36:  
  37. 37:  
  38. 38:     /**
  39. 39:      * Returns selected keys.
  40. 40:      * @return array 
  41. 41:      */
  42. 42:     public function getValue()
  43. 43:     {
  44. 44:         $allowed array_keys($this->allowed);
  45. 45:         if ($this->isFirstSkipped()) {
  46. 46:             unset($allowed[0]);
  47. 47:         }
  48. 48:         return array_intersect($this->getRawValue()$allowed);
  49. 49:     }
  50. 50:  
  51. 51:  
  52. 52:  
  53. 53:     /**
  54. 54:      * Returns selected keys (not checked).
  55. 55:      * @return array 
  56. 56:      */
  57. 57:     public function getRawValue()
  58. 58:     {
  59. 59:         if (is_scalar($this->value)) {
  60. 60:             $value array($this->value);
  61. 61:  
  62. 62:         elseif (!is_array($this->value)) {
  63. 63:             $value array();
  64. 64:  
  65. 65:         else {
  66. 66:             $value $this->value;
  67. 67:         }
  68. 68:  
  69. 69:         $res array();
  70. 70:         foreach ($value as $val{
  71. 71:             if (is_scalar($val)) {
  72. 72:                 $res[$val;
  73. 73:             }
  74. 74:         }
  75. 75:         return $res;
  76. 76:     }
  77. 77:  
  78. 78:  
  79. 79:  
  80. 80:     /**
  81. 81:      * Returns selected values.
  82. 82:      * @return array 
  83. 83:      */
  84. 84:     public function getSelectedItem()
  85. 85:     {
  86. 86:         if (!$this->useKeys{
  87. 87:             return $this->getValue();
  88. 88:  
  89. 89:         else {
  90. 90:             $res array();
  91. 91:             foreach ($this->getValue(as $value{
  92. 92:                 $res[$value$this->allowed[$value];
  93. 93:             }
  94. 94:             return $res;
  95. 95:         }
  96. 96:     }
  97. 97:  
  98. 98:  
  99. 99:  
  100. 100:     /**
  101. 101:      * Generates control's HTML element.
  102. 102:      * @return Html 
  103. 103:      */
  104. 104:     public function getControl()
  105. 105:     {
  106. 106:         $control parent::getControl();
  107. 107:         $control->name .= '[]';
  108. 108:         $control->multiple TRUE;
  109. 109:         return $control;
  110. 110:     }
  111. 111: