<?php

require_once 'appfunctions.php';
if (is_logged_in()) {
    show_header();
} else {
    redirect_to_login();
}
$remove_url = get_curr_dir() . "/remove.php?type=3&typecnt=";
?>


<?php 
if (isset($_REQUEST['typecnt'])) {
    $dbh = get_database_handler();
    $dbh->beginTransaction();
    $query = 'SELECT t.typecnt,c.typecnt,c.campocnt,c.nome FROM tipo_registo as t, campo as c
WHERE t.userid=c.userid AND c.userid=? AND c.ativo=1 AND t.ativo=1 AND t.typecnt=c.typecnt AND t.typecnt=?
GROUP BY c.nome;';
    $sth = $dbh->prepare($query);
    try {
        $sth->execute(array($_SESSION['userid'], $_REQUEST['typecnt']));
        echo "<h2>campos do registo {$_REQUEST['typecnt']}</h2>";
        echo "<table border=\"1\" cellspacing=\"5\">\n";
        echo "<tr>\n";
        echo "<th>campocnt</th>";
        echo "<th>nome</th>";
        echo "<th></th>";
        echo "</tr>\n";
        foreach ($sth as $row) {
            echo "<tr>\n";
<?php

require_once 'appfunctions.php';
if (is_logged_in()) {
    show_header();
    echo '<h1> Tipos de registo ativos </h1>';
} else {
    redirect_to_login();
}
$remove_url = get_curr_dir() . "/remove.php?type=2";
// expects id=...
$viewregtype_url = get_curr_dir() . "/viewregtype.php?typecnt=";
?>
<p><a href="newregtype.php">Criar tipo de registo</a>

<table border="1">
<tr> <th>Nome</th> <th></th>  <th></th>  </tr>
<?php 
$dbh = get_database_handler();
$query = "SELECT tipo_registo.userid as userid, tipo_registo.typecnt as typecnt, tipo_registo.nome as nome\nFROM utilizador, tipo_registo\nWHERE utilizador.userid = tipo_registo.userid AND utilizador.userid=? AND ativo = 1;";
$sth = $dbh->prepare($query);
try {
    $sth->execute(array($_SESSION['userid']));
    if ($sth->rowCount()) {
        $rows = $sth->fetchAll(PDO::FETCH_ASSOC);
        foreach ($rows as $row) {
            echo '<tr><td>' . $row['nome'] . ' </td><td> ' . generate_anchor('Abrir', $viewregtype_url . $row['typecnt']) . ' </td><td> ' . generate_anchor('Remover', $remove_url . '&id=' . $row['typecnt']) . '</td></tr>';
        }
    }
    //echo '<p> NOTA: remocao tambem funciona se uma pagina estiver inativa. <a href="' . $allpages_url . '">Clique aqui</a> para versao onde todas as paginas sao mostradas.</p>';
} catch (PDOException $e) {
/*
    Yes, this should be done inside pages.php, but we have less than 13hrs till deadline.
*/
require_once 'appfunctions.php';
if (is_logged_in()) {
    show_header();
    echo '<h1> Paginas Ativas E Inativas </h1>';
} else {
    redirect_to_login();
}
$remove_url = get_curr_dir() . "/remove.php?type=1";
$viewpage_url = get_curr_dir() . "/viewpage.php?pageid=";
$activepages_url = get_curr_dir() . '/pages.php';
?>
<p><a href="<?php 
echo get_curr_dir() . "/newpage.php";
?>
">New Page</a>
<table border="1">
<th> Nome </th>
<?php 
$dbh = get_database_handler();
$query = "SELECT utilizador.userid as userid, pagina.pagecounter as pagecounter, pagina.nome as nome\nFROM utilizador, pagina\nWHERE utilizador.userid = pagina.userid AND utilizador.userid=?;";
$sth = $dbh->prepare($query);
try {
    $sth->execute(array($_SESSION['userid']));
    if ($sth->rowCount()) {
        $rows = $sth->fetchAll(PDO::FETCH_ASSOC);
        foreach ($rows as $row) {
            echo '<tr><td>' . $row['nome'] . ' | ' . generate_anchor('Abrir', $viewpage_url . $row['pagecounter']) . ' | ' . generate_anchor('Remover', $remove_url . '&id=' . $row['pagecounter']) . '</td></tr>';
        }
<?php

define('TIMESTAMP_FORMAT', 'Y-m-d H:i:s');
$dashboard = get_curr_dir() . '/dashboard.php';
$logout = get_curr_dir() . '/logout.php';
$login = get_curr_dir() . '/login.php';
function is_logged_in()
{
    require_once 'startsession.php';
    return isset($_SESSION['userid']);
}
/* Get database handler */
function get_database_handler()
{
    require_once 'connectvars.php';
    return new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
function show_header()
{
    global $dashboard, $logout;
    echo '<p>Logged in as ' . $_SESSION['username'] . ' <a href=" ' . $dashboard . '"> Dashboard</a> | <a href = ' . $logout . '>Logout</a> <br/>';
    echo '<br/>';
}
function redirect_to_login()
{
    global $login;
    header('Location: ' . $login);
}
function log_login_attempt($userid, $success, $timestamp)
{
    /* exeption must be handled by caller */