<?php
include "classes/DatabaseConnection.php";
// return JSON string of the request and results for list of words
echo '{"request_type":"words_list"';
if ($_GET["from_language"]) {
    $from_language = $_GET["from_language"];
    echo ',"parm_from_language":"' . $from_language . '"';
} else {
    $from_language = 2; // default Greek
    echo ',"parm_from_language":"dft-' . $from_language . '"';
}

if ($_GET["to_language"]) {
    $to_language = $_GET["to_language"];
    echo ',"parm_to_language":"' . $to_language . '"';
} else {
    $to_language = 1; // default English
    echo ',"parm_to_language":"dft-' . $to_language . '"';
}

// read words and linked meanings from database
$db_conn = DatabaseConnection::getConnection();

$query = "SELECT fw.word as from_word, tw.word as to_word, fw.id as from_word_id, tw.id as to_word_id, wm.id as meaning_id, nvl(pos.part,'unspecified') as part, nvl(pos.part_abbr,'-') as part_abbr FROM words fw LEFT JOIN word_meanings wm on fw.id = wm.from_word_id LEFT JOIN words tw on wm.to_word_id = tw.id LEFT JOIN parts_of_speech pos on wm.part_of_speech_id = pos.id WHERE fw.language = $from_language and nvl(tw.language, $to_language) = $to_language ORDER BY fw.word";

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

// initialise variables
$first_time = TRUE;
$prv_word_id = "";
$word_details = "";
$meaning_cell = "";

// start array of words 
echo ',"words":[';

foreach ($result as $row) {
    // when "from word" changes...
    if ($row['from_word_id'] != $prv_word_id) {
        // close array of meanings and object bracket of previous word if not first time
        if (!$first_time) {
            echo ']},';
        }
        // set up details of "from word"
        echo '{"type":"word","word_id":"' . $row['from_word_id'] . '","word_language":"' . $from_language . '","word":"' . $row['from_word'] . '","to_language":"' . $to_language . '","meanings":[';
        // not all words have meanings set up...
        if ($row['to_word']) {
            echo '{"from_word_id":"' . $row['from_word_id'] . '","meaning_id":"' . $row['to_word_id'] . '","meaning":"' . $row['to_word'] . '","meaning_id":"' . $row['meaning_id'] . '","meaning_language":"' . $to_language . '","part_of_speech":"' . $row['part'] . '","part_of_speech_abbr":"' . $row['part_abbr'] . '"}';
        }
        $prv_word_id = $row['from_word_id'];
    } else {
        // rows with same word_id should have a meaning (non-null to_word) but check anyway
        if ($row['to_word']) {
            echo ',{"from_word_id":"' . $row['from_word_id'] . '","meaning_id":"' . $row['to_word_id'] . '","meaning":"' . $row['to_word'] . '","meaning_id":"' . $row['meaning_id'] . '","meaning_language":"' . $to_language . '","part_of_speech":"' . $row['part'] . '","part_of_speech_abbr":"' . $row['part_abbr'] . '"}';
        }
    }
    $first_time = FALSE;
}

// output last word's details
if (!$first_time) {
    // close array of meanings and object bracket of previous word if not first time
    if (!$first_time) {
        echo ']}';
    }
}

// close array of words and request object
echo ']}';
?>