editsitepage.php 4.44 KB
Newer Older
Richard Mansfield's avatar
Richard Mansfield committed
1
2
<?php
/**
Richard Mansfield's avatar
Richard Mansfield committed
3
 * This program is part of Mahara
Richard Mansfield's avatar
Richard Mansfield committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage admin
Richard Mansfield's avatar
Richard Mansfield committed
21
 * @author     Richard Mansfield <richard.mansfield@catalyst.net.nz>
Richard Mansfield's avatar
Richard Mansfield committed
22
23
24
25
26
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL',1);
28
define('ADMIN', 1);
29
define('MENUITEM','pageeditor');
Richard Mansfield's avatar
Richard Mansfield committed
30
31
32
33
34
35
36
37
38
39
40
41
42
require(dirname(dirname(__FILE__)).'/init.php');
require_once('form.php');

$sitepages = get_records('site_content');
$pageoptions = array();
foreach ($sitepages as $page) {
    $pageoptions[$page->name] = get_string($page->name);
}
asort($pageoptions);

$f = array(
    'name' => 'editsitepage',
    'method' => 'post',
43
44
45
46
    'ajaxpost' => true,
    'ajaxsuccessfunction' => 'contentSaved',
    'ajaxformhandler' => 'savesitepage.json.php',
    //'onsubmit' => 'return submitForm(\'editsitepage\',\'savesitepage.json.php\',contentSaved);',
Richard Mansfield's avatar
Richard Mansfield committed
47
48
49
50
51
52
53
54
55
56
57
    'action' => '',
    'elements' => array(
        'pagename' => array(
            'type' => 'select',
            'title' => get_string('pagename'),
            'value' => 'home',
            'options' => $pageoptions
        ),
        'pagetext' => array(
            'name' => 'pagetext',
            'type' => 'wysiwyg',
58
59
            'rows' => 20,
            'cols' => 80,
Richard Mansfield's avatar
Richard Mansfield committed
60
61
            'title' => get_string('pagetext'),
            'description' => get_string('pagecontents'),
62
            'value' => '',
Richard Mansfield's avatar
Richard Mansfield committed
63
64
65
66
67
68
69
70
71
72
73
74
            'rules' => array(
                'required' => true
            )
        ),
        'submit' => array(
            'value' => get_string('savechanges'),
            'type'  => 'submit',
        )
    )
);

$form = form($f);
75
$js = array('mochikit','mahara');
Richard Mansfield's avatar
Richard Mansfield committed
76
77
78
79
80
if (use_html_editor()) {
    array_unshift($js,'tinymce');
}

$ijs = <<< EOJS
81
82
83
84
85
86
87

// global stuff, set in onLoad().
setEditorContent = function () {};
getEditorContent = function () {};
var oldpagename = '';
var originalcontent = '';

Richard Mansfield's avatar
Richard Mansfield committed
88
function requestPageText() {
89
90
    // Allow the user to abort change if changes have been made in the editor.
    if (getEditorContent() != originalcontent) {
91
        var answer = confirm(get_string('discardpageedits'));
92
93
94
95
96
        if (!answer) {
            $('pagename').value = oldpagename;
            return;
        }
    }
97
    logDebug(get_string('loadingpagecontent', $('pagename').value));
Richard Mansfield's avatar
Richard Mansfield committed
98
99
100
    var d = loadJSONDoc('editchangepage.json.php',{'pagename':$('pagename').value});
    d.addCallback(function(data) {
        if (data.success) {
101
            logDebug(get_string('loadedsuccessfully', $('pagename').value));
Richard Mansfield's avatar
Richard Mansfield committed
102
            setEditorContent(data.content);
103
104
            originalcontent = getEditorContent();
            oldpagename = $('pagename').value;
Richard Mansfield's avatar
Richard Mansfield committed
105
106
107
108
109
110
111
        }
        else {
            displayMessage({'message':get_string('failedloadingpagecontent', $('pagename').value),
                                'type':'error'});
        }
    });
}
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

// Called from submitForm on successful page save.
function contentSaved () {  
    originalcontent = getEditorContent();
    requestPageText();
}

function onLoad() {
    if (typeof(tinyMCE) != 'undefined') {
        setEditorContent = function (c) {
            tinyMCE.setContent(c);
            tinyMCE.execCommand('mceFocus',false,'mce_editor_0');
        }
        getEditorContent = tinyMCE.getContent;
    }
    else {
128
129
        setEditorContent = function (c) { $('pagetext').value = c; };
        getEditorContent = function () { return $('pagetext').value; };
130
    }
131
132
133
    originalcontent = getEditorContent();
    requestPageText();
    connect('pagename', 'onchange', requestPageText);
134
135
}

Richard Mansfield's avatar
Richard Mansfield committed
136
137
138
addLoadEvent(onLoad);
EOJS;

139
$jsstrings = array('discardpageedits');
Richard Mansfield's avatar
Richard Mansfield committed
140
141
142
143
144
145

$smarty = smarty($js,array(),$jsstrings);
$smarty->assign('pageeditform', $form);
$smarty->assign('INLINEJAVASCRIPT', $ijs);
$smarty->display('admin/editsitepage.tpl');

Richard Mansfield's avatar
Richard Mansfield committed
146
?>