Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String duplicateForm(@RequestParam(name = "id") Long id, Authentication a
if (baseItem == null)
throw new ResourceNotFoundException();

var item = baseItem.duplicate();
var item = baseItem.duplicate();

item.setAuthor(user.getUser());
item.setAuthorDisplay(user.getName());
Expand All @@ -73,14 +73,29 @@ public String editForm(@RequestParam(name = "id") Long id, Model model) {
}

@PostMapping("/admin/edit")
public String doEdit(@RequestParam(required = false) String publish, @Valid Item item, BindingResult bindingResult, Model model) {
public String doEdit(Authentication auth, @RequestParam(required = false) String publish, @Valid Item item, BindingResult bindingResult, Model model) {
model.addAttribute("now", LocalDateTime.now().format(formatter));
model.addAttribute("formatter", formatter);

// Check for form errors
if (bindingResult.hasErrors())
return "edit";

Item originalItem = null;
if (item.getId() != null) originalItem = itemRepository.findById(item.getId()).get();

var user = (DAuthUserDetails) auth.getPrincipal();
var mandates = user.getMandates();
if (originalItem != null && !mandates.containsKey(item.getPublishAs())) {
item.setPublishAs(originalItem.getPublishAs());
item.setPublishAsDisplay(originalItem.getPublishAsDisplay());
}
else if (item.getPublishAs() != null && !item.getPublishAs().isEmpty()) {
String mandateDisplay = mandates.get(item.getPublishAs());
if (mandateDisplay != null)
item.setPublishAsDisplay(mandateDisplay);
}

// If the special publish param (name of a submit button) is present, we publish
if (publish != null)
item.setPublishDate(LocalDateTime.now());
Expand Down
11 changes: 9 additions & 2 deletions src/main/resources/templates/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ <h3 style="margin-top: 0;">Metadata</h3>
</div>

<div th:if="${#authentication.getPrincipal().getMandates().size() > 0}">
<select id="publishAs" class="form-select form-control" name="publishAs">
<select id="publishAs"
class="form-select form-control"
name="publishAs"
th:disabled="${!#authentication.getPrincipal().getMandates().containsKey(item.publishAs) && item.id > 0}">
<option th:if="${!#authentication.getPrincipal().getMandates().containsKey(item.publishAs)}"
th:value="${item.publishAs}"
th:text="${item.publishAsDisplay}"
th:selected="true"></option>
<optgroup label="Dina positioner">
<option th:each="mandate : ${#authentication.getPrincipal().getMandates()}"
th:value="${mandate.getKey()}"
Expand Down Expand Up @@ -352,4 +359,4 @@ <h3>Event</h3>
</div>

</body>
</html>
</html>