Location: PHPKode > projects > Readenglish > readenglish/english_readenglish_words_con.php
<?php
include_once ("basedatos_con.php");
//include_once ("english_readenglish_con.php");

function english_readenglish_words_listado ()
{
    $ret = true;
    $num_vocales = 0;
    if (isset($_SESSION ["array_no_encontrados"])) {
        foreach ($_SESSION ["array_no_encontrados"] as $palabra) {
            $ret = english_readenglish_words_linea_listado ($palabra);
            if (! $ret) {
                break;
            }
        }
    }
    return $ret;
}

function english_readenglish_words_linea_listado ($palabra)
{
    $ret = true;
    $palabra = html_entity_decode ($palabra);
    $ret = english_readenglish_words_cambiar ($palabra, $palabra_cambiada);
    if (! $ret) {
        break;
    }
    $ret = english_readenglish_words_contar_vocales ($palabra_cambiada, $num_vocales);
    if (! $ret) {
        break;
    }
    echo '<tr>';
    echo '<td><input type="checkbox" name="id_'.$palabra.'" value="'. $palabra . '" checked></td>';
    echo '<td>'. $palabra . '</td>';
    echo '<td><div align="right">';
    if ($num_vocales >= 3) {
        echo '    <label>';
        echo '    <input name="accent_'.$palabra.'"" type="radio" value="1">';
        echo '    1<span class="subindice">st</span></label>';
    }
    if ($num_vocales >= 4) {
        echo '    <label>';
        echo '    <input name="accent_'.$palabra.'"" type="radio" value="2">';
        echo '    2<span class="subindice">nd</span></label>';
    }
    if ($num_vocales >= 5) {
        echo '    <label>';
        echo '    <input name="accent_'.$palabra.'"" type="radio" value="3">';
        echo '    3<span class="subindice">th</span></label>';
    }
    if ($num_vocales >= 3) {
        echo "'";
    }
    echo '  </div></td>';
    echo '<td>';
    echo '<input name="name_'.$palabra.'" type="text" class="texto_palabra" id="id_'.$palabra.'"';
    echo ' size="35" maxlength="35" value="'. htmlentities ($palabra_cambiada) .'">';
    echo '</td>';
    echo '<td><div align="left">';
    if ($num_vocales >= 2) {
        echo '    \'<label>';
        echo '    <input name="accent_'.$palabra.'"" type="radio" value="0">';
        echo '    None</label>';
        echo '    <label>';
        echo '    <input name="accent_'.$palabra.'"" type="radio" value="-1">';
        echo '    last</label>';
    }
    if ($num_vocales <= 1) {
        echo '    <input name="accent_'.$palabra.'"" type="hidden" value="0">';
    }
    echo '  </div></td>';
    echo '</tr>';
    return $ret;
}

function english_readenglish_words_nueva_modificar ($palabra_cambiada)
{
    $ret = true;
    $num_vocales = 0;
    $palabra = "";
    if (isset($_REQUEST["palabra"])) {
        $palabra = $_REQUEST["palabra"];
    }
    if ($ret) {
        if ($palabra_cambiada == "") {
            $opcion = "insert";
            $ret = english_readenglish_words_cambiar ($palabra, $palabra_cambiada);
        } else {
            $opcion = "update";
            $palabra_cambiada = htmlentities ($palabra_cambiada);
        }
    }
    if ($ret) {
        $ret = english_readenglish_words_contar_vocales ($palabra_cambiada, $num_vocales);
    }
    if ($ret) {
        echo '<tr>';
        echo '<td>'. $palabra . '</td>';
        echo '<td><div align="right">';
        if ($num_vocales >= 3) {
            echo '    <label>';
            echo '    <input name="accent_'.$palabra.'" type="radio" value="1">';
            echo '    1<span class="subindice">st</span></label>';
        }
        if ($num_vocales >= 4) {
            echo '    <label>';
            echo '    <input name="accent_'.$palabra.'" type="radio" value="2">';
            echo '    2<span class="subindice">nd</span></label>';
        }
        if ($num_vocales >= 5) {
            echo '    <label>';
            echo '    <input name="accent_'.$palabra.'" type="radio" value="3">';
            echo '    3<span class="subindice">th</span></label>';
        }
        if ($num_vocales >= 3) {
            echo "'";
        }
        echo '  </div></td>';
        echo '<td>';
        echo '<input name="palabra" type="hidden" value="'.$palabra.'">';
        echo '<input name="palabra_encontrada" type="hidden" value="'.$palabra_cambiada.'">';
        echo '<input name="opcion" type="hidden" value="'.$opcion.'">';
        echo '<input name="name_'.$palabra.'"  class="texto_palabra" type="text" id="name_'.$palabra.'"';
        echo ' size="35" maxlength="35" value="'. str_replace ("'", "", $palabra_cambiada) .'">';
        echo '</td>';
        echo '<td><div align="left">';
        if ($num_vocales >= 2) {
            echo '    \'<label>';
            echo '    <input name="accent_'.$palabra.'" type="radio" value="0">';
            echo '    None</label>';
            echo '    <label>';
            echo '    <input name="accent_'.$palabra.'" type="radio" value="-1">';
            echo '    last</label>';
        }
        if ($num_vocales <= 1) {
            echo '    <input name="accent_'.$palabra.'" type="hidden" value="0">';
        }
        echo '  </div></td>';
        echo '</tr>';
    }
    return $ret;
}

function english_readenglish_words_cambiar ($palabra, & $palabra_cambiada)
{
    $ret = true;
    $palabra_cambiada = "";
    $i = 0;
    $palabra = strtolower($palabra);
    $tam = strlen ($palabra);
    if (strpos($palabra,"pre")===0) {
        $palabra_cambiada = "prE";
        $i = $i + 3;
    } else if (strpos($palabra,"be")===0) {
        $palabra_cambiada = "bE";
        $i = $i + 2;
    } else if (strpos($palabra,"bi")===0) {
        $palabra_cambiada = "bI";
        $i = $i + 2;
    } else if (strpos($palabra,"bio")===0) {
        $palabra_cambiada = "bIo";
        $i = $i + 3;
    } else if (strpos($palabra,"psy")===0) {
        $palabra_cambiada = "psY";
        $i = $i + 3;
    } else if (strpos($palabra,"eco")===0) {
        $palabra_cambiada = "Eco";
        $i = $i + 3;
    } else if (strpos($palabra,"fore")===0) {
        $palabra_cambiada = "for&middot;";
        $i = $i + 4;
    } else if (strpos($palabra,"micro")===0) {
        $palabra_cambiada = "mIcro";
        $i = $i + 5;
    } else if (strpos($palabra,"out")===0) {
        $palabra_cambiada = ":out";
        $i = $i + 3;
    } else if (strpos($palabra,"iser")===0) {
        $palabra_cambiada = "Iser";
        $i = $i + 4;
    } else if (strpos($palabra,"sub")===0) {
        $palabra_cambiada = "s:ub";
        $i = $i + 3;
    } else if (strpos($palabra,"un")===0) {
        $palabra_cambiada = ":un";
        $i = $i + 2;
    } else if (strpos($palabra,"re")===0) {
        $palabra_cambiada = "rhE";
        $i = $i + 2;
    } else if (strpos($palabra,"de")===0) {
        $palabra_cambiada = "dE";
        $i = $i + 2;
    }
    while (true) {
        if ($i >= $tam) {
            break;
        }
        if ($i + 1 < $tam && $palabra [$i] == 't' && $palabra [$i+1] == 'h') {
            $palabra_cambiada .= "thz";
            $i = $i + 2;
        } else if ($i + 2 < $tam && $palabra [$i] == 't' && $palabra [$i+1] == 'i' && $palabra [$i+2] == 'o') {
            $palabra_cambiada .= "tsio";
            $i = $i + 3;
        } else if ($i + 2 < $tam && $palabra [$i] == 't' && $palabra [$i+1] == 'i' && $palabra [$i+2] == 'a') {
            $palabra_cambiada .= "tsia";
            $i = $i + 3;
        } else if ($i + 2 < $tam && $palabra [$i] == 't' && $palabra [$i+1] == 'i' && $palabra [$i+2] == 'e') {
            $palabra_cambiada .= "tsie";
            $i = $i + 3;
        } else if ($i + 1 < $tam && $palabra [$i] == 'c' && $palabra [$i+1] == 'y') {
            $palabra_cambiada .= "csy";
            $i = $i + 2;
        } else if ($i + 1 < $tam && $palabra [$i] == 'c' && $palabra [$i+1] == 'i') {
            $palabra_cambiada .= "csi";
            $i = $i + 2;
        } else if ($i + 1 < $tam && $palabra [$i] == 'c' && $palabra [$i+1] == 'e') {
            if ($i + 2 == $tam){
                $palabra_cambiada .= "cs&middot;";
            } else {
                $palabra_cambiada .= "cse";
            }
            $i = $i + 2;
        } else if ($i + 1 < $tam && $palabra [$i] == 'g' && $palabra [$i+1] == 'n') {
            $palabra_cambiada .= "gjn";
            $i = $i + 2;
        } else if ($i + 2 < $tam && $palabra [$i] == 'c' && $palabra [$i+1] == 'h' && $palabra [$i+2] == 'e') {
            if ($i + 3 == $tam){
                $palabra_cambiada .= "cch&middot;";
            } else {
                $palabra_cambiada .= "cche";
            }
            $i = $i + 3;
        } else if ($i + 2 < $tam && $palabra [$i] == 'c' && $palabra [$i+1] == 'c' && $palabra [$i+2] == 'e') {
            if ($i + 3 == $tam){
                $palabra_cambiada .= "ccs&middot;";
            } else {
                $palabra_cambiada .= "ccse";
            }
            $i = $i + 3;
        } else if ($i == 0 && $palabra [$i] == 'r') {
            $palabra_cambiada .= "rh";
            $i = $i + 1;
        } else if ($i + 3 == $tam && $palabra [$i] == 't' && $palabra [$i+1] == 'e' && $palabra [$i+2] == 'd') {
            $palabra_cambiada .= "tEd";
            $i = $i + 3;
        } else if ($i + 3 == $tam && $palabra [$i] == 'd' && $palabra [$i+1] == 'e' && $palabra [$i+2] == 'd') {
            $palabra_cambiada .= "dEd";
            $i = $i + 3;
        } else if ($i + 2 == $tam && $palabra [$i] == 'e' && $palabra [$i+1] == 'd') {
            if (($i>=1 && $palabra [$i-1] != 't') || $i == 0) {
                $palabra_cambiada .= "&middot;d";
                $i = $i + 2;
            }
        } else if ($i + 1 < $tam && $palabra [$i] == 'g' && $palabra [$i+1] == 'n') {
            $palabra_cambiada .= "gjn";
            $i = $i + 2;
        } else if ($i + 2 < $tam && $palabra [$i] == 'g' && $palabra [$i+1] == 'e' && $palabra [$i+2] == 't') {
            $palabra_cambiada .= "ghet";
            $i = $i + 3;
        } else if ($i + 2 < $tam && $palabra [$i] == 'g' && $palabra [$i+1] == 'i' && $palabra [$i+2] == 'v') {
            $palabra_cambiada .= "ghiv";
            $i = $i + 3;
        } else if ($i + 1 < $tam && $palabra [$i] == 'g' && $palabra [$i+1] == 'e') {
            if ($i + 2 == $tam){
                $palabra_cambiada .= "gj&middot;";
            } else {
                $palabra_cambiada .= "gje";
            }
            $i = $i + 2;
        } else if ($i > 2 && $i + 1 < $tam && $palabra [$i] == 'a' && $palabra [$i+1] == 'i') {
            $palabra_cambiada .= "Ai";
            $i = $i + 2;
        } else if ($i + 1 < $tam && $palabra [$i] == 'g' && $palabra [$i+1] == 'i') {
            $palabra_cambiada .= "gji";
            $i = $i + 2;
        } else if ($i + 1 < $tam && $palabra [$i] == 'g' && $palabra [$i+1] == 'y') {
            $palabra_cambiada .= "gjy";
            $i = $i + 2;
        } else if ($i + 1 < $tam && $palabra [$i] == 'g'
          && $palabra [$i+1] != 'h'
          && $palabra [$i+1] != 'l'
          && $palabra [$i+1] != 'r'
          && $palabra [$i+1] != 'a'
          && $palabra [$i+1] != 'o'
          && $palabra [$i+1] != 'u') {
            $palabra_cambiada .= "gh";
            $i++;
        } else if ($i + 1 == $tam && $palabra [$i] == 'g') {
            $palabra_cambiada .= "gh";
            $i++;
        } else if ($i + 1 == $tam && $palabra [$i] == 'e') {
            $palabra_cambiada .= "&middot;";
            $i++;
        } else if ($i == $tam-3 && strrpos($palabra,"age")===$tam-3) {
            $palabra_cambiada .= "Agj&middot;";
            $i = $i + 3;
        } else if ($i == $tam-4 && strrpos($palabra,"able")===$tam-4) {
            $palabra_cambiada .= "Abl&middot;";
            $i = $i + 4;
        } else if ($i == $tam-5 && strrpos($palabra,"ation")===$tam-5) {
            $palabra_cambiada .= "Atsion";
            $i = $i + 5;
        } else if ($i == $tam-3 && strrpos($palabra,"ate")===$tam-3) {
            $palabra_cambiada .= "At&middot;";
            $i = $i + 3;
        } else if ($i == $tam-3 && strrpos($palabra,"ize")===$tam-3) {
            $palabra_cambiada .= "Iz&middot;";
            $i = $i + 3;
        } else if ($i == $tam-3 && strrpos($palabra,"gue")===$tam-3) {
            $palabra_cambiada .= "gu&middot;";
            $i = $i + 3;
        } else if ($i == $tam-3 && strrpos($palabra,"que")===$tam-3) {
            $palabra_cambiada .= "qu&middot;";
            $i = $i + 3;
        } else if ($i == $tam-3 && strrpos($palabra,"ise")===$tam-3) {
            $palabra_cambiada .= "Is&middot;";
            $i = $i + 3;
        } else if ($i == $tam-4 && strrpos($palabra,"cide")===$tam-4) {
            $palabra_cambiada .= "cId&middot;";
            $i = $i + 4;
        } else if ($i == $tam-5 && strrpos($palabra,"cycle")===$tam-5) {
            $palabra_cambiada .= "cYcl&middot;";
            $i = $i + 5;
        } else if ($i == $tam-5 && strrpos($palabra,"phone")===$tam-5) {
            $palabra_cambiada .= "phon&middot;";
            $i = $i + 5;
        } else if ($i == $tam-4 && strrpos($palabra,"wise")===$tam-4) {
            $palabra_cambiada .= "wIs&middot;";
            $i = $i + 4;
        } else if ($i == $tam-4 && strrpos($palabra,"eful")===$tam-4) {
            $palabra_cambiada .= "&middot;ful";
            $i = $i + 4;
        } else if ($i == $tam-5 && strrpos($palabra,"ement")===$tam-5) {
            $palabra_cambiada .= "&middot;ment";
            $i = $i + 5;
        } else if ($i == $tam-4 && strrpos($palabra,"ciou")===$tam-4) {
            $palabra_cambiada .= "csiou";
            $i = $i + 4;
        } else if ($i == $tam-3 && strrpos($palabra,"ely")===$tam-3) {
            $palabra_cambiada .= "&middot;ly";
            $i = $i + 3;
        } else if ($i == $tam-4 && strrpos($palabra,"ness")===$tam-4) {
            $palabra_cambiada .= "nEss";
            $i = $i + 4;
        } else if ($i == $tam-4 && strrpos($palabra,"ator")===$tam-4) {
            $palabra_cambiada .= "Ator";
            $i = $i + 4;
        } else if ($i == $tam-3 && strrpos($palabra,"ute")===$tam-3) {
            $palabra_cambiada .= "Ut&middot;";
            $i = $i + 3;
        } else {
            $palabra_cambiada .= $palabra [$i];
            $i++;
        }
    }
    return $ret;
}

function english_readenglish_words_contar_vocales ($palabra, & $num_vocales)
{
    $ret = true;
    $num_vocales = 0;
    $ultima_vocal = -2;
    $i = 0;
    $palabra= html_entity_decode($palabra);
    $tam = strlen ($palabra);
    while (true){
        if ($i >= $tam) {
            break;
        }
        if ($palabra[$i] == 'a' || $palabra[$i] == 'e'
          || $palabra[$i] == 'i' || $palabra[$i] == 'o'
          || $palabra[$i] == 'u' || $palabra[$i] == 'A'
          || $palabra[$i] == 'E' || $palabra[$i] == 'I'
          || $palabra[$i] == 'O' || $palabra[$i] == 'U' ) {
            if ($ultima_vocal + 1 != $i) {
                $ultima_vocal = $i;
                $num_vocales ++;
            }
        }
        $i ++;
    }
    return $ret;
}

function english_readenglish_words_insertar ()
{
    $ret = true;
    $mensajes_error = false;
    $_SESSION["array_no_encontrados"] = array ();
    $ret = basedatos_abrir ();
    if ($ret) {
        foreach ($_REQUEST as $parametro => $palabra) {
            if (strpos ($parametro, "id_") === 0) {
                if (isset ($_REQUEST ["name_".$palabra])) {
                    $palabra_cambiada = html_entity_decode($_REQUEST ["name_".$palabra]);
                    $ret = english_readenglish_words_cambiar_palabra ($palabra, $palabra_cambiada);
                    if ($ret) {
                        $ret = english_readenglish_words_insertar_palabra ($palabra, $palabra_cambiada);
                        if (! $ret) {
                            break;
                        }
                    } else {
                        $mensajes_error = true;
                        if (isset ($_SESSION["array_no_encontrados"])){
                            $_SESSION["array_no_encontrados"][] = $palabra;
                        }
                    }
                } else {
                    errores_mensaje_mas ("There is no change for: ".$palabra);
                    $ret = false;
                }
            }
        }
        basedatos_cerrar ();
    }
    return ($ret && ! $mensajes_error);
}

function english_readenglish_words_cambiar_palabra ($palabra, & $palabra_cambiada)
{
    $ret = true;
    if (isset ($_REQUEST["accent_".$palabra])) {
        $num_acento = $_REQUEST["accent_".$palabra];
        if ($num_acento == 1) {
            $palabra_cambiada = "'".$palabra_cambiada;
        } else if ($num_acento == 2) {
            $palabra_cambiada = "''".$palabra_cambiada;
        } else if ($num_acento == 3) {
            $palabra_cambiada = "'''".$palabra_cambiada;
        } else if ($num_acento == -1) {
            $palabra_cambiada = $palabra_cambiada."'";
        }
    } else {
        $ret = false;
        errores_mensaje_mas ("No accent type indicated in : ". $palabra . " -> ". htmlentities($palabra_cambiada) . " ");
    }
    if ($ret) {
        $ret = english_readenglish_words_comparar_palabra ($palabra, $palabra_cambiada);
    }
    return $ret;
}

function english_readenglish_words_comparar_palabra ($palabra, $palabra_cambiada)
{
    $ret = true;
    $i = 0;
    $j = 0;
    $palabra = strtolower ($palabra);
    $palabra_cambiada_original = $palabra_cambiada;
    $palabra_cambiada = strtolower ($palabra_cambiada_original);
    $tam_palabra = strlen ($palabra);
    $tam_palabra_cambiada = strlen ($palabra_cambiada);
    while (true) {
        if (! $ret) {
            break;
        }
        if ($i >= $tam_palabra) {
            break;
        }
        if ($j >= $tam_palabra_cambiada) {
            $ret = false;
            errores_mensaje ("Incorrect character missed or added changing the word: " . $palabra . " -> " . $palabra_cambiada);
            break;
        }
        if ($palabra[$i]!= $palabra_cambiada[$j]
         && !($palabra_cambiada_original[$j] == 'ยท'
           &&  $palabra[$i] == 'e')) {
            if ($palabra[$i]!= $palabra_cambiada[$j]
             && ($palabra_cambiada[$j] == 'a'
              || $palabra_cambiada[$j] == 'e'
              || $palabra_cambiada[$j] == 'i'
              || $palabra_cambiada[$j] == 'o'
              || $palabra_cambiada[$j] == 'u')) {
                $j ++;
            } else if (($palabra_cambiada[$j] == '\'' && $j == 0)
              || ($palabra_cambiada[0] == '\'' && $palabra_cambiada[$j] == '\'' && $j == 1)
              || ($palabra_cambiada[0] == '\'' && $palabra_cambiada[1] == '\'' && $palabra_cambiada[$j] == '\'' && $j == 2)
              || ($palabra_cambiada[0] == '\'' && $palabra_cambiada[1] == '\'' && $palabra_cambiada[2] == '\'' && $palabra_cambiada[$j] == '\'' && $j == 3)
              || ($palabra_cambiada[$j] == '\'' && $j == $tam_palabra_cambiada-1)) {
                $j ++;
            } else if ($palabra_cambiada[$j] == '/' && $j == 0) {
                $j ++;
            } else {
                $num = 0;
                while (true) {
                    if ($j >= $tam_palabra_cambiada) {
                        break;
                    }
                    if ($palabra_cambiada[$j] == ':'
                    || $palabra_cambiada[$j] == '.') {
                        $j ++;
                    } else if ($palabra[$i]!= $palabra_cambiada[$j]
                     && ($palabra_cambiada[$j] == 'h'
                      || $palabra_cambiada[$j] == 'c'
                      || $palabra_cambiada[$j] == 'j'
                      || $palabra_cambiada[$j] == 's'
                      || $palabra_cambiada[$j] == 'z')) {
                        $j ++;
                    } else {
                        if ($num == 0) {
                            $ret = false;
                            errores_mensaje ("Incorrect character missed or added changing the word: " . $palabra . " -> " . htmlentities($palabra_cambiada_original) . " (". htmlentities($palabra_cambiada_original[$j]) .")");
                        }
                        break;
                    }
                    $num++;
                    if ($num > 3) {
                        $ret = false;
                        errores_mensaje ("Incorrect character missed or added changing the word: " . $palabra . " -> " . htmlentities($palabra_cambiada_original) . " (". htmlentities($palabra_cambiada_original[$j]) .")");
                        break;
                    }
                }
            }
        } else {
            $i ++;
            $j ++;
        }
    }
    return $ret;
}

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