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