<?php


    # Posts categories



    # login
    include_once 'data_connect.php';
    include_once 'log.php';



    # user rights - admins only
    if (get_account($_SESSION['userID'])['rights'] != 'administrator') {
        header('Location:opensaved.php');
        exit;
    }

    #remove arrays from GET
    foreach($_GET as $str)
        if (!is_string($str)) {
            
            if (isset($_GET['deln'])) # or any other get parameter included in options, in the future
                $_SESSION['MON_get_is_array'] = true;
            
            header('Location: catmanage.php');
            exit;
        }


    $ctg = get_categories();


    
    /*if (isset($_POST['allmain'])) {
    
        $xml = simplexml_object('categories.xml','e',null);
        foreach($xml->category as $val)
            $val->main = 'off';
            

        simplexml_object_save('categories.xml',$xml,null);
        
        $_SESSION['MON_cat_main_all'] = true;
        header('Location:catmanage.php');
        exit;
    
    }*/
    

    # Save or remove from main category
    if (isset($_GET['maincat']) || isset($_GET['delmain'])) {
        
        $mdcat = current($_GET);
                

        if (isset($ctg[$mdcat])) {
        
            $xml = simplexml_object('categories.xml','e',null);
            
            $x=0;
            foreach($xml->category as $val) {
                
                if ($mdcat == $val->cid) {
                    
                    $xml->category[$x]->main = $mdcat == $_GET['maincat'] ? 'on' : 'off';
                    simplexml_object_save('categories.xml',$xml,null);
                    break;
                }
                $x++;
            }
        }
        
        
        $_SESSION['MON_cat_main_set'] = true;
        header('Location:catmanage.php');
        exit;
    }


    # Delete a category by id
    function delete_category($deln) {
        
        
        if ($deln != 'uncategorized') {
            
            
            # if parameter is set to true, result contains at least one simplexml object with key [?]
            $remcat = get_categories(true);
            
            
            if ($remcat !== false) {
                
                $sxmlobj = $remcat['?'];
                unset($remcat['?'],$remcat['uncategorized']);
                
                
                if (isset($remcat[$deln])) {
                    
                    $delf = '../'.$remcat[$deln][0];
                    
                    if (file_exists($delf))
                        unlink($delf.'/index.php');

                    
                    unset($remcat[$deln]);
                
                }

                if (!empty($sxmlobj)) {
                
                    unset($sxmlobj->category);
                    foreach($remcat as $key=>$val) {
                        
                        $xmlval = $sxmlobj->addChild('category',PHP_EOL);
                        $xmlval->addChild('cid',$key);
                        $xmlval->addChild('folder',$val[0]);
                        $xmlval->addChild('name',$val[1]);
                        $xmlval->addChild('main',$val[2]);
                        $xmlmrg = $xmlval->addChild('merge');
                        if (!empty($val[3]))
                            foreach($val[3] as $mcat)
                                $xmlmrg->addChild->cat = $mcat;
                            
                        
                        $xmlval->addChild('catindex',$val[4]);
                        $xmlval->addChild('catlink',$val[5]);
                        
                    }        
                }

                    # save the categories
                    if (simplexml_object_save('categories.xml',$sxmlobj,null) !== false)
                        return true;
                    else
                        return false;
            }
        }
    }




    if (isset($_GET['deln'])) {
        
        if (delete_category($_GET['deln']) !== false)
            $_SESSION['MON_delcat'] = true;    
        else
            $_SESSION['MON_cat_falsexml'] = true;

        
        header('Location:catmanage.php');
        exit;
        
    }


?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Categories</title>
<link rel="stylesheet" type="text/css" href="cmstyle.css">
<?php 
    $cssid = '#categories';    
    include 'csstheme.php';
    print '<style>'.$curval.'</style>' ?>
<script type="text/javascript">
function conf(){ return confirm('Delete category?'); }
</script>
</head>
<body>
<?php


        # panel
        include 'mypanel.php';
        action_confirm();
        
        $folders = get_categories(true)

    ?>
    <div class="main">
        <h1>
        Categories  <a class="newpostbut" href="category.php" title="Create a post category"><span class="addnew">+</span>&nbsp;Create a category</a>
        <a class="newpostbut" href="category.php?catid=maincategory" title="Categories settings"><span class="addnew"></span>Settings &rarr;</a>
        </h1>
        <div class="center-container">
            <!--<div class="savedfile savedcat">
                <span class="cat-name"><?php
                
                /*
                echo $folders['?']->maincatname; 
                
                if ($folders['?']->maincatname == '')
                    echo 'Published';
                
                $allc = 'All categories'; 
                $ac = '';
                 
                if ($folders['?']->mainall == 'on')
                     $ac = $allc;
                 
                else {
                    $c=0;
                    foreach($ctg as $k=>$v)
                        if ($v[2] == 'on')
                            $c++;
                    $ac = $c.' categories';
                            
                }
                
                echo ' [ '.$ac.' ]'
                 */
                 ?>
</span>
                <div class="post-options cat-options">
                    <a href="category.php?catid=maincategory" class="options-button" title="Edit category">Edit</a>
                </div>
            </div>
            <div class="clear"></div>
            -->
            
            <div class="ctg">
            <?php
                
                
                
                # Search for categories inside print_content.xml
                $prc = simplexml_object('print_content.xml','l',null);
                $cts=[];
                foreach($prc as $entry) {
                
                    $attr = $entry[0]->attributes();
                    
                    if (empty($attr->status) || $attr->status != 'off') {
                    
                        $ec = explode(',',(string)$entry->categories);
                        if ($entry->force != 'off')
                            $cts = array_merge($cts,$ec);
                    }
                }
                #
                
                
                
                if ($folders !== false) {
                    
                    
                    unset($folders['?']);
                    #unset($folders['uncategorized']);
        
                
                    $fsort = [];
                    foreach($folders as $k=>$v)
                        $fsort[$k] = $v[1];
                        
                    asort($fsort);    
                    
                    
                    $usth = '<div class="hidcat"><img src="images/hidden.png" title="Not published in main feed"></div>';
                    
                    
                    $c=0;
                    foreach($fsort as $k=>$v) {
                        
                        
                        $delc = '<a href="catmanage.php?deln='.$k.'" title="Delete category" class="options-button" onclick="return conf(this)">Delete</a>';
                        
                        if ($folders[$k][4] == 'on')
                            $chp = '<a href="../'.$folders[$k][0].'" target="_blank" class="options-button" title="Visit category home page">Visit</a>';
                        else
                            $chp = '<span class="options-button option-off" title="Index page off">Visit</span>' ;

                        if ($folders[$k][2] != 'on')
                            $cstyle = $usth;
                        else
                            $cstyle = '';
                    
                    
                    
                        if ($v == 'uncategorized') {
                            echo
                            '<div class="clear"></div>
                             <div class="newoptions">
                             <div class="clear"></div>
                             <div class="clear"></div>
                             <div class="clear"></div>
                             ';
                            $v = 'Uncategorized';
                            $delc = '';
                        }
                        
                        $pc = in_array($v,$cts) ? '<div class="pc_ind" title="Published by XML settings"><span class="codeicon">&lsaquo;&sol;&rsaquo;</span></div>' : '';
                        
                        
                        echo
                        '<div class="savedfile savedcat">
                            <span class="cat-name">'.$v.'</span>
                            <div class="post-options cat-options">
                                <a href="category.php?catid='.$k.'" class="options-button" title="Edit category">Edit category</a>
                                '.$delc.$chp.'
                            </div>'
                            .$cstyle.$pc.'</div>
                         ';
                        
                        $c++;
                        
                    }

                    if ($c == 0)
                        echo '<i id="no-results">:: No categories</i>';
                }
                
                else
                    echo '<i id="no-results">:: Error was found in categories file or categories xml is missing.</i>'
                ?>
            </div>
            </div>
        </div>
    </div>
</body>
</html>