diff --git a/src/php/.htaccess b/src/php/.htaccess index 790c499..43599f9 100644 --- a/src/php/.htaccess +++ b/src/php/.htaccess @@ -1,12 +1,12 @@ RewriteEngine On # Turn on the rewriting engine -RewriteRule ^view/([0-9]+)/([0-9]+)/?$ api/router.php?view=publicview&dict=$1&word=$2 [NC,L] # Handle word ids. +RewriteRule ^view/([0-9]+)/([0-9]+)/?$ api/router.php?view=word&dict=$1&word=$2 [NC,L] # Handle word ids. -RewriteRule ^([0-9]+)/([0-9]+)/?$ api/router.php?view=publicview&dict=$1&word=$2 [NC,L] # Handle word ids. +RewriteRule ^([0-9]+)/([0-9]+)/?$ api/router.php?view=word&dict=$1&word=$2 [NC,L] # Handle word ids. -RewriteRule ^view/([0-9]+)/?$ api/router.php?view=publicview&dict=$1 [NC,L] # Handle dictionary ids. +RewriteRule ^view/([0-9]+)/?$ api/router.php?view=dictionary&dict=$1 [NC,L] # Handle dictionary ids. -RewriteRule ^([0-9]+)/?$ api/router.php?view=publicview&dict=$1 [NC,L] # Handle dictionary ids. +RewriteRule ^([0-9]+)/?$ api/router.php?view=dictionary&dict=$1 [NC,L] # Handle dictionary ids. #RewriteRule ^issues/?$ https://github.com/Alamantus/Lexiconga/issues [R=301,L] # Shorten issues url. diff --git a/src/php/api/Dictionary.php b/src/php/api/Dictionary.php index a1bc1c3..f3e3f66 100644 --- a/src/php/api/Dictionary.php +++ b/src/php/api/Dictionary.php @@ -160,6 +160,26 @@ VALUES ($new_id, ?, ?, ?, ?)"; return array(); } + public function getSpecificPublicDictionaryWord ($dictionary, $word) { + if (is_numeric($dictionary) && is_numeric($word)) { + $query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=? AND word_id=? AND is_public=1"; + $result = $this->db->query($query, array($dictionary, $word))->fetch(); + if ($result) { + return array( + 'name' => $result['name'], + 'pronunciation' => $result['pronunciation'], + 'partOfSpeech' => $result['part_of_speech'], + 'definition' => $result['definition'], + 'details' => $result['details'], + 'lastUpdated' => is_null($result['last_updated']) ? intval($result['created_on']) : intval($result['last_updated']), + 'createdOn' => intval($result['created_on']), + 'wordId' => intval($result['word_id']), + ); + } + } + return false; + } + public function getDetails ($user, $dictionary) { $query = "SELECT * FROM dictionaries JOIN dictionary_linguistics ON dictionary = id WHERE user=$user AND id=$dictionary"; $result = $this->db->query($query)->fetch(); diff --git a/src/php/api/router.php b/src/php/api/router.php index b81d896..4c5e3c9 100644 --- a/src/php/api/router.php +++ b/src/php/api/router.php @@ -2,7 +2,7 @@ $view = isset($_GET['view']) ? $_GET['view'] : false; switch ($view) { - case 'publicview': { + case 'dictionary': { $html = file_get_contents('../view.html'); $dict = isset($_GET['dict']) ? $_GET['dict'] : false; if ($dict !== false) { @@ -26,4 +26,43 @@ switch ($view) { } break; } + case 'word': { + $html = file_get_contents('../view.html'); + $dict = isset($_GET['dict']) ? $_GET['dict'] : false; + $word = isset($_GET['word']) ? $_GET['word'] : false; + if ($dict !== false && $word !== false) { + require_once('./Dictionary.php'); + $dictionary = new Dictionary(); + $dictionary_data = $dictionary->getPublicDictionaryDetails($dict); + if ($dictionary_data !== false) { + $dictionary_name = $dictionary_data['name'] . ' ' . $dictionary_data['specification']; + $word_data = $dictionary->getSpecificPublicDictionaryWord($dict, $word); + if ($word_data === false) { + $word_data = array( + 'name' => 'Error: Word Not Found', + 'pronunciation' => '', + 'partOfSpeech' => '', + 'definition' => 'No word with the id ' . $word . ' was found in the ' . $dictionary_name, + 'details' => '', + 'lastUpdated' => null, + 'createdOn' => null, + 'wordId' => null, + ); + } + $dictionary_data['words'] = array($word_data); + $html = str_replace('{{dict}}', $dict, $html); + $html = str_replace('{{dict_name}}', $word_data['name'] . ' in the ' . $dictionary_name, $html); + $html = str_replace('{{public_name}}', $dictionary_data['createdBy'], $html); + $dictionary_json = json_encode($dictionary_data); + $html = str_replace('{{dict_json}}', addslashes($dictionary_json), $html); + } else { + $html = str_replace('{{dict}}', 'error', $html); + $html = str_replace('{{dict_name}}', 'Error: Dictionary Not Found', $html); + $html = str_replace('{{public_name}}', 'Error', $html); + $html = str_replace('{{dict_json}}', '{"name": "Error:", "specification": "Dictionary Not Found", "words": []}', $html); + } + echo $html; + } + break; + } } \ No newline at end of file