TextAutocomplete.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ o3G6HU4cFHnvT/tnuA1MMubmKptzSsHADkemC1ga2RM=
  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. * text element with autocomplete widget
  14. */
  15. namespace Ppb\Form\Element;
  16. use Cube\Form\Element;
  17. class TextAutocomplete extends Element
  18. {
  19. /**
  20. *
  21. * type of element - override the variable from the parent class
  22. *
  23. * @var string
  24. */
  25. protected $_element = 'textAutocomplete';
  26. /**
  27. *
  28. * autocomplete source (formatted)
  29. *
  30. * @var string
  31. */
  32. protected $_source;
  33. /**
  34. *
  35. * class constructor
  36. *
  37. * @param string $name
  38. */
  39. public function __construct($name)
  40. {
  41. parent::__construct('text', $name);
  42. }
  43. /**
  44. * @param string $source
  45. */
  46. public function setSource($source)
  47. {
  48. if (is_array($source)) {
  49. $array = array();
  50. foreach ($source as $key => $value) {
  51. $array[] = '{ label: "' . $value . '", value: "' . $key . '" }';
  52. }
  53. $source = implode(', ', $array);
  54. }
  55. $this->setBodyCode(
  56. "<script type=\"text/javascript\"> " . "\n"
  57. . " $(document).ready(function() { " . "\n"
  58. . " $('input[name=\"" . $this->_name . "\"]').autocomplete({ " . "\n"
  59. . " minLength: 0, " . "\n"
  60. . " source: [ " . $source . " ] " . "\n"
  61. . " }); " . "\n"
  62. . " }); " . "\n"
  63. . "</script>");
  64. $this->_source = $source;
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getSource()
  70. {
  71. return $this->_source;
  72. }
  73. }