1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class CodeMirror {
25:
26: 27: 28: 29: 30:
31: private $_properties = array();
32:
33: 34: 35: 36: 37:
38: private $_textareaId = '';
39:
40: 41: 42: 43: 44:
45: private $_activated = true;
46:
47: 48: 49: 50: 51:
52: private $_addScript = true;
53:
54: 55: 56: 57: 58:
59: private $_cfg = array();
60:
61: 62: 63: 64: 65:
66: private $_language = '';
67:
68: 69: 70: 71: 72:
73: private $_syntax = '';
74:
75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:
91: public function __construct($id, $syntax, $lang, $addScript, $cfg, $editable = true) {
92:
93: $this->_properties = array();
94: $this->_cfg = (array) $cfg;
95: $this->_addScript = (boolean) $addScript;
96: $this->_textareaId = (string) $id;
97: $this->_activated = true;
98: $this->_language = (string) $lang;
99: $this->_syntax = (string) $syntax;
100:
101:
102: if ($editable == false) {
103: $this->setProperty('readOnly', 'true', true);
104: }
105:
106: $this->setProperty('lineNumbers', 'true', true);
107: $this->setProperty('lineWrapping', 'true', true);
108: $this->setProperty('matchBrackets', 'true', true);
109: $this->setProperty('indentUnit', 4, true);
110: $this->setProperty('indentWithTabs', 'true', true);
111: $this->setProperty('enterMode', 'keep', false);
112: $this->setProperty('tabMode', 'shift', false);
113:
114:
115:
116:
117:
118: $this->_getSystemProperties();
119: }
120:
121: 122: 123: 124: 125: 126: 127: 128:
129: private function _getSystemProperties() {
130:
131: if (getEffectiveSetting('codemirror', 'activated', 'true') == 'false') {
132: $this->_activated = false;
133: }
134:
135: $userSettings = getEffectiveSettingsByType('codemirror');
136: foreach ($userSettings as $key => $value) {
137: if ($key != 'activated') {
138: if ($value == 'true' || $value == 'false' || is_numeric($value)) {
139: $this->setProperty($key, $value, true);
140: } else {
141: $this->setProperty($key, $value, false);
142: }
143: }
144: }
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154: 155: 156:
157: public function setProperty($name, $value, $isNumeric = false) {
158:
159: $name = (string) $name;
160: $value = (string) $value;
161: $isNumeric = (boolean) $isNumeric;
162:
163:
164: $record = array();
165: $record['name'] = $name;
166: $record['value'] = $value;
167: $record['is_numeric'] = $isNumeric;
168:
169:
170:
171: $this->_properties[$name] = $record;
172: }
173:
174: private function _getSyntaxScripts() {
175: $path = $this->_cfg['path']['contenido_fullhtml'] . 'external/codemirror';
176:
177: $js = '';
178: $jsTemplate = '<script type="text/javascript" src="%s/mode/%s/%s.js"></script>';
179:
180: $modes = array();
181:
182: $syntax = $this->_syntax;
183: if ($syntax == 'js' || $syntax == 'html' || $syntax == 'php') {
184: $modes[] = 'javascript';
185: }
186:
187: if ($syntax == 'css' || $syntax == 'html' || $syntax == 'php') {
188: $modes[] = 'css';
189: }
190:
191: if ($syntax == 'html' || $syntax == 'php') {
192: $modes[] = 'xml';
193: }
194:
195: if ($syntax == 'php') {
196: $modes[] = 'php';
197: $modes[] = 'clike';
198: }
199:
200: if ($syntax == 'html') {
201: $modes[] = 'htmlmixed';
202: }
203:
204: foreach ($modes as $mode) {
205: $js .= sprintf($jsTemplate, $path, $mode, $mode) . PHP_EOL;
206: }
207:
208: return $js;
209: }
210:
211: private function _getSyntaxName() {
212: if ($this->_syntax == 'php') {
213: return 'application/x-httpd-php';
214: }
215:
216: if ($this->_syntax == 'html') {
217: return 'text/html';
218: }
219:
220: if ($this->_syntax == 'css') {
221: return 'text/css';
222: }
223:
224: if ($this->_syntax == 'js') {
225: return 'text/javascript';
226: }
227: }
228:
229: 230: 231: 232: 233:
234: public function renderScript() {
235:
236: if ($this->_activated == false) {
237: return '';
238: }
239:
240:
241: $js = '';
242: if ($this->_addScript) {
243: $conPath = $this->_cfg['path']['contenido_fullhtml'];
244: $path = $conPath . 'external/codemirror/';
245:
246: $language = $this->_language;
247: if (!file_exists($this->_cfg['path']['contenido'] . 'external/codemirror/lib/lang/' . $language . '.js')) {
248: $language = 'en';
249: }
250:
251: $js .= '<script type="text/javascript" src="' . $path . 'lib/lang/' . $language . '.js"></script>' . PHP_EOL;
252: $js .= '<script type="text/javascript" src="' . $path . 'lib/codemirror.js"></script>' . PHP_EOL;
253: $js .= '<script type="text/javascript" src="' . $path . 'lib/util/foldcode.js"></script>' . PHP_EOL;
254: $js .= '<script type="text/javascript" src="' . $path . 'lib/util/dialog.js"></script>' . PHP_EOL;
255: $js .= '<script type="text/javascript" src="' . $path . 'lib/util/searchcursor.js"></script>' . PHP_EOL;
256: $js .= '<script type="text/javascript" src="' . $path . 'lib/util/search.js"></script>' . PHP_EOL;
257: $js .= '<script type="text/javascript" src="' . $path . 'lib/contenido_integration.js"></script>' . PHP_EOL;
258: $js .= $this->_getSyntaxScripts();
259: $js .= '<link rel="stylesheet" href="' . $path . 'lib/codemirror.css" />' . PHP_EOL;
260: $js .= '<link rel="stylesheet" href="' . $path . 'lib/util/dialog.css" />' . PHP_EOL;
261: }
262:
263:
264: $js .= "<script type=\"text/javascript\">
265: function toggleCodeMirrorFullscreen_{ID}() {
266: toggleCodeMirrorFullscreenEditor('{ID}');
267: }
268:
269: properties_{ID} = {
270: extraKeys: {\"F11\": toggleCodeMirrorFullscreen_{ID}, \"Esc\": toggleCodeMirrorFullscreen_{ID}}
271: {PROPERTIES}
272: };
273:
274: window.setTimeout('initCodeMirror(\"{ID}\", properties_{ID})', 100);
275: </script>";
276:
277: $this->setProperty('mode', $this->_getSyntaxName(), false);
278: $this->setProperty('theme', 'default ' . $this->_textareaId, false);
279:
280:
281:
282: $properties = '';
283: foreach ($this->_properties as $property) {
284: if ($property['is_numeric'] == true) {
285: $properties .= ', ' . $property['name'] . ':' . $property['value'] . "\n";
286: } else {
287: $properties .= ', ' . $property['name'] . ': "' . $property['value'] . "\"\n";
288: }
289: }
290:
291:
292: $textareaId = $this->_textareaId;
293: $jsResult = str_replace('{ID}', $textareaId, $js);
294: $jsResult = str_replace('{PROPERTIES}', $properties, $jsResult);
295:
296: return $jsResult;
297: }
298:
299: }
300: