Adding and changing dictionaries.

This commit is contained in:
Robbie Antenesse 2015-11-27 23:17:52 -07:00
parent 8a025d14b5
commit 8861ca24b5
5 changed files with 129 additions and 48 deletions

View File

@ -224,7 +224,7 @@ elseif (isset($_GET['loggedout']) && $current_user <= 0) {
<label><button type="button" onclick="ExportDictionary()" style="cursor:pointer;">Export Current Dictionary</button></label>
<?php if ($current_user > 0) { //If logged in, show the log out button. ?>
<label><span>Change Dictionaries</span>
<select id="userDictionaries" onchange="LoadOtherDictionary();"></select>
<select id="userDictionaries" onchange="ChangeDictionary();"></select>
</label>
<label><button type="button" onclick="CreateNewDictionary()" style="cursor:pointer;">Create a New Dictionary</button></label>
<?php } ?>

View File

@ -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'));

View File

@ -13,11 +13,11 @@ window.onload = function () {
function LoadUserDictionaries() {
var getDictionariesRequest = new XMLHttpRequest();
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) {
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]);
@ -25,7 +25,7 @@ function LoadUserDictionaries() {
}
var dictionaries = getDictionariesRequest.responseText.split("_DICTIONARYSEPARATOR_");
for (var j = 0; j < dictionaries.length; j++) {
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]));
@ -33,11 +33,12 @@ function LoadUserDictionaries() {
userDictionariesSelect.appendChild(dictionaryOption);
}
if (dictionaries.length > 1) {
userDictionariesSelect.value = "";
userDictionariesSelect.value = currentDictionary.externalID;
}
}
}
getDictionariesRequest.send();
}
}
function GetTextFile(filename) {

View File

@ -1,10 +1,12 @@
<?php
require_once("../required.php");
// require_once("../required.php");
require_once('config.php');
require_once(SITE_LOCATION . '/php/functions.php');
session_start();
if ($_GET['action'] == 'getall') {
Get_Dictionaries();
Get_Dictionaries(true);
}
elseif ($_GET['action'] == 'load') {
Load_Current_Dictionary();
@ -16,10 +18,13 @@ elseif ($_GET['action'] == 'update') {
Update_Current_Dictionary();
}
elseif ($_GET['action'] == 'switch') {
Switch_Current_Dictionary();
Switch_Current_Dictionary($_POST['newdictionaryid'], true);
}
elseif ($_GET['action'] == 'delete') {
Switch_Current_Dictionary($_POST['deletedictionaryid']);
}
function Get_Dictionaries() {
function Get_Dictionaries($return_list = true) {
if (isset($_SESSION['user'])) {
if ($_SESSION['user'] > 0) {
$query = "SELECT `id`, `name` FROM `dictionaries` WHERE `user`=" . $_SESSION['user'] . " ORDER BY `name` ASC;";
@ -27,6 +32,7 @@ function Get_Dictionaries() {
if ($dictionaries) {
if (num_rows($dictionaries) > 0) {
if ($return_list) {
$list = "";
$_SESSION['dictionaries'] = [];
while ($dict = fetch($dictionaries)) {
@ -35,6 +41,7 @@ function Get_Dictionaries() {
$list .= $dict['id'] . '_IDNAMESEPARATOR_' . $dict['name'] . '_DICTIONARYSEPARATOR_';
}
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) {
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;
}*/
?>

View File

@ -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);