<?php
/**
 * Mola más - anyadir2.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/>.
 */

include('funciones.php');
$db = conectaDb();
cabecera('Añadir términos 2', CABECERA_SIN_CURSOR);

$termino            = recogeParaConsulta($db, 'termino');
$terminoSinComillas = quitaComillasExteriores($termino);
$terminoOk          = FALSE;

if ($termino=="''") {
    print "<p style=\"color: red\">No ha escrito el término.</p>\n";
} elseif (!ctype_alpha($terminoSinComillas)) {
    print "<p style=\"color: red\">Los términos deben contener únicamente letras.</p>\n";
} else {
    $terminoOk = TRUE;
}

if ($terminoOk) {
    $consulta = "SELECT COUNT(*) FROM $dbTerminos";
    $result = $db->query($consulta);
    if (!$result) {
        print "<p>Error en la consulta.</p>\n";
    } elseif ($result->fetchColumn()>=MAX_REG_TERMINOS) {
        print "<p>Se ha alcanzado el número máximo de registros que se pueden "
            ."guardar.</p>\n<p>Por favor, borre algún registro antes.</p>\n";
    } else { 
        $consulta = "SELECT COUNT(*) FROM $dbTerminos 
                WHERE termino=$termino";
        $result = $db->query($consulta);
        if (!$result) {
            print "<p>Error en la consulta.</p>\n";
        } elseif ($result->fetchColumn()!=0) {
            print "<p>El registro ya existe.</p>\n";
        } else { 
            $consulta = "INSERT INTO $dbTerminos 
                VALUES (NULL, $termino, 1)";
            if ($db->query($consulta)) {
                print "<p>Registro creado correctamente.</p>\n";
            } else {
                print "<p>Error al crear el registro.<p>\n";
            }
        }
    }
}

$db = NULL; 
pie();
?>
