Liferay.PortletURL is undefined - liferay

Want to implement:
I have two pages with one portlet each. On click of link i want to move from first page portlet to another page portlet.
For that i have written:
<aui:script>
function openCompanyPage(companyId) {
AUI().use(
'liferay-portlet-url',
'aui-resize-iframe',
function(A) {
var navigationURL;
var portletURL = Liferay.PortletURL.createRenderURL();
var url = themeDisplay.getLayoutURL();
portletURL.setParameter("employerId", companyId);
portletURL.setPortletId(A.one('#custSupportPortletId'));
navigationURL = portletURL.toString();
window.location = navigationURL;
}
);
</aui:script>
but i am getting error as Liferay.PortletURL is undefined on bold line.
I have already provided :
<%# taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%# taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%# taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%# taglib uri="http://liferay.com/tld/util" prefix="liferay-util"%>
Please let me know the possible reason for this.
OR
What is other way to create the Render portlet URL.

Update:
I resolved the issue by brute force.
Before:
<a href="" onclick="openCDPPage('${individual.individualId}')">${individual.individualName}
After :
<a onclick="openCDPPage('${individual.individualId}')">${individual.individualName}</a>
Changes in the script:
<script>
function openCompanyPage(companyId) {
AUI().use(**'liferay-portlet-url'**,
function(A) {
var navigationURL;
var portletURL = Liferay.PortletURL.createRenderURL();
var url = themeDisplay.getLayoutURL();
portletURL.setParameter("employerId", companyId);
portletURL.setPortletId(A.one('#custSupportPortletId'));
navigationURL = portletURL.toString();
window.location = navigationURL;
}
);
</script>
This solved my issue.
I am not sure whether this is the perfect solution for the problem or not.
Expert please let us know.

Related

How do i make the ejs variable work that i send from nodejs?

I am tring to get a variable that i send from nodejs to ejs to work. But for some reason it wont, i cant figure out why.
This is the index.js:
var newOne = "Yes"
router.get('/main', ensureAuthenticated, (req, res) =>
res.render('main', {
user: req.user,
newOneInView : newOne
})
)
And this is in the main.ejs file:
<%if (newOneInView == "Yes") { %>
document.getElementById("avatar").src = "/static/images/Apocaliptic1.png";
<% } %>
So what i am trying to achieve is that variable will be seen from the nodejs at the main.ejs page but some reason it wont change the image SRC. What am i doing wrong here?
In your template you need to make sure that you place the code for changing the src attribute in a script that is placed after the element (alternatively you can use a listener for the content to be loaded), e.g:
<body>
<img id="avatar" src="/some-other-path"/>
<script>
<% if (newOneInView === "Yes") { %>
document.getElementById("avatar").src = "/static/images/Apocaliptic1.png";
<% } %>
</script>
</body>

Can't get to Liferay's (Spring Portlet MVC's) controller from JS by doing the following:

Here's the JSP from where my Javascript function is being called:
JSP code
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="continueTour" onclick="showTutorial()">Take a Quick Tour</button>
Skip Tour
</div>
Here's the Javascript function from where I need to render another JSP, and hence need to get to the Render method in the controller. Notice the 'simulate' method that I'm calling to simulate the click of the hyperlink (!Not sure if this is right or not!):
Javascript Code showTutorial() method:
function showTutorial(){
launchTutorial();
}
function launchTutorial(){
var enjoyhint_instance = new EnjoyHint({
onEnd: function(){
AUI().use('liferay-portlet-url', function(A) {
var plid = Liferay.ThemeDisplay.getPlid();
var url=Liferay.PortletURL.createRenderURL();
/*url.setPortletId(plid);*/
url.setPortletName(Liferay.ThemeDisplay.getp)
url.setParameter('render','redirectToEmpInfo');
alert(url);
A.one(document.createElement('a')).attr('href',url).simulate('click');
});
}
});
var enjoyhint_script_steps = [
{
"next #newAuthorizationActive": 'To create an authorization form'
}
];
enjoyhint_instance.set(enjoyhint_script_steps);
enjoyhint_instance.run();
}
Here's the controller method which I've written to catch the render request from the Javascript.
Controller Method (Not getting to this method)
#RenderMapping(params = "render=redirectToEmpInfo")
protected ModelAndView redirectToEmpInfoForAuthTour(ModelMap map, RenderRequest renderRequest, RenderResponse response) {
LiferayPortal.logInfo(_log, "Inside the render method for Emp Info");
return null;
/*return new ModelAndView("emailsuccess", map);*/
}
You add this code in head of jsp:
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%# taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<liferay-theme:defineObjects/>
<portlet:defineObjects />
Also in you code:
var plid = Liferay.ThemeDisplay.getPlid();
var url=Liferay.PortletURL.createRenderURL();
/*url.setPortletId(plid);*/
url.setPortletName(Liferay.ThemeDisplay.getp)
url.setParameter('render','redirectToEmpInfo');
alert(url);
Replace, similar to this:
var plid = Liferay.ThemeDisplay.getPlid();
var url = Liferay.PortletURL.createRenderURL();
url.setPortletId('<%=themeDisplay.getPortletDisplay().getId() %>');
url.setParameter('render', 'redirectToEmpInfo');
alert(url);

Liferay 6.2 How to display images stored in Documents and Media

I would like to know all possible ways of displaying a DLFileEntry image in a jsp of a custom portlet.
More specifically, I currently use the following way but I have some issues with DLFileEntry objects that have zero values for 'largeimageid'
DLFileEntry image = DLFileEntryLocalServiceUtil.getFileEntry(long_id);
String imageUrl = themeDisplay.getPathImage() + "/image_gallery?img_id=" + image.getLargeImageId() + "&t=" + WebServerServletTokenUtil.getToken(image.getLargeImageId());
Which are the alternatives of getting the image url without use of the large image id?
Following is the pattern similar to the one that is used by Liferay Documents and Media portlet:
DLFileEntry image = DLFileEntryLocalServiceUtil.getFileEntry(long_id);
String imageUrl = "";
if (image != null) {
imageUrl =
PortalUtil.getPortalURL(request) + "/documents/" + image.getGroupId() + "/" +
image.getFolderId() + "/" + image.getTitle() + "/" + image.getUuid() + "?t=" +
System.currentTimeMillis();
}
Where PortalUtil.getPortalURL(request) will return you base URL of your portal based on httpServletRequest, System.currentTimeMillis() will give you current time (miliseconds), and rest of the parameters are all available through DLFileEntry object.
I think this can help you
<%# page import="com.liferay.portlet.documentlibrary.model.DLFolder" %>
<%# page import="com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil" %>
<%# page import="com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil" %>
<%# page import="com.liferay.portlet.documentlibrary.model.DLFileEntry" %>
<%# page import="java.util.List" %>
<%# page import="com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil" %>
<%# page import="com.liferay.portlet.imagegallery.model.IGImage" %>
<%# include file="init.jsp" %>
<%
String igFolderId = portletPreferences.getValue("igFolderId", "0");
String cycleSpeed = portletPreferences.getValue("cycleSpeed", "1000");
String fxSpeed = portletPreferences.getValue("fxSpeed", "1000");
String type = portletPreferences.getValue("type", "fade");
String height = portletPreferences.getValue("height", "480");
String width = portletPreferences.getValue("width", "640");
List<IGImage> images = IGImageLocalServiceUtil.getImages(Long.valueOf(igFolderId));
%>
<c:choose>
<c:when test="<%= Long.valueOf(igFolderId) != 0%>">
<div id="<portlet:namespace />images">
<%
for (int i = 0; i < images.size(); i++) {
IGImage image = images.get(i);
%>
<img width="<%= width %>" height="<%= height %>" src="/image/image_gallery?img_id=<%=image.getLargeImageId()%>" alt="<%=image.getDescription()%>" <%= i == 0 ? "" : "style=\"display:none;\""%>/>
<%
}
%>
</div>
</c:when>
<c:otherwise>
<span class="portlet-msg-info">
Please configure this portlet.
</span>
</c:otherwise>
</c:choose>
<script type="text/javascript">
jQuery(
function() {
jQuery("#<portlet:namespace />images").cycle({
fx: '<%= type %>',
speed: <%= fxSpeed %>,
timeout: <%= cycleSpeed %>
});
}
);
</script>
Greetings!

How to Include AlloyUI

I have some troubles including AlloyUI in my Liferay Portlet.
Following this article, I have generated the following jsp:
<%# taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<input type="text" id="some-input" />
<span id="counter"></span> character(s) remaining
<aui:script>
YUI().use(
'aui-char-counter',
function(Y) {
new Y.CharCounter(
{
counter: '#counter',
input: '#some-input',
maxLength: 10
}
);
}
);
</aui:script>
But the rendered page looks like this:
I made sure that the taglib is correctly defined in the web.xml:
<taglib>
<taglib-uri>http://liferay.com/tld/aui</taglib-uri>
<taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
</taglib>
AUI does work, when I include it in the jsp as follows:
<script src="http://cdn.alloyui.com/2.0.0/aui/aui-min.js"></script>
<link href="http://cdn.alloyui.com/2.0.0/aui/aui-min.js" rel="stylesheet"></link>
<input type="text" id="some-input" />
<span id="counter"></span> character(s) remaining
<script>
YUI().use(
'aui-char-counter',
function(Y) {
new Y.CharCounter(
{
counter: '#counter',
input: '#some-input',
maxLength: 10
}
);
}
);
</script>
I'm using Liferay 6.1.20 EE GA2
Liferay uses alloy-ui (also referred to asAUI) library developed on top of Yahoo UI (also referred to as yui) library.
The instance terms for both these libraries are different i.e. AUI for Alloy-UI and YUI for the other.
Replacing these terms in your code will resolve your issue i.e. have AUI instead of YUI.
The seed file declaration is all you need to access AlloyUI for this char counter code.
<script src="http://cdn.alloyui.com/2.0.0/aui/aui-min.js"></script>
You shouldn't need the taglib reference in your web.xml. In fact, you're inhibiting access to the seed file. The taglib you're referencing may be inconsistent with the version of AlloyUI that is expected.
Also, accessing YUI's CharCounter is fine. See the API example at http://alloyui.com/api/classes/A.CharCounter.html.

liferay display connected user in jsp

I work with liferay 5.2
in my jsp I can get the connected user with this code :
<%#page import="com.nbs.fw.portal.PortalUtil"%>
String id_employe=PortalUtil.getConnectedUserID(request).toUpperCase();
but now I want to know the role of connected user
Updated :
<%#page import="com.liferay.portal.service.RoleServiceUtil"%>
<%# page import="com.liferay.portal.model.User" %>
<%# page import="com.liferay.portal.model.Role" %>
<%#page import="com.liferay.portal.theme.ThemeDisplay"%>
<%#page import="com.liferay.portal.util.WebKeys"%>
<%
User user = ((ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY)).getUser();
List<Role> roles = (List<Role>) RoleServiceUtil.getUserRoles(user.getUserId());
for (Role role : roles) {
out.println( role.getRoleId() );
}
%>
but when I test I have an error :
Caused by: org.apache.jasper.JasperException: Impossible de compiler la classe pour la JSP:
An error occurred at line: 13 in the generated java file
Only a type can be imported. com.liferay.portal.util.WebKeys resolves to a package
Une erreur s'est produite � la ligne: 1 208 dans le fichier jsp: /jsp/_correspondencelist/html/correspondenceList.jsp
WebKeys.THEME_DISPLAY cannot be resolved to a type
1205: %>
1206:
1207: <%
1208: User user = ((ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY)).getUser();
1209: List<Role> roles = (List<Role>) RoleServiceUtil.getUserRoles(user.getUserId());
1210: for (Role role : roles) {
1211: out.println( role.getRoleId() );
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
It seems that you didn't import all classes like WebKeys or ThemeDisplay
<%# page import="com.liferay.portal.theme.ThemeDisplay" %>
<%# page import="com.liferay.portal.util.WebKeys" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<liferay-theme:defineObjects />
Change the import to com.liferay.portal.PortalUtil
<%
User user = PortalUtil.getUser(request);
List<Role> roleList = user.getRoles();
%>

Resources