<?php 
/**
 * Tablero - tablero.php
 * 
 * @author    Bartolomé Sintes Marco <bartolome.sintes+mclibre@gmail.com>
 * @copyright 2010 Bartolomé Sintes Marco
 * @license   http://www.gnu.org/licenses/agpl.txt AGPL 3 or later
 * @version   2010-06-18
 * @link      http://www.mclibre.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

print "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">\n"; 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Tablero. Exámenes. PHP. Bartolomé Sintes Marco</title>
  <link href="mclibre_php_soluciones.css" rel="stylesheet" type="text/css" 
  title="Color" />
  <style type="text/css">
    td { width: 60px; height: 60px; border: black 1px solid; }
    th { text-align: center; border: none; font-size: 200%;  }
    th a { text-decoration: none; color: black; } 
  </style>
</head>
<body>

<h1>tablero</h1>
<?php
function recoge($var)
{ 
    $tmp = (isset($_REQUEST[$var])) ? trim(strip_tags($_REQUEST[$var])) : '';
    if (get_magic_quotes_gpc()) {
        $tmp = stripslashes($tmp);
    }
    $tmp = str_replace('&', '&amp;',  $tmp);
    $tmp = str_replace('"', '&quot;', $tmp);
    return $tmp;
}

$tamanyoInicial = 2;
$tamanyoX      = recoge('tamanyoX');
$tamanyoY      = recoge('tamanyoY');
$minimoX        = 1;
$maximoX        = 8;
$minimoY        = 1;
$maximoY        = 8;
$tamanyoXOk = $tamanyoYOk = FALSE;

if (($tamanyoX=="") && ($tamanyoY=="")) {
    $tamanyoX = $tamanyoY = $tamanyoInicial;
    $tamanyoXOk = $tamanyoYOk = TRUE;
}

if ($tamanyoX=="") {
    print "<p style=\"color: red\">No se ha recibido el ancho de la tabla.</p>\n";
} elseif (!is_int($tamanyoX) && !ctype_digit($tamanyoX)) { 
    print "<p style=\"color: red\">No se ha recibido el ancho de la tabla "
        ."como número entero positivo.</p>\n";
} else {
    $tamanyoXOk = TRUE;
}

if ($tamanyoY=="") {
    print "<p style=\"color: red\">No se ha recibido el alto de la tabla.</p>\n";
} elseif (!is_int($tamanyoY) && !ctype_digit($tamanyoY)) { 
    print "<p style=\"color: red\">No se ha recibido el alto de la tabla "
        ."como número entero positivo.</p>\n";
} else {
    $tamanyoYOk = TRUE;
}

if ($tamanyoX<$minimoX) {
   $tamanyoX = $minimoX;
} elseif ($tamanyoX>$maximoX) {
   $tamanyoX = $maximoX;
}

if ($tamanyoY<$minimoY) {
   $tamanyoY = $minimoY;
} elseif ($tamanyoY>$maximoY) {
   $tamanyoY = $maximoY;
}

if ($tamanyoXOk && $tamanyoYOk) {
    print "<p>Haga clic en + o - para ampliar o reducir la tabla.</p>\n";
    print "  <table border=\"0\">\n    <tbody>\n";
    print "      <tr>\n";
    for ($j=1; $j<=$tamanyoX; $j++) {
        print "        <td></td>\n";
    }
    print "        <th rowspan=\"$tamanyoY\">
        <a href=\"".$_SERVER['PHP_SELF']."?tamanyoX=".($tamanyoX+1)."&amp;tamanyoY=$tamanyoY\">+</a>
        <a href=\"".$_SERVER['PHP_SELF']."?tamanyoX=".($tamanyoX-1)."&amp;tamanyoY=$tamanyoY\">-</a>
        </th>\n      </tr>\n";
    for ($i=2; $i<=$tamanyoY; $i++) {
        print "      <tr>\n";
        for ($j=1; $j<=$tamanyoX; $j++) {
            print "        <td></td>\n";
        }
        print "      </tr>\n";
    }
    print "    <tr>\n      <th colspan=\"$tamanyoX\">
        <a href=\"".$_SERVER['PHP_SELF']."?tamanyoX=$tamanyoX&amp;tamanyoY=".($tamanyoY+1)."\">+</a>
        <a href=\"".$_SERVER['PHP_SELF']."?tamanyoX=$tamanyoX&amp;tamanyoY=".($tamanyoY-1)."\">-</a>
        </th>\n      <th></th>\n      </tr>\n";
    print "    </tbody>\n  </table>\n";
}

?>

<address>
  Esta página forma parte del curso "Páginas web con PHP" disponible en <a
  href="http://www.mclibre.org/">http://www.mclibre.org</a><br />
  Autor: Bartolomé Sintes Marco<br />
  Última modificación de esta página: 18 de junio de 2010 
</address>
<p class="licencia">El programa PHP que genera esta página está bajo 
<a rel="license" href="http://www.gnu.org/licenses/agpl.txt">licencia AGPL 3 o
posterior</a>.</p>
</body>
</html>