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: $js .= '<link rel="stylesheet" href="' . $path . 'lib/contenido_integration.css">' . PHP_EOL;
262: }
263:
264:
265: $js .= <<<JS
266: <script type="text/javascript">
267: (function(Con, $) {
268: $(function() {
269: if (!$('#{ID}')[0]) {
270: // Node is missing, nothing to initialize here...
271: return;
272: }
273: Con.CodeMirrorHelper.init('{ID}', {
274: extraKeys: {
275: 'F11': function() {
276: Con.CodeMirrorHelper.toggleFullscreenEditor('{ID}');
277: },
278: 'Esc': function() {
279: Con.CodeMirrorHelper.toggleFullscreenEditor('{ID}');
280: }
281: }
282: {PROPERTIES}
283: });
284: });
285: })(Con, Con.$);
286: </script>
287: JS;
288:
289: $this->setProperty('mode', $this->_getSyntaxName(), false);
290: $this->setProperty('theme', 'default ' . $this->_textareaId, false);
291:
292:
293:
294: $properties = '';
295: foreach ($this->_properties as $property) {
296: if ($property['is_numeric'] == true) {
297: $properties .= ', ' . $property['name'] . ': ' . $property['value'];
298: } else {
299: $properties .= ', ' . $property['name'] . ': "' . $property['value'] . '"';
300: }
301: }
302:
303:
304: $textareaId = $this->_textareaId;
305: $jsResult = str_replace('{ID}', $textareaId, $js);
306: $jsResult = str_replace('{PROPERTIES}', $properties, $jsResult);
307:
308: return $jsResult;
309: }
310:
311: }
312: