ChznSelect.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ DsUHaDvgZpTaPSGk/UESoo/CopwNkhxDPw8bmHNxk6k=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.0
  11. */
  12. /**
  13. * multi select form element
  14. *
  15. * creates a select element that uses the jquery chosen plugin
  16. */
  17. namespace Ppb\Form\Element;
  18. use Cube\Form\Element\Select,
  19. Cube\Controller\Front;
  20. class ChznSelect extends Select
  21. {
  22. const ELEMENT_CLASS = 'chzn-select';
  23. const SELECT_MULTIPLE_SIZE = '5';
  24. /**
  25. *
  26. * type of element - override the variable from the parent class
  27. *
  28. * @var string
  29. */
  30. protected $_element = 'ChznSelect';
  31. /**
  32. *
  33. * base url of the application
  34. *
  35. * @var string
  36. */
  37. protected $_baseUrl;
  38. /**
  39. *
  40. * class constructor
  41. *
  42. * @param string $name
  43. * @param bool $initialize
  44. */
  45. public function __construct($name, $initialize = true)
  46. {
  47. parent::__construct($name);
  48. if ($initialize === true) {
  49. $this->_baseUrl = Front::getInstance()->getRequest()->getBaseUrl();
  50. $this->setHeaderCode('<link href="' . $this->_baseUrl . '/js/chosen/chosen.css" media="screen" rel="stylesheet" type="text/css">')
  51. ->setBodyCode('<script type="text/javascript" src="' . $this->_baseUrl . '/js/chosen/chosen.jquery.min.js"></script>')
  52. ->setBodyCode(
  53. "<script type=\"text/javascript\">" . "\n"
  54. . " $('." . self::ELEMENT_CLASS . "').chosen(); " . "\n"
  55. . "</script>");
  56. }
  57. $this->addAttribute('class', self::ELEMENT_CLASS);
  58. }
  59. public function render()
  60. {
  61. $output = null;
  62. $value = $this->getValue();
  63. if ($this->getMultiple() === true) {
  64. $this->_attributes['multiple'] = 'multiple';
  65. $brackets = '';
  66. // used for when having a table with chznselect fields
  67. if (isset($this->_customData['doubleBrackets'])) {
  68. if ($this->_customData['doubleBrackets'] == true) {
  69. $brackets = $this->getBrackets();
  70. $this->setBrackets($brackets . '[]');
  71. }
  72. }
  73. $output .= '<input type="hidden" name="' . $this->_name . $brackets . '" value=""'
  74. . $this->_endTag;
  75. }
  76. if (!isset($this->_attributes['size'])) {
  77. $this->_attributes['size'] = self::SELECT_MULTIPLE_SIZE;
  78. }
  79. $output .= '<select name="' . $this->_name . $this->getBrackets() . '" '
  80. . $this->renderAttributes() . '>';
  81. foreach ((array)$this->_multiOptions as $key => $option) {
  82. $selected = (in_array($key, (array)$value)) ? ' selected' : '';
  83. $output .= '<option value="' . $key . '"' . $selected . '>' . $option . '</option>';
  84. }
  85. $output .= '</select>';
  86. return $output;
  87. }
  88. }