<?php
include "classes/DatabaseConnection.php";

// return JSON string of the request and results for getting word_meaning
echo '{"request_type":"get_meaning"';

$error = FALSE;
$error_text = "";

if ($_GET["meaning_id"]) {
    $i_meaning_id = $_GET["meaning_id"];
    echo ',"parm_meaning_id":"' . $i_meaning_id . '"';
} else {
    $error = TRUE;
    $error_text = "Mandatory parameter meaning_id not supplied";
}

if ($error) {
    echo ',"error_found":"' . $error_text . '"}';
    exit;
}
// get db connection
$db_conn = DatabaseConnection::getConnection();
echo ',"db_status":"' . $db_conn->status . '"';

// read meaning together with from_word and to_word and part of speech
$query = "SELECT m.id as meaning_id, fw.id as from_word_id, fw.word as from_word, tw.id as to_word_id, tw.word as to_word, nvl(pos.part,'unspecified') as part, nvl(pos.part_abbr,'-') as part_abbr FROM word_meanings m LEFT JOIN words fw on m.from_word_id = fw.id LEFT JOIN words tw on m.to_word_id = tw.id LEFT JOIN parts_of_speech pos on m.part_of_speech_id = pos.id WHERE m.id = $i_meaning_id";

$result = $db_conn->connection->query($query);

if ($result->num_rows == 0) {
    // not found
    echo ',"error_found":"no word meaning found with id ' . $i_meaning_id . '"}';
    exit;
} else {
    $fields = $result->fetch_fields();
    $row = $result->fetch_row();
    foreach ($result as $row) {
        foreach($fields as $field) {
            echo ',"' . $field->name . '":"' . $row[$field->name] . '"';
        }
    }
    $result->close();
}


// close request object
echo '}';
?>