<?php

require_once '../models/tvlist.php';
require_once '../models/tvform.php';
require_once '../models/userlist.php';
require_once '../models/userform.php';
require_once '../models/user.php';
require_once '../models/datalist.php';

$PAGE = 'editor';
$myusername = substr($_SERVER['eppn'], 0, strpos($_SERVER['eppn'], '@'));

$TV = new Tvlist(User::getAccesNum($myusername));
$TVLIST = $TV->getTvlist();
$myusername = substr($_SERVER['eppn'], 0, strpos($_SERVER['eppn'], '@'));


if(isset($_GET['tvitem'])){
    $old = $_GET['tvitem'];
    $TVFORM = new TvForm($_POST, $old);
    if(isset($TVFORM)){
        $TVFORM->validate();

        if($TVFORM->isSubmitted() && $TVFORM->isValid()){
            $TVFORM->edit();
        }
    }
}elseif(isset($_GET['usritem'])){
    $old = $_GET['usritem'];
    $UFORM = new UserForm($_POST, $old);
    if(isset($UFORM)){
        $UFORM->validate();

        if($UFORM->isSubmitted() && $UFORM->isValid()){
            $UFORM->edit();
        }
    }
}


include '../views/header.php'; // used: $PAGE $TVLIST

if(isset($_GET['tvitem'])){
    $EDITTV = new Tv($_GET['tvitem']);
    $ACTIONS = $EDITTV->getAllActId(substr($_SERVER['eppn'], 0, strpos($_SERVER['eppn'], '@')));
    include '../views/tv_editor.php'; // used: $EDITTV, $ACTIONS
}elseif(isset($_GET['usritem'])){
    $EDITUSER = User::find($_GET['usritem']);
    $GROUPS = $EDITUSER->getAllGroups(substr($_SERVER['eppn'], 0, strpos($_SERVER['eppn'], '@')));
    include '../views/user_editor.php'; //used: $EDITUSER, $GROUPS
}else{

    header('Location: index.php');
}
