File: Zend_Controller-ActionHelpers-AutoComplete.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (353 lines) | stat: -rw-r--r-- 13,999 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect3 id="zend.controller.actionhelpers.autocomplete">
    <title>AutoComplete</title>

    <para>
        Many <acronym>AJAX</acronym> javascript libraries offer functionality for providing
        autocompletion whereby a selectlist of potentially matching results is
        displayed as the user types. The <emphasis>AutoComplete</emphasis> helper aims
        to simplify returning acceptable responses to such methods.
    </para>

    <para>
        Since not all JS libraries implement autocompletion in the same way, the
        <emphasis>AutoComplete</emphasis> helper provides some abstract base
        functionality necessary to many libraries, and concrete implementations
        for individual libraries. Return types are generally either <acronym>JSON</acronym> arrays
        of strings, <acronym>JSON</acronym> arrays of arrays (with each member array being an
        associative array of metadata used to create the selectlist), or <acronym>HTML</acronym>.
    </para>

    <para>
        Basic usage for each implementation is the same:
    </para>

   <programlisting language="php"><![CDATA[
class FooController extends Zend_Controller_Action
{
    public function barAction()
    {
        // Perform some logic...

        // Encode and send response;
        $this->_helper->autoCompleteDojo($data);

        // Or explicitly:
        $response = $this->_helper->autoCompleteDojo
                                  ->sendAutoCompletion($data);

        // Or simply prepare autocompletion response:
        $response = $this->_helper->autoCompleteDojo
                                  ->prepareAutoCompletion($data);
    }
}
]]></programlisting>

    <para>
        By default, autocompletion does the following:
    </para>

    <itemizedlist>
        <listitem><para>Disables layouts and ViewRenderer.</para></listitem>
        <listitem><para>Sets appropriate response headers.</para></listitem>

        <listitem>
            <para>Sets response body with encoded or formatted autocompletion data.</para>
        </listitem>

        <listitem><para>Sends response.</para></listitem>
    </itemizedlist>

    <para>
        Available methods of the helper include:
    </para>

    <itemizedlist>
        <listitem>
            <para>
                <methodname>disableLayouts()</methodname> can be used to disable layouts and
                the ViewRenderer. Typically, this is called within
                <methodname>prepareAutoCompletion()</methodname>.
            </para>
        </listitem>

        <listitem>
            <para>
                <methodname>encodeJson($data, $keepLayouts = false)</methodname> will encode
                data to <acronym>JSON</acronym>, optionally enabling or disabling layouts.
                Typically, this is called within
                <methodname>prepareAutoCompletion()</methodname>.
            </para>
        </listitem>

        <listitem>
            <para>
                <methodname>prepareAutoCompletion($data, $keepLayouts = false)</methodname>
                is used to prepare data in the response format necessary for the
                concrete implementation, optionally enabling or disabling
                layouts. The return value will vary based on the implementation.
            </para>
        </listitem>

        <listitem>
            <para>
                <methodname>sendAutoCompletion($data, $keepLayouts = false)</methodname>
                is used to send data in the response format necessary for the
                concrete implementation. It calls
                <methodname>prepareAutoCompletion()</methodname>, and then sends the
                response.
            </para>
        </listitem>

        <listitem>
            <para>
                <methodname>direct($data, $sendNow = true, $keepLayouts =
                    false)</methodname> is used when calling the helper as a method of
                the helper broker. The <varname>$sendNow</varname> flag is used to
                determine whether to call <methodname>sendAutoCompletion()</methodname> or
                <methodname>prepareAutoCompletion()</methodname>, respectively.
            </para>
        </listitem>
    </itemizedlist>

    <para>
        Currently, <emphasis>AutoComplete</emphasis> supports the Dojo and Scriptaculous
        <acronym>AJAX</acronym> libraries.
    </para>

    <sect4 id="zend.controller.actionhelpers.autocomplete.dojo">
        <title>AutoCompletion with Dojo</title>

        <para>
            Dojo does not have an AutoCompletion widget per se, but has two
            widgets that can perform AutoCompletion: ComboBox and
            FilteringSelect. In both cases, they require a data store that
            implements the QueryReadStore; for more information on these topics, see the <ulink
                url="http://dojotoolkit.org/reference-guide/dojo/data.html">dojo.data</ulink>
            documentation.
        </para>

        <para>
            In Zend Framework, you can pass a simple indexed array to the
            AutoCompleteDojo helper, and it will return a <acronym>JSON</acronym> response suitable
            for use with such a store:
        </para>

        <programlisting language="php"><![CDATA[
// within a controller action:
$this->_helper->autoCompleteDojo($data);
]]></programlisting>

        <example id="zend.controller.actionhelpers.autocomplete.dojo.example1">
            <title>AutoCompletion with Dojo Using Zend MVC</title>

            <para>
                AutoCompletion with Dojo via the Zend <acronym>MVC</acronym> requires several
                things: generating a form object for the ComboBox on which you
                want AutoCompletion, a controller action for serving the
                AutoCompletion results, creating a custom QueryReadStore to connect
                to the AutoCompletion action, and generation of the javascript
                to use to initialize AutoCompletion on the server side.
            </para>

            <para>
                First, let's look at the javascript necessary. Dojo offers a
                complete framework for creating <acronym>OOP</acronym> javascript, much as Zend
                Framework does for <acronym>PHP</acronym>. Part of that is the ability to create
                pseudo-namespaces using the directory hierarchy. We'll create a
                'custom' directory at the same level as the Dojo directory
                that's part of the Dojo distribution. Inside that directory,
                we'll create a javascript file, <filename>TestNameReadStore.js</filename>, with the
                following contents:
            </para>

            <programlisting language="javascript"><![CDATA[
dojo.provide("custom.TestNameReadStore");
dojo.declare("custom.TestNameReadStore", dojox.data.QueryReadStore, {
    fetch:function (request) {
        request.serverQuery = { test:request.query.name };
        return this.inherited("fetch", arguments);
    }
});
]]></programlisting>

            <para>
                This class is simply an extension of Dojo's own QueryReadStore,
                which is itself an abstract class. We simply define a method by
                which to request, and assigning it to the 'test' element.
            </para>

            <para>
                Next, let's create the form element for which we want
                AutoCompletion:
            </para>

            <programlisting language="php"><![CDATA[
class TestController extends Zend_Controller_Action
{
    protected $_form;

    public function getForm()
    {
        if (null === $this->_form) {
            $this->_form = new Zend_Form();
            $this->_form->setMethod('get')
                ->setAction(
                    $this->getRequest()->getBaseUrl() . '/test/process'
                )
                ->addElements(array(
                    'test' => array('type' => 'text', 'options' => array(
                        'filters'        => array('StringTrim'),
                        'dojoType'       => array('dijit.form.ComboBox'),
                        'store'          => 'testStore',
                        'autoComplete'   => 'false',
                        'hasDownArrow'   => 'true',
                        'label' => 'Your input:',
                    )),
                    'go' => array('type' => 'submit',
                                  'options' => array('label' => 'Go!'))
                ));
        }
        return $this->_form;
    }
}
]]></programlisting>

            <para>
                Here, we simply create a form with 'test' and 'go' methods. The
                'test' method adds several special, Dojo-specific attributes:
                dojoType, store, autoComplete, and hasDownArrow. The dojoType is
                used to indicate that we are creating a ComboBox, and we will
                link it to a data store (key 'store') of 'testStore' -- more on
                that later. Specifying 'autoComplete' as <constant>FALSE</constant> tells
                Dojo not to automatically select the first match, but instead show a list of
                matches. Finally, 'hasDownArrow' creates a down arrow similar to
                a select box so we can show and hide the matches.
            </para>

            <para>
                Let's add a method to display the form, as well as an end point
                for processing AutoCompletion:
            </para>

            <programlisting language="php"><![CDATA[
class TestController extends Zend_Controller_Action
{
    // ...

    /**
     * Landing page
     */
    public function indexAction()
    {
        $this->view->form = $this->getForm();
    }

    public function autocompleteAction()
    {
        if ('ajax' != $this->_getParam('format', false)) {
            return $this->_helper->redirector('index');
        }
        if ($this->getRequest()->isPost()) {
            return $this->_helper->redirector('index');
        }

        $match = trim($this->getRequest()->getQuery('test', ''));

        $matches = array();
        foreach ($this->getData() as $datum) {
            if (0 === strpos($datum, $match)) {
                $matches[] = $datum;
            }
        }
        $this->_helper->autoCompleteDojo($matches);
    }
}
]]></programlisting>

            <para>
                In our <methodname>autocompleteAction()</methodname> we do a number of
                things. First, we look to make sure we have a post request, and
                that there is a 'format' parameter set to the value 'ajax';
                these are simply to help reduce spurious queries to the action.
                Next, we check for a 'test' parameter, and compare it against
                our data. (I purposely leave out the implementation of
                <methodname>getData()</methodname> here -- it could be any sort of data
                source.) Finally, we send our matches to our AutoCompletion
                helper.
            </para>

            <para>
                Now that we have all the pieces on the backend, let's look at
                what we need to deliver in our view script for the landing page.
                First, we need to setup our data store, then render our form,
                and finally ensure that the appropriate Dojo libraries --
                including our custom data store -- get loaded. Let's look at the
                view script, which comments the steps:
            </para>

            <programlisting language="php"><![CDATA[
<?php // setup our data store: ?>
<div dojoType="custom.TestNameReadStore" jsId="testStore"
    url="<?php echo $this->baseUrl() ?>/unit-test/autocomplete/format/ajax"
    requestMethod="get"></div>

<?php // render our form: ?>
<?php echo $this->form ?>

<?php // setup Dojo-related CSS to load in HTML head: ?>
<?php $this->headStyle()->captureStart() ?>
@import "<?php echo $this->baseUrl()
?>/javascript/dijit/themes/tundra/tundra.css";
@import "<?php echo $this->baseUrl() ?>/javascript/dojo/resources/dojo.css";
<?php $this->headStyle()->captureEnd() ?>

<?php // setup javascript to load in HTML head, including all required
   // Dojo libraries: ?>
<?php $this->headScript()
        ->setAllowArbitraryAttributes(true)
        ->appendFile($this->baseUrl() . '/javascript/dojo/dojo.js',
            'text/javascript',
            array('djConfig' => 'parseOnLoad: true'))
        ->captureStart() ?>
djConfig.usePlainJson=true;
dojo.registerModulePath("custom","../custom");
dojo.require("dojo.parser");
dojo.require("dojox.data.QueryReadStore");
dojo.require("dijit.form.ComboBox");
dojo.require("custom.TestNameReadStore");
<?php $this->headScript()->captureEnd() ?>
]]></programlisting>

            <para>
                Note the calls to view helpers such as headStyle and headScript;
                these are placeholders, which we can then render in the <acronym>HTML</acronym>
                head section of our layout view script.
            </para>

            <para>
                We now have all the pieces to get Dojo AutoCompletion working.
            </para>
        </example>
    </sect4>

    <sect4 id="zend.controller.actionhelpers.autocomplete.scriptaculous">
        <title>AutoCompletion with Scriptaculous</title>

        <para>
            <ulink
                url="http://wiki.script.aculo.us/scriptaculous/show/Ajax.Autocompleter">Scriptaculous</ulink>
            expects an <acronym>HTML</acronym> response in a specific format.
        </para>

        <para>
            The helper to use with this library is 'AutoCompleteScriptaculous'.
            Simply provide it an array of data, and the helper will create an
            <acronym>HTML</acronym> response compatible with Ajax.Autocompleter.
        </para>
    </sect4>
</sect3>
<!--
vim:se ts=4 sw=4 et:
-->