<?php
$t = 'Редактор меню';
require_once '../../wcore/core.php';
require_once '../../wcore/head.php';
iank(3);
$id = (isset($_GET['id']) && !empty($_GET['id'])?intval($_GET['id']):0);
if (isset($_GET['act']) && $_GET['act'] == 'create' && $id == 0){
    if (isset($_POST['ok'])){
        if ($_SESSION['csrf_token'] != antixs($_POST['csrf'])){}
        if (!isset($_POST['name']) || empty($_POST['name'])){echo msg_err('err',$lang['edm_t_e_name']);require_once '../../wcore/foot.php';exit();}
        if (!isset($_POST['icon']) || empty($_POST['icon'])){echo msg_err('err',$lang['edm_t_e_icon']);require_once '../../wcore/foot.php';exit();}
        if (!isset($_POST['link']) || empty($_POST['link'])){echo msg_err('err',$lang['edm_t_e_link']);require_once '../../wcore/foot.php';exit();}
        mysqli_query($mysqli,"INSERT INTO `wcore_mmenu`(`name`, `icon`, `link`, `count_table`) VALUES ('".$_POST['name']."','".$_POST['icon']."','".$_POST['link']."','".$_POST['count']."')");
        echo msg_err('suc',$lang['act_suc']);
    } else {
        echo $twig->render('admin_editor_menu.tpl', array('lang' =>$lang,'act'=>'create','csrf'=>_csrf()));
    }
} elseif (isset($_GET['act']) && $_GET['act'] == 'delete' && $id != 0){
    $elid = mysqli_query($mysqli,"SELECT * FROM `wcore_mmenu` WHERE `id` = ".intval($_GET['id']));
    if (mysqli_num_rows($elid) != 0) {$page = mysqli_fetch_object($elid);} else {echo msg_err('err',$lang['edm_t_none_page']);require_once '../../wcore/foot.php';exit();}
    if (isset($_POST['ok'])){
        if ($_SESSION['csrf_token'] != antixs($_POST['csrf'])){echo msg_err('err',$lang['csrf']);require_once '../../wcore/foot.php';exit();}
        mysqli_query($mysqli,"DELETE FROM `wcore_mmenu` WHERE `id` = '$id'");
        echo msg_err('suc',$lang['act_suc']);
    } else {
        echo $twig->render('admin_editor_menu.tpl', array('lang' =>$lang,'act'=>'delete','csrf'=>_csrf(),'page'=>$page));
    }
} elseif (isset($_GET['act']) && $_GET['act'] == 'edit' && $id != 0){
    $elid = mysqli_query($mysqli,"SELECT * FROM `wcore_mmenu` WHERE `id` = ".intval($_GET['id']));
    if (mysqli_num_rows($elid) != 0) {$page = mysqli_fetch_object($elid);} else {echo msg_err('err',$lang['edm_t_none_page']);require_once '../../wcore/foot.php';exit();}
    if (isset($_POST['ok'])){
        if ($_SESSION['csrf_token'] != antixs($_POST['csrf'])){}
        if (!isset($_POST['name']) || empty($_POST['name'])){echo msg_err('err',$lang['edm_t_e_name']);require_once '../../wcore/foot.php';exit();}
        if (!isset($_POST['icon']) || empty($_POST['icon'])){echo msg_err('err',$lang['edm_t_e_icon']);require_once '../../wcore/foot.php';exit();}
        if (!isset($_POST['link']) || empty($_POST['link'])){echo msg_err('err',$lang['edm_t_e_link']);require_once '../../wcore/foot.php';exit();}
        mysqli_query($mysqli,"UPDATE `wcore_mmenu` SET `name`='".$_POST['name']."',`icon`='".$_POST['icon']."',`link`='".$_POST['link']."',`count_table`='".$_POST['count']."' WHERE `id`='$id'");
        echo msg_err('suc',$lang['act_suc']);
    } else {
        echo $twig->render('admin_editor_menu.tpl', array('lang' =>$lang,'act'=>'edit','csrf'=>_csrf(),'page'=>$page));
    }
} else {
    echo $twig->render('admin_editor_menu.tpl', array('lang' =>$lang,'act'=>'home','menu'=>wcore_mmenu()));
}

require_once '../../wcore/foot.php';