From 3902ba96bafd7a2db3beca13901f1e513ce304df Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Mon, 20 Jun 2016 17:13:51 -0600 Subject: [PATCH] Got public word pages working! Added link to public words from editing dictionary. Separated some templating functions so styling will be easier moving forward. --- index.php | 151 ++++++++++++++++++++++------------------ js/dictionaryBuilder.js | 38 ++++++---- js/publicView.js | 129 +++++++++++++++++++++------------- 3 files changed, 186 insertions(+), 132 deletions(-) diff --git a/index.php b/index.php index 539ed42..7c566fb 100644 --- a/index.php +++ b/index.php @@ -13,6 +13,8 @@ $word_to_load = (isset($_GET['word'])) ? intval($_GET['word']) : 0; $the_public_word = '"That word doesn\'t exist."'; $word_name = 'ERROR'; +$is_owner = false; + $display_mode = ($dictionary_to_load > 0) ? (($word_to_load > 0) ? "word" : "view") : "build"; $announcement = get_include_contents(SITE_LOCATION . '/announcement.php'); @@ -34,60 +36,62 @@ if ($display_mode != "build") { $dbconnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $dbconnection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); - if ($display_mode == "word") { - // only query for specific word. - $query = "SELECT `d`.`name` AS `dname`, `u`.`public_name`, `w`.`word_id`, `w`.`name`, `w`.`pronunciation`, `w`.`part_of_speech`, `w`.`simple_definition`, `w`.`long_definition` "; - $query .= "FROM `words` AS `w` LEFT JOIN `dictionaries` AS `d` ON `w`.`dictionary`=`d`.`id` "; - $query .= "LEFT JOIN `users` AS `u` ON `d`.`user`=`u`.`id` "; - $query .= "WHERE `d`.`is_public`=1 AND `w`.`dictionary`=" . $dictionary_to_load . " AND `w`.`word_id`=" . $dictionary_to_load . ";"; - - try { - $queryResults = $dbconnection->prepare($query); - $queryResults->execute(); - if ($queryResults) { - if (num_rows($queryResults) === 1) { - while ($word = fetch($queryResults)) { - $dictionary_name = $word['dname']; - $dictionary_creator = $word['public_name']; - $the_public_word = '{"name":"' . $word['name'] . '",'; - $the_public_word .= '"pronunciation":"' . $word['pronunciation'] . '",'; - $the_public_word .= '"partOfSpeech":"' . $word['part_of_speech'] . '",'; - $the_public_word .= '"simpleDefinition":"' . $word['simple_definition'] . '",'; - $the_public_word .= '"longDefinition":"' . $word['long_definition'] . '",'; - $the_public_word .= '"wordId":"' . $word['word_id'] . '"'; - $the_public_word .= '}'; - } - } - } - } - catch (PDOException $ex) {} - } else { - // Otherwise, grab everything. - $query = "SELECT `d`.`id`, `d`.`name`, `d`.`description`, `u`.`public_name`, `d`.`parts_of_speech`, `d`.`is_complete` "; - $query .= "FROM `dictionaries` AS `d` LEFT JOIN `users` AS `u` ON `d`.`user`=`u`.`id` WHERE `d`.`is_public`=1 AND `d`.`id`=" . $dictionary_to_load . ";"; + $dictionary_query = "SELECT `d`.`id`, `d`.`user`, `d`.`name`, `d`.`description`, `u`.`public_name`, `d`.`parts_of_speech`, `d`.`is_complete` "; + $dictionary_query .= "FROM `dictionaries` AS `d` LEFT JOIN `users` AS `u` ON `d`.`user`=`u`.`id`"; + $dictionary_query .= "WHERE `d`.`is_public`=1 AND `d`.`id`=" . $dictionary_to_load . ";"; - try { - $queryResults = $dbconnection->prepare($query); - $queryResults->execute(); - if ($queryResults) { - if (num_rows($queryResults) === 1) { - while ($dict = fetch($queryResults)) { - $dictionary_name = $dict['name']; - $dictionary_creator = $dict['public_name']; - $the_public_dictionary = '{"name":"' . $dict['name'] . '",'; - $the_public_dictionary .= '"description":"' . $dict['description'] . '",'; - $the_public_dictionary .= '"createdBy":"' . $dict['public_name'] . '",'; - $the_public_dictionary .= '"words":' . Get_Dictionary_Words($dictionary_to_load) . ','; - $the_public_dictionary .= '"settings":{'; - $the_public_dictionary .= '"partsOfSpeech":"' . $dict['parts_of_speech'] . '",'; - $the_public_dictionary .= '"isComplete":' . (($dict['is_complete'] == 1) ? 'true' : 'false') . '},'; - $the_public_dictionary .= '}'; + $word_query = "SELECT `w`.`word_id`, `w`.`name`, `w`.`pronunciation`, `w`.`part_of_speech`, `w`.`simple_definition`, `w`.`long_definition` "; + $word_query .= "FROM `words` AS `w` LEFT JOIN `dictionaries` AS `d` ON `w`.`dictionary`=`d`.`id` "; + $word_query .= "WHERE `d`.`is_public`=1 AND `w`.`dictionary`=" . $dictionary_to_load . (($display_mode == "word") ? " AND `w`.`word_id`=" . $word_to_load : "") . " "; + $word_query .= "ORDER BY IF(`d`.`sort_by_equivalent`, `w`.`simple_definition`, `w`.`name`) COLLATE utf8_unicode_ci;"; + + try { + $dictionary_results = $dbconnection->prepare($dictionary_query); + $dictionary_results->execute(); + if ($dictionary_results) { + $word_results = $dbconnection->prepare($word_query); + $word_results->execute(); + $dictionary_words = "["; + if ($word_results) { + $words_counted = 0; + $words_total = num_rows($word_results); + while ($word = fetch($word_results)) { + $words_counted++; + $word_name = $word['name']; + $dictionary_words .= '{"name":"' . $word['name'] . '",'; + $dictionary_words .= '"pronunciation":"' . $word['pronunciation'] . '",'; + $dictionary_words .= '"partOfSpeech":"' . $word['part_of_speech'] . '",'; + $dictionary_words .= '"simpleDefinition":"' . $word['simple_definition'] . '",'; + $dictionary_words .= '"longDefinition":"' . $word['long_definition'] . '",'; + $dictionary_words .= '"wordId":"' . $word['word_id'] . '"'; + $dictionary_words .= '}'; + + if ($words_counted < $words_total) { + $dictionary_words .= ','; } } } + $dictionary_words .= "]"; + + if (num_rows($dictionary_results) === 1) { + while ($dict = fetch($dictionary_results)) { + $dictionary_name = $dict['name']; + $dictionary_creator = $dict['public_name']; + $is_owner = $current_user == $dict['user']; + $the_public_dictionary = '{"name":"' . $dict['name'] . '",'; + $the_public_dictionary .= '"description":"' . $dict['description'] . '",'; + $the_public_dictionary .= '"createdBy":"' . $dict['public_name'] . '",'; + $the_public_dictionary .= '"words":' . $dictionary_words . ','; + $the_public_dictionary .= '"settings":{'; + $the_public_dictionary .= '"partsOfSpeech":"' . $dict['parts_of_speech'] . '",'; + $the_public_dictionary .= '"isComplete":' . (($dict['is_complete'] == 1) ? 'true' : 'false') . '},'; + $the_public_dictionary .= '"id":"' . $dictionary_to_load . '"'; + $the_public_dictionary .= '}'; + } + } } - catch (PDOException $ex) {} } + catch (PDOException $ex) {} } ?> @@ -104,12 +108,8 @@ if ($display_mode != "build") { Dictionary" /> - - - - - + + Lexiconga Dictionary Builder @@ -129,13 +129,22 @@ if ($display_mode != "build") { About Lexiconga
+ 0) { //If logged in, show the log out button. ?> Account Settings Log Out Log In/Create Account Can't Login - + +

Viewing

+ + « Edit Dictionary + + « Go Home to Lexiconga + +
@@ -188,11 +197,14 @@ if ($display_mode != "build") {

- +

+ + View Full Dictionary + HideShow Description
blocknone;">
@@ -232,9 +244,7 @@ if ($display_mode != "build") {
-
- document.write(DictionaryEntryTemplate(" . $the_public_word . "));"; } ?> -
+