Location: PHPKode > projects > Readenglish > readenglish/dictionary_addset_con.php
<?php
include_once "lib/errores_lib.php";
include_once "lib/texto_palabras_lib.php";
include_once "basedatos_con.php";
include_once "english_readenglish_con.php";

function dictionary_addset_adaptset ()
{
    $ret = true;
    $inicio = 0;
    $separador = "";
    $palabra = "";
    $palabra_encontrada = "";
    $_SESSION ["array_palabras"] = array ();
    $tam = strlen ($_REQUEST["inputset"]);
    if (! isset ($_REQUEST["wordtype"])) {
        $ret = false;
        errores_mensaje ("You have to select the type of original. ");
    }
    if ($ret) {
        while (true) {
            if ($inicio >= $tam) {
                break;
            }
            if (! $ret) {
                break;
            }
            $separador = "";
            $palabra = "";
            $ret = texto_palabras ($_REQUEST["inputset"], $inicio, $separador, $palabra);
            if ($ret && $palabra != "") {
                $nueva_palabra = true;
                $array_adaptaciones = array ();
                if ($_REQUEST["wordtype"] == "noun_plural" || $_REQUEST["wordtype"] == "verb_he") {
                    $ret = dictionary_addset_noun_plural ($palabra, $array_adaptaciones);
                } else if ($_REQUEST["wordtype"] == "verb_past") {
                    $ret = dictionary_addset_verb_past ($palabra, $array_adaptaciones);
                } else if ($_REQUEST["wordtype"] == "verb_ing") {
                    $ret = dictionary_addset_verb_ing ($palabra, $array_adaptaciones);
                } else if ($_REQUEST["wordtype"] == "verb_able") {
                    $ret = dictionary_addset_verb_able ($palabra, $array_adaptaciones);
                } else if ($_REQUEST["wordtype"] == "verb_edly") {
                    $ret = dictionary_addset_verb_edly ($palabra, $array_adaptaciones);
                } else if ($_REQUEST["wordtype"] == "verb_er") {
                    $ret = dictionary_addset_verb_er ($palabra, $array_adaptaciones);
                } else if ($_REQUEST["wordtype"] == "un") {
                    $ret = dictionary_addset_un ($palabra, $array_adaptaciones);
                } else if ($_REQUEST["wordtype"] == "adverb") {
                    $ret = dictionary_addset_adverb ($palabra, $array_adaptaciones);
                    if (count($array_adaptaciones) == 0) {
                        $nueva_palabra = false;
                    }
                }
                if (ret && $nueva_palabra) {
                    $_SESSION ["array_palabras"][] = $array_adaptaciones;
                }
            }
        }
    }
    return $ret;
}

function dictionary_addset_noun_plural ($palabra, & $array_adaptaciones)
{
    $ret = true;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $palabra;
        if ($palabra[$tam-1] == 's'
        || $palabra[$tam-1] == 'x'
        || $palabra[$tam-1] == 'o'
        || $palabra[$tam-1] == 'z') {
            $array_adaptaciones ["adaptation"] = $palabra . "es";
        } else if ($tam > 1 && $palabra[$tam-1] == 'y'
        && $palabra[$tam-2] != 'a'
        && $palabra[$tam-2] != 'e'
        && $palabra[$tam-2] != 'i'
        && $palabra[$tam-2] != 'o'
        && $palabra[$tam-2] != 'u') {
            $array_adaptaciones ["adaptation"] = substr ($palabra, 0, $tam-1) . "ies";
        } else if ($tam > 1 && $palabra[$tam-1] == 'e'
        && $palabra[$tam-2] == 'f') {
            $array_adaptaciones ["adaptation"] = substr ($palabra, 0, $tam-2) . "ves";
        } else if ($tam > 1 && $palabra[$tam-1] == 'h'
        && $palabra[$tam-2] == 's') {
            $array_adaptaciones ["adaptation"] = $palabra. "es";
        } else if ($tam > 1 && $palabra[$tam-1] == 'h'
        && $palabra[$tam-2] == 'c') {
            $array_adaptaciones ["adaptation"] = $palabra. "es";
        } else {
            $array_adaptaciones ["adaptation"] = $palabra . "s";
        }
    }
    return $ret;
}

function dictionary_addset_verb_past ($palabra, & $array_adaptaciones)
{
    $ret = true;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $palabra;
        if ($palabra [$tam-1] != 'e') {
            $array_adaptaciones ["adaptation"] = $palabra . "ed";
        } else {
            $array_adaptaciones ["adaptation"] = $palabra . "d";
        }
    }
    return $ret;
}

function dictionary_addset_verb_edly ($palabra, & $array_adaptaciones)
{
    $ret = true;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $palabra;
        if ($palabra [$tam-1] == 'y') {
            $array_adaptaciones ["adaptation"] = substr($palabra,0,$tam-1) . "iedly";
        } else if ($palabra [$tam-1] != 'e') {
            $array_adaptaciones ["adaptation"] = $palabra . "edly";
        } else {
            $array_adaptaciones ["adaptation"] = $palabra . "dly";
        }
    }
    return $ret;
}

function dictionary_addset_verb_er ($palabra, & $array_adaptaciones)
{
    $ret = true;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $palabra;
        if ($palabra [$tam-1] == 'y') {
            $array_adaptaciones ["adaptation"] = substr($palabra,0,$tam-1) . "ier";
        } else if ($palabra [$tam-1] != 'e') {
            $array_adaptaciones ["adaptation"] = $palabra . "er";
        } else {
            $array_adaptaciones ["adaptation"] = $palabra . "r";
        }
    }
    return $ret;
}

function dictionary_addset_verb_ing ($palabra, & $array_adaptaciones)
{
    $ret = true;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $palabra;
        if ($palabra [$tam-1] == 'i') {
            $array_adaptaciones ["adaptation"] = $palabra . "ng";
        } else if ($palabra [$tam-1] == 'e') {
            $array_adaptaciones ["adaptation"] = substr($palabra, 0, $tam-1) . "ing";
        } else {
            $array_adaptaciones ["adaptation"] = $palabra . "ing";
        }
    }
    return $ret;
}

function dictionary_addset_verb_able ($palabra, & $array_adaptaciones)
{
    $ret = true;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $palabra;
        if ($palabra [$tam-1] == 'a') {
            $array_adaptaciones ["adaptation"] = $palabra . "ble";
        } else {
            $array_adaptaciones ["adaptation"] = $palabra . "able";
        }
    }
    return $ret;
}

function dictionary_addset_adverb ($palabra, & $array_adaptaciones)
{
    $ret = true;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 2 && ! ($palabra[$tam-2]=='l' && $palabra[$tam-1]=='y')) {
        $array_adaptaciones ["original"] = $palabra;
        if ($palabra[$tam-1]=='y'){
            $array_adaptaciones ["adaptation"] = substr($palabra,0,$tam-1) . "ily";
        } else {
            $array_adaptaciones ["adaptation"] = $palabra . "ly";
        }
    }
    return $ret;
}

function dictionary_addset_un ($palabra, & $array_adaptaciones)
{
    $ret = true;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $palabra;
        $array_adaptaciones ["adaptation"] = "un" . $palabra;
    }
    return $ret;
}

function dictionary_addset_noun_plural_vis ()
{
    $ret = true;
    if (isset ($_SESSION ["array_palabras"])) {
        $i = 0;
        foreach ($_SESSION ["array_palabras"] as $array_adaptaciones) {
            echo '        <tr>';
            echo '            <td><input name="up_'. $i .'" type="checkbox" id="up_'. $i .'" value="'.$array_adaptaciones["original"].'" checked></td>';
            echo '            <input name="new_'. $i .'" type="hidden" id="new_'. $i .'" value="'.$array_adaptaciones["adaptation"].'" >';
            echo '            <td>'.$array_adaptaciones["original"].'</td>';
            echo '            <td>'.$array_adaptaciones["adaptation"].'</td>';
            echo '        </tr>';
            $i ++;
        }
    }
    return $ret;
}

function dictionary_addset_newwords ()
{
    $ret = true;
    $_SESSION ["array_newwords"] = array ();
    $_SESSION ["array_newwords_notfound"] = array ();
    $ret = basedatos_abrir ();
    if ($ret) {
        foreach ($_REQUEST as $nombre_parametro => $palabra) {
            if (! $ret) {
                break;
            }
            if (strpos ($nombre_parametro,"up_") === 0) {
                $adaptacion = $_REQUEST ["new_". substr ($nombre_parametro,3)];
                $ret = english_readenglish_buscar ($palabra, $palabra_encontrada);
                if ($ret) {
                    if ($palabra_encontrada != "") {
                        $array_adaptaciones = array ();
                        if ($_REQUEST["hidden_wordtype"] == "noun_plural" || $_REQUEST["hidden_wordtype"] == "verb_he") {
                            $ret = dictionary_addset_noun_plural_new ($palabra, $adaptacion, $palabra_encontrada, $array_adaptaciones);
                        } else if ($_REQUEST["hidden_wordtype"] == "verb_past") {
                            $ret = dictionary_addset_verb_past_new ($palabra, $adaptacion, $palabra_encontrada, $array_adaptaciones);
                        } else if ($_REQUEST["hidden_wordtype"] == "verb_ing") {
                            $ret = dictionary_addset_verb_ing_new ($palabra, $adaptacion, $palabra_encontrada, $array_adaptaciones);
                        } else if ($_REQUEST["hidden_wordtype"] == "verb_able") {
                            $ret = dictionary_addset_verb_able_new ($palabra, $adaptacion, $palabra_encontrada, $array_adaptaciones);
                        } else if ($_REQUEST["hidden_wordtype"] == "verb_edly") {
                            $ret = dictionary_addset_verb_edly_new ($palabra, $adaptacion, $palabra_encontrada, $array_adaptaciones);
                        } else if ($_REQUEST["hidden_wordtype"] == "verb_er") {
                            $ret = dictionary_addset_verb_er_new ($palabra, $adaptacion, $palabra_encontrada, $array_adaptaciones);
                        } else if ($_REQUEST["hidden_wordtype"] == "un") {
                            $ret = dictionary_addset_un_new ($palabra, $adaptacion, $palabra_encontrada, $array_adaptaciones);
                        } else if ($_REQUEST["hidden_wordtype"] == "adverb") {
                            $ret = dictionary_addset_adverb_new ($palabra, $adaptacion, $palabra_encontrada, $array_adaptaciones);
                        }
                        if ($ret) {
                            $_SESSION ["array_newwords"][] = $array_adaptaciones;
                        }
                    }
                }
                if ($palabra_encontrada == "") {
                    $_SESSION ["array_newwords_notfound"][] = $palabra;
                }
            }
        }
        basedatos_cerrar ();
    }
    return $ret;
}

function dictionary_addset_noun_plural_new ($original, $adaptacion, $palabra_encontrada, & $array_adaptaciones)
{
    $ret = true;
    $es_e = false;
    $sufijo = "";
    $palabra = $palabra_encontrada;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        if ($palabra[$tam-1] == "'") {
            $palabra = substr($palabra,0, $tam-1);
            $sufijo = "'";
            $tam --;
        }
    }
    if ($tam > 1) {
        if ($palabra[$tam-2] == 's' && $palabra[$tam-1] == '_') {
            $palabra = substr($palabra,0, $tam-1);
            $es_e = true;
            $tam --;
        } else if ($palabra[$tam-2] == 'z' && $palabra[$tam-1] == '_') {
            $palabra = substr($palabra,0, $tam-1);
            $es_e = true;
            $tam --;
        }
    }
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $original;
        $array_adaptaciones ["adaptacion"] = $adaptacion;
        if ($palabra[$tam-1] == 's'
        || $palabra[$tam-1] == 'x'
        || $palabra[$tam-1] == 'o'
        || $palabra[$tam-1] == 'z') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "es" . $sufijo;
        } else if ($es_e) {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "es";
        } else if ($tam > 1 && $palabra[$tam-1] == 'y'
        && $palabra[$tam-2] != 'a'
        && $palabra[$tam-2] != 'e'
        && $palabra[$tam-2] != 'i'
        && $palabra[$tam-2] != 'o'
        && $palabra[$tam-2] != 'u') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = substr($palabra, 0, $tam-1) . "i_s";
        } else if ($tam > 1 && $palabra[$tam-1] == 'e'
        && $palabra[$tam-2] == 'f') {
            $array_adaptaciones ["adaptation"] = substr ($palabra, 0, $tam-2) . "v_s";
        } else if ($tam > 1 && $palabra[$tam-1] == 'h'
        && $palabra[$tam-2] == 's') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "es";
        } else if ($tam > 1 && $palabra[$tam-1] == 'h'
        && $palabra[$tam-2] == 'c') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "es";
        } else {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "s";
            $array_adaptaciones ["adaptation_palabra_encontrada"] .= $sufijo;
        }
    }
    return $ret;
}

function dictionary_addset_verb_past_new ($original, $adaptacion, $palabra_encontrada, & $array_adaptaciones)
{
    $ret = true;
    $palabra = $palabra_encontrada;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        if ($palabra[$tam-1] == "'") {
            $palabra = substr($palabra,0, $tam-1);
            $sufijo = "'";
            $tam --;
        }
    }
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $original;
        $array_adaptaciones ["adaptacion"] = $adaptacion;
        if ($palabra[$tam-1] == 'd' || $palabra[$tam-1] == 't') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . ":ed";
        } else if ($palabra[$tam-1] == '_' || $palabra[$tam-1] == 'e') {
            if ($tam>1 && ($palabra[$tam-2] == 'd' || $palabra[$tam-2] == 't')) {
                $array_adaptaciones ["adaptation_palabra_encontrada"] = substr($palabra,0,$tam-1) . ":ed";
            } else {
                $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "d" . $sufijo;
            }
        } else {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "_d" . $sufijo;
        }
    }
    return $ret;
}

function dictionary_addset_verb_edly_new ($original, $adaptacion, $palabra_encontrada, & $array_adaptaciones)
{
    $ret = true;
    $palabra = $palabra_encontrada;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        if ($palabra[$tam-1] == "'") {
            $palabra = substr($palabra,0, $tam-1);
            $sufijo = "'";
            $tam --;
        }
    }
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $original;
        $array_adaptaciones ["adaptacion"] = $adaptacion;
        if ($palabra[$tam-1] == 'd' || $palabra[$tam-1] == 't') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . ":edly";
        } else if ($palabra [$tam-1] == 'y') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = substr($palabra,0,$tam-1) . "i_dly";
        } else if ($palabra[$tam-1] == '_' || $palabra[$tam-1] == 'e') {
            if ($tam>1 && ($palabra[$tam-2] == 'd' || $palabra[$tam-2] == 't')) {
                $array_adaptaciones ["adaptation_palabra_encontrada"] = substr($palabra,0,$tam-1) . ":edly";
            } else {
                $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "dly" . $sufijo;
            }
        } else {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "_dly" . $sufijo;
        }
    }
    return $ret;
}

function dictionary_addset_verb_ing_new ($original, $adaptacion, $palabra_encontrada, & $array_adaptaciones)
{
    $ret = true;
    $palabra = $palabra_encontrada;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        if ($palabra[$tam-1] == "'") {
            $palabra = substr($palabra,0, $tam-1);
            $sufijo = "'";
            $tam --;
        }
    }
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $original;
        $array_adaptaciones ["adaptacion"] = $adaptacion;
        if ($palabra [$tam-1] == 'i') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "ng";
        } else if ($palabra [$tam-1] == 'e' || $palabra [$tam-1] == '_') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = substr($palabra, 0, $tam-1) . "ing";
        } else {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "ing";
        }
    }
    return $ret;
}

function dictionary_addset_verb_able_new ($original, $adaptacion, $palabra_encontrada, & $array_adaptaciones)
{
    $ret = true;
    $palabra = $palabra_encontrada;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        if ($palabra[$tam-1] == "'") {
            $palabra = substr($palabra,0, $tam-1);
            $sufijo = "'";
            $tam --;
        }
    }
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $original;
        $array_adaptaciones ["adaptacion"] = $adaptacion;
        if ($palabra [$tam-1] == 'a') {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = substr($palabra, 0, $tam-1) . ":abl_";
        } else {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . ":abl_";
        }
    }
    return $ret;
}

function dictionary_addset_verb_er_new ($original, $adaptacion, $palabra_encontrada, & $array_adaptaciones)
{
    $ret = true;
    $palabra = $palabra_encontrada;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    if ($tam > 0) {
        if ($palabra[$tam-1] == "'") {
            $palabra = substr($palabra,0, $tam-1);
            $sufijo = "'";
            $tam --;
        }
    }
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $original;
        $array_adaptaciones ["adaptacion"] = $adaptacion;
        if ($palabra[$tam-1]=='e'){
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "r";
        } else if ($palabra[$tam-1]=='_'){
            $array_adaptaciones ["adaptation_palabra_encontrada"] = substr($palabra, 0, $tam-1) . "er";
        } else if ($palabra[$tam-1]=='y'){
            $array_adaptaciones ["adaptation_palabra_encontrada"] = substr($palabra, 0, $tam-1) . "ier";
        } else {
            $array_adaptaciones ["adaptation_palabra_encontrada"] = $palabra . "er";
        }
    }
    return $ret;
}

function dictionary_addset_un_new ($original, $adaptacion, $palabra_encontrada, & $array_adaptaciones)
{
    $ret = true;
    $palabra = $palabra_encontrada;
    $tam = strlen ($palabra);
    $array_adaptaciones = array ();
    $prefijo = '';
    if ($tam > 0) {
        if ($palabra[0] == "'") {
            $palabra = substr($palabra,1, $tam-1);
            $prefijo .= "'" . "'";
            $tam --;
        }
    }
    if ($tam > 0) {
        if ($palabra[0] == "'") {
            $palabra = substr($palabra,1, $tam-1);
            $prefijo .= "'";
            $tam --;
        }
    }
    if ($tam > 0) {
        if ($palabra[0] == "'") {
            $palabra = substr($palabra,1, $tam-1);
            $prefijo .= "'";
            $tam --;
        }
    }
    if ($tam > 0) {
        $array_adaptaciones ["original"] = $original;
        $array_adaptaciones ["adaptacion"] = $adaptacion;
        $array_adaptaciones ["adaptation_palabra_encontrada"] = $prefijo . ".un" . $palabra;
    }
    return $ret;
}

function dictionary_addset_noun_plural_new_vis ()
{
    $ret = true;
    if (isset ($_SESSION ["array_newwords"])) {
        $i = 0;
        foreach ($_SESSION ["array_newwords"] as $array_adaptaciones) {
            echo '        <tr>';
            echo '            <td><input name="id_'. $i .'" type="checkbox" id="id_'. $i .'" value="'.$array_adaptaciones ["adaptacion"].'" checked></td>';
            echo '            <input name="idnew_'. $i .'" type="hidden" id="idnew_'. $i .'" value="'.htmlentities($array_adaptaciones ["adaptation_palabra_encontrada"]).'" >';
            echo '            <td>'.$array_adaptaciones["original"].'</td>';
            echo '            <td>'.$array_adaptaciones["adaptacion"].'</td>';
            echo '            <td>'.$array_adaptaciones["adaptation_palabra_encontrada"].'</td>';
            echo '        </tr>';
            $i++;
        }
        $cabecera = true;
        foreach ($_SESSION ["array_newwords_notfound"] as $notfound) {
            if ($cabecera) {
                echo '        <tr>';
                echo '            <td>&nbsp;</td>';
                echo '            <td class = "cabecera">Not found:</td>';
                echo '            <td>&nbsp;</td>';
                echo '            <td>&nbsp;</td>';
                echo '        </tr>';
                $cabecera = false;
            }
            echo '        <tr>';
            echo '            <td>&nbsp;</td>';
            echo '            <td class="error">'.$notfound.'</td>';
            echo '            <td>&nbsp;</td>';
            echo '            <td>&nbsp;</td>';
            echo '        </tr>';
            $i++;
        }

    }
    return $ret;
}

function dictionary_addset_insert ()
{
    $ret = true;
    $mensaje = "";
    $ret = basedatos_abrir ();
    if ($ret) {
        foreach ($_REQUEST as $nombre_parametro => $palabra) {
            if (strpos ($nombre_parametro,"id_") === 0) {
                $adaptacion = $_REQUEST ["idnew_". substr ($nombre_parametro,3)];
                $ret = english_addset_insertar ($palabra, $adaptacion);
                if (! $ret) {
                    $mensaje .= errores_leer ();
                    $mensaje .= "<br>";
                }
            }
        }
        basedatos_cerrar ();
        if ($mensaje != "") {
            errores_mensaje ($mensaje);
            $ret = false;
        }
    }
    return $ret;
}

function english_addset_insertar ($palabra, $adaptacion)
{
    $ret = true;
    $comando = "insert into englishes_readenglishes (ref_english, ref_readenglish) values"
    . " ('". mysql_real_escape_string (strtolower($palabra)) ."','"
    . mysql_real_escape_string ($adaptacion) . "')";
    $resultado = mysql_query($comando);
    if ($resultado === false) {
        errores_mensaje ("Error inserting: ".$palabra ." -> ". $adaptacion);
        errores_log(mysql_error ());
        $ret = false;
    }
    if ($ret) {
        if (mysql_affected_rows ()==0) {
            errores_mensaje ("Word not inserted: ". $palabra);
            $ret = false;
        }
    }
    return $ret;
}
?>
Return current item: Readenglish