navigation.php

File Type: php File Location: monofiles/ File name: navigation.php
<?php
    
    
// MonoCMS Navigation Editor 1.0
    //
    // Main navigation is saved inside navigation.xml. Sub menus are saved in different xml files.
    
    
    //monocms login and user rights
    
include_once 'data_connect.php';
    include 
'log.php';
    
    if (
get_account($_SESSION['userID'])['rights'] != 'administrator'){
        
header('Location:panel.php');
        exit();
    }
    
    
$file 'filesinfo/navigation.xml';
    
$xmlstr '<?xml version="1.0" encoding="utf-8"?><menu></menu>';
    
    
//Menu item submitted
    // POST info-> itemtitle,itemid,itempos,itemurl,itemtar,subfile
    
if (isset($_POST['itemtitle'])) {
        
        
$ctitle str_replace('&','&amp;',$_POST['itemtitle']);
        
$curl htmlspecialchars($_POST['itemurl'],ENT_COMPAT ENT_HTML5);
        
        
        
// Main menu file
        
$savefile $file;
        
        
        
// Sub item string
        
if (isset($_POST['subfile'])) {
            
            
$sfile 'filesinfo/'.$_POST['subfile'].'.xml';
            if (!
file_exists($sfile))
                
file_put_contents($sfile,$xmlstr);
            
            
$savefile $sfile;
        
        }
        
        
$menu = new simpleXMLElement($savefile,null,true);
        
        
$lim 0;
        
$nu $_POST['itempos'];
        
        
// ID 0 means new item
        
if ($_POST['itemid'] == '0'){
            
            
            
//pos must be integer(numeric)
            
if (!is_numeric($nu))
                
$nu 999;
            
            
            
// We must check that the file we save in exists
            // if not, the following actions are not needed.
            
if (file_exists($savefile)){
                
                
                
// Total number of items already existing(in a single menu row)
                
foreach ($menu as $count$lim++;
                

                
// position cannot be negative or 0
                
if ($nu <= 0)
                    
$nu 1;
                    
                    
                
// if a user submits i.e. 999 as a position, it will drop down
                // to the maximum allowed by the limit
                
if ($nu $lim+1)
                    
$nu $lim+1;
                
                
                
//new item means we must move some items up +1 position
                
$fi=0;
                foreach(
$menu->menuitem as $item){
                    if ((int)
$item->pos >= $nu)
                        
$menu->menuitem[$fi]->pos = (int)$item->pos+1;
                    
$fi++;
                }
            }
            
            
//if this is the first item in the menu
            
else
            
//position must be 1                
            
$nu 1;

            
            
// create and save file
            
$idname md5(time() + microtime());
        
            
$newitem $menu->addChild('menuitem','');
            
$newitem->addChild('title',$ctitle);
            
$newitem->addChild('id',$idname);
            
$newitem->addChild('url',$curl);
            
$newitem->addChild('pos',$nu);
            
            
$nt = ($_POST['itemtar'] == '') ? ' ' 'checked';
            
            
$newitem->addChild('tar',$nt);
            
            
$menu->asXML($savefile);
            
        }
        
        
        
//if ID is not 0, item already exists
        
else {
        
        
$c=0;
        foreach(
$menu->menuitem as $item){
            
            if (
$_POST['itemid'] == (string)$item->id){
                
                
$ol = (int)$item->pos;
                
$ne $nu;
            
                if (
$nu != $ol) {
                
                
//pos must be numeric
                    
if (!is_numeric($nu))
                        
$nu $ol;
                    
                    
                    
//minimum pos is 1
                    
if ($nu <= 0)
                        
$nu 1;
                    
                    
                    
//pos must be limited to max = sum of items
                    
foreach ($menu as $count
                            
$lim++;
                            
                    if (
$nu $lim)
                        
$nu $lim;
                    
                    
                    
// when creating a menu item or changing an item's position
                    // we must also change position of all the items affected
                    
$d=0;
                    foreach(
$menu->menuitem as $it){
                            
                        if ((
$nu $ol) && ((int)$it->pos >= $nu) && ((int)$it->pos $ol))
                            
$menu->menuitem[$d]->pos = ((int)$it->pos)+1;
                        if ((
$nu $ol) && ((int)$it->pos <= $nu) && ((int)$it->pos $ol))
                            
$menu->menuitem[$d]->pos = ((int)$it->pos)-1;
                            
                        
$d++;
                        if (
$d == $lim)
                            break;
                    }
                }
                
//update title,url,target,pos and save file
                
$menu->menuitem[$c]->title $ctitle;
                
$menu->menuitem[$c]->url $curl;
                
$menu->menuitem[$c]->tar= ($_POST['itemtar'] == '') ? ' ' 'checked';
                
$menu->menuitem[$c]->pos $nu;
                
                
$menu->asXML($savefile);
                break;
            }
            
            
$c++;
        }
        if(!isset(
$ne))
            
$error true;
        }
    
    
//save in session and redirect
    
$_SESSION['MONitemset'] = '<div id="action-confirm"><span>Navigation saved</span></div>';
    
    if (isset(
$error))
        
$_SESSION['MONitemset'] = '<div id="error-confirm"><span>Please create a new item.</span></div>';
    
    
header('Location:navigation.php');
    exit();

    }



    
// Delete item
    // actions-> 'del'
    
if (isset($_GET['del'])){
        
        if (isset(
$_GET['file']))
            
$file 'filesinfo/'.$_GET['file'].'.xml';
        
        
$loadfile simplexml_load_file($file);
        
        
        
// find out if this is the last item we delete 
        
if (($loadfile->menuitem[1] == null) || ($loadfile->menuitem[0] == null))
            if (
$file != 'filesinfo/navigation.xml')
                
unlink($file);
            else
                
file_put_contents($file,$xmlstr);
        
        
        else {
        foreach(
$loadfile->menuitem as $item){
            if ((string)
$item->id == $_GET['del']){
                
$minpos = (int)$item->pos;
                break;
            }
        }
            
        
//create a new element and include every object except the one that is going to be deleted
        
$newelem = new simpleXMLElement($xmlstr);
        foreach(
$loadfile->menuitem as $item){
            
            if ((string)
$item->id != $_GET['del']) {
                    
                
$min=((int)$item->pos $minpos)? 1:0;
        
                
$addnew $newelem->addChild('menuitem','');
                
$addnew->addChild('title',$item->title);
                
$addnew->addChild('id',$item->id);
                
$addnew->addChild('pos',((int)$item->pos) - $min);
                
$addnew->addChild('url',$item->url);
                }
        }
        
$newelem->asXML($file);
        }


        
//save in session and redirect
        
$_SESSION['MONitemset'] = '<div id="action-confirm"><span>Item deleted</span></div>';
        
header('Location:navigation.php');
        exit();
        
        
    }
    
    
    
//prepare screen message & unset session
    
if (isset($_SESSION['MONitemset'])){
        
$message $_SESSION['MONitemset'];
        unset(
$_SESSION['MONitemset']);
    }
    
    
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit navigation</title>
<link rel="stylesheet" type="text/css" href="cmstyle.css">
<?php
    
    
include 'csstheme.php'
?>
<style>
@charset utf-8;

@media(max-width:700px) {
.center-container{margin:0}
.settings-container{margin:0;left:0}
}

</style>
<script type="text/javascript">function conf(form){return confirm('Delete navigation item?');}</script>
</head>
<body>
    
    <?php
        
        
include 'mypanel.php';
            
        if (isset(
$message))
            echo 
$message
        
    ?>
    
    <div class="main">

        <h1 class="inbl">
        <?php 
        
if (isset($_GET['id']))
        print 
'&nbsp;<a href="navigation.php" title="Back to Navigation">&larr;</a>'
        
?>
        Navigation links</h1>

        <div class="settings-container">
            
            <div class="naveditor">

                <?php

                
//Create a new or open an existing item for modification.
                //We use GET parameters for different occasions:id,del,file.

                // actions -> 'id','file'

                
if (isset($_GET['id'])){

                    if (isset(
$_GET['file'])) {
                        
$file 'filesinfo/'.$_GET['file'].'.xml';    
                        
$input '<input type="text" name="subfile" value="'.$_GET['file'].'" hidden>';
                    }    
                
                    else
                    
$input '';

                    
                    
$f simplexml_load_file($file);

                    
// a new item
                    
if ($_GET['id'] == '0') {

                        
$hd '<h3>New item</h3>';
                        if(isset(
$_GET['file']))
                        
$hd '<h3>Sub item</h3>';
                        
$id '0';
                        
$ttl '';
                        
$url '';
                        
$pos 'value=""';
                        if (
$f->menuitem[0] == null)
                        
$pos 'value="" disabled';
                        
$tar '';

                    }

                    else {
                        
                        
// existing item
                        
foreach($f->menuitem as $item){

                            if (
$_GET['id'] == $item->id) {

                                
$hd '<h2>'.$item->title.'</h2>';
                                
$id $item->id;
                                
$ttl $item->title;
                                
$url $item->url;
                                
$pos 'value="'.$item->pos.'"';
                                
$tar $item->tar;
                                break;
                            }
                        }
                    }
                }

                
                if (isset(
$_GET['id'])) : print $hd ?>
                
                <form action="navigation.php" method="POST">
                    
                    <?php print $input ?>
                    
                    <input type="text" name="itemid" value="<?php print $id ?>" hidden>
                    
                    Title<br>
                    <input type="text" name="itemtitle" class="navitem" value="<?php print $ttl ?>">
                    
                    URL<br>
                    <input type="text" name="itemurl" class="navitem" value="<?php print $url ?>">
                    
                    Position<br>
                    <input type="text" name="itempos" class="navitem" <?php print $pos ?>>
                    
                    <input name="itemtar" type="checkbox" id="target" class="gencheck" value="checked" <?php print $tar ?>>
                    <label for="target" title="Open link in new tab or window." style="line-height:22px">Open in new tab</label>
                    <div style="height:50px"></div>
                    
                    <!-- SUBMIT -->
                    <input type="submit" class="stylish-button" value="Save">
                
                </form>
            </div>
            <?php    

            
endif;

            
// function to print navigation. Returns a sorted array with the menu or false in case of 0 items
            
include 'display-order.php';

            
// Print navigation menu on editor screen
            
$start display_order('filesinfo/navigation.xml');

            if (!isset(
$_GET['id'])) :

            
?>
            <div class="add-item">
                <a href="navigation.php?id=0" title="New item">Add item <span style="position: relative; top: 3px; left: 5px; font-size: 21px;">+</span></a>
            </div>
            
            <div class="equals">
            <?php
            
                
if ($start !== false) {
                    foreach(
$start as $navitem){

                        echo 
'<div class="itemcon"><a href="navigation.php?id='.$navitem->id.'" class="item" title="Edit">'.$navitem->title.'</a><a href="navigation.php?del='.$navitem->id.'" class="delitem" onclick="return conf(this)" title="Delete item">delete x</a><a href="navigation.php?id=0&amp;file='.$navitem->id.'" class="newsub" title="Create a sub item">sub item +</a></div>';

                        if (
is_file('filesinfo/'.$navitem->id.'.xml')){

                            
$second display_order('filesinfo/'.$navitem->id.'.xml');
                            if (
$second !== false)
                                foreach(
$second as $sec)
                                    echo 
'<div class="subitemcon"><a href="navigation.php?id='.$sec->id.'&amp;file='.$navitem->id.'" class="sub-item" title="Edit">'.$sec->title .'</a><a href="navigation.php?del='.$sec->id.'&amp;file='.$navitem->id.'" class="delitem" onclick="return conf(this)" title="Delete item">delete x</a></div>'.PHP_EOL;

                        }
                    }
                }
                else 
                    echo 
'<span style="color:#555;font-size:14px">No menu items</span>'
            
?> 
            </div>
            <?php endif ?>
        </div>
    </div>
</body>
</html>