<?php
include "classes/DatabaseConnection.php";
include "classes/Word.php";
include "classes/Word_Meaning.php";
// return JSON string of the request and results for adding word_meaning
echo '{"request_type":"add_meaning"';

if ($_GET["debug"]) {
    $debug = TRUE;
    echo ',"parm_debug":"TRUE"';
} else {
    $debug = FALSE;
}

$error = FALSE;
$error_text = "";

if ($_GET["from_word_id"]) {
    $i_from_word_id = $_GET["from_word_id"];
    echo ',"parm_from_word_id":"' . $i_from_word_id . '"';
} else {
    $error = TRUE;
    $error_text .= "from_word_id not supplied, ";
}
if ($_GET["to_word"]) {
    $i_to_word = $_GET["to_word"];
    echo ',"parm_to_word":"' . $i_to_word . '"';
} else {
    $error = TRUE;
    $error_text .= "to_word not supplied, ";
}
if ($_GET["to_word_lang"]) {
    $i_to_word_lang = $_GET["to_word_lang"];
    echo ',"parm_to_word_lang":"' . $i_to_word_lang . '"';
} else {
    $error = TRUE;
    $error_text .= "to_word_lang not supplied, ";
}

if ($_GET["part_of_speech_id"]) {
    $i_part_of_speech_id = $_GET["part_of_speech_id"];
    echo ',"parm_part_of_speech_id":"' . $i_part_of_speech_id . '"';
} else {
    $i_part_of_speech_id = "";
    echo ',"parm_part_of_speech_id":""';
}

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

// if to_word doesn't exist, create
$to_word = new Word($i_to_word, $i_to_word_lang);
$to_word->getByWord($db_conn);
if ($to_word->is_on_db) {
    //echo ',"to_word":"already exists"';
} else {
    $to_word->store($db_conn);
}
echo ',"to_word":' . $to_word->toJson();


// create Word_Meaning object, store on Db and return object as JSON
$word_meaning = new Word_Meaning($i_from_word_id, $to_word->id, $i_part_of_speech_id);

$word_meaning->store($db_conn);

echo ',"word_meaning":' . $word_meaning->toJson();

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