Skip to content

Commit

Permalink
#213 custom email/pdf: initial work for preview
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Oct 28, 2016
1 parent 4e11f52 commit 3c36054
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
25 changes: 21 additions & 4 deletions src/main/java/alfio/controller/api/admin/ResourceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@

import alfio.manager.UploadedResourceManager;
import alfio.manager.user.UserManager;
import alfio.model.Event;
import alfio.model.UploadedResource;
import alfio.model.modification.UploadBase64FileModification;
import alfio.model.user.Organization;
import alfio.repository.EventRepository;
import alfio.repository.user.OrganizationRepository;
import alfio.util.TemplateManager;
import alfio.util.TemplateResource;
import org.apache.commons.lang3.Validate;
Expand Down Expand Up @@ -71,19 +74,22 @@ public class ResourceController {
private final EventRepository eventRepository;
private final MessageSource messageSource;
private final TemplateManager templateManager;
private final OrganizationRepository organizationRepository;


@Autowired
public ResourceController(UploadedResourceManager uploadedResourceManager,
UserManager userManager,
EventRepository eventRepository,
MessageSource messageSource,
TemplateManager templateManager) {
TemplateManager templateManager,
OrganizationRepository organizationRepository) {
this.uploadedResourceManager = uploadedResourceManager;
this.userManager = userManager;
this.eventRepository = eventRepository;
this.messageSource = messageSource;
this.templateManager = templateManager;
this.organizationRepository = organizationRepository;
}

@RequestMapping(value = "/overridable-template/", method = RequestMethod.GET)
Expand Down Expand Up @@ -111,13 +117,24 @@ public void previewTemplate(@PathVariable("name") TemplateResource name, @PathVa
@RequestParam(required = false, value = "eventId") Integer eventId,
@RequestBody UploadBase64FileModification template,
Principal principal,
HttpServletResponse response) {
HttpServletResponse response) throws IOException {


Locale loc = Locale.forLanguageTag(locale);

templateManager.renderString(template.getFileAsString(), null, loc, name.getTemplateOutput());

if(organizationId != null && eventId != null) {
checkAccess(organizationId, eventId, principal);
Event event = eventRepository.findById(eventId);
Organization organization = organizationRepository.getById(organizationId);
Map<String, Object> model = name.prepareSampleModel(organization, event);
String renderedTemplate = templateManager.renderString(template.getFileAsString(), model, loc, name.getTemplateOutput());
if("text/plain".equals(name.getRenderedContentType())) {
response.setContentType("text/plain");
response.getWriter().print(renderedTemplate);
} else if ("application/pdf".equals(name.getRenderedContentType())) {

}
}
}


Expand Down
69 changes: 63 additions & 6 deletions src/main/java/alfio/util/TemplateResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package alfio.util;

import alfio.controller.support.TemplateProcessor;
import alfio.manager.TicketReservationManager;
import alfio.manager.support.OrderSummary;
import alfio.manager.support.SummaryRow;
import alfio.model.*;
import alfio.model.modification.SendCodeModification;
import alfio.model.transaction.PaymentProxy;
Expand All @@ -31,21 +33,52 @@

public enum TemplateResource {
GOOGLE_ANALYTICS("/alfio/templates/google-analytics.ms", false, "text/plain", TemplateManager.TemplateOutput.TEXT),
CONFIRMATION_EMAIL_FOR_ORGANIZER("/alfio/templates/confirmation-email-for-organizer-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),

CONFIRMATION_EMAIL_FOR_ORGANIZER("/alfio/templates/confirmation-email-for-organizer-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT) {
@Override
public Map<String, Object> prepareSampleModel(Organization organization, Event event) {
return prepareSampleDataForConfirmationEmail(organization, event);
}
},
SEND_RESERVED_CODE("/alfio/templates/send-reserved-code-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
CONFIRMATION_EMAIL("/alfio/templates/confirmation-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
OFFLINE_RESERVATION_EXPIRED_EMAIL("/alfio/templates/offline-reservation-expired-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
REMINDER_EMAIL("/alfio/templates/reminder-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
CONFIRMATION_EMAIL("/alfio/templates/confirmation-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT) {
@Override
public Map<String, Object> prepareSampleModel(Organization organization, Event event) {
return prepareSampleDataForConfirmationEmail(organization, event);
}
},
OFFLINE_RESERVATION_EXPIRED_EMAIL("/alfio/templates/offline-reservation-expired-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT) {
@Override
public Map<String, Object> prepareSampleModel(Organization organization, Event event) {
return prepareSampleDataForConfirmationEmail(organization, event);
}
},
REMINDER_EMAIL("/alfio/templates/reminder-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT) {
@Override
public Map<String, Object> prepareSampleModel(Organization organization, Event event) {
return prepareSampleDataForConfirmationEmail(organization, event);
}
},
REMINDER_TICKET_ADDITIONAL_INFO("/alfio/templates/reminder-ticket-additional-info.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
REMINDER_TICKETS_ASSIGNMENT_EMAIL("/alfio/templates/reminder-tickets-assignment-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
REMINDER_TICKETS_ASSIGNMENT_EMAIL("/alfio/templates/reminder-tickets-assignment-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT) {
@Override
public Map<String, Object> prepareSampleModel(Organization organization, Event event) {
return prepareSampleDataForConfirmationEmail(organization, event);
}
},


TICKET_EMAIL("/alfio/templates/ticket-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
TICKET_HAS_CHANGED_OWNER("/alfio/templates/ticket-has-changed-owner-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),

TICKET_HAS_BEEN_CANCELLED("/alfio/templates/ticket-has-been-cancelled-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
TICKET_PDF("/alfio/templates/ticket.ms", true, "application/pdf", TemplateManager.TemplateOutput.HTML),
RECEIPT_PDF("/alfio/templates/receipt.ms", true, "application/pdf", TemplateManager.TemplateOutput.HTML),
RECEIPT_PDF("/alfio/templates/receipt.ms", true, "application/pdf", TemplateManager.TemplateOutput.HTML) {
@Override
public Map<String, Object> prepareSampleModel(Organization organization, Event event) {
return prepareSampleDataForConfirmationEmail(organization, event);
}
},

WAITING_QUEUE_JOINED("/alfio/templates/waiting-queue-joined.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT),
WAITING_QUEUE_RESERVATION_EMAIL("/alfio/templates/waiting-queue-reservation-email-txt.ms", true, "text/plain", TemplateManager.TemplateOutput.TEXT);
Expand Down Expand Up @@ -76,10 +109,34 @@ public String classPath() {
return classPathUrl;
}

public String getRenderedContentType() {
return renderedContentType;
}

public TemplateManager.TemplateOutput getTemplateOutput() {
return templateOutput;
}

public Map<String, Object> prepareSampleModel(Organization organization, Event event) {
return Collections.emptyMap();
}


private static Map<String, Object> prepareSampleDataForConfirmationEmail(Organization organization, Event event) {
TicketReservation reservation = new TicketReservation("597e7e7b-c514-4dcb-be8c-46cf7fe2c36e", new Date(), TicketReservation.TicketReservationStatus.COMPLETE,
"Firstname Lastname", "FirstName", "Lastname", "[email protected]", "billing address", ZonedDateTime.now(), ZonedDateTime.now(),
PaymentProxy.STRIPE, true, null, false, "en", false);
Optional<String> vat = Optional.of("VAT-NR");
List<Ticket> tickets = Collections.singletonList(new Ticket(0, "597e7e7b-c514-4dcb-be8c-46cf7fe2c36e", ZonedDateTime.now(), 0, "ACQUIRED", 0,
"597e7e7b-c514-4dcb-be8c-46cf7fe2c36e", "Firstname Lastname", "Firstname", "Lastname", "[email protected]", false, "en",
1000, 1000, 80, 0));
OrderSummary orderSummary = new OrderSummary(new TicketReservationManager.TotalPrice(1000, 80, 0, 0),
Collections.singletonList(new SummaryRow("Ticket", "10.00", 1, "10.00", 1000, SummaryRow.SummaryType.TICKET)), false, "10.00", "0.80", false, false);
String reservationUrl = "http://your-domain.tld/reservation-url/";
String reservationShortId = "597e7e7b";
return prepareModelForConfirmationEmail(organization, event, reservation, vat, tickets, orderSummary, reservationUrl, reservationShortId);
}

//used by multiple enum:
// - CONFIRMATION_EMAIL_FOR_ORGANIZER
// - OFFLINE_RESERVATION_EXPIRED_EMAIL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ <h2>Edit resource: {{$ctrl.resourceName}}</h2>
<div class="pull-right">
<button ng-if="$ctrl.resourcesMetadata[locale.locale]" type="button" class="btn btn-lg btn-warning" ng-click="$ctrl.deleteFor(locale.locale)">Delete</button>
<button type="button" ng-click="$ctrl.resetFor(locale.locale)" class="btn btn-lg">Reset</button>
<button type="button" ng-click="$ctrl.previewFor(locale.locale)" class="btn btn-lg">Preview</button>
<button type="submit" class="btn btn-lg btn-default">Save</button>
</div>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ function ResourcesEditCtrl(ResourceService, EventService) {
ctrl.saveFor = saveFor;
ctrl.deleteFor = deleteFor;
ctrl.resetFor = resetFor;
ctrl.previewFor = previewFor;

ctrl.$onInit = function() {
loadAll()
}

function previewFor(locale) {
var newText = ctrl.resources[locale];
ResourceService.preview(ctrl.event.organizationId, ctrl.event.id, ctrl.resourceName, locale, {fileAsString: newText}).then(function(res) {
console.log(res);
});
}

function saveFor(locale) {
var newText = ctrl.resources[locale];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ angular.module('adminApplication').service('ResourceService', function($http) {
},
deleteFile: function(orgId, eventId, name) {
return $http.delete('api/resource-event/'+orgId+'/'+eventId+'/'+name);
},
preview: function(orgId, eventId, name, locale, file) {
return $http.post('api/overridable-template/'+name+'/'+locale+'/preview?organizationId='+orgId+"&eventId="+eventId, file);
}
};
});
Expand Down

0 comments on commit 3c36054

Please sign in to comment.