menu.php 10 KB
Newer Older
Richard Mansfield's avatar
Richard Mansfield committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
/**
 * This program is part of Mahara
 *
 *  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
 * @author     Richard Mansfield <richard.mansfield@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

27
define('INTERNAL', 1);
Richard Mansfield's avatar
Richard Mansfield committed
28
define('ADMIN', 1);
29
define('MENUITEM', 'configsite');
Nigel McNie's avatar
Nigel McNie committed
30
define('SUBMENUITEM', 'sitemenu');
31
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
Richard Mansfield's avatar
Richard Mansfield committed
32

33
34
35
$strings = array('edit','delete','update','cancel','add','name','unknownerror');
$adminstrings = array('deletefailed','deletingmenuitem','savingmenuitem',
                      'noadminfiles','loggedinmenu','loggedoutmenu','linkedto','externallink','adminfile',
36
37
                      'loadingmenuitems','menuitemsloaded','failedloadingadminfiles',
                      'failedloadingmenuitems');
38
39
40
41
42
43
44
foreach ($strings as $string) {
    $getstring[$string] = "'" . get_string($string) . "'";
}
foreach ($adminstrings as $string) {
    $getstring[$string] = "'" . get_string($string,'admin') . "'";
}

45
$thead = array(get_string('type','admin'),get_string('name','admin'),get_string('linkedto','admin'),'');
Richard Mansfield's avatar
Richard Mansfield committed
46
$ijs = "var thead = TR(null,map(partial(TH,null),['" . implode($thead,"','") . "']));\n";
47
48
$ijs .= "var externallink = '" . get_string('externallink','admin') . "';\n";
$ijs .= "var adminfile = '" . get_string('adminfile','admin') . "';\n";
Richard Mansfield's avatar
Richard Mansfield committed
49
50
51

$ijs .= <<< EOJS
// Request a list of menu items from the server
Richard Mansfield's avatar
Richard Mansfield committed
52
function getitems() {
Richard Mansfield's avatar
Richard Mansfield committed
53
    processingStart();
54
    var d = loadJSONDoc('getmenuitems.json.php',{'public':selectedmenu == 'loggedoutmenu'});
Richard Mansfield's avatar
Richard Mansfield committed
55
56
    d.addCallback(function(data) {
        if (!data.error) {
57
            //logDebug({$getstring['menuitemsloaded']});
Richard Mansfield's avatar
Richard Mansfield committed
58
59
60
61
            displaymenuitems(data.menuitems);
            processingStop();
        }
        else {
62
            displayMessage({$getstring['failedloadingmenuitems']},'error');
Richard Mansfield's avatar
Richard Mansfield committed
63
64
65
66
67
            processingStop();
        }
    });
}

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Get a list of the available admin files
function getadminfiles() {
    processingStart();
    var d = loadJSONDoc('getadminfiles.json.php',{'public':selectedmenu == 'loggedoutmenu'});
    d.addCallback(function(data) {
        if (!data.error) {
            adminfiles = data.adminfiles;
            processingStop();
        }
        else {
            displayMessage({$getstring['failedloadingadminfiles']},'error');
            adminfiles = null;
            processingStop();
        }
    });
}

Richard Mansfield's avatar
Richard Mansfield committed
85
86
87
// Puts the list of menu items into the empty table.
function displaymenuitems(itemlist) {
    var rows = map(formatrow,itemlist);
88
89
90
91
    var form = FORM({'id':'form','method':'post','enctype':'multipart/form-data',
                         'encoding':'multipart/form-data'},
                    TABLE(null,TBODY(null,[thead,rows,addform()])));
    replaceChildNodes($('menuitemlist'),form);
Richard Mansfield's avatar
Richard Mansfield committed
92
93
94
95
96
97
}

// Creates one table row
function formatrow (item) {
    // item has id, type, name, link, linkedto
    var type = eval(item.type);
98
    var linkedto = A({'href':item.linkedto},item.linktext);
99
    var del = INPUT({'type':'button','value':{$getstring['delete']}});
Richard Mansfield's avatar
Richard Mansfield committed
100
    del.onclick = function () { delitem(item.id); };
101
    var edit = INPUT({'type':'button','value':{$getstring['edit']}});
Richard Mansfield's avatar
Richard Mansfield committed
102
103
    edit.onclick = function () { edititem(item); };
    var cells = map(partial(TD,null),[type,item.name,linkedto,[del,edit]]);
Richard Mansfield's avatar
Richard Mansfield committed
104
    return TR({'id':'menuitem_'+item.id},cells);
Richard Mansfield's avatar
Richard Mansfield committed
105
106
107
108
109
110
}

// Returns the form which adds a new menu item
function addform(type) {
    var item = {'id':'new'};
    item.type = type ? type : 'externallist';
111
    return editform(item);
Richard Mansfield's avatar
Richard Mansfield committed
112
113
114
115
116
117
118
}

// Creates the contents of a menu item edit form
// This is formatted as a table within the form (which is within a row of the table).
function editform(item) {
    // item has id, type, name, link, linkedto
    // The form has two radio buttons to select the type, external link or admin file
Richard Mansfield's avatar
Richard Mansfield committed
119
120
    var elink = INPUT({'type':'radio','name':'type'+item.id,'value':'externallink'});
    var afile = INPUT({'type':'radio','name':'type'+item.id,'value':'adminfile'});
Richard Mansfield's avatar
Richard Mansfield committed
121
122
123

    // Either a save, a cancel button, or both.
    var savecancel = [];
124
    var save = INPUT({'type':'button'});
Richard Mansfield's avatar
Richard Mansfield committed
125
    save.onclick = function () { saveitem(item.id); };
Richard Mansfield's avatar
Richard Mansfield committed
126
127
128
129

    // The link field will be a text box or a select in the case of an admin file.
    var linkedto = null;

130
    var rowtype = 'add';
Richard Mansfield's avatar
Richard Mansfield committed
131
132
133
134
135
136
137
138
139
140
141
    if (!item) {
        // This is the 'add' form rather than the edit form
        // Set defaults.
        item = {'type':'externallist'};
    }
    if (!item.linkedto) {
        item.linkedto = '';
        item.name = '';
        elink.onclick = function () { changeaddform('externallink'); };
        afile.onclick = function () { changeaddform('adminfile'); };
        // The save button says 'add', and there's no cancel button.
142
        setNodeAttribute(save,'value',{$getstring['add']});
Richard Mansfield's avatar
Richard Mansfield committed
143
144
145
146
        savecancel = [save];
    }
    else { // Editing an existing menu item.
        // The save button says 'update' and there's a cancel button.
147
        var rowtype = 'edit';
148
149
        setNodeAttribute(save,'value',{$getstring['update']});
        var cancel = INPUT({'type':'button','value':{$getstring['cancel']}});
Richard Mansfield's avatar
Richard Mansfield committed
150
151
152
153
154
155
156
        cancel.onclick = closeopenedits;
        savecancel = [save,cancel];
        elink.onclick = function () { changeeditform(item,'externallink'); };
        afile.onclick = function () { changeeditform(item,'adminfile'); };
    }

    // A text field for the name
Richard Mansfield's avatar
Richard Mansfield committed
157
    var name = INPUT({'type':'text','id':'name'+item.id,'value':item.name});
Richard Mansfield's avatar
Richard Mansfield committed
158
159
160
161

    if (item.type == 'adminfile') {
        if (adminfiles == null) {
            // There are no admin files, we don't need the select or save button
162
            linkedto = {$getstring['noadminfiles']};
Richard Mansfield's avatar
Richard Mansfield committed
163
164
165
166
            savecancel = [cancel];
        }
        else {
            // Select the currently selected file.
167
168
169
170
171
172
173
174
175
            linkedto = SELECT({'id':'linkedto'+item.id});
            for (var i = 0; i < adminfiles.length; i++) {
                if (item.file == adminfiles[i].id) {
                    appendChildNodes(linkedto, OPTION({'value':adminfiles[i].id, 'selected':true}, adminfiles[i].name));
                }
                else {
                    appendChildNodes(linkedto, OPTION({'value':adminfiles[i].id}, adminfiles[i].name));
                }
            }
Richard Mansfield's avatar
Richard Mansfield committed
176
177
178
179
        }
        setNodeAttribute(afile,'checked',true);
    }
    else { // type = externallist
Richard Mansfield's avatar
Richard Mansfield committed
180
        linkedto = INPUT({'type':'text','id':'linkedto'+item.id,'value':item.linkedto});
Richard Mansfield's avatar
Richard Mansfield committed
181
182
        setNodeAttribute(elink,'checked',true);
    }
183
184
    var radios = [DIV(null, LABEL(null,elink,{$getstring['externallink']})),
                  DIV(null, LABEL(null,afile,{$getstring['adminfile']}))];
Richard Mansfield's avatar
Richard Mansfield committed
185
    var row = TR({'id':'row'+item.id, 'class':rowtype},
186
187
                 map(partial(TD,null),[radios,name,linkedto,savecancel]));
    return row;
Richard Mansfield's avatar
Richard Mansfield committed
188
189
190
191
192
193
}

// Close all open edit forms
function closeopenedits() {
    var rows = getElementsByTagAndClassName('tr',null,$('menuitemlist'));
    for (var i=0; i<rows.length; i++) {
194
        if (hasElementClass(rows[i],'edit')) {
Richard Mansfield's avatar
Richard Mansfield committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
            removeElement(rows[i]);
        }
        else if (hasElementClass(rows[i],'invisible')) {
            removeElementClass(rows[i],'invisible');
        }
    }
}

// Change the type of an edit form
function changeeditform(item, type) {
    item.type = type;
    edititem(item);
}

// Change the type of the add form
function changeaddform(type) {
211
212
    var newrow = addform(type);
    swapDOM($('rownew'),newrow);
Richard Mansfield's avatar
Richard Mansfield committed
213
214
215
216
217
218
219
}

// Open a new edit form
function edititem(item) {
    closeopenedits();
    var menuitem = $('menuitem_'+item.id);
    addElementClass(menuitem,'invisible');
220
    var newrow = editform(item);
221
    insertSiblingNodesBefore(menuitem, newrow);
Richard Mansfield's avatar
Richard Mansfield committed
222
223
}

Richard Mansfield's avatar
Richard Mansfield committed
224
// Receive standard json error message
Richard Mansfield's avatar
Richard Mansfield committed
225
226
// Request deletion of a menu item from the db
function delitem(itemid) {
227
    logDebug({$getstring['deletingmenuitem']});
228
    sendjsonrequest('deletemenuitem.json.php',{'itemid':itemid}, getitems);
Richard Mansfield's avatar
Richard Mansfield committed
229
230
231
}

// Send the menu item in the form to the database.
232
233
234
235
function saveitem(itemid) {
    var f = $('form');
    var name = $('name'+itemid).value;
    var linkedto = $('linkedto'+itemid).value;
Richard Mansfield's avatar
Richard Mansfield committed
236
    if (name == '') {
237
        displayMessage(get_string('namedfieldempty',{$getstring['name']}),'error');
Richard Mansfield's avatar
Richard Mansfield committed
238
239
240
        return false;
    }
    if (linkedto == '') {
241
        displayMessage(get_string('namedfieldempty',{$getstring['linkedto']}),'error');
Richard Mansfield's avatar
Richard Mansfield committed
242
243
        return false;
    }
244
    logDebug({$getstring['savingmenuitem']});
245
    var data = {'type':eval('f.type'+itemid+'[0].checked') ? 'externallink' : 'adminfile',
Richard Mansfield's avatar
Richard Mansfield committed
246
247
                'name':name,
                'linkedto':linkedto,
248
                'itemid':itemid,
249
                'public':selectedmenu == 'loggedoutmenu'};
250
    sendjsonrequest('updatemenu.json.php', data, getitems);
Richard Mansfield's avatar
Richard Mansfield committed
251
252
253
    return false;
}

254
255
function changemenu() {
    selectedmenu = $('menuselect').value;
Richard Mansfield's avatar
Richard Mansfield committed
256
    getitems();
257
    getadminfiles();
Richard Mansfield's avatar
Richard Mansfield committed
258
259
}

260
var selectedmenu = 'loggedoutmenu';
261
var adminfiles = null;
262
263
264
265
266
addLoadEvent(function () {
    $('menuselect').value = selectedmenu;
    $('menuselect').onchange = changemenu;
    changemenu();
});
Richard Mansfield's avatar
Richard Mansfield committed
267
268
EOJS;

269
270
271
$menulist = array('loggedinmenu', 'loggedoutmenu');
foreach ($menulist as &$menu) {
    $menu = array('value' => $menu,
272
                  'name' => get_string($menu,'admin'));
273
274
}

275
$style = '<style type="text/css">.invisible{display:none;}</style>';
276
277
278
279
$smarty = smarty(array(), array($style));
$smarty->assign('INLINEJAVASCRIPT', $ijs);
$smarty->assign('MENUS', $menulist);
$smarty->display('admin/site/menu.tpl');
Richard Mansfield's avatar
Richard Mansfield committed
280
281

?>