From 47c4c765978cd7c2652c73e22cd51cdfa1747b44 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Fri, 30 Jan 2009 20:54:47 -0500 Subject: [PATCH] Initial TagAutoCompleter class --- kronolith/lib/Imple/TagAutoCompleter.php | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 kronolith/lib/Imple/TagAutoCompleter.php diff --git a/kronolith/lib/Imple/TagAutoCompleter.php b/kronolith/lib/Imple/TagAutoCompleter.php new file mode 100644 index 000000000..24fa07d0d --- /dev/null +++ b/kronolith/lib/Imple/TagAutoCompleter.php @@ -0,0 +1,90 @@ + + * @package Kronolith + */ +class Kronolith_Imple_TagAutoCompleter extends Kronolith_Imple +{ + /** + * Constructor. + * + * @param array $params Configuration parameters. + *
+     * 'triggerId' => TODO (optional)
+     * 'resultsId' => TODO (optional)
+     * 
+ */ + 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); + } + +} -- 2.11.0