1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class XoopsEditor extends Xoops\Form\TextArea
22: {
23: 24: 25:
26: const CACHE_KEY_EDITOR_LIST = 'system/editorlist';
27:
28: 29: 30:
31: public $isEnabled;
32:
33: 34: 35:
36: public $configs;
37:
38: 39: 40:
41: public $rootPath;
42:
43: 44: 45: 46: 47:
48: protected $cols;
49:
50: 51: 52: 53: 54:
55: protected $rows;
56:
57: 58: 59:
60: public function __construct()
61: {
62: $this->rows = 5;
63: $this->cols = 50;
64: $this->set('rows', 5);
65: $this->set('cols', 50);
66:
67: $args = func_get_args();
68: $configs = array();
69:
70: if (!empty($args)) {
71: if (!is_array($args[0])) {
72: $i = 0;
73: foreach (array('caption', 'name', 'value', 'rows', 'cols', 'hiddentext') as $key) {
74: if (isset($args[$i])) {
75: $configs[$key] = $args[$i];
76: }
77: ++$i;
78: }
79: $configs = (isset($args[$i]) && is_array($args[$i])) ? array_merge($configs, $args[$i]) : $configs;
80: } else {
81: $configs = $args[0];
82: }
83: }
84:
85: $vars = get_class_vars(__CLASS__);
86: foreach ($configs as $key => $val) {
87: $method = "set" . ucfirst($key);
88: if (method_exists($this, $method)) {
89: $this->$method($val);
90: } else {
91: if (array_key_exists("_{$key}", $vars)) {
92: $this->{"_{$key}"} = $val;
93: } else {
94: if (array_key_exists($key, $vars)) {
95: $this->{$key} = $val;
96: } else {
97: $this->configs[$key] = $val;
98: }
99: }
100: }
101: }
102: $this->isActive();
103: }
104:
105: 106: 107:
108: public function isActive()
109: {
110: $this->isEnabled = true;
111: return $this->isEnabled;
112: }
113:
114: 115: 116: 117:
118: public function setConfig($options)
119: {
120: foreach ($options as $key => $val) {
121: $this->$key = $val;
122: }
123: }
124: }
125:
126: 127: 128: 129: 130: 131: 132: 133: 134:
135: class XoopsEditorHandler
136: {
137: 138: 139:
140: public $root_path = "";
141:
142: 143: 144:
145: public $nohtml = false;
146:
147: 148: 149:
150: public $allowed_editors = array();
151:
152: 153: 154:
155: private function __construct()
156: {
157: $this->root_path = \XoopsBaseConfig::get('root-path') . '/class/xoopseditor';
158: }
159:
160: 161: 162: 163: 164: 165: 166:
167: static function getInstance()
168: {
169: static $instance;
170: if (!isset($instance)) {
171: $class = __CLASS__;
172: $instance = new $class();
173: }
174: return $instance;
175: }
176:
177: 178: 179: 180: 181: 182: 183: 184: 185:
186: public function get($name = '', $options = null, $noHtml = false, $OnFailure = '')
187: {
188: if ($editor = $this->_loadEditor($name, $options)) {
189: return $editor;
190: }
191: $list = array_keys($this->getList($noHtml));
192: if (empty($OnFailure) || !in_array($OnFailure, $list)) {
193: $OnFailure = $list[0];
194: }
195: $editor = $this->_loadEditor($OnFailure, $options);
196: return $editor;
197: }
198:
199:
200: 201: 202: 203:
204: public function buildEditorList()
205: {
206: $list = array();
207: $order = array();
208: $fileList = XoopsLists::getDirListAsArray($this->root_path . '/');
209:
210: foreach ($fileList as $item) {
211: if (XoopsLoad::fileExists($file = $this->root_path . '/' . $item . '/language/' . XoopsLocale::getLegacyLanguage() . '.php')) {
212: include_once $file;
213: } else {
214: if (XoopsLoad::fileExists($file = $this->root_path . '/' . $item . '/language/english.php')) {
215: include_once $file;
216: }
217: }
218: if (XoopsLoad::fileExists($file = $this->root_path . '/' . $item . '/editor_registry.php')) {
219: include $file;
220: if (empty($config['order'])) {
221: continue;
222: }
223: $order[] = $config['order'];
224: $list[$item] = array('title' => $config['title'], 'nohtml' => $config['nohtml']);
225: }
226: }
227: array_multisort($order, $list);
228: return $list;
229: }
230:
231: 232: 233: 234:
235: public function getList($noHtml = false)
236: {
237: $xoops = Xoops::getInstance();
238: $list = $xoops->cache()->cacheRead(
239: XoopsEditor::CACHE_KEY_EDITOR_LIST,
240: array($this, 'buildEditorList')
241: );
242: $editors = array_keys($list);
243: if (!empty($this->allowed_editors)) {
244: $editors = array_intersect($editors, $this->allowed_editors);
245: }
246: $returnList = array();
247: foreach ($editors as $name) {
248: if (!empty($noHtml) && empty($list[$name]['nohtml'])) {
249: continue;
250: }
251: $returnList[$name] = $list[$name]['title'];
252: }
253: return $returnList;
254: }
255:
256: 257: 258: 259: 260:
261: function setConfig(XoopsEditor $editor, $options)
262: {
263: $editor->setConfig($options);
264: }
265:
266: 267: 268: 269: 270: 271: 272:
273: private function _loadEditor($name, $options = null)
274: {
275: $xoops = Xoops::getInstance();
276: $editor = null;
277: if (empty($name) || !array_key_exists($name, $this->getList())) {
278: return $editor;
279: }
280: $editor_path = $this->root_path . '/' . $name;
281: if (XoopsLoad::fileExists($file = $editor_path . '/language/' . XoopsLocale::getLegacyLanguage() . '.php')) {
282: include_once $file;
283: } else {
284: if (XoopsLoad::fileExists($file = $editor_path . '/language/english.php')) {
285: include_once $file;
286: }
287: }
288: if (XoopsLoad::fileExists($file = $editor_path . '/editor_registry.php')) {
289: include $file;
290: } else {
291: return $editor;
292: }
293: if (empty($config['order'])) {
294: return $editor;
295: }
296: include_once $config['file'];
297: $editor = new $config['class']($options);
298: return $editor;
299: }
300: }
301: