Skip to content

Instantly share code, notes, and snippets.

@markward
Created May 4, 2012 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markward/2593208 to your computer and use it in GitHub Desktop.
Save markward/2593208 to your computer and use it in GitHub Desktop.
Moodle 2 - Generate pure HTML menu
//custom navigation bar - pure HTML
function themename_get_menu(){
//get the menu string from CFG
global $CFG;
$menu = $CFG->custommenuitems;
//now get everything into a two dimensional array [entry][value]
$items = explode("\r\n", $menu);
$count = 0;
while (isset($items[$count])){
$items[$count] = explode("|", $items[$count]);
$count++;
}
//generate our list
$count = 0;
$indent = 0;
$output = "";
while (isset($items[$count])){
$strpos = strrpos($items[$count][0], "-");
//work out this lines indent
if ($strpos === false){
$itemindent = 0;
}
else{
$itemindent = $strpos +1;
$items[$count][0] = str_replace("-", "", $items[$count][0]);
}
//compare this indent to the previous line and adjust if necessary
while($itemindent != $indent){
if ($itemindent > $indent){
$output = substr($output, 0, -7);
$output = $output.str_repeat("\r\n ", $indent);
$indent++;
$output = $output."<ul>\r";
}
elseif ($itemindent < $indent){
$indent--;
$output = $output.str_repeat("\r ", $indent);
$output = $output."</ul></li>";
}
}
//is this to open on a new page or on self?
if(isset($items[$count][1])){
if (strstr($items[$count][1],$CFG->wwwroot) === FALSE){
$target = "_blank";
}
else{
$target= "_self";
}
}
//work out the class
if($indent == 0){
$class = "top_level";
}else{
$class = "child_level";
}
//finally, output the list item
$output = $output.str_repeat(" ", $indent);
$id = $items[$count][0];
$id = str_replace("›", "", $id);
$id = str_replace(" ", "", $id);
$id = strtolower($id);
if ($items[$count][1] != ''){
$output = $output."<li class='".$class."' id='item-".$id."'>
<a class='button' href='".$items[$count][1]."' target='".$target."' alt='".$items[$count][0]."'>".$items[$count][0]."</a>
</li>";
}else{
$output = $output."<li class='".$class." c2h' id='item-".$id."'>
<a class='button' href='#' target='_self' alt='".$items[$count][0]."'>".$items[$count][0]."</a>
</li>";
}
$count++;
}
return "<ul id='navlist'>\n".$output."</ul>\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment