
META-INF.resources.object_entries.object_entry.form.jsp Maven / Gradle / Ivy
The newest version!
<%--
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
--%>
<%@ include file="/init.jsp" %>
<%
ObjectEntryDisplayContext objectEntryDisplayContext = (ObjectEntryDisplayContext)request.getAttribute(WebKeys.PORTLET_DISPLAY_CONTEXT);
String backURL = objectEntryDisplayContext.getBackURL();
boolean defaultObjectLayout = objectEntryDisplayContext.getObjectLayoutTab() == null;
ObjectDefinition objectDefinition = objectEntryDisplayContext.getObjectDefinition1();
ObjectEntry objectEntry = objectEntryDisplayContext.getObjectEntry();
String portletNamespace = portletDisplay.getNamespace();
portletDisplay.setShowBackIcon(true);
portletDisplay.setURLBack(backURL);
%>
<%= objectEntryDisplayContext.renderDDMForm(pageContext) %>
<%@ include file="/object_entries/object_entry/categorization.jspf" %>
const hasObjectLayout =
<%= objectEntryDisplayContext.getObjectLayoutTab() != null %>;
function getExternalReferenceCode() {
return String(
'<%= (objectEntry == null) ? "" : objectEntry.getExternalReferenceCode() %>'
);
}
function getInputValues(element, selector) {
return Array.from(element.querySelectorAll(selector)).map(
(item) => item.value
);
}
function getPath(externalReferenceCode) {
const scope = '<%= objectDefinition.getScope() %>';
const contextPath = '/o<%= objectDefinition.getRESTContextPath() %>';
const pathScopedBySite = contextPath.concat(
`/scopes/\${themeDisplay.getSiteGroupId()}`
);
let path = scope === 'site' ? pathScopedBySite : contextPath;
if (!externalReferenceCode) {
return path;
}
return path.concat(
'/by-external-reference-code/',
`\${externalReferenceCode}`
);
}
function getValues(fields) {
return fields.reduce((obj, field) => {
if (field.readOnly) {
return obj;
}
let value = field.value;
if (
field.type === 'select' &&
!field.multiple &&
!field.localizedObjectField
) {
value = {key: value.length ? field.value[0] : ''};
}
let fieldName = field.fieldName;
if (value && field.localizable) {
fieldName += '_i18n';
if (typeof value == 'string') {
value = JSON.parse(value);
}
}
return Object.assign(obj, {[fieldName]: value});
}, {});
}
function hasEmptyString(object) {
return Object.values(object).some((value) => value === '');
}
function isPastDate(date) {
if (!date) {
return false;
}
const inputDateTime = new Date(date.replace(/Z$/, ''));
const languageId = Liferay.ThemeDisplay.getBCP47LanguageId();
const timeZone = Liferay.ThemeDisplay.getTimeZone();
const timeZoneDateTime = new Date(
new Date().toLocaleString(languageId, {timeZone})
);
return timeZoneDateTime >= inputDateTime;
}
Liferay.provide(window, ' submitObjectEntry', () => {
const form = document.getElementById(' fm');
const DDMFormInstance = Liferay.component('editObjectEntry');
const current = DDMFormInstance.reactComponentRef.current;
let loadingElement = form.querySelector('.loading-animation');
if (!loadingElement) {
loadingElement = document.createElement('span');
loadingElement.className =
'loading-animation loading-animation-secondary loading-animation-sm';
loadingElement.ariaHidden = 'true';
form.insertAdjacentElement('afterbegin', loadingElement);
}
current.validate().then((result) => {
if (result) {
const fields = current.getFields();
let shouldSubmitForm = true;
fields.forEach((field) => {
if (
field.displayStyle === 'singleline' &&
field.type === 'text' &&
field.value.length > 280
) {
shouldSubmitForm = false;
loadingElement.remove();
Liferay.Util.openToast({
message: Liferay.Util.sub(
' ',
'280',
'"' + field.fieldName + '"'
),
type: 'danger',
});
return false;
}
});
let scheduleContainerInputValue;
const scheduleContainerInput = document.getElementById(
' scheduleContainer'
);
if (Liferay.FeatureFlags['LPD-17564'] && scheduleContainerInput) {
scheduleContainerInputValue = JSON.parse(
scheduleContainerInput.value
);
if (
hasEmptyString(scheduleContainerInputValue) ||
isPastDate(scheduleContainerInputValue.expirationDate)
) {
shouldSubmitForm = false;
loadingElement.remove();
return false;
}
}
if (shouldSubmitForm) {
let values = getValues(fields);
const categoriesContent = document.getElementById(
' categorization'
);
const externalReferenceCode =
getExternalReferenceCode();
const path = getPath(
externalReferenceCode
);
if (categoriesContent) {
values = Object.assign(
values,
{
['keywords']: getInputValues(
categoriesContent,
'input[name^=" assetTagNames"]'
),
},
{
['taxonomyCategoryIds']:
getInputValues(
categoriesContent,
'input[name^=" assetCategoryIds"]'
),
}
);
}
const autoRelatedValue = {
['relationshipField']:
'<%= objectEntryDisplayContext.getObjectRelationshipERCObjectFieldName() %>',
['parentObjectEntryERC']:
'<%= objectEntryDisplayContext.getParentObjectEntryId() %>',
};
if (autoRelatedValue['relationshipField'] !== 'null') {
values = Object.assign(values, {
[autoRelatedValue['relationshipField']]:
autoRelatedValue['parentObjectEntryERC'],
});
}
const friendlyURLInputs = document.querySelectorAll(
'[data-field-name="friendlyURL"]'
);
if (friendlyURLInputs) {
const friendlyURLValues = {};
friendlyURLInputs.forEach((input) => {
friendlyURLValues[input.dataset.languageid] =
input.value;
});
values = Object.assign(values, {
['friendlyUrlPath']: '',
['friendlyUrlPath_i18n']: friendlyURLValues,
});
}
if (
Liferay.FeatureFlags['LPD-17564'] &&
scheduleContainerInputValue
) {
values = {
...values,
...scheduleContainerInputValue,
};
}
const method = !externalReferenceCode
? 'POST'
: hasObjectLayout
? 'PATCH'
: 'PUT';
Liferay.Util.fetch(path, {
body: JSON.stringify(values),
headers: new Headers({
'Accept': 'application/json',
'Accept-Language':
'<%= LanguageUtil.getBCP47LanguageId(request) %>',
'Content-Type': 'application/json',
}),
method: method,
})
.then((response) => {
Liferay.fire('submitButtonClicked');
if (response.status === 401) {
window.location.reload();
}
else if (response.ok) {
Liferay.Util.openToast({
message:
'<%=
HtmlUtil.escapeJS(LanguageUtil.get(
LocaleUtil.fromLanguageId(LanguageUtil.getBCP47LanguageId(request)), "your-request-completed-successfully")) %>',
type: 'success',
});
response.json().then((payload) => {
const portletURL =
Liferay.Util.PortletURL.createPortletURL(
'<%= currentURLObj %>',
{
externalReferenceCode:
payload.externalReferenceCode,
}
);
Liferay.Util.navigate(portletURL.toString());
});
}
else {
return response.json();
}
})
.then((response) => {
if (response && response.detail) {
const errorMessageArray = JSON.parse(
response.detail
);
const alertClassName = ' alert';
const alertElements =
document.getElementsByClassName(alertClassName);
for (let i = 0; i < alertElements.length; i++) {
alertElements[i].remove();
}
for (const error of errorMessageArray) {
const portletBody =
document.querySelector('.portlet-body');
const existingAlert =
portletBody.querySelector('.alert');
if (existingAlert) {
existingAlert.remove();
}
const alertElement =
document.createElement('div');
alertElement.className =
'alert alert-danger ' + alertClassName;
alertElement.setAttribute('role', 'alert');
alertElement.style.bottom = '20px';
alertElement.style.margin = '2rem auto 0';
alertElement.style.width = '800px';
alertElement.insertAdjacentHTML(
'afterbegin',
" Error:"
);
alertElement.insertAdjacentHTML(
'beforeend',
error.errorMessage
);
const closeButton =
document.createElement('button');
closeButton.classList.add('close');
closeButton.setAttribute('aria-label', 'Close');
closeButton.setAttribute('type', 'button');
closeButton.style.fontSize = '32px';
closeButton.style.fontWeight = '300';
closeButton.innerHTML = '×';
closeButton.onclick = () => {
alertElement.remove();
};
alertElement.appendChild(closeButton);
form.insertAdjacentElement(
'afterbegin',
alertElement
);
}
scroll(0, 0);
}
else if (response && response.title) {
Liferay.Util.openToast({
message: response.title,
type: 'danger',
});
}
loadingElement.remove();
});
}
}
else {
current.updateLocalesDropdownToDefaultLanguage();
loadingElement.remove();
}
});
});
© 2015 - 2025 Weber Informatics LLC | Privacy Policy