Custom actions in KCenter
The following sample demonstrates how to create a new custom action in the KCenter.
Sample based on a JSP
Section titled “Sample based on a JSP”Step 1 - Create a sample JSP file
Section titled “Step 1 - Create a sample JSP file”Create the following sample JSP file and copy it to the webapps/openkm folder with the name test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@ page isErrorPage="true" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html><head> <script type="text/javascript" src="<%=request.getContextPath() %>/kcenter/api/common.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/kcenter/api/modal.js"></script></head><body> <h1>Quick sample about how to create a custom action in the KCenter</h1>
<p><b>UserId:</b> <%=request.getParameter("userId")%></p> <p><b>Uuid:</b> <%=request.getParameter("uuid")%></p> <p><b>Encrypted token:</b> <%=request.getParameter("token")%></p>
<button onclick="_closeCustomFeatureModal()">Close popup</button></body></html>Step 2 - Register the custom action
Section titled “Step 2 - Register the custom action”Create a new custom action with these values. More information at Custom actions.
| Field | Value |
|---|---|
| Name | test |
| Label | Label test |
| Icon | mdi-copy-all |
| Description | |
| Document | false |
| Folder | true |
| Record | false |
| false | |
| URL | ../test.jsp Another option: http://localhost:8080/openkm/test.jsp |
| Action | popup-wizard |
| Location | node-toolbar |
| Width | 50% |
| Height | 50% |
| Show close | true |
Step 3 - Enable the custom action in the profile
Section titled “Step 3 - Enable the custom action in the profile”- Go to Administration > Profiles
- Edit a profile and enable the custom action in the Misc tab.
Step 4 - Check the feature
Section titled “Step 4 - Check the feature”- Refresh the KCenter UI
- Select a folder in the browser view.
- A new menu option named “Extra” is shown in the contextual menu or in the toolbar menu.
Full JSP sample
Section titled “Full JSP sample”The next JSP sample allows everybody to unlock a node.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.openkm.util.ContextWrapper" %><%@ page import="com.openkm.util.StackTraceUtils" %><%@ page import="com.openkm.api.OKMDocument" %><%@ page import="com.openkm.module.db.DbNodeModule" %><%@ page import="com.openkm.bean.Document" %><%@ page import="com.openkm.principal.PrincipalUtils" %><%@ page import="com.openkm.module.db.stuff.DbSessionManager" %><%@ page import="org.apache.commons.lang3.StringUtils" %><%@ page import="com.openkm.principal.SecurityHolder" %>
<html><head> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/kcenter/css/bootstrap/bootstrap.css" /> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/kcenter/css/mdb/mdb.css" /> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/kcenter/css/okm.css" /> <script type="text/javascript" src="<%=request.getContextPath() %>/kcenter/api/common.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/kcenter/api/modal.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/kcenter/api/browser.js"></script> <script> function closeButton() { _refreshUI(); _closeCustomFeatureModal(); } </script></head><body>
<% String error = ""; boolean isCheckedOut = false; boolean isLocked = false;
try { String uuid = request.getParameter("uuid"); OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); DbNodeModule dbNodeModule = ContextWrapper.getContext().getBean(DbNodeModule.class); boolean isDocument = okmDocument.isValid(null, uuid);
if (isDocument) { Document doc = okmDocument.getProperties(null, uuid); if (doc.isCheckedOut()) { isCheckedOut = true; } else if (doc.isLocked()) { isLocked = true; } } else { isLocked = dbNodeModule.isLocked(null, uuid); }
if (isLocked) { // Must set security directly becasue forceUnlock check if the current user has ROLE_ADMIN long tenantId = PrincipalUtils.getTenantId(); String systemToken = DbSessionManager.getInstance().getSystemToken(tenantId); try { SecurityHolder.set(PrincipalUtils.getAuthenticationByToken(systemToken)); dbNodeModule.forceUnlock(null, uuid); } finally { SecurityHolder.unset(); } } } catch (Exception e) { error = StackTraceUtils.toString(e).replaceAll("\n","<br/>"); e.printStackTrace(); }%>
<div class="d-flex justify-content-center w-100 my-2"> <% if (StringUtils.isEmpty(error)) { %> <% if (isCheckedOut) { %> <p class="text-danger">Not allowed to unlock a document on edition.</p> <% } else if (!isLocked) { %> <p class="text-danger">The selected node is not locked.</p> <% } else { %> <p class="text-success">Node unlocked successfuly</p> <% } %> <% } %> </div>
<div class=" d-flex justify-content-center w-100 my-2"> <button type="button" class="btn btn-success btn-sm my-0 m-0" onclick="closeButton()"> Close </button> </div>
<% if (StringUtils.isNotEmpty(error)) { %> <div class="d-flex justify-content-center w-100 my-2"> <p class="text-danger">Error found:</p> <%= error %> </div> <% } %>
</body></html>Sample based on JavaScript
Section titled “Sample based on JavaScript”Step 1 - Register the custom action
Section titled “Step 1 - Register the custom action”Create a new custom action with these values. More information at Custom actions.
| Field | Value |
|---|---|
| Name | test |
| Label | Label test |
| Icon | mdi-assistant-direction |
| Description | |
| Document | true |
| Folder | false |
| Record | false |
| false | |
| URL | |
| Action | run-script |
| Location | node-toolbar |
| Width | 50% |
| Height | 50% |
| Show close | true |
| Script | const kcenterApp = window.kcenterApp; // KCenter app const uuid = ‘1aff985f-9243-4c1a-8d43-482026f3fa93’; // UUID to jump to kcenterApp.$store.dispatch(‘browser/refreshByUuid’, uuid, { root: true }); // internal function to jump to the node Sample for accessing web services: kcenterApp.$ws.node.getNodeByUuid(‘095cdb6e-c28e-410c-a6e3-19e23bd4b3ef’).then(response => { console.log(‘here1’); console.log(response); console.log(‘here2’); }); |
Step 2 - Enable the custom action in the profile
Section titled “Step 2 - Enable the custom action in the profile”- Go to Administration > Profiles
- Edit a profile and enable the custom action in the Misc tab.
Step 3 - Check the feature
Section titled “Step 3 - Check the feature”- Refresh the KCenter UI
- Select a document in the browser view.
- In the toolbar menu, a new menu icon will appear.