diff --git a/server/router/projects/index.js b/server/router/projects/index.js index 2888129..943d2ac 100644 --- a/server/router/projects/index.js +++ b/server/router/projects/index.js @@ -926,6 +926,200 @@ module.exports = function (options) { }) + router.route('/:id/sheets') + .post(function(request, response, next) { + db.Project.findByPk(request.params.id, { + include: [ + { + association: db.Project.Owner, + include: [ + { + association: db.Owner.OwnerEntities, + include: [ + { + association: db.OwnerEntity.Entity, + include: [ + { + association: db.Entity.EntityUsers, + required: false, + where: { + userId: request.user?.id ?? null + } + } + ] + } + ] + } + ] + } + ] + }).then(async (project) => { + if (project) { + db.TranslationChapter.findAll({ + include: [ + { + association: db.TranslationChapter.TranslationChunks, + include: [ + { + association: db.TranslationChunk.Translation + } + ] + }, + { + association: db.TranslationChapter.TranslationArtifactVersion, + required: true, + include: [ + { + association: db.TranslationArtifactVersion.ProjectTranslationArtifactVersions, + required: true, + where: { + projectId: request.params.id + }, + }, + { + association: db.TranslationArtifactVersion.ArtifactVersion, + include: [ + { + association: db.ArtifactVersion.ProjectArtifactVersions + }, + { + association: db.ArtifactVersion.Artifact, + include: [ + { + association: db.Artifact.Dialect + } + ] + } + ] + }, + { + association: db.TranslationArtifactVersion.Dialect + }, + ] + } + ] + }).then(translationChapters => { + const workbook = new XL.Workbook(); + const cellWidth = 45 + const cellHeight = 25 + + translationChapters.sort((a, b) => a.index - b.index).forEach((translationChapter, index) => { + const worksheet_name = `${index+1}-${translationChapter.translationArtifactVersion.dialect.countryId}-${translationChapter.translationArtifactVersion.dialect.languageId} - ${translationArtifactVersion.artifactVersion.artifact.name}`.substring(0, 30); + + // Don't have [ ] * ? : / \ + const worksheet = workbook.addWorksheet(worksheet_name.replace(/[`*?:'\[\]\\\/]/gi, "")) + + worksheet.cell(2,2,2,3,true) + .string(translationChapter.translationArtifactVersion.artifactVersion.artifact.name) + .style({ + font: { + bold: true, + }, + alignment: { + horizontal: 'center', + wrapText: true + } + }) + + worksheet.cell(4,2) + .string(translationChapter.translationArtifactVersion.artifactVersion.artifact.dialect.name) + .style({ + font: { + bold: true, + }, + alignment: { + horizontal: 'center', + wrapText: true + } + }) + + worksheet.cell(4,3) + .string(translationChapter.translationArtifactVersion.dialect.name) + .style({ + font: { + bold: true, + }, + alignment: { + horizontal: 'center', + wrapText: true + } + }) + // Meta data for the translation and artifact + worksheet.cell(4,4) + .string(translationChapter.translationArtifactVersion.artifactVersion.id) + worksheet.cell(4,5) + .string(translationChapter.translationArtifactVersion.id) + + var cellIndex = 6 + const translationChunks = translationChapter.translationChunks.sort((a,b) => a.index - b.index) + for (const j in translationChunks) { + const translationChunk = translationChunks[j] + + worksheet.cell(cellIndex,2) + .string(translationChunk.text) + .style({ + alignment: { + wrapText: true, + vertical: 'justify' + } + }) + + const translationCell = worksheet.cell(cellIndex,3) + .style({ + alignment: { + wrapText: true, + vertical: 'justify' + } + }) + + if (translationChunk.translation) { + translationCell.string(translationChunk.translation.text) + } + + const textLength = translationChunk.text.length + worksheet.row(cellIndex).setHeight( + Math.ceil(Math.max(textLength/cellWidth, 1)*cellHeight) + ) + + // Meta data... + // ID for translationChunk + worksheet.cell(cellIndex,4) + .string(translationChunk.id) + // ID for translation + worksheet.cell(cellIndex,5) + .string(translationChunk.translation?.id ?? "") + + cellIndex++ + } + + + // Format Columns + worksheet.column(2).setWidth(cellWidth); + worksheet.column(3).setWidth(cellWidth); + worksheet.column(4).hide(); + worksheet.column(5).hide(); + + // Format Rows... + worksheet.row(5).freeze() + worksheet.row(1).setHeight(5) + worksheet.row(3).setHeight(5) + worksheet.row(5).setHeight(5) + }) + if (translationChapters.length > 0) { + workbook.write(`${project.name}.xlsx`, response); + } else { + response.redirect(`/projects/${request.params.id}`) + } + }).catch(error => { + next(error) + }) + } else { + next() + } + }) + + }) + const getProjectArtifactVersion = (userId, project, artifactVersion) => { if (project.projectArtifactVersions.length == 0) { return db.ProjectArtifactVersion.create({ diff --git a/server/views/project.pug b/server/views/project.pug index 1202477..fee40b0 100644 --- a/server/views/project.pug +++ b/server/views/project.pug @@ -19,7 +19,7 @@ block content a.btn.black(href=`/projects/${project.id}/campaign`) funding campaign .row .col.s12 - form(action=`/projects/${project.id}/spreadsheet`, method="post") + form(action=`/projects/${project.id}/sheets`, method="post") button.btn.black(type="submit") export to spreadsheets .divider