diff --git a/index.php b/index.php index fdb6ff0..50fa702 100644 --- a/index.php +++ b/index.php @@ -224,7 +224,7 @@ elseif (isset($_GET['loggedout']) && $current_user <= 0) { 0) { //If logged in, show the log out button. ?> diff --git a/js/dictionaryBuilder.js b/js/dictionaryBuilder.js index 061e8b4..4f89f9e 100644 --- a/js/dictionaryBuilder.js +++ b/js/dictionaryBuilder.js @@ -1,4 +1,4 @@ -/* global markdown */ +/* global markdown */ /* global Defiant */ var currentUser = 0; @@ -299,7 +299,7 @@ function SaveSettings() { SaveAndUpdateDictionary(true); } -function CheckForPartsOfSpeechChange () { +function CheckForPartsOfSpeechChange() { if (htmlEntities(document.getElementById("dictionaryPartsOfSpeechEdit").value) != currentDictionary.settings.partsOfSpeech) { if (htmlEntities(document.getElementById("dictionaryPartsOfSpeechEdit").value) != "") { currentDictionary.settings.partsOfSpeech = htmlEntities(document.getElementById("dictionaryPartsOfSpeechEdit").value); @@ -310,13 +310,26 @@ function CheckForPartsOfSpeechChange () { function EmptyWholeDictionary() { if (confirm("This will delete the entire current dictionary. If you do not have a backed up export, you will lose it forever!\n\nDo you still want to delete?")) { - currentDictionary = JSON.parse(defaultDictionaryJSON); + ResetDictionaryToDefault(); SaveAndUpdateDictionary(false); SetPartsOfSpeech(); HideSettings(); } } +function CreateNewDictionary() { + // This is EmptyWholeDictionary() without the confirmation. ONLY USE WHEN LOGGED IN. + ResetDictionaryToDefault(); + SaveAndUpdateDictionary(false); + LoadUserDictionaries(); + SetPartsOfSpeech(); + HideSettings(); +} + +function ResetDictionaryToDefault() { + currentDictionary = JSON.parse(defaultDictionaryJSON); +} + function SaveDictionary(sendToDatabase, sendWords) { localStorage.setItem('dictionary', JSON.stringify(currentDictionary)); @@ -368,7 +381,7 @@ function DataToSend(doSendWords) { if (currentDictionary.externalID == 0) { data = "name=" + encodeURIComponent(currentDictionary.name) + "&description=" + encodeURIComponent(currentDictionary.description) + "&words=" + encodeURIComponent(JSON.stringify(currentDictionary.words)); data += "&allowduplicates=" + ((currentDictionary.settings.allowDuplicates) ? "1" : "0") + "&casesensitive=" + ((currentDictionary.settings.caseSensitive) ? "1" : "0"); - data += "&partsofspeech=" + encodeURIComponent(currentDictionary.settings.partsOfSpeech) + "&iscomplete=" + ((currentDictionary.settings.isComplete) ? "1" : "0") + "&ispublic=0"; + data += "&partsofspeech=" + encodeURIComponent(currentDictionary.settings.partsOfSpeech) + "&sortbyequivalent=" + ((currentDictionary.settings.sortByEquivalent) ? "1" : "0") + "&iscomplete=" + ((currentDictionary.settings.isComplete) ? "1" : "0") + "&ispublic=0"; } else { if (currentDictionary.name != previousDictionary.name) { data += "name=" + encodeURIComponent(currentDictionary.name); @@ -407,13 +420,12 @@ function LoadDictionary() { if (loadDictionary.readyState == 4 && loadDictionary.status == 200) { if (loadDictionary.responseText == "no dictionaries") { SendDictionary(false); - console.log(loadDictionary.responseText); - } else if (loadDictionary.responseText.length < 20) { - console.log(loadDictionary.responseText); - } else { + } else if (loadDictionary.responseText.length > 50) { currentDictionary = JSON.parse(loadDictionary.responseText); SaveDictionary(false, false); ProcessLoad(); + } else { + console.log(loadDictionary.responseText); } return true; } else { @@ -423,6 +435,35 @@ function LoadDictionary() { loadDictionary.send(); } +function ChangeDictionary() { + var userDictionariesSelect = document.getElementById("userDictionaries"); + if (currentDictionary.externalID != userDictionariesSelect.value && userDictionariesSelect.options.length > 1) { + var changeDictionaryRequest = new XMLHttpRequest(); + changeDictionaryRequest.open('POST', "php/ajax_dictionarymanagement.php?action=switch"); + changeDictionaryRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + var postString = "newdictionaryid=" + userDictionariesSelect.value.toString(); + changeDictionaryRequest.onreadystatechange = function() { + if (changeDictionaryRequest.readyState == 4 && changeDictionaryRequest.status == 200) { + if (changeDictionaryRequest.responseText == "no dictionaries") { + SendDictionary(false); + console.log(changeDictionaryRequest.responseText); + } else if (changeDictionaryRequest.responseText.length > 50) { + currentDictionary = JSON.parse(changeDictionaryRequest.responseText); + SaveDictionary(false, false); + ProcessLoad(); + HideSettings(); + } else { + console.log(changeDictionaryRequest.responseText); + } + return true; + } else { + return false; + } + } + changeDictionaryRequest.send(postString); + } +} + function LoadLocalDictionary() { if (localStorage.getItem('dictionary')) { var tmpDictionary = JSON.parse(localStorage.getItem('dictionary')); diff --git a/js/ui.js b/js/ui.js index 8a64a4c..562e271 100644 --- a/js/ui.js +++ b/js/ui.js @@ -13,31 +13,32 @@ window.onload = function () { function LoadUserDictionaries() { var getDictionariesRequest = new XMLHttpRequest(); - getDictionariesRequest.open('GET', "php/ajax_dictionarymanagement.php?action=getall"); - getDictionariesRequest.onreadystatechange = function() { - if (getDictionariesRequest.readyState == 4 && getDictionariesRequest.status == 200) { - console.log() - var userDictionariesSelect = document.getElementById("userDictionaries"); - if (userDictionariesSelect.options.length > 0) { - for (var i = userDictionariesSelect.options.length - 1; i >= 0; i--) { - userDictionariesSelect.removeChild(userDictionariesSelect.options[i]); + var userDictionariesSelect = document.getElementById("userDictionaries"); + if (userDictionariesSelect != null) { + getDictionariesRequest.open('GET', "php/ajax_dictionarymanagement.php?action=getall"); + getDictionariesRequest.onreadystatechange = function() { + if (getDictionariesRequest.readyState == 4 && getDictionariesRequest.status == 200) { + if (userDictionariesSelect.options.length > 0) { + for (var i = userDictionariesSelect.options.length - 1; i >= 0; i--) { + userDictionariesSelect.removeChild(userDictionariesSelect.options[i]); + } + } + + var dictionaries = getDictionariesRequest.responseText.split("_DICTIONARYSEPARATOR_"); + for (var j = 0; j < dictionaries.length - 1; j++) { + var dictionaryOption = document.createElement('option'); + var dictionaryValues = dictionaries[j].split("_IDNAMESEPARATOR_"); + dictionaryOption.appendChild(document.createTextNode(dictionaryValues[1])); + dictionaryOption.value = dictionaryValues[0]; + userDictionariesSelect.appendChild(dictionaryOption); + } + if (dictionaries.length > 1) { + userDictionariesSelect.value = currentDictionary.externalID; } } - - var dictionaries = getDictionariesRequest.responseText.split("_DICTIONARYSEPARATOR_"); - for (var j = 0; j < dictionaries.length; j++) { - var dictionaryOption = document.createElement('option'); - var dictionaryValues = dictionaries[j].split("_IDNAMESEPARATOR_"); - dictionaryOption.appendChild(document.createTextNode(dictionaryValues[1])); - dictionaryOption.value = dictionaryValues[0]; - userDictionariesSelect.appendChild(dictionaryOption); - } - if (dictionaries.length > 1) { - userDictionariesSelect.value = ""; - } } + getDictionariesRequest.send(); } - getDictionariesRequest.send(); } function GetTextFile(filename) { diff --git a/php/ajax_dictionarymanagement.php b/php/ajax_dictionarymanagement.php index bd0db14..1a31d68 100644 --- a/php/ajax_dictionarymanagement.php +++ b/php/ajax_dictionarymanagement.php @@ -1,10 +1,12 @@ 0) { $query = "SELECT `id`, `name` FROM `dictionaries` WHERE `user`=" . $_SESSION['user'] . " ORDER BY `name` ASC;"; @@ -27,14 +32,16 @@ function Get_Dictionaries() { if ($dictionaries) { if (num_rows($dictionaries) > 0) { - $list = ""; - $_SESSION['dictionaries'] = []; - while ($dict = fetch($dictionaries)) { - $_SESSION['dictionaries'][] = $dict['id']; // Save a list of all dictionaries user has. - //list for the switch dictionaries dropdown. - $list .= $dict['id'] . '_IDNAMESEPARATOR_' . $dict['name'] . '_DICTIONARYSEPARATOR_'; + if ($return_list) { + $list = ""; + $_SESSION['dictionaries'] = []; + while ($dict = fetch($dictionaries)) { + $_SESSION['dictionaries'][] = $dict['id']; // Save a list of all dictionaries user has. + //list for the switch dictionaries dropdown. + $list .= $dict['id'] . '_IDNAMESEPARATOR_' . $dict['name'] . '_DICTIONARYSEPARATOR_'; + } + echo $list; } - echo $list; return true; } else { echo "no dictionaries"; @@ -99,13 +106,14 @@ function Save_Current_DictionaryAsNew() { $dbconnection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $query = "INSERT INTO `dictionaries`(`user`, `is_current`, `name`, `description`, `words`, `allow_duplicates`, `case_sensitive`, `parts_of_speech`, `sort_by_equivalent`, `is_complete`, `is_public`) "; - $query .= "VALUES (" . $_SESSION['user'] . ",1,'" . $_POST['name'] . "','" . $_POST['description'] . "','" . $_POST['words'] . "'," . $_POST['allowduplicates'] . "," . $_POST['casesensitive'] . ",'" . $_POST['partsofspeech'] . "'," . $_POST['sortbyequivalent'] . "," . $_POST['iscomplete'] . "," . $_POST['ispublic'] . ")"; + $query .= "VALUES (" . $_SESSION['user'] . ",0,'" . $_POST['name'] . "','" . $_POST['description'] . "','" . $_POST['words'] . "'," . $_POST['allowduplicates'] . "," . $_POST['casesensitive'] . ",'" . $_POST['partsofspeech'] . "'," . $_POST['sortbyequivalent'] . "," . $_POST['iscomplete'] . "," . $_POST['ispublic'] . ")"; try { $update = $dbconnection->prepare($query); $update->execute(); $_SESSION['dictionary'] = $conn->lastInsertId; $_SESSION['dictionaries'][] = $_SESSION['dictionary']; //Add new id to valid dictionaries. + Switch_Current_Dictionary($_SESSION['dictionary'], false); echo $_SESSION['dictionary']; return true; } @@ -167,16 +175,20 @@ function Update_Current_Dictionary() { return false; } -function Switch_Current_Dictionary() { - if (isset($_POST['newdictionaryid']) && isset($_SESSION['user'])) { - if (in_array($_POST['newdictionaryid'], $_SESSION['dictionaries'])) { +function Switch_Current_Dictionary($newdictionaryid, $returndictionary = true) { + if (isset($newdictionaryid) && isset($_SESSION['user'])) { + if (in_array($newdictionaryid, $_SESSION['dictionaries'])) { //Clear is_current from all user's dictionaries and then update the one they chose, only if the chosen dictionary is valid. $query = "UPDATE `dictionaries` SET `is_current`=0 WHERE `user`=" . $_SESSION['user'] . ";"; - $query .= "UPDATE `dictionaries` SET `is_current`=1 WHERE `id`=" . $_POST['newdictionaryid'] . " AND `user`=" . $_SESSION['user'] . ";"; + $query .= "UPDATE `dictionaries` SET `is_current`=1 WHERE `id`=" . $newdictionaryid . " AND `user`=" . $_SESSION['user'] . ";"; $update = query($query); if ($update) { - Load_Current_Dictionary(); + if ($returndictionary) { + Load_Current_Dictionary(); + } else { + echo "dictionary switched"; + } return true; } else { echo "could not update"; @@ -189,4 +201,31 @@ function Switch_Current_Dictionary() { } return false; } + +/*function Delete_Current_Dictionary($deletedictionaryid) { + if (isset($deletedictionaryid) && isset($_SESSION['user'])) { + if (in_array($deletedictionaryid, $_SESSION['dictionaries'])) { + //Clear is_current from all user's dictionaries and then update the one they chose, only if the chosen dictionary is valid. + $query = "DELETE FROM `dictionaries` WHERE `id`=" . $deletedictionaryid . " AND `user`=" . $_SESSION['user'] . ";"; + $update = query($query); + + if ($update) { + Get_Dictionaries(false); + $query = "UPDATE `dictionaries` SET `is_current`=1 WHERE `id`=" . $_SESSION['dictionaries'][0] . " AND `user`=" . $_SESSION['user'] . ";"; + $update = query($query); + if ($update) { + Load_Current_Dictionary(); + } + return true; + } else { + echo "could not update"; + } + } else { + echo "invalid dictionary"; + } + } else { + echo "no info provided"; + } + return false; +}*/ ?> diff --git a/php/helpers.php b/php/helpers.php index 0f0a4b6..1a01a28 100644 --- a/php/helpers.php +++ b/php/helpers.php @@ -3,7 +3,7 @@ function query ($query_string) { $dbconnection = new PDO('mysql:host=' . DATABASE_SERVERNAME . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD); $dbconnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $dbconnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + $dbconnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $dbconnection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); try { $queryResults = $dbconnection->prepare($query_string);