<?php
include_once("../includes/ctrlacc.php");
include_once("../includes/CreaComando.php");
include_once("../clases/AdoPhp.php");

function del_cal_from_db ($id) {
    global $cadenaconexion;
    $cmd = CreaComando ($cadenaconexion);
    if (!$cmd) {
        die ($TbMsg['ACCESS_ERROR']);
    }
    $cmd->CreaParametro ('@id', $id, 0);
    $cmd->texto = 'DELETE FROM calendarios WHERE idcalendario=@id;';
    if (!$cmd->Ejecutar()) {
        return $cmd->DescripUltimoError();
    }
    return;
}

function fetch_cals() {
    global $cadenaconexion;
    $tbl = array();

    $cmd = CreaComando ($cadenaconexion);
    if (!$cmd) die ($TbMsg['ACCESS_ERROR']);

    $cmd->texto = 'SELECT idcalendario, description, json_text FROM calendarios';
    $rs = new Recordset;
    $rs->Comando = &$cmd;

    if (!$rs->Abrir()) return (false);

    $rs->Primero();
    if ($rs->EOF) {
        return ($tbl);
    }
    $id = $rs->campos['idcalendario'];
    $desc = $rs->campos['description'];
    $txt = $rs->campos['json_text'];
    array_push ($tbl, array (
        'id'        => $id,
        'desc'      => (empty ($desc) ? 'none' : $desc),
        'json_text' => (empty ($txt) ? 'none' : $txt),
    ));

    while (1) {
        $rs->Siguiente();
        if ($rs->EOF) {
            break;
        }
        $id = $rs->campos['idcalendario'];
        $desc = $rs->campos['description'];
        $txt = $rs->campos['json_text'];
        array_push ($tbl, array (
            'id'        => $id,
            'desc'      => (empty ($desc) ? 'none' : $desc),
            'json_text' => (empty ($txt) ? 'none' : $txt),
        ));
    }

    $rs->Cerrar();	
    return ($tbl);
}

function count_rules ($t1) {
    if (0 === sizeof ($t1)) {
        return $t1;
    }
    foreach (range (0, count ($t1)-1) as $idx) {
        $cal = json_decode ($t1[$idx]['json_text'], true);
        if ($cal) {
            $ruleset = $cal['remotepc_calendar']['ruleset'];
            $t1[$idx]['_count'] = count ($ruleset);
        } else {
            $t1[$idx]['_count'] = '(error decoding json)';
        }
    }
    return $t1;
}

function build_html_table_cal_list ($tbl) {
    $tbl_str = '<table border="1">';
    
    //$tbl_str .= '<tr><th>Calendario</th><th>Número de reglas</th></tr>';
    foreach ($tbl as $row) {
        $tbl_str .= sprintf ('<tr><td><a href="calendars-modify.php?cal_id=%s">%s</a></td><td>%s reglas</td><td><input type="submit" value="Borrar" onclick=\'document.forms[0].cal_to_del.value=%s;\' /></td></tr>', $row['id'], $row['desc'], $row['_count'], $row['id']);
    }

    $tbl_str .= '</table>';
    return ($tbl_str);
}

$db_err = '';
if (isset ($_POST['cal_to_del'])) {
    $db_err = del_cal_from_db ($_POST['cal_to_del']);
}
$tbl = fetch_cals();
$tbl = count_rules ($tbl);
$table_html = build_html_table_cal_list ($tbl);
?>

<html>
<head>
<title>Administración web de calendarios</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<link rel="shortcut icon" href="images/iconos/logocirculos.png" type="image/png" />
<link rel="stylesheet" type="text/css" href="estilos.css" />
</head>

<body>
    <?php if ($db_err) { printf ('<p>Error: (%s)</p>', $db_err); } ?>
    <form method="post">
        <input type="hidden" name="cal_id" value="-1" />
        <input type="hidden" name="cal_to_del" value="" />
        <p><?= $table_html ?></p>
        <input type="submit" value="Nuevo calendario" onclick='document.forms[0].action="./calendars-modify.php";' />
    </form>
</body>
</html>
