Abstract.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Zend
  16. * @package Zend_Controller
  17. * @subpackage Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2015 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 Zend_Controller_Action_Helper_Abstract
  24. */
  25. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  26. /**
  27. * Create and send autocompletion lists
  28. *
  29. * @uses Zend_Controller_Action_Helper_Abstract
  30. * @category Zend
  31. * @package Zend_Controller
  32. * @subpackage Zend_Controller_Action_Helper
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_Controller_Action_Helper_AutoComplete_Abstract extends Zend_Controller_Action_Helper_Abstract
  37. {
  38. /**
  39. * Suppress exit when sendJson() called
  40. *
  41. * @var boolean
  42. */
  43. public $suppressExit = false;
  44. /**
  45. * Validate autocompletion data
  46. *
  47. * @param mixed $data
  48. * @return boolean
  49. */
  50. abstract public function validateData($data);
  51. /**
  52. * Prepare autocompletion data
  53. *
  54. * @param mixed $data
  55. * @param boolean $keepLayouts
  56. * @return mixed
  57. */
  58. abstract public function prepareAutoCompletion($data, $keepLayouts = false);
  59. /**
  60. * Disable layouts and view renderer
  61. *
  62. * @return Zend_Controller_Action_Helper_AutoComplete_Abstract Provides a fluent interface
  63. */
  64. public function disableLayouts()
  65. {
  66. /**
  67. * @see Zend_Layout
  68. */
  69. require_once 'Zend/Layout.php';
  70. if (null !== ($layout = Zend_Layout::getMvcInstance())) {
  71. $layout->disableLayout();
  72. }
  73. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
  74. return $this;
  75. }
  76. /**
  77. * Encode data to JSON
  78. *
  79. * @param mixed $data
  80. * @param bool $keepLayouts
  81. * @throws Zend_Controller_Action_Exception
  82. * @return string
  83. */
  84. public function encodeJson($data, $keepLayouts = false)
  85. {
  86. if ($this->validateData($data)) {
  87. return Zend_Controller_Action_HelperBroker::getStaticHelper('Json')->encodeJson($data, $keepLayouts);
  88. }
  89. /**
  90. * @see Zend_Controller_Action_Exception
  91. */
  92. require_once 'Zend/Controller/Action/Exception.php';
  93. throw new Zend_Controller_Action_Exception('Invalid data passed for autocompletion');
  94. }
  95. /**
  96. * Send autocompletion data
  97. *
  98. * Calls prepareAutoCompletion, populates response body with this
  99. * information, and sends response.
  100. *
  101. * @param mixed $data
  102. * @param bool $keepLayouts
  103. * @return string|void
  104. */
  105. public function sendAutoCompletion($data, $keepLayouts = false)
  106. {
  107. $data = $this->prepareAutoCompletion($data, $keepLayouts);
  108. $response = $this->getResponse();
  109. $response->setBody($data);
  110. if (!$this->suppressExit) {
  111. $response->sendResponse();
  112. exit;
  113. }
  114. return $data;
  115. }
  116. /**
  117. * Strategy pattern: allow calling helper as broker method
  118. *
  119. * Prepares autocompletion data and, if $sendNow is true, immediately sends
  120. * response.
  121. *
  122. * @param mixed $data
  123. * @param bool $sendNow
  124. * @param bool $keepLayouts
  125. * @return string|void
  126. */
  127. public function direct($data, $sendNow = true, $keepLayouts = false)
  128. {
  129. if ($sendNow) {
  130. return $this->sendAutoCompletion($data, $keepLayouts);
  131. }
  132. return $this->prepareAutoCompletion($data, $keepLayouts);
  133. }
  134. }