00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009 class HtmlText {
00010
00011
00012
00013
00014
00015
00016
00017 const WIDGET = 'html';
00018
00019
00020
00021
00022 const CONVERSION_DEFAULT = 'default';
00023
00024
00025
00026
00027 const EDITOR_DEFAULT = 'default';
00028
00029
00030
00031
00032 const STORAGE = 'STORAGE';
00033
00034
00035
00036 const OUTPUT = 'OUTPUT';
00037
00038
00039
00040 const EDIT = 'EDIT';
00041
00042 private static $conversions = array();
00043 private static $richtexteditors = array();
00044 private static $richtexteditors_enabled = array();
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 public static function set_conversion($type, $conversions, $model = self::CONVERSION_DEFAULT) {
00055 foreach(Arr::force($model, false) as $m) {
00056 self::$conversions[$type][$m] = Arr::force($conversions, false);
00057 }
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 public static function get_conversion($type, $model = self::CONVERSION_DEFAULT, $use_fallback = true) {
00069 if ($model instanceof IDataObject) {
00070 $model = $model->get_table_name();
00071 }
00072 $ret = array();
00073 if (isset(self::$conversions[$type][$model])) {
00074 $ret = self::$conversions[$type][$model];
00075 }
00076 else if ($use_fallback) {
00077 $ret = Arr::get_item_recursive(self::$conversions, array($type, self::CONVERSION_DEFAULT), array());
00078 }
00079 return $ret;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 public static function add_conversion($type, $conversions, $model = self::CONVERSION_DEFAULT, $to_front = false) {
00091 $conversions = Arr::force($conversions, false);
00092 foreach(Arr::force($model, false) as $m) {
00093 $recent = self::get_conversion($type, $m, true);
00094 if ($to_front) {
00095 $recent = array_merge($conversions, $recent);
00096 }
00097 else {
00098 $recent = array_merge($recent, $conversions);
00099 }
00100 self::set_conversion($type, $recent, $m);
00101 }
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 public static function apply_conversion($type, $text, $model = self::CONVERSION_DEFAULT) {
00113 $key = array($model, sha1($text));
00114 $ret = RuntimeCache::get($key, null);
00115 if (is_null($ret)) {
00116 $conversions = self::get_conversion($type, $model, true);
00117 $chain = ConverterFactory::create_chain($conversions);
00118 $ret = $chain->encode($text);
00119 RuntimeCache::set($key, $ret);
00120 }
00121 return $ret;
00122 }
00123
00124
00125
00126
00127 public static function register_editor($key, IRichtTextEditor $editor) {
00128 self::$richtexteditors[$key] = $editor;
00129 }
00130
00131
00132
00133
00134 public static function enable_editor($key) {
00135 self::$richtexteditors_enabled[$key] = $key;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 public static function apply_enabled_editors(PageData $page_data) {
00147 foreach(self::$richtexteditors_enabled as $name) {
00148 $editor = Arr::get_item(self::$richtexteditors, $name, false);
00149 if ($editor instanceof IRichtTextEditor) {
00150 $editor->apply($page_data, $name);
00151 }
00152 }
00153 }
00154 }