Commit 48e8c6ba authored by Curtis Adam's avatar Curtis Adam

Add Mnemosyne button dummy

parent ba17c97f
......@@ -81,6 +81,7 @@
"upload-form": {
"popover-content": "Unterstützt werden die Dateiformate JSON und CSV (Excel, kommagetrennt). Die folgenden Links zeigen die erforderliche Dateistruktur:",
"upload-title": "Karten importieren",
"upload-card-title-placeholder": "Mnemosyne Import",
"upload-type": "Dateityp",
"upload-type1": ".cards",
"upload-type2": "Mnemosyne",
......
......@@ -89,6 +89,7 @@
"upload-form": {
"popover-content": "Here you can upload your own cards to the cardset. Currently the formats JSON and CSV (Excel, comma-separated) are supported. Please have a look at these templates:",
"upload-title": "Import cards",
"upload-card-title-placeholder": "Mnemosyne Import",
"upload-type": "File type",
"upload-type1": ".cards",
"upload-type2": "Mnemosyne",
......
......@@ -4,99 +4,118 @@ import {Cards} from "./cards.js";
import {check} from "meteor/check";
Meteor.methods({
parseUpload: function (data, cardset_id) {
parseUpload: function (data, cardset_id, importType) {
check(cardset_id, String);
check(importType, Number);
var cardset = Cardsets.findOne(cardset_id);
if (!Meteor.userId() || cardset.owner !== Meteor.userId() || Roles.userIsInRole(this.userId, ["firstLogin", "blocked"])) {
throw new Meteor.Error("not-authorized");
}
for (let i = 0; i < data.length; i++) {
let item = data[i];
if (importType === 1) {
for (let i = 0; i < data.length; i++) {
let item = data[i];
if (item.front === undefined || item.front === "") {
throw new Meteor.Error("entry number: " + i, "front missing");
} else if (item.back === undefined || item.back === "") {
throw new Meteor.Error("entry number: " + i, "back missing");
}
if (item.subject === undefined) {
item.subject = "Kein Titel";
}
if (item.hint === undefined) {
item.hint = "";
}
if (item.difficulty === undefined) {
item.difficulty = Number(1);
}
if (item.cardType === undefined) {
item.cardType = Number(0);
}
if (item.lecture === undefined) {
item.lecture = "";
}
if (item.learningGoalLevel === undefined) {
item.learningGoalLevel = 0;
}
if (item.backgroundStyle === undefined) {
item.backgroundStyle = 0;
}
if (item.centerTextElement === undefined) {
if (item.cardType === 1) {
item.centerTextElement = [true, true, false, false];
} else {
item.centerTextElement = [false, false, false, false];
if (item.front === undefined || item.front === "") {
throw new Meteor.Error("entry number: " + i, "front missing");
} else if (item.back === undefined || item.back === "") {
throw new Meteor.Error("entry number: " + i, "back missing");
}
if (item.subject === undefined) {
item.subject = "Kein Titel";
}
if (item.hint === undefined) {
item.hint = "";
}
if (item.difficulty === undefined) {
item.difficulty = Number(0);
}
if (item.cardType === undefined) {
item.cardType = Number(0);
}
if (item.lecture === undefined) {
item.lecture = "";
}
if (item.learningGoalLevel === undefined) {
item.learningGoalLevel = 0;
}
if (item.backgroundStyle === undefined) {
item.backgroundStyle = 0;
}
if (item.centerTextElement === undefined) {
if (item.cardType === 1) {
item.centerTextElement = [true, true, false, false];
} else {
item.centerTextElement = [false, false, false, false];
}
}
}
if (item.date === undefined) {
item.date = new Date();
}
if (item.learningUnit === undefined) {
item.learningUnit = "";
if (item.date === undefined) {
item.date = new Date();
}
}
}
for (let i = 0; i < data.length; i++) {
let item = data[i];
for (let i = 0; i < data.length; i++) {
let item = data[i];
let subject, front, back, hint, cardType, lecture;
try {
// If the string is UTF-8, this will work and not throw an error.
subject = decodeURIComponent(encodeURIComponent(item.subject));
front = decodeURIComponent(encodeURIComponent(item.front));
back = decodeURIComponent(encodeURIComponent(item.back));
hint = decodeURIComponent(encodeURIComponent(item.hint));
lecture = decodeURIComponent(encodeURIComponent(item.lecture));
} catch (e) {
// If it isn't, an error will be thrown, and we can assume that we have an ISO string.
subject = item.subject;
front = item.front;
back = item.back;
hint = item.hint;
cardType = item.cardType;
lecture = item.lecture;
}
let subject, front, back, hint, cardType, lecture;
try {
// If the string is UTF-8, this will work and not throw an error.
subject = decodeURIComponent(encodeURIComponent(item.subject));
front = decodeURIComponent(encodeURIComponent(item.front));
back = decodeURIComponent(encodeURIComponent(item.back));
hint = decodeURIComponent(encodeURIComponent(item.hint));
lecture = decodeURIComponent(encodeURIComponent(item.lecture));
} catch (e) {
// If it isn't, an error will be thrown, and we can assume that we have an ISO string.
subject = item.subject;
front = item.front;
back = item.back;
hint = item.hint;
cardType = item.cardType;
lecture = item.lecture;
}
if (item.front !== "") {
let hlcodeReplacement = "\n```\n";
let regex = /<hlcode>|<\/hlcode>/g;
front = front.replace(regex, hlcodeReplacement);
back = back.replace(regex, hlcodeReplacement);
if (item.front !== "") {
let hlcodeReplacement = "\n```\n";
let regex = /<hlcode>|<\/hlcode>/g;
front = front.replace(regex, hlcodeReplacement);
back = back.replace(regex, hlcodeReplacement);
Cards.insert({
subject: subject,
difficulty: item.difficulty,
front: front,
back: back,
hint: hint,
cardset_id: cardset_id,
cardGroup: -1,
cardType: item.cardType,
lecture: lecture,
centerTextElement: item.centerTextElement,
learningGoalLevel: item.learningGoalLevel,
backgroundStyle: item.backgroundStyle,
learningUnit: item.learningUnit
});
}
}
} else {
for (let i = 0; i < data.length; i++) {
let item = data[i];
console.log(item[0]);
Cards.insert({
subject: subject,
difficulty: item.difficulty,
front: front,
back: back,
hint: hint,
subject: TAPi18n.__('upload-card-title-placeholder'),
difficulty: 0,
front: item[0],
back: item[1],
hint: "",
cardset_id: cardset_id,
cardGroup: -1,
cardType: item.cardType,
lecture: lecture,
centerTextElement: item.centerTextElement,
learningGoalLevel: item.learningGoalLevel,
backgroundStyle: item.backgroundStyle,
learningUnit: item.learningUnit
cardType: 2,
lecture: "",
centerTextElement: [true, true, false, false],
learningGoalLevel: 0,
backgroundStyle: 0
});
}
}
......
......@@ -1103,49 +1103,75 @@ Template.cardsetImportForm.events({
},
'change [name="uploadFile"]': function (evt, tmpl) {
tmpl.uploading.set(true);
var cardset_id = Template.parentData(1)._id;
if (evt.target.files[0].name.match(/\.(json)$/)) {
var reader = new FileReader();
reader.onload = function () {
try {
var res = $.parseJSON('[' + this.result + ']');
Meteor.call('parseUpload', res, cardset_id, function (error) {
if (error) {
tmpl.uploading.set(false);
$('#uploadError').html('<br><div class="alert alert-danger" role="alert">' + TAPi18n.__('upload-form.wrong-template') + ": " + error.message + '</div>');
} else {
tmpl.uploading.set(false);
Bert.alert(TAPi18n.__('upload-form.success'), 'success', 'growl-top-left');
$('#importModal').modal('toggle');
}
});
} catch (e) {
tmpl.uploading.set(false);
$('#uploadError').html('<br><div class="alert alert-danger" role="alert">' + TAPi18n.__('upload-form.wrong-template') + ": " + e.message + '</div>');
}
};
reader.readAsText(evt.target.files[0]);
} else if (evt.target.files[0].name.match(/\.(csv)$/)) {
Papa.parse(evt.target.files[0], {
header: true,
complete: function (results) {
Meteor.call('parseUpload', results.data, cardset_id, function (error) {
if (error) {
tmpl.uploading.set(false);
Bert.alert(TAPi18n.__('upload-form.wrong-template'), 'danger', 'growl-top-left');
} else {
tmpl.uploading.set(false);
Bert.alert(TAPi18n.__('upload-form.success'), 'success', 'growl-top-left');
$('#importModal').modal('toggle');
}
});
}
});
let cardset_id = Template.parentData(1)._id;
let importType = Session.get('importType');
if (importType === 1) {
if (evt.target.files[0].name.match(/\.(json)$/)) {
var reader = new FileReader();
reader.onload = function () {
try {
var res = $.parseJSON('[' + this.result + ']');
Meteor.call('parseUpload', res, cardset_id, Number(importType), function (error) {
if (error) {
tmpl.uploading.set(false);
$('#uploadError').html('<br><div class="alert alert-danger" role="alert">' + TAPi18n.__('upload-form.wrong-template') + ": " + error.message + '</div>');
} else {
tmpl.uploading.set(false);
Bert.alert(TAPi18n.__('upload-form.success'), 'success', 'growl-top-left');
$('#importModal').modal('toggle');
}
});
} catch (e) {
tmpl.uploading.set(false);
$('#uploadError').html('<br><div class="alert alert-danger" role="alert">' + TAPi18n.__('upload-form.wrong-template') + ": " + e.message + '</div>');
}
};
reader.readAsText(evt.target.files[0]);
} else if (evt.target.files[0].name.match(/\.(csv)$/)) {
Papa.parse(evt.target.files[0], {
header: true,
complete: function (results) {
Meteor.call('parseUpload', results.data, cardset_id, Number(importType), function (error) {
if (error) {
tmpl.uploading.set(false);
Bert.alert(TAPi18n.__('upload-form.wrong-template'), 'danger', 'growl-top-left');
} else {
tmpl.uploading.set(false);
Bert.alert(TAPi18n.__('upload-form.success'), 'success', 'growl-top-left');
$('#importModal').modal('toggle');
}
});
}
});
} else {
tmpl.uploading.set(false);
$('#uploadError').html('<br><div class="alert alert-danger" role="alert">' + TAPi18n.__('upload-form.wrong-file') + '</div>');
}
} else {
tmpl.uploading.set(false);
$('#uploadError').html('<br><div class="alert alert-danger" role="alert">' + TAPi18n.__('upload-form.wrong-file') + '</div>');
if (evt.target.files[0].name.match(/\.(txt)$/)) {
Papa.parse(evt.target.files[0], {
delimiter: "\t",
header: true,
newline: "\r\n",
complete: function (results) {
console.log(results);
Meteor.call('parseUpload', results.data, cardset_id, Number(importType), function (error) {
if (error) {
tmpl.uploading.set(false);
Bert.alert(TAPi18n.__('upload-form.wrong-template'), 'danger', 'growl-top-left');
} else {
tmpl.uploading.set(false);
Bert.alert(TAPi18n.__('upload-form.success'), 'success', 'growl-top-left');
$('#importModal').modal('toggle');
}
});
}
});
} else {
tmpl.uploading.set(false);
$('#uploadError').html('<br><div class="alert alert-danger" role="alert">' + TAPi18n.__('upload-form.wrong-file') + '</div>');
}
}
}
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment