Import SpellChecker imple object from IMP.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 16 Jul 2009 19:56:22 +0000 (13:56 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 16 Jul 2009 20:05:41 +0000 (14:05 -0600)
framework/Ajax/lib/Horde/Ajax/Imple/SpellChecker.php [new file with mode: 0644]
framework/Ajax/package.xml

diff --git a/framework/Ajax/lib/Horde/Ajax/Imple/SpellChecker.php b/framework/Ajax/lib/Horde/Ajax/Imple/SpellChecker.php
new file mode 100644 (file)
index 0000000..b062ba9
--- /dev/null
@@ -0,0 +1,110 @@
+<?php
+/**
+ * Attach the spellchecker to a javascript element.
+ *
+ * Copyright 2005-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 Horde_Ajax
+ */
+class Horde_Ajax_Imple_SpellChecker extends Horde_Ajax_Imple_Base
+{
+    /**
+     * Constructor.
+     *
+     * @param array $params  Configuration parameters.
+     * <pre>
+     * 'id' => TODO (optional)
+     * 'locales' => TODO (optional)
+     * 'states' => TODO (optional)
+     * 'targetId' => TODO (optional)
+     * 'triggerId' => TODO (optional)
+     * </pre>
+     */
+    public function __construct($params = array())
+    {
+        if (empty($params['id'])) {
+            $params['id'] = $this->_randomid();
+        }
+        if (empty($params['targetId'])) {
+            $params['targetId'] = $this->_randomid();
+        }
+        if (empty($params['triggerId'])) {
+            $params['triggerId'] = $params['targetId'] . '_trigger';
+        }
+        if (empty($params['states'])) {
+            $params['states'] = '""';
+        } else {
+            $params['states'] = Horde_Serialize::serialize($params['states'], Horde_Serialize::JSON, Horde_Nls::getCharset());
+        }
+        if (empty($params['locales'])) {
+            $params['locales'] = array();
+            foreach (array_keys(Horde_Nls::$config['spelling']) as $lcode) {
+                $params['locales'][$lcode] = Horde_Nls::$config['languages'][$lcode];
+            }
+        }
+        asort($params['locales'], SORT_LOCALE_STRING);
+        $params['locales'] = Horde_Serialize::serialize($params['locales'], Horde_Serialize::JSON, Horde_Nls::getCharset());
+
+        parent::__construct($params);
+    }
+
+    /**
+     */
+    public function attach()
+    {
+        Horde::addScriptFile('prototype.js', 'horde', true);
+        Horde::addScriptFile('effects.js', 'horde', true);
+        Horde::addScriptFile('KeyNavList.js', 'horde', true);
+        Horde::addScriptFile('SpellChecker.js', 'horde', true);
+
+        $url = $this->_getUrl('SpellChecker', 'horde', array('input' => $this->_params['targetId']));
+
+        Horde::addInlineScript($this->_params['id'] . ' = new SpellChecker("' . $url . '", "' . $this->_params['targetId'] . '", "' . $this->_params['triggerId'] . '", ' . $this->_params['states'] . ', ' . $this->_params['locales'] . ', \'widget\');', 'dom');
+    }
+
+    /**
+     */
+    public function handle($args)
+    {
+        $spellArgs = array();
+
+        if (!empty($GLOBALS['conf']['spell']['params'])) {
+            $spellArgs = $GLOBALS['conf']['spell']['params'];
+        }
+
+        if (isset($args['locale'])) {
+            $spellArgs['locale'] = $args['locale'];
+        } elseif (isset($GLOBALS['language'])) {
+            $spellArgs['locale'] = $GLOBALS['language'];
+        }
+
+        /* Add local dictionary words. */
+        try {
+            $result = Horde::loadConfiguration('spelling.php', 'ignore_list', 'horde');
+            $spellArgs['localDict'] = $result;
+        } catch (Horde_Exception $e) {}
+
+        if (!empty($args['html'])) {
+            $spellArgs['html'] = true;
+        }
+
+        try {
+            $speller = Horde_SpellChecker::getInstance($GLOBALS['conf']['spell']['driver'], $spellArgs);
+        } catch (Horde_Exception $e) {
+            Horde::logMessage($e, __FILE__, __LINE__, PEAR_LOG_ERR);
+            return array();
+        }
+
+        try {
+            return $speller->spellCheck(Horde_Util::getPost($args['input']));
+        } catch (Horde_Exception $e) {
+            Horde::logMessage($e, __FILE__, __LINE__, PEAR_LOG_ERR);
+            return array('bad' => array(), 'suggestions' => array());
+        }
+    }
+
+}
index 4094921..94bcf8b 100644 (file)
@@ -33,6 +33,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
      <dir name="Ajax">
       <dir name="Imple">
        <file name="Base.php" role="php" />
+       <file name="SpellChecker.php" role="php" />
       </dir> <!-- /lib/Horde/Ajax/Imple -->
       <file name="Imple.php" role="php" />
      </dir> <!-- /lib/Horde/Ajax -->
@@ -48,16 +49,26 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <pearinstaller>
     <min>1.7.0</min>
    </pearinstaller>
+   <package>
+    <name>Core</name>
+    <channel>pear.horde.org</channel>
+   </package>
   </required>
-  <package>
-   <name>Core</name>
-   <channel>pear.horde.org</channel>
-  </package>
-  <optional/>
+  <optional>
+   <package>
+    <name>SpellChecker</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
+    <name>Serialize</name>
+    <channel>pear.horde.org</channel>
+   </package>
+  </optional>
  </dependencies>
  <phprelease>
   <filelist>
    <install name="lib/Horde/Ajax/Imple/Base.php" as="Horde/Ajax/Imple/Base.php" />
+   <install name="lib/Horde/Ajax/Imple/SpellChecker.php" as="Horde/Ajax/Imple/SpellChecker.php" />
    <install name="lib/Horde/Ajax/Imple.php" as="Horde/Ajax/Imple.php" />
   </filelist>
  </phprelease>