Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Jonathan Mattox
My Sites Portlet
May 29, 2014 1:15 PM
Answer

Jonathan Mattox

Rank: New Member

Posts: 13

Join Date: November 5, 2013

Recent Posts

Hello, I am developing a hook for the "My Sites" portlet. My question is regarding how to edit the functionality of what happens when the clicks the "Save" button after requesting access to a site and filling in a comment. What I'm trying to accomplish is having an email go out after the user fills in a comment and clicks the "Save" button. I have been able to do this as soon as the user clicks the "Request Membership" hyperlink, but I also want to capture the user's comment and sent it in the email. Plus, if the user clicked the "Cancel" button instead of "Save" I do not want the email to go out. For these two reasons I need the email to go out after the "Save" button is clicked.

I'm having a hard time understanding how to handle this action. I am working in the following customized jsp in my hook: html/portlet/sites_admin/post_membership_request.jsp

I checked the liferay-portlet.xml file for any relevant information and here is the section for the "My Sites" portlet:

<portlet>
<portlet-name>29</portlet-name>
<icon>/html/icons/communities.png</icon>
<struts-path>my_sites</struts-path>
<parent-struts-path>sites_admin</parent-struts-path>
<portlet-url-class>com.liferay.portal.struts.StrutsActionPortletURL</portlet-url-class>
<asset-renderer-factory>com.liferay.portal.asset.LayoutRevisionAssetRendererFactory</asset-renderer-factory>
<workflow-handler>com.liferay.portal.workflow.LayoutRevisionWorkflowHandler</workflow-handler>
<preferences-owned-by-group>true</preferences-owned-by-group>
<use-default-template>false</use-default-template>
<private-request-attributes>false</private-request-attributes>
<private-session-attributes>false</private-session-attributes>
<render-weight>50</render-weight>
<header-portlet-css>/html/portlet/layouts_admin/css/main.css</header-portlet-css>
<css-class-wrapper>portlet-communities</css-class-wrapper>
</portlet>

Then I looked at the struts-config.xml file for help. Here is the relevant section for the My Sites Portlet from the struts-config.xml file which can be found on github at the following link: https://github.com/liferay/liferay-portal/blob/master/portal-web/docroot/WEB-INF/struts-config.xml

<!-- My Sites -->

<action path="/my_sites/edit_layouts" type="com.liferay.portlet.myplaces.action.EditPagesAction" />

<action path="/my_sites/view" type="com.liferay.portlet.myplaces.action.ViewAction">
<forward name="portlet.my_sites.view" path="portlet.my_sites.view" />
</action>

I then went and looked at the com.liferay.portlet.myplaces.action.ViewAction class file to see if I could determine what code is being executed when the "Save" button is clicked. Here is the link to that file: http://docs.liferay.com/portal/5.1/javadocs/portal-impl/com/liferay/portlet/myplaces/action/ViewAction.java.html

If someone could please explain to me how this kind of thing works I would be greatly appreciative. Here is the code of the html/portlet/sites_admin/post_membership_request.jsp that I have been working with and the email code I added highlighted in bold.

<%--
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of the Liferay Enterprise
* Subscription License ("License"). You may not use this file except in
* compliance with the License. You can obtain a copy of the License by
* contacting Liferay, Inc. See the License for the specific language governing
* permissions and limitations under the License, including but not limited to
* distribution rights of the Software.
*
*
*
*/
--%>

<%@ include file="/html/portlet/sites_admin/init.jsp" %>

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ page import="com.liferay.mail.service.MailServiceUtil"%>
<%@ page import="com.liferay.portal.kernel.mail.MailMessage"%>
<%
String redirect = ParamUtil.getString(request, "redirect");

ActionUtil.getGroup(request);

Group group = (Group)request.getAttribute(WebKeys.GROUP);

long groupId = BeanParamUtil.getLong(group, request, "groupId");

MembershipRequest membershipRequest = (MembershipRequest)request.getAttribute(WebKeys.MEMBERSHIP_REQUEST);




/* START MAIL CODE */
/*
InternetAddress from = new InternetAddress("test@test.com", "Admin admin");
InternetAddress to = new InternetAddress("jmattox@dminc.com", "Jonathan");

MailMessage mailMessage = new MailMessage();

mailMessage.setFrom(from);
mailMessage.setTo(to);

mailMessage.setBody("email body text");
mailMessage.setSubject("email subject text");
MailServiceUtil.sendEmail(mailMessage);

*/

/* END MAIL CODE */

%>

<portlet:actionURL var="postMembershipRequestURL">
<portlet:param name="struts_action" value="/sites_admin/post_membership_request" />
</portlet:actionURL>

<aui:form action="<%= postMembershipRequestURL %>" method="post" name="fm">
<aui:input name="<%= Constants.CMD %>" type="hidden" />
<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
<aui:input name="groupId" type="hidden" value="<%= groupId %>" />

<liferay-ui:header
backURL="<%= redirect %>"
escapeXml="<%= false %>"
localizeTitle="<%= false %>"
title='<%= LanguageUtil.format(pageContext, "request-membership-for-x", HtmlUtil.escape(group.getDescriptiveName(locale))) %>'
/>

<liferay-ui:error exception="<%= MembershipRequestCommentsException.class %>" message="please-enter-valid-comments" />

<aui:model-context bean="<%= membershipRequest %>" model="<%= MembershipRequest.class %>" />

<aui:fieldset>
<c:if test="<%= Validator.isNotNull(group.getDescription()) %>">
<aui:field-wrapper label="description">
<%= HtmlUtil.escape(group.getDescription()) %>
</aui:field-wrapper>
</c:if>

<aui:input name="comments" />
</aui:fieldset>

<aui:button-row>
<aui:button type="submit" />


<aui:button href="<%= redirect %>" type="cancel" />
</aui:button-row>
</aui:form>

<c:if test="<%= windowState.equals(WindowState.MAXIMIZED) %>">
<aui:script>
Liferay.Util.focusFormField(document.<portlet:namespace />fm.<portlet:namespace />comments);
</aui:script>
</c:if>
Andrew Jardine
RE: My Sites Portlet
May 29, 2014 3:02 PM
Answer

Andrew Jardine

Community Moderator

Rank: Liferay Legend

Posts: 1102

Join Date: December 22, 2010

Recent Posts

Hey Jonathan,

I think what you are looking for is a struts portlet action. Since you want to maintain the existing functionality (unless I misunderstood) and simply augment it with "more stuff" you could try thing.

Step #1: Create a class --

1
2public class CustomEditPagesAction extends BaseStrutsPortletAction
3{
4
5}


Step #2: Override the method you want to augment -- in your case the processAction

1
2public class CustomEditPagesAction extends BaseStrutsPortletAction
3{
4    public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception
5    {
6
7        }
8}



Step #3: Make a call to the existing action, and then add your stuff...

 1
 2public class CustomEditPagesAction extends BaseStrutsPortletAction
 3{
 4    public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception
 5    {
 6               // execute the core functionality and save the content editing.
 7        originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
 8
 9               // .. add your email stuff here
10        }
11}



Step #4: Add the configuration for the hook in your liferay-hook.xml file
1
2  <struts-action>
3        <struts-action-path>/my_sites/view</struts-action-path>
4        <struts-action-impl>com.your.package.name.here.CustomEditPagesAction</struts-action-impl>
5    </struts-action>

and deploy. Hope this helps.

PS> Forgot to add.. the struts action is stored in a parameter called "cmd". Check the page source to see what the command the save button says is. Probably either "save" or "add" You can use the Command class with it's contants to check for the save and only send the email in that case.

e.g. if ( ParamUtil.getString("cmd").equals(Command.ADD)) { ... do your stuff ... }