Words delete correctly and update the dictionary's next_word_id when new words are added.

This commit is contained in:
Robbie Antenesse 2016-06-15 17:48:52 -06:00
parent ed8eec71cc
commit 6370de1651
3 changed files with 81 additions and 38 deletions

View File

@ -45,7 +45,7 @@ function AddWord() {
if (wordIndex >= 0) { if (wordIndex >= 0) {
if (WordAtIndexWasChanged(wordIndex, word, pronunciation, partOfSpeech, simpleDefinition, longDefinition)) { if (WordAtIndexWasChanged(wordIndex, word, pronunciation, partOfSpeech, simpleDefinition, longDefinition)) {
document.getElementById("newWordButtonArea").style.display = "none"; document.getElementById("newWordButtonArea").style.display = "none";
DisableForm(wordIndex.toString()); DisableForm('');
updateConflictArea.style.display = "block"; updateConflictArea.style.display = "block";
var updateConflictText = "<span id='updateConflictMessage'>\"" + word + "\" is already in the dictionary"; var updateConflictText = "<span id='updateConflictMessage'>\"" + word + "\" is already in the dictionary";
@ -69,10 +69,9 @@ function AddWord() {
} }
} else { } else {
currentDictionary.words.push({name: word, pronunciation: pronunciation, partOfSpeech: ((partOfSpeech.length > 0) ? partOfSpeech : " "), simpleDefinition: simpleDefinition, longDefinition: longDefinition, wordId: currentDictionary.nextWordId++}); 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(); FocusAfterAddingNewWord();
NewWordNotification(word); NewWordNotification(word);
SaveAndUpdateDictionary(false);
} }
errorMessageArea.innerHTML = ""; errorMessageArea.innerHTML = "";
@ -167,7 +166,7 @@ function UpdateWord(wordIndex, word, pronunciation, partOfSpeech, simpleDefiniti
currentDictionary.words[wordIndex].longDefinition = longDefinition; currentDictionary.words[wordIndex].longDefinition = longDefinition;
SaveAndUpdateWords("update", wordIndex); SaveAndUpdateWords("update", wordIndex);
// SaveAndUpdateDictionary(); ClearForm();
window.scroll(savedScroll.x, savedScroll.y); window.scroll(savedScroll.x, savedScroll.y);
@ -177,12 +176,27 @@ function UpdateWord(wordIndex, word, pronunciation, partOfSpeech, simpleDefiniti
} }
function DeleteWord(index) { function DeleteWord(index) {
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 != "") if (document.getElementById("editIndex").value != "")
ClearForm(); ClearForm();
currentDictionary.words.splice(index, 1); currentDictionary.words.splice(index, 1);
SaveAndUpdateDictionary(true); SaveAndUpdateDictionary(true);
}
console.log(deleteWord.responseText);
return true;
} else {
return false;
}
}
deleteWord.send("word=" + currentDictionary.words[index].wordId.toString());
} }
function ShowDictionary() { function ShowDictionary() {
@ -373,6 +387,8 @@ function CreateNewDictionary() {
SaveAndUpdateDictionary(false); SaveAndUpdateDictionary(false);
SetPartsOfSpeech(); SetPartsOfSpeech();
HideSettings(); HideSettings();
ShowSettings();
document.getElementById("dictionaryNameEdit").focus();
} }
function DeleteCurrentDictionary() { function DeleteCurrentDictionary() {
@ -424,21 +440,17 @@ function SaveAndUpdateWords(action, wordIndex) {
} }
var sendWords = new XMLHttpRequest(); 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.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
sendWords.onreadystatechange = function() { sendWords.onreadystatechange = function() {
if (sendWords.readyState == 4 && sendWords.status == 200) { if (sendWords.readyState == 4 && sendWords.status == 200) {
if (sendWords.responseText == "updated successfully") { if (!currentDictionary.settings.sortByEquivalent) {
// If updated successfully, then reload the dictionary from server. currentDictionary.words.sort(dynamicSort(['name', 'partOfSpeech']));
console.log(sendWords.responseText); } else {
LoadUserDictionaries(); currentDictionary.words.sort(dynamicSort(['simpleDefinition', 'partOfSpeech']));
ProcessLoad();
} else { // Otherwise it is creating (a) new word(s).
currentDictionary.externalID = parseInt(sendWords.responseText);
LoadUserDictionaries();
ProcessLoad();
console.log("saved successfully");
} }
ProcessLoad();
console.log(sendWords.responseText);
return true; return true;
} else { } else {
return false; return false;
@ -448,12 +460,12 @@ function SaveAndUpdateWords(action, wordIndex) {
} }
function SaveAndUpdateDictionary(keepFormContents) { function SaveAndUpdateDictionary(keepFormContents) {
if (!currentDictionary.settings.sortByEquivalent) { // if (!currentDictionary.settings.sortByEquivalent) {
currentDictionary.words.sort(dynamicSort(['name', 'partOfSpeech'])); // currentDictionary.words.sort(dynamicSort(['name', 'partOfSpeech']));
} else { // } else {
currentDictionary.words.sort(dynamicSort(['simpleDefinition', 'partOfSpeech'])); // currentDictionary.words.sort(dynamicSort(['simpleDefinition', 'partOfSpeech']));
} // }
SaveDictionary(true, true); SaveDictionary(true);
ShowDictionary(); ShowDictionary();
if (!keepFormContents) { if (!keepFormContents) {
ClearForm(); ClearForm();
@ -462,13 +474,13 @@ function SaveAndUpdateDictionary(keepFormContents) {
} }
function SaveDictionary(sendToDatabase) { function SaveDictionary(sendToDatabase) {
localStorage.setItem('dictionary', JSON.stringify(currentDictionary));
//Always save local copy of current dictionary, but if logged in also send to database. //Always save local copy of current dictionary, but if logged in also send to database.
if (sendToDatabase) { if (sendToDatabase) {
SendDictionary(); SendDictionary();
} }
localStorage.setItem('dictionary', JSON.stringify(currentDictionary));
SavePreviousDictionary(); SavePreviousDictionary();
} }
@ -496,10 +508,12 @@ function SendDictionary() {
console.log(sendDictionary.responseText); console.log(sendDictionary.responseText);
} else { // It will only be a number if it is a new dictionary. } else { // It will only be a number if it is a new dictionary.
currentDictionary.externalID = parseInt(sendDictionary.responseText); currentDictionary.externalID = parseInt(sendDictionary.responseText);
if (currentDictionary.words.length > 0) {
SaveAndUpdateWords("all"); SaveAndUpdateWords("all");
}
LoadUserDictionaries(); LoadUserDictionaries();
ProcessLoad(); ProcessLoad();
console.log("saved successfully"); console.log("saved " + parseInt(sendDictionary.responseText).toString() + " successfully");
} }
return true; return true;
} else { } else {
@ -563,7 +577,7 @@ function LoadDictionary() {
console.log(loadDictionary.responseText); console.log(loadDictionary.responseText);
} else { } else {
currentDictionary = JSON.parse(loadDictionary.responseText); currentDictionary = JSON.parse(loadDictionary.responseText);
SaveDictionary(false, false); SaveDictionary(false);
} }
} }
ProcessLoad(); ProcessLoad();
@ -587,7 +601,7 @@ function ChangeDictionary(userDictionariesSelect) {
console.log(changeDictionaryRequest.responseText); console.log(changeDictionaryRequest.responseText);
} else { } else {
currentDictionary = JSON.parse(changeDictionaryRequest.responseText); currentDictionary = JSON.parse(changeDictionaryRequest.responseText);
SaveDictionary(false, false); SaveDictionary(false);
ProcessLoad(); ProcessLoad();
LoadUserDictionaries(); LoadUserDictionaries();
HideSettings(); HideSettings();
@ -677,7 +691,7 @@ function ImportDictionary() {
currentDictionary = JSON.parse(reader.result); currentDictionary = JSON.parse(reader.result);
currentDictionary.externalID = 0; // Reset external id for imported dictionary. currentDictionary.externalID = 0; // Reset external id for imported dictionary.
currentDictionary.settings.isPublic = false; // Reset public setting for imported dictionary. currentDictionary.settings.isPublic = false; // Reset public setting for imported dictionary.
SaveDictionary(true, true); SaveDictionary(true);
ProcessLoad(); ProcessLoad();
HideSettings(); HideSettings();
document.getElementById("importFile").value = ""; document.getElementById("importFile").value = "";

View File

@ -373,7 +373,11 @@ function LockWordForm() {
function CloseUpdateConflictArea(wordIndexString) {// displayId, hideId) { function CloseUpdateConflictArea(wordIndexString) {// displayId, hideId) {
// displayId = (typeof displayId !== 'undefined' && displayId != null) ? displayId : false; // displayId = (typeof displayId !== 'undefined' && displayId != null) ? displayId : false;
// if (displayId != false) { // if (displayId != false) {
if (wordIndexString == "") {
document.getElementById("newWordButtonArea").style.display = "block";
} else {
document.getElementById("editWordButtonArea" + wordIndexString).style.display = "block"; document.getElementById("editWordButtonArea" + wordIndexString).style.display = "block";
}
// } // }
document.getElementById("updateConflict" + wordIndexString).style.display = "none"; document.getElementById("updateConflict" + wordIndexString).style.display = "none";
EnableForm(wordIndexString); EnableForm(wordIndexString);

View File

@ -32,6 +32,9 @@ if ($_SESSION['user'] > 0) {
elseif ($_GET['action'] == 'delete') { elseif ($_GET['action'] == 'delete') {
Delete_Current_Dictionary(); Delete_Current_Dictionary();
} }
elseif ($_GET['action'] == 'worddelete') {
Delete_Word();
}
} else { } else {
echo "not signed in"; 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 = 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_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); $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 "; $query .= "VALUES ";
if ($multiple) { if ($multiple) {
for ($i = 0; $i < count($worddata); $i++) { for ($i = 0; $i < count($worddata); $i++) {
@ -228,11 +232,11 @@ function Save_New_Word($multiple = false) {
try { try {
$update = $dbconnection->prepare($query); $update = $dbconnection->prepare($query);
$update->execute(); $update->execute();
echo "added successfully";
return true; return true;
} }
catch (PDOException $ex) { catch (PDOException $ex) {
$errorMessage = $dbconnection->errorInfo(); echo "could not update:\n" . $ex->getMessage() . "\n" . $query;
echo "could not update:\n" . $errorMessage[2] . "\n" . $query;
} }
return false; return false;
} }
@ -308,4 +312,25 @@ function Delete_Current_Dictionary() {
} }
return false; 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;
}
?> ?>