1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: cInclude('external', 'codemirror/class.codemirror.php');
20: cInclude('includes', 'functions.file.php');
21:
22: 23: 24: 25: 26: 27:
28: class cModuleTemplateHandler extends cModuleHandler {
29:
30:
31: private $_code;
32: private $_file;
33: private $_tmp_file;
34: private $_area;
35: private $_frame;
36: private $_status;
37: private $_action;
38: private $_new;
39: private $_delete;
40: private $_selectedFile;
41: private $_reloadScript;
42: private $_page = NULL;
43: private $_notification = null;
44:
45: 46: 47: 48: 49:
50: private $_templateFileEnding = 'html';
51:
52: 53: 54: 55: 56:
57: private $_newFileName = 'newfilename';
58:
59: 60: 61: 62: 63:
64: private $_actionCreate = 'htmltpl_create';
65:
66: 67: 68: 69: 70:
71: private $_actionEdit = 'htmltpl_edit';
72:
73: 74: 75: 76: 77:
78: private $_actionDelete = 'htmltpl_delete';
79:
80: 81: 82: 83: 84:
85: private $_testArea = 'htmltpl';
86:
87: public function __construct($idmod, $page) {
88: parent::__construct($idmod);
89: $this->_page = $page;
90: $this->_notification = new cGuiNotification();
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: public function setNewDelete($new, $delete) {
101: $this->_new = $new;
102: $this->_delete = $delete;
103: }
104:
105: 106: 107: 108: 109:
110: public function setCode($code) {
111: $this->_code = stripslashes($code);
112: }
113:
114: 115: 116: 117: 118:
119: public function setSelectedFile($selectedFile) {
120: $this->_selectedFile = $selectedFile;
121: }
122:
123: 124: 125: 126: 127: 128: 129:
130: public function setFiles($file, $tmpFile) {
131: $this->_file = $file;
132: $this->_tmp_file = $tmpFile;
133: }
134:
135: 136: 137: 138: 139:
140: public function setStatus($status) {
141: $this->_status = $status;
142: }
143:
144: 145: 146: 147: 148: 149: 150:
151: public function setFrameIdmodArea($frame, $idmod, $area) {
152: $this->_frame = $frame;
153: $this->_idmod = $idmod;
154: $this->_area = $area;
155: }
156:
157: 158: 159: 160: 161:
162: public function setAction($action) {
163: $this->_action = $action;
164: }
165:
166: 167: 168: 169: 170: 171:
172: public function checkWritePermissions() {
173: if ($this->moduleWriteable('template') == false) {
174: return $this->_notification->displayNotification(cGuiNotification::LEVEL_WARNING, i18n("You have no write permissions for this module"));
175: } else {
176: return true;
177: }
178: }
179:
180: 181: 182: 183: 184: 185: 186:
187: private function _getAction() {
188: global $newModTpl, $deleteModTpl;
189:
190: if (isset($this->_status)) {
191:
192: if (isset($newModTpl)) {
193: return 'new';
194: }
195:
196: if (isset($deleteModTpl)) {
197: return 'delete';
198: }
199:
200: if (isset($this->_file) && isset($this->_tmp_file)) {
201: if ($this->_file == $this->_tmp_file) {
202:
203:
204: if (empty($this->_file)) {
205: return 'empty';
206: } else {
207: return 'save';
208: }
209: }
210:
211: if ($this->_file != $this->_tmp_file) {
212: return 'rename';
213: }
214: } else {
215:
216: throw new cException(i18n('Field of the file name is empty!'));
217: }
218: } else {
219: return 'default';
220: }
221: }
222:
223: 224: 225: 226: 227:
228: private function _hasSelectedFileChanged() {
229: if ($this->_file != $this->_selectedFile) {
230: return true;
231: } else {
232: return false;
233: }
234: }
235:
236: 237: 238:
239: private function _save() {
240:
241: $ret = $this->createModuleFile('template', $this->_file, $this->_code);
242:
243: if ($ret) {
244: $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Saved changes successfully!'));
245: }
246:
247: if ($this->_hasSelectedFileChanged()) {
248: $this->_file = $this->_selectedFile;
249: $this->_tmp_file = $this->_selectedFile;
250: }
251: }
252:
253: 254: 255: 256: 257:
258: private function _rename() {
259: if ($this->renameModuleFile('template', $this->_tmp_file, $this->_file) == false) {
260: throw new cException(i18n('Rename of the file failed!'));
261: } else {
262: $this->createModuleFile('template', $this->_file, $this->_code);
263: $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Renamed the template file successfully!'));
264: $this->_tmp_file = $this->_file;
265: }
266: }
267:
268: 269: 270:
271: private function _new() {
272: $fileName = '';
273: if ($this->existFile('template', $this->_newFileName . '.' . $this->_templateFileEnding)) {
274: $fileName = $this->_newFileName . $this->getRandomCharacters(5) . '.' . $this->_templateFileEnding;
275: $this->createModuleFile('template', $fileName, '');
276: $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Created a new template file successfully!'));
277: } else {
278: $this->createModuleFile('template', $this->_newFileName . '.' . $this->_templateFileEnding, '');
279: $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Created a new template file successfully!'));
280: $fileName = $this->_newFileName . '.' . $this->_templateFileEnding;
281: }
282:
283: $this->_file = $fileName;
284: $this->_tmp_file = $fileName;
285: }
286:
287: 288: 289:
290: private function _delete() {
291: $ret = $this->deleteFile('template', $this->_tmp_file);
292: if ($ret == true) {
293: $this->_notification->displayNotification(cGuiNotification::LEVEL_INFO, i18n('Deleted the template file successfully!'));
294: }
295: $files = $this->getAllFilesFromDirectory('template');
296:
297: if (is_array($files)) {
298: if (!key_exists('0', $files)) {
299: $this->_file = '';
300: $this->_tmp_file = '';
301: } else {
302: $this->_file = $files[0];
303: $this->_tmp_file = $files[0];
304: }
305: }
306: }
307:
308: 309: 310:
311: public function _default() {
312: $files = $this->getAllFilesFromDirectory('template');
313:
314:
315: if (count($files) > 0) {
316: $this->_tmp_file = $files[0];
317: $this->_file = $files[0];
318: } else {
319:
320: $this->_file = '';
321: $this->_tmp_file = '';
322: }
323: }
324:
325: 326: 327: 328: 329: 330: 331: 332: 333:
334: private function _havePremission($perm, $notification, $action) {
335: switch ($action) {
336: case 'new':
337: if (!$perm->have_perm_area_action($this->_testArea, $this->_actionCreate)) {
338: $notification->displayNotification('error', i18n('Permission denied'));
339: return -1;
340: }
341: break;
342: case 'save':
343: case 'rename':
344: if (!$perm->have_perm_area_action($this->_testArea, $this->_actionEdit)) {
345: $notification->displayNotification('error', i18n('Permission denied'));
346: return -1;
347: }
348: break;
349: case 'delete':
350: if (!$perm->have_perm_area_action($this->_testArea, $this->_actionDelete)) {
351: $notification->displayNotification('error', i18n('Permission denied'));
352: return -1;
353: }
354: break;
355: default:
356: return true;
357: break;
358: }
359: }
360:
361: 362: 363: 364:
365: private function _validateHTML($notification) {
366:
367: if (getEffectiveSetting('layout', 'htmlvalidator', 'true') == 'true' && $this->_code !== '') {
368: $v = new cHTMLValidator();
369: $v->validate($this->_code);
370: $msg = '';
371:
372: foreach ($v->missingNodes as $value) {
373: $idqualifier = '';
374:
375: $attr = array();
376:
377: if ($value['name'] != '') {
378: $attr['name'] = "name '" . $value['name'] . "'";
379: }
380:
381: if ($value['id'] != '') {
382: $attr['id'] = "id '" . $value['id'] . "'";
383: }
384:
385: $idqualifier = implode(', ', $attr);
386:
387: if ($idqualifier != '') {
388: $idqualifier = "($idqualifier)";
389: }
390: $msg .= sprintf(i18n("Tag '%s' %s has no end tag (start tag is on line %s char %s)"), $value['tag'], $idqualifier, $value['line'], $value['char']) . '<br />';
391: }
392:
393: if ($msg != '') {
394: $notification->displayNotification('warning', $msg) . '<br />';
395: }
396: }
397: }
398:
399: private function _makeFormular($belang) {
400: $fileForm = new cGuiTableForm("file_editor");
401: $fileForm->addHeader(i18n('Choose file'));
402: $fileForm->setTableid('choose_mod_template_file');
403: $fileForm->setVar('area', $this->_area);
404: $fileForm->setVar('action', $this->_action);
405: $fileForm->setVar('frame', $this->_frame);
406: $fileForm->setVar('status', 'send');
407: $fileForm->setVar('tmp_file', $this->_tmp_file);
408: $fileForm->setVar('idmod', $this->_idmod);
409: $fileForm->setVar('file', $this->_file);
410:
411: $form = new cGuiTableForm('file_editor');
412: $form->setTableid('mod_template');
413: $form->addHeader(i18n('Edit file'));
414: $form->setVar('area', $this->_area);
415: $form->setVar('action', $this->_action);
416: $form->setVar('frame', $this->_frame);
417: $form->setVar('status', 'send');
418: $form->setVar('tmp_file', $this->_tmp_file);
419: $form->setVar('idmod', $this->_idmod);
420: $form->setVar('file', $this->_file);
421: $form->setVar('selectedFile', $this->_file);
422:
423: $selectFile = new cHTMLSelectElement('selectedFile');
424: $selectFile->setClass("fileChooser");
425:
426: $filesArray = $this->getAllFilesFromDirectory('template');
427:
428:
429: foreach ($filesArray as $key => $file) {
430: $optionField = new cHTMLOptionElement($file, $file);
431:
432:
433: if ($file == $this->_file) {
434: $optionField->setAttribute('selected', 'selected');
435: }
436:
437: $selectFile->addOptionElement($key, $optionField);
438: }
439:
440: $aDelete = new cHTMLLink('main.php');
441: $aDelete->setId("deleteLink");
442: $aDelete->setContent(i18n("Delete template"));
443: $aDelete->setClass('deletefunction');
444: $aDelete->setCustom("deleteModTpl", "1");
445: $aDelete->setCustom('area', $this->_area);
446: $aDelete->setCustom('action', $this->_action);
447: $aDelete->setCustom('frame', $this->_frame);
448: $aDelete->setCustom('status', 'send');
449: $aDelete->setCustom('idmod', $this->_idmod);
450: $aDelete->setCustom('file', $this->_file);
451: $aDelete->setCustom('tmp_file', $this->_tmp_file);
452:
453: $aAdd = new cHTMLLink('main.php');
454: $aAdd->setContent(i18n('New template'));
455: $aAdd->setClass('addfunction');
456: $aAdd->setCustom("newModTpl", "1");
457: $aAdd->setCustom('area', $this->_area);
458: $aAdd->setCustom('action', $this->_action);
459: $aAdd->setCustom('frame', $this->_frame);
460: $aAdd->setCustom('status', 'send');
461: $aAdd->setCustom('tmp_file', $this->_tmp_file);
462: $aAdd->setCustom('idmod', $this->_idmod);
463: $aAdd->setCustom('file', $this->_file);
464:
465:
466: $tb_name = new cHTMLTextbox('file', $this->_file, 60);
467:
468: $ta_code = new cHTMLTextarea('code', conHtmlSpecialChars($this->_code), 100, 35, 'code');
469:
470: $ta_code->setStyle('font-family: monospace;width: 100%;');
471:
472: $ta_code->updateAttributes(array(
473: 'wrap' => getEffectiveSetting('html_editor', 'wrap', 'off')
474: ));
475:
476: $fileForm->add(i18n('Action'), $aAdd->toHTML());
477:
478: if ($this->_file) {
479: $fileForm->add(i18n('Action'), $aDelete->toHTML());
480: $fileForm->add(i18n('File'), $selectFile);
481: }
482:
483:
484: if ($this->_file) {
485: $form->add(i18n('Name'), $tb_name);
486: $form->add(i18n('Code'), $ta_code);
487: }
488: $this->_page->setContent(array(
489: $fileForm
490: ));
491: if ($this->_file) {
492: $this->_page->appendContent($form);
493: }
494:
495: $oCodeMirror = new CodeMirror('code', 'html', substr(strtolower($belang), 0, 2), true, $this->_cfg);
496: $this->_page->addScript($oCodeMirror->renderScript());
497:
498:
499: }
500:
501: 502: 503: 504: 505: 506:
507: public function display($perm, $notification, $belang) {
508: $myAction = $this->_getAction();
509:
510:
511: if ($this->_havePremission($perm, $notification, $myAction) === -1) {
512: return;
513: }
514:
515: try {
516: switch ($myAction) {
517: case 'save':
518: $this->_save();
519: break;
520: case 'rename':
521: $this->_rename();
522: break;
523: case 'new':
524: $this->_new();
525: break;
526: case 'delete':
527: $this->_delete();
528: break;
529: default:
530: $this->_default();
531: break;
532: }
533:
534: $this->_code = $this->getFilesContent('template', '', $this->_file);
535: $this->_validateHTML($notification);
536: $this->_makeFormular($belang);
537: } catch (Exception $e) {
538: $this->_page->displayError(i18n($e->getMessage()));
539: }
540: }
541:
542: }