From 015ba71af040d44fd1042aacffa615b5a07af20f Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sat, 23 Oct 2010 18:43:31 +0000 Subject: [PATCH] MDL-24777 replace_smilies() replaced with the new standard filter --- filter/emoticon/filter.php | 154 ++++++++++++++++++++ filter/emoticon/filtersettings.php | 33 +++++ filter/emoticon/lang/en/filter_emoticon.php | 31 ++++ filter/emoticon/version.php | 27 ++++ lib/adminlib.php | 1 + lib/deprecatedlib.php | 9 ++ lib/weblib.php | 65 --------- 7 files changed, 255 insertions(+), 65 deletions(-) create mode 100644 filter/emoticon/filter.php create mode 100644 filter/emoticon/filtersettings.php create mode 100644 filter/emoticon/lang/en/filter_emoticon.php create mode 100644 filter/emoticon/version.php diff --git a/filter/emoticon/filter.php b/filter/emoticon/filter.php new file mode 100644 index 00000000000..b312c9e9176 --- /dev/null +++ b/filter/emoticon/filter.php @@ -0,0 +1,154 @@ +. + +/** + * Filter converting emoticon texts into images + * + * This filter uses the emoticon settings in Site admin > Appearance > HTML settings + * and replaces emoticon texts with images. + * + * @package filter + * @subpackage emoticon + * @see emoticon_manager + * @copyright 2010 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +class filter_emoticon extends moodle_text_filter { + + /** + * @var array global configuration for this filter + * + * This might be eventually moved into parent class if we found it + * useful for other filters, too. + */ + protected static $globalconfig; + + /** + * Apply the filter to the text + * + * @see filter_manager::apply_filter_chain() + * @param string $text to be processed by the text + * @param array $options filter options + * @return string text after processing + */ + public function filter($text, array $options = array()) { + + if (!isset($options['originalformat'])) { + // if the format is not specified, we are probably called by {@see format_string()} + // in that case, it would be dangerous to replace text with the image because it could + // be stripped. therefore, we do nothing + return $text; + } + if (in_array($options['originalformat'], explode(',', $this->get_global_config('formats')))) { + $this->replace_emoticons($text); + } + return $text; + } + + //////////////////////////////////////////////////////////////////////////// + // internal implementation starts here + //////////////////////////////////////////////////////////////////////////// + + /** + * Returns the global filter setting + * + * If the $name is provided, returns single value. Otherwise returns all + * global settings in object. Returns null if the named setting is not + * found. + * + * @param mixed $name optional config variable name, defaults to null for all + * @return string|object|null + */ + protected function get_global_config($name=null) { + $this->load_global_config(); + if (is_null($name)) { + return self::$globalconfig; + + } elseif (array_key_exists($name, self::$globalconfig)) { + return self::$globalconfig->{$name}; + + } else { + return null; + } + } + + /** + * Makes sure that the global config is loaded in $this->globalconfig + * + * @return void + */ + protected function load_global_config() { + if (is_null(self::$globalconfig)) { + self::$globalconfig = get_config('filter_emoticon'); + } + } + + /** + * Replace emoticons found in the text with their images + * + * @param string $text to modify + * @return void + */ + protected function replace_emoticons(&$text) { + global $CFG, $OUTPUT, $PAGE; + static $emoticontexts = array(); // internal cache used for replacing + static $emoticonimgs = array(); // internal cache used for replacing + + $lang = current_language(); + $theme = $PAGE->theme->name; + + if (!isset($emoticontexts[$lang][$theme]) or !isset($emoticonimgs[$lang][$theme])) { + // prepare internal caches + $manager = get_emoticon_manager(); + $emoticons = $manager->get_emoticons(); + $emoticontexts[$lang][$theme] = array(); + $emoticonimgs[$lang][$theme] = array(); + foreach ($emoticons as $emoticon) { + $emoticontexts[$lang][$theme][] = $emoticon->text; + $emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon)); + } + unset($emoticons); + } + + if (empty($emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here + return; + } + + // detect all the