Initial TagAutoCompleter class
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 31 Jan 2009 01:54:47 +0000 (20:54 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sat, 31 Jan 2009 14:54:39 +0000 (09:54 -0500)
kronolith/lib/Imple/TagAutoCompleter.php [new file with mode: 0644]

diff --git a/kronolith/lib/Imple/TagAutoCompleter.php b/kronolith/lib/Imple/TagAutoCompleter.php
new file mode 100644 (file)
index 0000000..24fa07d
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+/**
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package Kronolith
+ */
+class Kronolith_Imple_TagAutoCompleter extends Kronolith_Imple
+{
+    /**
+     * Constructor.
+     *
+     * @param array $params  Configuration parameters.
+     * <pre>
+     * 'triggerId' => TODO (optional)
+     * 'resultsId' => TODO (optional)
+     * </pre>
+     */
+    public function __construct($params)
+    {
+        if (empty($params['triggerId'])) {
+            $params['triggerId'] = $this->_randomid();
+        }
+        if (empty($params['resultsId'])) {
+            $params['resultsId'] = $params['triggerId'] . '_results';
+        }
+
+        parent::__construct($params);
+    }
+
+    /**
+     * Attach the Imple object to a javascript event.
+     */
+    public function attach()
+    {
+        parent::attach();
+        Horde::addScriptFile('autocomplete.js', 'kronolith', true);
+
+        $params = array(
+            '"' . $this->_params['triggerId'] . '"',
+            '"' . $this->_params['resultsId'] . '"',
+            '"' . Horde::url($GLOBALS['registry']->get('webroot', 'kronolith') . '/imple.php?imple=TagAutoCompleter/input=' . rawurlencode($this->_params['triggerId']), true) . '"'
+        );
+
+        $js_params = array(
+            'tokens: [",", ";"]',
+            'indicator: "' . $this->_params['triggerId'] . '_loading_img"',
+            'afterUpdateElement: function(f, t) { if (!f.value.endsWith(";")) { f.value += ","; } f.value += " "; }'
+        );
+
+        $params[] = '{' . implode(',', $js_params) . '}';
+
+        Kronolith::addInlineScript('new Ajax.Autocompleter(' . implode(',', $params) . ')', 'dom');
+    }
+
+    /**
+     * TODO
+     *
+     * @param array $args  TODO
+     *
+     * @return string  TODO
+     */
+    public function handle($args)
+    {
+        // Avoid errors if 'input' isn't set and short-circuit empty searches.
+        if (empty($args['input']) ||
+            !($input = Util::getFormData($args['input']))) {
+            return array();
+        }
+        return array_map('htmlspecialchars', $this->getTagList($input));
+    }
+
+    /**
+     * Get a list of existing, used tags that match the search term.
+     *
+     * @param string $search  The term to search by.
+     *
+     * @return array  All matching tags.
+     */
+    static public function getTagList($search = '')
+    {
+        $tagger = new Kronolith_Tagger();
+        $tags = $tagger->listTags($search);
+        return array_values($tags);
+    }
+
+}