<?php
    
//Error display
    
ini_set('display_errors''1');
    
ini_set('display_startup_errors''1');
    
error_reporting(E_ALL);

    
// define the pages
    
$options = array('contact','welcome','test');

        
    
// check if no page has been selected
    
if(!isset($_POST['DATA_page']))
        
$_POST['DATA_page']=$options[0];

    
// check if the page exists in the array
    
if(!in_array($_POST['DATA_page'],$options))
        
$_POST['DATA_page']=$options[0];


    
//check the folder pages
    
$folder="pages/";
    
//$files=array_diff(scandir($folder), array('..', '.')); //get all the files in the folder and remove .. and .


    // define the pages
    
$files = array('contact','welcome','test');
    
$filename='pages/'.$_POST['DATA_page'].'.php';

    
//save on this point
    
if(isset($_POST['BUTTON_save']))
    {
        if(
file_exists($filename))
        {
            
file_put_contents($filename,$_POST['DATA_code']);
        }
    }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CMS</title>
</head>
<body>

<?php
    $title 
"This is my CMS";
    
$description "This is the description of my CMS";
    echo 
"<h1>$title</h1>";
    echo 
"<p>$description</p>";
?>

<!-- display dropdown menu with pages inside, to edit pages -->
 <form id="form" action="cms.php" method="POST">
    <nav>
        <select name="DATA_page" onchange="document.getElementById('form').submit();">
                <?php
                    
// display the select box
                    
foreach($files as $file)
                    {
                        echo 
$files;
                        echo 
'<option';
                        
// select the actual page
                        
if($_POST['DATA_page'] == $file
                            echo 
' selected="selected"';
                        echo 
'>'.$file.'</option>';
                    }
                
?>
            </select>
    </nav>
        
    <button type="submit" name="BUTTON_edit">Edit</button>
    <div id="main">
        <?php
           
            
//echo '<pre>' . print_r($_POST,true).'</pre>';
                // check if the file exists

                
if(file_exists($filename))
                {
                    
$code file_get_contents($filename);
                    
?>
                    <textarea name="DATA_code"><?=$code?></textarea>
                    <button type="submit" name="BUTTON_save">Save</button>
                    <?php
                
}
                else
                    echo 
"Sorry, the file page does not exist on the server.";
            
?>

    </div>
 </form>

    
</body>
</html>