Skip to content

Commit

Permalink
#499 add organization level template editing
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Aug 20, 2018
1 parent 11b90ac commit 421e1b5
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 22 deletions.
20 changes: 17 additions & 3 deletions src/main/java/alfio/controller/api/admin/ResourceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<TemplateResource.ImageData> image = TemplateProcessor.extractImageModel(event, fileUploadManager);
Map<String, Object> model = name.prepareSampleModel(organization, event, image);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/alfio/model/ContentLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
9 changes: 0 additions & 9 deletions src/main/resources/alfio/templates/invoice.ms
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,10 +80,6 @@
min-height: 25px;
}

.strong, strong {
font-family: "DejaVu Sans Bold"
}

.mb {
margin-bottom: 3em;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,8 @@ <h2>Extensions</h2>
<uib-tab index="1" heading="Promo codes">
<promo-codes for-organization="true" organization-id="organizationConf.organization.id"></promo-codes>
</uib-tab>
<uib-tab index="2" heading="Templates">
<resources-show for-organization="true" organization-id="organizationConf.organization.id"></resources-show>
</uib-tab>
</uib-tabset>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
angular.module('adminApplication').component('resourcesEdit', {
bindings: {
event:'<',
forOrganization: '<',
organizationId: '<',
resourceName: '<'
},
controller: ResourcesEditCtrl,
Expand All @@ -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) {
Expand All @@ -39,6 +45,7 @@ function ResourcesEditCtrl(ResourceService, EventService, $q) {
}
});
editor.setValue(ctrl.resources[key], 0);
ctrl.editors[key] = editor;
}
};

Expand All @@ -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;
Expand All @@ -75,27 +82,48 @@ 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() {
ctrl.templateBodies = {};
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) {
Expand All @@ -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;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ <h4><i class="fa fa-warning"></i> This is an advanced configuration. Please be c
</div>

<ul>
<li ng-repeat="templateName in $ctrl.availableTemplates"><a ui-sref="events.single.edit-resources({event: $ctrl.event.shortName, resourceName: templateName})">{{templateName}}</a></li>
<li ng-repeat="templateName in $ctrl.availableTemplates">
<a ng-if="!$ctrl.forOrganization" ui-sref="events.single.edit-resources({event: $ctrl.event.shortName, resourceName: templateName})">{{templateName}}</a>
<a ng-if="$ctrl.forOrganization" target="_blank" ui-sref="organization-edit-resources({organizationId: $ctrl.organizationId, resourceName: templateName})">{{templateName}}</a>
</li>
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
}
}
})
.state('organization-edit-resources', {
url: '/organizations/:organizationId/show-resources/:resourceName/',
template: '<resources-edit for-organization="true" organization-id="ctrl.organizationId" resource-name="ctrl.resourceName"></resources-show>',
controller: function($state) {
this.organizationId = $state.params.organizationId;
this.resourceName = $state.params.resourceName;
},
controllerAs: 'ctrl'
})
.state('users', {
url: "/users/",
template: "<users data-title='Users' type='user'></users>"
Expand Down
15 changes: 14 additions & 1 deletion src/main/webapp/resources/js/admin/service/resource-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
},
Expand All @@ -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')) {
Expand Down

0 comments on commit 421e1b5

Please sign in to comment.