TreeMenuInclude.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.web.includes;

  35. import fr.paris.lutece.portal.business.XmlContent;
  36. import fr.paris.lutece.portal.business.page.Page;
  37. import fr.paris.lutece.portal.business.page.PageHome;
  38. import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
  39. import fr.paris.lutece.portal.business.style.ModeHome;
  40. import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
  41. import fr.paris.lutece.portal.service.content.PageData;
  42. import fr.paris.lutece.portal.service.html.XmlTransformerService;
  43. import fr.paris.lutece.portal.service.includes.PageInclude;
  44. import fr.paris.lutece.portal.service.portal.PortalMenuService;
  45. import fr.paris.lutece.portal.service.portal.PortalService;
  46. import fr.paris.lutece.portal.service.util.AppLogService;
  47. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  48. import fr.paris.lutece.portal.web.constants.Markers;
  49. import fr.paris.lutece.portal.web.constants.Parameters;
  50. import fr.paris.lutece.util.xml.XmlUtil;

  51. import java.util.Collection;
  52. import java.util.HashMap;
  53. import java.util.Map;
  54. import java.util.Properties;

  55. import javax.servlet.http.HttpServletRequest;

  56. /**
  57.  * This class provides the list of the page associated by the main menu of the site
  58.  */
  59. public class TreeMenuInclude implements PageInclude
  60. {
  61.     // ///////////////////////////////////////////////////////////////////////////////////////////
  62.     // Constants
  63.     private static final int PORTAL_COMPONENT_MENU_TREE = 7;

  64.     // Properties
  65.     private static final String PROPERTY_ROOT_TREE = "lutece.root.tree";

  66.     /**
  67.      * Substitue specific Freemarker markers in the page template.
  68.      *
  69.      * @param rootModel
  70.      *            the HashMap containing markers to substitute
  71.      * @param data
  72.      *            A PageData object containing applications data
  73.      * @param nMode
  74.      *            The current mode
  75.      * @param request
  76.      *            The HTTP request
  77.      */
  78.     public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
  79.     {
  80.         if ( request != null )
  81.         {
  82.             int nCurrentPageId;

  83.             try
  84.             {
  85.                 nCurrentPageId = ( request.getParameter( Parameters.PAGE_ID ) == null ) ? PortalService.getRootPageId( )
  86.                         : Integer.parseInt( request.getParameter( Parameters.PAGE_ID ) );
  87.             }
  88.             catch( NumberFormatException nfe )
  89.             {
  90.                 AppLogService.info( "TreeMenuInclude.fillTemplate() : {}", nfe.getLocalizedMessage( ) );
  91.                 nCurrentPageId = 0;
  92.             }

  93.             data.setTreeMenu( buildTreeMenuContent( nCurrentPageId, nMode, request ) );
  94.             rootModel.put( Markers.PAGE_TREE_MENU, ( data.getTreeMenu( ) == null ) ? "" : data.getTreeMenu( ) );
  95.         }
  96.     }

  97.     /**
  98.      * Builds the tree menu bar
  99.      *
  100.      * @param nIdPage
  101.      *            The page id
  102.      * @param nMode
  103.      *            the mode id
  104.      * @param request
  105.      *            The HttpServletRequest
  106.      * @return The list of the tree menus layed out with the stylesheet of the mode
  107.      */
  108.     public String buildTreeMenuContent( int nIdPage, int nMode, HttpServletRequest request )
  109.     {
  110.         StringBuffer strXml = new StringBuffer( );

  111.         String strCurrentPageId = Integer.toString( nIdPage );

  112.         String strTreeOnRoot = AppPropertiesService.getProperty( PROPERTY_ROOT_TREE );
  113.         Collection<Page> listPagesMenu;

  114.         // If the current page is the home page or the string strTreeOnRoot equals false, not display the treeMenu
  115.         if ( strTreeOnRoot.equalsIgnoreCase( "true" ) )
  116.         {
  117.             listPagesMenu = PageHome.getChildPagesMinimalData( getPageTree( nIdPage ) );
  118.         }
  119.         else
  120.         {
  121.             listPagesMenu = PageHome.getChildPagesMinimalData( nIdPage );
  122.         }

  123.         strXml.append( XmlUtil.getXmlHeader( ) );
  124.         XmlUtil.beginElement( strXml, XmlContent.TAG_MENU_LIST );

  125.         int nMenuIndex = 1;

  126.         for ( Page menuPage : listPagesMenu )
  127.         {
  128.             if ( ( menuPage.isVisible( request ) ) || ( nMode == PortalMenuService.MODE_ADMIN ) )
  129.             {
  130.                 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU );
  131.                 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
  132.                 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, menuPage.getId( ) );
  133.                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, menuPage.getName( ) );
  134.                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_DESCRIPTION, menuPage.getDescription( ) );
  135.                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );

  136.                 // Seek of the sub-menus
  137.                 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );

  138.                 Collection<Page> listSubLevelMenuPages = PageHome.getChildPagesMinimalData( menuPage.getId( ) );
  139.                 int nSubLevelMenuIndex = 1;

  140.                 for ( Page subLevelMenuPage : listSubLevelMenuPages )
  141.                 {
  142.                     if ( ( subLevelMenuPage.isVisible( request ) ) || ( nMode == PortalMenuService.MODE_ADMIN ) )
  143.                     {
  144.                         XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
  145.                         XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
  146.                         XmlUtil.addElement( strXml, XmlContent.TAG_SUBLEVEL_INDEX, nSubLevelMenuIndex );
  147.                         XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, subLevelMenuPage.getId( ) );
  148.                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, subLevelMenuPage.getName( ) );
  149.                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_DESCRIPTION, subLevelMenuPage.getDescription( ) );
  150.                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
  151.                         XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
  152.                     }
  153.                 }

  154.                 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
  155.                 XmlUtil.endElement( strXml, XmlContent.TAG_MENU );
  156.                 nMenuIndex++;
  157.             }
  158.         }

  159.         XmlUtil.endElement( strXml, XmlContent.TAG_MENU_LIST );

  160.         StyleSheet xslSource;

  161.         // Selection of the XSL stylesheet
  162.         switch( nMode )
  163.         {
  164.             case PortalMenuService.MODE_NORMAL:
  165.             case PortalMenuService.MODE_ADMIN:
  166.                 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_TREE, PortalMenuService.MODE_NORMAL );

  167.                 break;

  168.             default:
  169.                 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_TREE, nMode );

  170.                 break;
  171.         }

  172.         Properties outputProperties = ModeHome.getOuputXslProperties( nMode );

  173.         // Added in v1.3
  174.         // Add a path param for choose url to use in admin or normal mode
  175.         Map<String, String> mapParamRequest = new HashMap<>( );
  176.         PortalService.setXslPortalPath( mapParamRequest, nMode );

  177.         XmlTransformerService xmlTransformerService = new XmlTransformerService( );

  178.         return xmlTransformerService.transformBySourceWithXslCache( strXml.toString( ), xslSource, mapParamRequest, outputProperties );
  179.     }

  180.     /**
  181.      *
  182.      * @param nPageId
  183.      *            The page identifier
  184.      * @return The parent page identifier
  185.      */
  186.     private int getPageTree( int nPageId )
  187.     {
  188.         Page page = PageHome.getPage( nPageId );
  189.         int nParentPageId = page.getParentPageId( );

  190.         if ( nParentPageId == 0 )
  191.         {
  192.             return nPageId;
  193.         }

  194.         int nParentTree = nParentPageId;

  195.         while ( nParentPageId != 0 )
  196.         {
  197.             nParentTree = nParentPageId;

  198.             Page parentPage = PageHome.getPageWithoutImageContent( nParentPageId );
  199.             nParentPageId = parentPage.getParentPageId( );
  200.         }

  201.         return nParentTree;
  202.     }
  203. }