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