PageRenderTime 83ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Forms/Controls/MultiSelectBox.php

https://github.com/DocX/nette
PHP | 113 lines | 49 code | 24 blank | 40 comment | 6 complexity | 7b58fe343c2eb60d63d3f057d2a6705e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Forms
  17. */
  18. /*namespace Nette\Forms;*/
  19. require_once dirname(__FILE__) . '/../../Forms/Controls/SelectBox.php';
  20. /**
  21. * Select box control that allows multiple item selection.
  22. *
  23. * @author David Grudl
  24. * @copyright Copyright (c) 2004, 2009 David Grudl
  25. * @package Nette\Forms
  26. */
  27. class MultiSelectBox extends SelectBox
  28. {
  29. /**
  30. * Returns selected keys.
  31. * @return array
  32. */
  33. public function getValue()
  34. {
  35. $allowed = array_keys($this->allowed);
  36. if ($this->isFirstSkipped()) {
  37. unset($allowed[0]);
  38. }
  39. return array_intersect($this->getRawValue(), $allowed);
  40. }
  41. /**
  42. * Returns selected keys (not checked).
  43. * @return array
  44. */
  45. public function getRawValue()
  46. {
  47. if (is_scalar($this->value)) {
  48. $value = array($this->value);
  49. } elseif (!is_array($this->value)) {
  50. $value = array();
  51. } else {
  52. $value = $this->value;
  53. }
  54. $res = array();
  55. foreach ($value as $val) {
  56. if (is_scalar($val)) {
  57. $res[] = $val;
  58. }
  59. }
  60. return $res;
  61. }
  62. /**
  63. * Returns selected values.
  64. * @return array
  65. */
  66. public function getSelectedItem()
  67. {
  68. if (!$this->useKeys) {
  69. return $this->getValue();
  70. } else {
  71. $res = array();
  72. foreach ($this->getValue() as $value) {
  73. $res[$value] = $this->allowed[$value];
  74. }
  75. return $res;
  76. }
  77. }
  78. /**
  79. * Generates control's HTML element.
  80. * @return Nette\Web\Html
  81. */
  82. public function getControl()
  83. {
  84. $control = parent::getControl();
  85. $control->name .= '[]';
  86. $control->multiple = TRUE;
  87. return $control;
  88. }
  89. }