From 421e1b5e5e05dc6a5aa9cd246fd663e35b967db3 Mon Sep 17 00:00:00 2001 From: Sylvain Jermini Date: Mon, 20 Aug 2018 20:05:55 +0200 Subject: [PATCH] #499 add organization level template editing --- .../api/admin/ResourceController.java | 20 ++++++- .../java/alfio/model/ContentLanguage.java | 1 + src/main/resources/alfio/templates/invoice.ms | 9 --- .../partials/configuration/organization.html | 3 + .../feature/resources/edit/resources-edit.js | 55 ++++++++++++++++--- .../resources/show/resources-show.html | 5 +- .../feature/resources/show/resources-show.js | 4 +- .../js/admin/ng-app/admin-application.js | 9 +++ .../js/admin/service/resource-service.js | 15 ++++- 9 files changed, 99 insertions(+), 22 deletions(-) diff --git a/src/main/java/alfio/controller/api/admin/ResourceController.java b/src/main/java/alfio/controller/api/admin/ResourceController.java index 7d9917c5d8..eafdec15cf 100644 --- a/src/main/java/alfio/controller/api/admin/ResourceController.java +++ b/src/main/java/alfio/controller/api/admin/ResourceController.java @@ -36,7 +36,9 @@ import alfio.manager.FileUploadManager; import alfio.manager.UploadedResourceManager; import alfio.manager.user.UserManager; +import alfio.model.ContentLanguage; import alfio.model.Event; +import alfio.model.PriceContainer; import alfio.model.UploadedResource; import alfio.model.modification.UploadBase64FileModification; import alfio.model.user.Organization; @@ -59,8 +61,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.math.BigDecimal; import java.nio.charset.StandardCharsets; import java.security.Principal; +import java.time.ZonedDateTime; import java.util.List; import java.util.Locale; import java.util.Map; @@ -140,9 +144,19 @@ public void previewTemplate(@PathVariable("name") TemplateResource name, @PathVa Locale loc = Locale.forLanguageTag(locale); - if(organizationId != null && eventId != null) { - checkAccess(organizationId, eventId, principal); - Event event = eventRepository.findById(eventId); + if (organizationId != null) { + Event event; + if (eventId!= null) { + checkAccess(organizationId, eventId, principal); + event = eventRepository.findById(eventId); + } else { + checkAccess(organizationId, principal); + event = new Event(-1, Event.EventType.INTERNAL, "TEST", "TEST", "TEST", "0", "0", ZonedDateTime.now(), + ZonedDateTime.now(), "Europe/Zurich", "http://localhost", "http://localhost", null, + "http://localhost", null, null, "CHF", BigDecimal.TEN, null, "42", organizationId, + ContentLanguage.ALL_LANGUAGES_IDENTIFIER, 0, PriceContainer.VatStatus.NONE, "1", Event.Status.PUBLIC); + } + Organization organization = organizationRepository.getById(organizationId); Optional image = TemplateProcessor.extractImageModel(event, fileUploadManager); Map model = name.prepareSampleModel(organization, event, image); diff --git a/src/main/java/alfio/model/ContentLanguage.java b/src/main/java/alfio/model/ContentLanguage.java index fd7f437696..f3d180de48 100644 --- a/src/main/java/alfio/model/ContentLanguage.java +++ b/src/main/java/alfio/model/ContentLanguage.java @@ -28,6 +28,7 @@ public class ContentLanguage { public static final int ENGLISH_IDENTIFIER = 0b00010; + public static final int ALL_LANGUAGES_IDENTIFIER = 0b00001 | ENGLISH_IDENTIFIER | 0b00100 | 0b01000 | 0b10000; public static final ContentLanguage ITALIAN = new ContentLanguage(Locale.ITALIAN, 0b00001, Locale.ITALIAN, "it"); public static final ContentLanguage ENGLISH = new ContentLanguage(Locale.ENGLISH, ENGLISH_IDENTIFIER, Locale.ENGLISH, "gb"); private static final ContentLanguage GERMAN = new ContentLanguage(Locale.GERMAN, 0b00100, Locale.GERMAN, "de"); diff --git a/src/main/resources/alfio/templates/invoice.ms b/src/main/resources/alfio/templates/invoice.ms index d43c35039d..c37d28ab8f 100644 --- a/src/main/resources/alfio/templates/invoice.ms +++ b/src/main/resources/alfio/templates/invoice.ms @@ -8,11 +8,6 @@ src: url('alfio-internal:/DejaVuSans.ttf'); } - @font-face { - font-family: 'DejaVu Sans Bold'; - src: url('alfio-internal:/DejaVuSans-Bold.ttf'); - } - body { font-family: "DejaVu Sans"; font-size:10pt; @@ -85,10 +80,6 @@ min-height: 25px; } - .strong, strong { - font-family: "DejaVu Sans Bold" - } - .mb { margin-bottom: 3em; } diff --git a/src/main/webapp/resources/angular-templates/admin/partials/configuration/organization.html b/src/main/webapp/resources/angular-templates/admin/partials/configuration/organization.html index 4cc5000f84..e5b26cdf13 100644 --- a/src/main/webapp/resources/angular-templates/admin/partials/configuration/organization.html +++ b/src/main/webapp/resources/angular-templates/admin/partials/configuration/organization.html @@ -234,5 +234,8 @@

Extensions

+ + + diff --git a/src/main/webapp/resources/js/admin/feature/resources/edit/resources-edit.js b/src/main/webapp/resources/js/admin/feature/resources/edit/resources-edit.js index 584f7c0ae7..179713ca25 100644 --- a/src/main/webapp/resources/js/admin/feature/resources/edit/resources-edit.js +++ b/src/main/webapp/resources/js/admin/feature/resources/edit/resources-edit.js @@ -4,6 +4,8 @@ angular.module('adminApplication').component('resourcesEdit', { bindings: { event:'<', + forOrganization: '<', + organizationId: '<', resourceName: '<' }, controller: ResourcesEditCtrl, @@ -23,6 +25,10 @@ function ResourcesEditCtrl(ResourceService, EventService, $q) { loadAll() }; + function getOrgId() { + return ctrl.forOrganization ? ctrl.organizationId : ctrl.event.organizationId; + } + ctrl.initLoadListener = function(locale) { var key = locale.locale; return function(editor) { @@ -39,6 +45,7 @@ function ResourcesEditCtrl(ResourceService, EventService, $q) { } }); editor.setValue(ctrl.resources[key], 0); + ctrl.editors[key] = editor; } }; @@ -62,7 +69,7 @@ function ResourcesEditCtrl(ResourceService, EventService, $q) { function previewFor(locale) { delete ctrl.error; var newText = ctrl.resources[locale]; - ResourceService.preview(ctrl.event.organizationId, ctrl.event.id, ctrl.resourceName, locale, {fileAsString: newText}).then(function(res) { + ResourceService.preview(getOrgId(), ctrl.forOrganization ? undefined : ctrl.event.id, ctrl.resourceName, locale, {fileAsString: newText}).then(function(res) { if(!res.download) { ctrl.previewedText = res.text; ctrl.previewMode = true; @@ -75,18 +82,31 @@ function ResourcesEditCtrl(ResourceService, EventService, $q) { delete ctrl.error; ctrl.previewMode = false; var newText = ctrl.resources[locale]; - ResourceService.uploadFile(ctrl.event.organizationId, ctrl.event.id, {fileAsString: newText, name: getFileName(locale), type: 'text/plain'}).then(loadAll, errorHandler); + var saver; + if(ctrl.forOrganization) { + saver = ResourceService.uploadOrganizationFile(getOrgId(), {fileAsString: newText, name: getFileName(locale), type: 'text/plain'}); + } else { + saver = ResourceService.uploadFile(ctrl.event.organizationId, ctrl.event.id, {fileAsString: newText, name: getFileName(locale), type: 'text/plain'}); + } + saver.then(loadAll, errorHandler); } function deleteFor(locale) { delete ctrl.error; - ctrl.previewMode = false - ResourceService.deleteFile(ctrl.event.organizationId, ctrl.event.id, getFileName(locale)).then(loadAll, errorHandler); + ctrl.previewMode = false; + var deleter; + if(ctrl.forOrganization) { + deleter = ResourceService.deleteOrganizationFile(getOrgId(), getFileName(locale)); + } else { + deleter = ResourceService.deleteFile(ctrl.event.organizationId, ctrl.event.id, getFileName(locale)); + } + deleter.then(loadAll, errorHandler); } function resetFor(locale) { ctrl.previewMode = false; ctrl.resources[locale] = ctrl.originalResources[locale] || ctrl.templateBodies[locale]; + ctrl.editors[locale].setValue(ctrl.resources[locale], 0); } function loadAll() { @@ -94,8 +114,16 @@ function ResourcesEditCtrl(ResourceService, EventService, $q) { ctrl.resources = {}; ctrl.resourcesMetadata = {}; ctrl.originalResources = {}; + ctrl.editors = {}; - EventService.getSelectedLanguages(ctrl.event.shortName).then(function(lang) { + var languageLoader; + if(ctrl.forOrganization) { + languageLoader = EventService.getAllLanguages(); + } else { + languageLoader = EventService.getSelectedLanguages(ctrl.event.shortName); + } + + languageLoader.then(function(lang) { ctrl.locales = lang.data; return lang.data; }).then(function(selectedLang) { @@ -108,9 +136,22 @@ function ResourcesEditCtrl(ResourceService, EventService, $q) { return res.data; }); - ResourceService.getMetadataForEventResource(ctrl.event.organizationId, ctrl.event.id, getFileName(locale)).then(function(res) { + var metadataLoader; + if(ctrl.forOrganization) { + metadataLoader = ResourceService.getMetadataForOrganizationResource(getOrgId(), getFileName(locale)); + } else { + metadataLoader = ResourceService.getMetadataForEventResource(getOrgId(), ctrl.event.id, getFileName(locale)) + } + + metadataLoader.then(function(res) { ctrl.resourcesMetadata[locale] = res.data; - ResourceService.getEventResource(ctrl.event.organizationId, ctrl.event.id, getFileName(locale)).then(function(resource) { + var resourceLoader; + if(ctrl.forOrganization) { + resourceLoader = ResourceService.getOrganizationResource(getOrgId(), getFileName(locale)); + } else { + resourceLoader = ResourceService.getEventResource(getOrgId(), ctrl.event.id, getFileName(locale)); + } + resourceLoader.then(function(resource) { ctrl.resources[locale] = resource.data; ctrl.originalResources[locale] = resource.data; }) diff --git a/src/main/webapp/resources/js/admin/feature/resources/show/resources-show.html b/src/main/webapp/resources/js/admin/feature/resources/show/resources-show.html index d3c6af7e62..5f6e043422 100644 --- a/src/main/webapp/resources/js/admin/feature/resources/show/resources-show.html +++ b/src/main/webapp/resources/js/admin/feature/resources/show/resources-show.html @@ -7,5 +7,8 @@

This is an advanced configuration. Please be c \ No newline at end of file diff --git a/src/main/webapp/resources/js/admin/feature/resources/show/resources-show.js b/src/main/webapp/resources/js/admin/feature/resources/show/resources-show.js index 9460ea815d..74d4e75c11 100644 --- a/src/main/webapp/resources/js/admin/feature/resources/show/resources-show.js +++ b/src/main/webapp/resources/js/admin/feature/resources/show/resources-show.js @@ -3,7 +3,9 @@ angular.module('adminApplication').component('resourcesShow', { bindings: { - event:'<' + event:'<', + forOrganization:'<', + organizationId: '<' }, controller: ResourcesShowCtrl, templateUrl: '../resources/js/admin/feature/resources/show/resources-show.html' diff --git a/src/main/webapp/resources/js/admin/ng-app/admin-application.js b/src/main/webapp/resources/js/admin/ng-app/admin-application.js index b07d005a0e..20bd073f08 100644 --- a/src/main/webapp/resources/js/admin/ng-app/admin-application.js +++ b/src/main/webapp/resources/js/admin/ng-app/admin-application.js @@ -49,6 +49,15 @@ } } }) + .state('organization-edit-resources', { + url: '/organizations/:organizationId/show-resources/:resourceName/', + template: '', + controller: function($state) { + this.organizationId = $state.params.organizationId; + this.resourceName = $state.params.resourceName; + }, + controllerAs: 'ctrl' + }) .state('users', { url: "/users/", template: "" diff --git a/src/main/webapp/resources/js/admin/service/resource-service.js b/src/main/webapp/resources/js/admin/service/resource-service.js index 4c376a62c9..5759e128d9 100644 --- a/src/main/webapp/resources/js/admin/service/resource-service.js +++ b/src/main/webapp/resources/js/admin/service/resource-service.js @@ -10,6 +10,19 @@ angular.module('adminApplication').service('ResourceService', function($http, $q getTemplateBody: function(name, locale) { return $http.get('/admin/api/overridable-template/'+name+'/'+locale); }, + getMetadataForOrganizationResource: function(orgId, name) { + return $http.get('/admin/api/resource-organization/'+orgId+'/'+name+'/metadata'); + }, + getOrganizationResource: function(orgId, name) { + return $http.get('/admin/api/resource-organization/'+orgId+'/'+name); + }, + uploadOrganizationFile: function(orgId, file) { + return $http.post('/admin/api/resource-organization/'+orgId+'/', file); + }, + deleteOrganizationFile: function(orgId, name) { + return $http.delete('/admin/api/resource-organization/'+orgId+'/'+name); + }, + /**/ getMetadataForEventResource: function(orgId, eventId, name) { return $http.get('/admin/api/resource-event/'+orgId+'/'+eventId+'/'+name+'/metadata'); }, @@ -24,7 +37,7 @@ angular.module('adminApplication').service('ResourceService', function($http, $q }, preview: function(orgId, eventId, name, locale, file) { - return $http.post('/admin/api/overridable-template/'+name+'/'+locale+'/preview?organizationId='+orgId+"&eventId="+eventId, file, {responseType: 'blob'}).then(function(res) { + return $http.post('/admin/api/overridable-template/'+name+'/'+locale+'/preview?organizationId='+orgId+(eventId !== undefined ? '&eventId='+eventId : ''), file, {responseType: 'blob'}).then(function(res) { var contentType = res.headers('Content-Type'); if(contentType.startsWith('text/plain')) {