Commit 8d8230e7 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Fix errors during loading and modification of empty menus

parent 267fcc78
......@@ -37,24 +37,26 @@ try {
// @todo: Get all the filenames of the files referred to in the $menuitems records.
// (files table doesn't exist yet)
$rows = array();
foreach ($menuitems as $i) {
$r = array();
$r['id'] = $i->id;
$r['name'] = $i->title;
if (empty($i->url) && !empty($i->file)) {
$r['type'] = 'adminfile';
$r['linkedto'] = $i->file; // @todo: substitute the appropriate filename.
// $r['link'] = ''; // @todo: provide a link to the file
if ($menuitems) {
foreach ($menuitems as $i) {
$r = array();
$r['id'] = $i->id;
$r['name'] = $i->title;
if (empty($i->url) && !empty($i->file)) {
$r['type'] = 'adminfile';
$r['linkedto'] = $i->file; // @todo: substitute the appropriate filename.
// $r['link'] = ''; // @todo: provide a link to the file
}
else if (!empty($i->url) && empty($i->file)) {
$r['type'] = 'externallink';
$r['linkedto'] = $i->url;
// $r['link'] = $i->url;
}
else {
throw new Exception ('Exactly one of {file,url} should be set in site_menu table');
}
$rows[] = $r;
}
else if (!empty($i->url) && empty($i->file)) {
$r['type'] = 'externallink';
$r['linkedto'] = $i->url;
// $r['link'] = $i->url;
}
else {
throw new Exception ('Exactly one of {file,url} should be set in site_menu table');
}
$rows[] = $r;
}
$result['menuitems'] = array_values($rows);
$result['error'] = false;
......
......@@ -28,14 +28,6 @@ define('INTERNAL', 1);
define('ADMIN', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
function send_fail_message() {
$result = array();
$result['error'] = 'local';
$result['message'] = get_string('savefailed');
echo json_encode($result);
exit;
}
$type = param_alpha('type'); // external list or admin file
$name = param_variable('name');
$linkedto = param_variable('linkedto');
......@@ -51,7 +43,7 @@ else if ($type == 'externallink') {
$data->url = $linkedto;
}
else { // Bad menu item type
send_fail_message();
json_reply('local',get_string('badmenuitemtype'));
}
$data->title = $name;
......@@ -61,14 +53,16 @@ if ($itemid == 'new') {
try {
$displayorders = get_rows('site_menu', 'public', $data->public, '', 'displayorder');
$max = 0;
foreach ($displayorders as $r) {
$max = $r['displayorder'] >= $max ? $r['displayorder'] + 1 : $max;
if ($displayorders) {
foreach ($displayorders as $r) {
$max = $r['displayorder'] >= $max ? $r['displayorder'] + 1 : $max;
}
}
$data->displayorder = $max;
insert_record('site_menu', $data);
}
catch (Exception $e) {
send_fail_message();
json_reply('local',get_string('savefailed'));
}
}
else {
......@@ -77,13 +71,10 @@ else {
update_record('site_menu', $data, 'id');
}
catch (Exception $e) {
send_fail_message();
json_reply('local',get_string('savefailed'));
}
}
$result = array();
$result['error'] = false;
$result['message'] = get_string('savedsuccessfully');
echo json_encode($result);
json_reply(false,get_string('savedsuccessfully'));
?>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment