From 6370de16510d078a1c3d2aaf711ef16b1b322f80 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Wed, 15 Jun 2016 17:48:52 -0600 Subject: [PATCH] Words delete correctly and update the dictionary's next_word_id when new words are added. --- js/dictionaryBuilder.js | 80 ++++++++++++++++++------------- js/ui.js | 6 ++- php/ajax_dictionarymanagement.php | 33 +++++++++++-- 3 files changed, 81 insertions(+), 38 deletions(-) diff --git a/js/dictionaryBuilder.js b/js/dictionaryBuilder.js index a5d41db..02a5040 100644 --- a/js/dictionaryBuilder.js +++ b/js/dictionaryBuilder.js @@ -45,7 +45,7 @@ function AddWord() { if (wordIndex >= 0) { if (WordAtIndexWasChanged(wordIndex, word, pronunciation, partOfSpeech, simpleDefinition, longDefinition)) { document.getElementById("newWordButtonArea").style.display = "none"; - DisableForm(wordIndex.toString()); + DisableForm(''); updateConflictArea.style.display = "block"; var updateConflictText = "\"" + word + "\" is already in the dictionary"; @@ -69,10 +69,9 @@ function AddWord() { } } else { currentDictionary.words.push({name: word, pronunciation: pronunciation, partOfSpeech: ((partOfSpeech.length > 0) ? partOfSpeech : " "), simpleDefinition: simpleDefinition, longDefinition: longDefinition, wordId: currentDictionary.nextWordId++}); - SaveAndUpdateWords("new", wordIndex); + SaveAndUpdateWords("new"); FocusAfterAddingNewWord(); NewWordNotification(word); - SaveAndUpdateDictionary(false); } errorMessageArea.innerHTML = ""; @@ -167,7 +166,7 @@ function UpdateWord(wordIndex, word, pronunciation, partOfSpeech, simpleDefiniti currentDictionary.words[wordIndex].longDefinition = longDefinition; SaveAndUpdateWords("update", wordIndex); - // SaveAndUpdateDictionary(); + ClearForm(); window.scroll(savedScroll.x, savedScroll.y); @@ -177,12 +176,27 @@ function UpdateWord(wordIndex, word, pronunciation, partOfSpeech, simpleDefiniti } function DeleteWord(index) { - if (document.getElementById("editIndex").value != "") - ClearForm(); + var deleteWord = new XMLHttpRequest(); + deleteWord.open('POST', "php/ajax_dictionarymanagement.php?action=worddelete"); + deleteWord.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + deleteWord.onreadystatechange = function() { + if (deleteWord.readyState == 4 && deleteWord.status == 200) { + if (deleteWord.responseText == "deleted successfully") { + // If updated successfully, then reload the dictionary from server. + if (document.getElementById("editIndex").value != "") + ClearForm(); - currentDictionary.words.splice(index, 1); - - SaveAndUpdateDictionary(true); + currentDictionary.words.splice(index, 1); + + SaveAndUpdateDictionary(true); + } + console.log(deleteWord.responseText); + return true; + } else { + return false; + } + } + deleteWord.send("word=" + currentDictionary.words[index].wordId.toString()); } function ShowDictionary() { @@ -373,6 +387,8 @@ function CreateNewDictionary() { SaveAndUpdateDictionary(false); SetPartsOfSpeech(); HideSettings(); + ShowSettings(); + document.getElementById("dictionaryNameEdit").focus(); } function DeleteCurrentDictionary() { @@ -424,21 +440,17 @@ function SaveAndUpdateWords(action, wordIndex) { } var sendWords = new XMLHttpRequest(); - sendWords.open('POST', "php/ajax_dictionarymanagement.php?action=word" + action); + sendWords.open('POST', "php/ajax_dictionarymanagement.php?action=word" + action + "&nextwordid=" + currentDictionary.nextWordId.toString()); sendWords.setRequestHeader("Content-Type", "application/json; charset=UTF-8"); sendWords.onreadystatechange = function() { if (sendWords.readyState == 4 && sendWords.status == 200) { - if (sendWords.responseText == "updated successfully") { - // If updated successfully, then reload the dictionary from server. - console.log(sendWords.responseText); - LoadUserDictionaries(); - ProcessLoad(); - } else { // Otherwise it is creating (a) new word(s). - currentDictionary.externalID = parseInt(sendWords.responseText); - LoadUserDictionaries(); - ProcessLoad(); - console.log("saved successfully"); + if (!currentDictionary.settings.sortByEquivalent) { + currentDictionary.words.sort(dynamicSort(['name', 'partOfSpeech'])); + } else { + currentDictionary.words.sort(dynamicSort(['simpleDefinition', 'partOfSpeech'])); } + ProcessLoad(); + console.log(sendWords.responseText); return true; } else { return false; @@ -448,12 +460,12 @@ function SaveAndUpdateWords(action, wordIndex) { } function SaveAndUpdateDictionary(keepFormContents) { - if (!currentDictionary.settings.sortByEquivalent) { - currentDictionary.words.sort(dynamicSort(['name', 'partOfSpeech'])); - } else { - currentDictionary.words.sort(dynamicSort(['simpleDefinition', 'partOfSpeech'])); - } - SaveDictionary(true, true); + // if (!currentDictionary.settings.sortByEquivalent) { + // currentDictionary.words.sort(dynamicSort(['name', 'partOfSpeech'])); + // } else { + // currentDictionary.words.sort(dynamicSort(['simpleDefinition', 'partOfSpeech'])); + // } + SaveDictionary(true); ShowDictionary(); if (!keepFormContents) { ClearForm(); @@ -462,13 +474,13 @@ function SaveAndUpdateDictionary(keepFormContents) { } function SaveDictionary(sendToDatabase) { - localStorage.setItem('dictionary', JSON.stringify(currentDictionary)); - //Always save local copy of current dictionary, but if logged in also send to database. if (sendToDatabase) { SendDictionary(); } + localStorage.setItem('dictionary', JSON.stringify(currentDictionary)); + SavePreviousDictionary(); } @@ -496,10 +508,12 @@ function SendDictionary() { console.log(sendDictionary.responseText); } else { // It will only be a number if it is a new dictionary. currentDictionary.externalID = parseInt(sendDictionary.responseText); - SaveAndUpdateWords("all"); + if (currentDictionary.words.length > 0) { + SaveAndUpdateWords("all"); + } LoadUserDictionaries(); ProcessLoad(); - console.log("saved successfully"); + console.log("saved " + parseInt(sendDictionary.responseText).toString() + " successfully"); } return true; } else { @@ -563,7 +577,7 @@ function LoadDictionary() { console.log(loadDictionary.responseText); } else { currentDictionary = JSON.parse(loadDictionary.responseText); - SaveDictionary(false, false); + SaveDictionary(false); } } ProcessLoad(); @@ -587,7 +601,7 @@ function ChangeDictionary(userDictionariesSelect) { console.log(changeDictionaryRequest.responseText); } else { currentDictionary = JSON.parse(changeDictionaryRequest.responseText); - SaveDictionary(false, false); + SaveDictionary(false); ProcessLoad(); LoadUserDictionaries(); HideSettings(); @@ -677,7 +691,7 @@ function ImportDictionary() { currentDictionary = JSON.parse(reader.result); currentDictionary.externalID = 0; // Reset external id for imported dictionary. currentDictionary.settings.isPublic = false; // Reset public setting for imported dictionary. - SaveDictionary(true, true); + SaveDictionary(true); ProcessLoad(); HideSettings(); document.getElementById("importFile").value = ""; diff --git a/js/ui.js b/js/ui.js index 74ade97..01cc100 100644 --- a/js/ui.js +++ b/js/ui.js @@ -373,7 +373,11 @@ function LockWordForm() { function CloseUpdateConflictArea(wordIndexString) {// displayId, hideId) { // displayId = (typeof displayId !== 'undefined' && displayId != null) ? displayId : false; // if (displayId != false) { - document.getElementById("editWordButtonArea" + wordIndexString).style.display = "block"; + if (wordIndexString == "") { + document.getElementById("newWordButtonArea").style.display = "block"; + } else { + document.getElementById("editWordButtonArea" + wordIndexString).style.display = "block"; + } // } document.getElementById("updateConflict" + wordIndexString).style.display = "none"; EnableForm(wordIndexString); diff --git a/php/ajax_dictionarymanagement.php b/php/ajax_dictionarymanagement.php index b808ced..2af7db0 100644 --- a/php/ajax_dictionarymanagement.php +++ b/php/ajax_dictionarymanagement.php @@ -32,6 +32,9 @@ if ($_SESSION['user'] > 0) { elseif ($_GET['action'] == 'delete') { Delete_Current_Dictionary(); } + elseif ($_GET['action'] == 'worddelete') { + Delete_Word(); + } } else { echo "not signed in"; } @@ -208,10 +211,11 @@ function Save_New_Word($multiple = false) { $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); - $query = "INSERT INTO `words`(`dictionary`, `word_id`, `name`, `pronunciation`, `part_of_speech`, `simple_definition`, `long_definition`) "; + $query = "UPDATE `dictionaries` SET `next_word_id`=" . $_GET['nextwordid'] . " WHERE `id`=" . $_SESSION['dictionary'] . "; "; + $query .= "INSERT INTO `words`(`dictionary`, `word_id`, `name`, `pronunciation`, `part_of_speech`, `simple_definition`, `long_definition`) "; $query .= "VALUES "; if ($multiple) { for ($i = 0; $i < count($worddata); $i++) { @@ -228,11 +232,11 @@ function Save_New_Word($multiple = false) { try { $update = $dbconnection->prepare($query); $update->execute(); + echo "added successfully"; return true; } catch (PDOException $ex) { - $errorMessage = $dbconnection->errorInfo(); - echo "could not update:\n" . $errorMessage[2] . "\n" . $query; + echo "could not update:\n" . $ex->getMessage() . "\n" . $query; } return false; } @@ -308,4 +312,25 @@ function Delete_Current_Dictionary() { } return false; } + +function Delete_Word() { + if (isset($_SESSION['dictionary'])) { + if (in_array($_SESSION['dictionary'], $_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 `words` WHERE `dictionary`=" . $_SESSION['dictionary'] . " AND `word_id`=" . $_POST['word'] . ";"; + $update = query($query); + + if ($update) { + echo "deleted successfully"; + } else { + echo "could not delete: " . $_SESSION['dictionary'] . "-" . $_POST['word'] . " caused a problem"; + } + } else { + echo "invalid dictionary"; + } + } else { + echo "no current dictionary"; + } + return false; +} ?>