2
0

AutoComplete.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category ZendX
  16. * @package ZendX_JQuery
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see ZendX_JQuery_View_Helper_UiWidget
  24. */
  25. require_once "ZendX/JQuery/View/Helper/UiWidget.php";
  26. /**
  27. * jQuery Autocomplete View Helper
  28. *
  29. * @uses Zend_Json, Zend_View_Helper_FormText
  30. * @package ZendX_JQuery
  31. * @subpackage View
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class ZendX_JQuery_View_Helper_AutoComplete extends ZendX_JQuery_View_Helper_UiWidget
  36. {
  37. /**
  38. * Builds an AutoComplete ready input field.
  39. *
  40. * This view helper builds an input field with the {@link Zend_View_Helper_FormText} FormText
  41. * Helper and adds additional javascript to the jQuery stack to initialize an AutoComplete
  42. * field. Make sure you have set one out of the two following options: $params['data'] or
  43. * $params['url']. The first one accepts an array as data input to the autoComplete, the
  44. * second accepts an url, where the autoComplete content is returned from. For the format
  45. * see jQuery documentation.
  46. *
  47. * @link http://docs.jquery.com/UI/Autocomplete
  48. * @throws ZendX_JQuery_Exception
  49. * @param String $id
  50. * @param String $value
  51. * @param array $params
  52. * @param array $attribs
  53. * @return String
  54. */
  55. public function autoComplete($id, $value = null, array $params = array(), array $attribs = array())
  56. {
  57. $attribs = $this->_prepareAttributes($id, $value, $attribs);
  58. if (!isset($params['source'])) {
  59. if (isset($params['url'])) {
  60. $params['source'] = $params['url'];
  61. unset($params['url']);
  62. } else if (isset($params['data'])) {
  63. $params['source'] = $params['data'];
  64. unset($params['data']);
  65. } else {
  66. require_once "ZendX/JQuery/Exception.php";
  67. throw new ZendX_JQuery_Exception(
  68. "Cannot construct AutoComplete field without specifying 'source' field, ".
  69. "either an url or an array of elements."
  70. );
  71. }
  72. }
  73. $params = ZendX_JQuery::encodeJson($params);
  74. $js = sprintf('%s("#%s").autocomplete(%s);',
  75. ZendX_JQuery_View_Helper_JQuery::getJQueryHandler(),
  76. $attribs['id'],
  77. $params
  78. );
  79. $this->jquery->addOnLoad($js);
  80. return $this->view->formText($id, $value, $attribs);
  81. }
  82. }