Alimozzaman

This is a very trivial but then again I want to share as I wasted some time to find this way to add a localization in my theme.

Step#1:  Set a  key in a variable into your init_custom.vm

#set ($a = $languageUtil.get($locale, “my-picture”))

step#2: Use the variable into your *.vm files

<span>$a</span>

step#3:  Add the corresponding value of the key  “my-picture” into your language file.

Ex- my-picture= <localized text>

Here you go… Very simple!

 

Multiple themes  selection

We can select the whole site theme from any navagation of our site which will be different user to user. Say , you  select your own theme that would be different than other users in a multi user site where user register to use the site. I hope this will help selecting theme from UI level in very convenient way.

1. Define a structs path in which you will be adding your Action class.

For example – I made a path using an existing portlet.

<action path=”/my_places/themes” type=”com.example.actions.themes.ThemeSwitchAction”>             <forward name=”portlet.my_places.view” path=”portlet.my_places.view” />

</action>

 

2. Now generate the url into your VM file, in my case I did add in dock.vm file like the following way.

#set ($my_places_url1 = $portletURLFactory.create($request, “49”, $page.getPlid(), “ACTION_PHASE”))

$my_places_url1.setWindowState(“normal”)

$my_places_url1.setPortletMode(“view”)

$my_places_url1.setParameter(“struts_action”, “/my_places/themes”)

// Set the theme ID

$my_places_url1.setParameter(“themeId”, “classic”)

<span>

<li>   <a  class = “theme-update-link” href=”#” onclick=”updateTheme(‘$my_places_url1.toString()’,this)” ><img src=”$images_folder/icon/stp-oranssi.png” alt=”Oranssi” /></a>

</li>

// set the second theme ID

$my_places_url1.setParameter(“themeId”, “classic_green “)

 

 

 

<li>

<a  class = “theme-update-link” href=”#” onclick=”updateTheme(‘$my_places_url1.toString()’,this)” ><img src=”$images_folder/icon/stp-red.png” alt=”Stp” /> </a>

</li>

//Set  the third theme ID

$my_places_url1.setParameter(“themeId”, “custom_theme_ID”)

<li>

<a  class = “theme-update-link” href=”#” onclick=”updateTheme(‘$my_places_url1.toString()’,this)” > <img src=”$images_folder/icon/stp-green.png” alt=”Classic” /> </a>

</li>

</span>

//Add the following Javascript to post your data.

<script type=”text/javascript”>

function updateTheme(url,obj){

var targetURL = url;

jQuery.post(targetURL, {ajax: ‘true’ },

function(){

location.reload();

});

}

</script>

 

 

 

 

3. Now you add the following lines in your Action class.

 

public void processAction(ActionMapping mapping, ActionForm form,

PortletConfig portletConfig, ActionRequest actionRequest,

ActionResponse actionResponse) throws Exception {

 

_log.info(ThemeSwitchAction.class.getName() + ”  WORKED”);

 

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest

.getAttribute(WebKeys.THEME_DISPLAY);

 

String themeId = ParamUtil.getString(actionRequest, “themeId”);

 

Group group = GroupLocalServiceUtil.getGroup(themeDisplay.getLayout()

.getGroupId());

 

// Setting group theme.

LayoutSetLocalServiceUtil.updateLookAndFeel(group.getGroupId(),

themeId, “01”, “”, false);

 

// Setting user home page theme.

LayoutSetLocalServiceUtil.updateLookAndFeel(themeDisplay.getUser()

.getGroup().getGroupId(), themeId, “01”, “”, false);

 

_log.info(ThemeSwitchAction.class.getName()

+ ”  set the theme for you.”);

 

 

 

}

4. To make sure your themeID is update every where , you can add this method into service preaction in your hook

 

protected void updateGroupThemes(HttpServletRequest request,

HttpServletResponse response)  {

try {

ThemeDisplay themeDisplay = (ThemeDisplay) request

.getAttribute(WebKeys.THEME_DISPLAY);

LayoutSet userHomeLayout = LayoutSetLocalServiceUtil.getLayoutSet(

themeDisplay.getUser().getGroup().getGroupId(), false);

String themeId = userHomeLayout.getThemeId();

LayoutSet updatedLayout = LayoutSetLocalServiceUtil.updateLookAndFeel(themeDisplay.getLayout().getGroupId(), false, themeId, “01”, “”, false);

 

LayoutSetLocalServiceUtil.updateLayoutSet(updatedLayout);

 

 

 

} catch (Exception e) {

//

}

}

 

5. Also you can add the follwoing lines into your community update method if u need to update your community layout theme on run time.

ThemeDisplay themeDisplay = (ThemeDisplay) request

.getAttribute(WebKeys.THEME_DISPLAY);

LayoutSet userHomeLayout = LayoutSetLocalServiceUtil.getLayoutSet(

themeDisplay.getUser().getGroup().getGroupId(), false);

String themeId = userHomeLayout.getThemeId();

LayoutSet updatedLayout = LayoutSetLocalServiceUtil.updateLookAndFeel(themeDisplay.getLayout().getGroupId(), false, themeId, “01”, “”, false);

 

LayoutSetLocalServiceUtil.updateLayoutSet(updatedLayout);

 

 

Enjoying making default theme according to user!!!

Drop me an email if u need further help  and also make valuable comment to make it much more clear!!

 

 

 

We can add any child page of any community from admin panel in the following way. See the picture. But we can do the same thing programmatically.

Here is the way to add a child page:

1.  Overwrite your EditGroupAction.java page into EXT.

2. Add the following lines  into updateGroup function.

Group group = GroupServiceUtil.addGroup(
name, description, type, friendlyURL, active, serviceContext);

after this line add ……..

boolean privateLayout = false;
long parentLayoutId = 0;
String childpageName = “childPage”;
String titleChild = “childPage”;
String descriptionChild = “childPage”;
String type = LayoutConstants.TYPE_PORTLET;
boolean hidden = false;
String friendlyURL = “/childPage”;

Layout layout = LayoutLocalServiceUtil.addLayout(userId,
group.getGroupId(),
privateLayout,
parentLayoutId,
childpageName,
titleChild,
descriptionChild,
type,
hidden,
friendlyURL);

/****You can add custom template and also u can add portlet  to it******/

LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

layoutTypePortlet.setLayoutTemplateId(0, “layouttpl-id”, false);
layoutTypePortlet.addPortletId(0, “20”, “column-1”, -1,false);
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());

//Then you can update the permission

updatePermissions(layout,true);

/*********End up the coding************/

//Here is the update permission method

public void updatePermissions(Layout layout, boolean addDefaultActionIds)
throws Exception {

long companyId = layout.getCompanyId();

Role role = RoleLocalServiceUtil
.getRole(companyId, RoleConstants.GUEST);

String[] actionIds = new String[0];

String name = Layout.class.getName();
int scope = ResourceConstants.SCOPE_INDIVIDUAL;
String primKey = String.valueOf(layout.getPrimaryKey());

Resource resource = ResourceLocalServiceUtil.getResource(companyId,
name, scope, primKey);

if (addDefaultActionIds) {
actionIds = new String[] { ActionKeys.VIEW };
}

PermissionLocalServiceUtil.setRolePermissions(role.getRoleId(),
actionIds, resource.getResourceId());

PermissionLocalServiceUtil.setRolePermissions(RoleLocalServiceUtil.getRole(companyId,
RoleConstants.POWER_USER).getRoleId(),
actionIds, resource.getResourceId());

PermissionLocalServiceUtil.setRolePermissions(RoleLocalServiceUtil.getRole(companyId,
RoleConstants.COMMUNITY_MEMBER).getRoleId(),
actionIds, resource.getResourceId());
}

3.  You can check the child page like http://localhost:8080/web/<community-name>/childPage or from admin panel.

Enjoy!!

Make comment to make this more helpful  to the community.

 

These are the  default Liferay 6 theme variables defined in init.vm.  I was searching but did not get any link ,so I hope this will help lot of people also around  the globe. If anything goes  wrong, pls make a comment and also try to add something that will help people around us.

 

## ———- Common variables ———- ##

$theme_display
$portlet_display
$theme_timestamp
$theme_settings
$css_class
$layout
$page_group
$liferay_toggle_controls
$liferay_dockbar_pinned
$css_folder
$images_folder
$javascript_folder
$templates_folder
$full_css_path
$full_templates_path
$css_main_file
$js_main_file
$company_id
$company_name
$company_logo
$company_logo_height
$company_logo_width
$company_url

$user_id
$is_default_user
$user_first_name
$user_middle_name
$user_last_name
$user_name
$is_male
$is_female
$user_birthday
$user_email_address
$language_id
$w3c_language_id
$time_zone
$user_greeting
$user_comments
$user_login_ip
$user_last_login_ip
$is_signed_in
$group_id

## ———- URLs ———- ##
$show_add_content
$add_content_text
$add_content_url
$layout_text
$layout_url
$show_control_panel
$control_panel_text
$control_panel_url
$show_home
$home_text
$home_url
$show_my_account
$my_account_text
$my_account_url
$show_page_settings
$page_settings_text
$page_settings_url
$show_sign_in
$sign_in_text
$sign_in_url
$show_sign_out
$sign_out_text
$sign_out_url
$show_toggle_controls
$toggle_controls_text
$toggle_controls_url
$update_available_url

## ———- Page ———- ##

$the_title
$selectable
$is_maximized
$is_freeform

$page_javascript_1
$page_javascript_2
$page_javascript_3
$layout
$page = $layout
$is_first_child
$is_first_parent
$the_title
$is_portlet_page

$all_portlets
$column_1_portlets
$column_2_portlets
$column_3_portlets
$column_4_portlets
$column_5_portlets
$maximized_portlet_id
$typeSettingsProperties
$page_javascript_1
$page_javascript_2
$page_javascript_3
$community_name
$css_class
$my_places_portlet_url
$community_default_public_url
$community_default_private_url
$community_default_url
$the_title
$layouts
$pages

## ———- Navigation ———- ##
$nav_items
$has_navigation

## ———- Staging ———- ##
$show_staging
$staging_text
## ———- My places ———- ##
$show_my_places
$my_places_text

## ———- Includes ———- ##
$dir_include
$bottom_include
$bottom_ext_include
$content_include
$top_head_include
$top_messages_include

## ———- Date ———- ##
$date
$current_time
$the_year

We can add any portlet into liferay theme files using the following way.

Create your portlet url using the help of portletURLFactory passig the fowwloing value according to your needs.

P_ID = Your required portlet ID.

PHASE = RENDER_PHASE/ACTION_PHASE

#set ($portlet_url = $portletURLFactory.create($request, “P_ID”, $page.getPlid(), “PHASE”))

Next, Set your window state , nomal/maximized for an example, mode and parameter in the following way.

$portlet_url.setWindowState(“maximized”)

$portlet_url.setPortletMode(“view”)
$portlet_url.setParameter(“struts_action”, “message/edit”)

Then use this url in the following way:

<a href=”$portlet_url.toString()”>my portlet link</a>

Enjoy adding portlet url in liferay theme files !

We can easily add content search into Liferay theme using the following velocity variable: $theme.journalContentSearch()