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