View Javadoc
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  
36  import fr.paris.lutece.portal.business.XmlContent;
37  import fr.paris.lutece.portal.business.page.Page;
38  import fr.paris.lutece.portal.business.page.PageHome;
39  import fr.paris.lutece.portal.business.portalcomponent.PortalComponentHome;
40  import fr.paris.lutece.portal.business.style.ModeHome;
41  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
42  import fr.paris.lutece.portal.service.content.PageData;
43  import fr.paris.lutece.portal.service.html.XmlTransformerService;
44  import fr.paris.lutece.portal.service.includes.PageInclude;
45  import fr.paris.lutece.portal.service.portal.PortalMenuService;
46  import fr.paris.lutece.portal.service.portal.PortalService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.service.util.AppPropertiesService;
49  import fr.paris.lutece.portal.web.constants.Markers;
50  import fr.paris.lutece.portal.web.constants.Parameters;
51  import fr.paris.lutece.util.xml.XmlUtil;
52  
53  import java.util.Collection;
54  import java.util.HashMap;
55  import java.util.Map;
56  import java.util.Properties;
57  
58  import javax.servlet.http.HttpServletRequest;
59  
60  /**
61   * This class provides the list of the page associated by the main menu of the site
62   */
63  public class TreeMenuInclude implements PageInclude
64  {
65      // ///////////////////////////////////////////////////////////////////////////////////////////
66      // Constants
67      private static final int PORTAL_COMPONENT_MENU_TREE = 7;
68  
69      // Properties
70      private static final String PROPERTY_ROOT_TREE = "lutece.root.tree";
71  
72      /**
73       * Substitue specific Freemarker markers in the page template.
74       * 
75       * @param rootModel
76       *            the HashMap containing markers to substitute
77       * @param data
78       *            A PageData object containing applications data
79       * @param nMode
80       *            The current mode
81       * @param request
82       *            The HTTP request
83       */
84      public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
85      {
86          if ( request != null )
87          {
88              int nCurrentPageId;
89  
90              try
91              {
92                  nCurrentPageId = ( request.getParameter( Parameters.PAGE_ID ) == null ) ? PortalService.getRootPageId( )
93                          : Integer.parseInt( request.getParameter( Parameters.PAGE_ID ) );
94              }
95              catch( NumberFormatException nfe )
96              {
97                  AppLogService.info( "TreeMenuInclude.fillTemplate() : {}", nfe.getLocalizedMessage( ) );
98                  nCurrentPageId = 0;
99              }
100 
101             data.setTreeMenu( buildTreeMenuContent( nCurrentPageId, nMode, request ) );
102             rootModel.put( Markers.PAGE_TREE_MENU, ( data.getTreeMenu( ) == null ) ? "" : data.getTreeMenu( ) );
103         }
104     }
105 
106     /**
107      * Builds the tree menu bar
108      *
109      * @param nIdPage
110      *            The page id
111      * @param nMode
112      *            the mode id
113      * @param request
114      *            The HttpServletRequest
115      * @return The list of the tree menus layed out with the stylesheet of the mode
116      */
117     public String buildTreeMenuContent( int nIdPage, int nMode, HttpServletRequest request )
118     {
119         StringBuffer strXml = new StringBuffer( );
120 
121         String strCurrentPageId = Integer.toString( nIdPage );
122 
123         String strTreeOnRoot = AppPropertiesService.getProperty( PROPERTY_ROOT_TREE );
124         Collection<Page> listPagesMenu;
125 
126         // If the current page is the home page or the string strTreeOnRoot equals false, not display the treeMenu
127         if ( strTreeOnRoot.equalsIgnoreCase( "true" ) )
128         {
129             listPagesMenu = PageHome.getChildPagesMinimalData( getPageTree( nIdPage ) );
130         }
131         else
132         {
133             listPagesMenu = PageHome.getChildPagesMinimalData( nIdPage );
134         }
135 
136         strXml.append( XmlUtil.getXmlHeader( ) );
137         XmlUtil.beginElement( strXml, XmlContent.TAG_MENU_LIST );
138 
139         int nMenuIndex = 1;
140 
141         for ( Page menuPage : listPagesMenu )
142         {
143             if ( ( menuPage.isVisible( request ) ) || ( nMode == PortalMenuService.MODE_ADMIN ) )
144             {
145                 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU );
146                 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
147                 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, menuPage.getId( ) );
148                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, menuPage.getName( ) );
149                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_DESCRIPTION, menuPage.getDescription( ) );
150                 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
151 
152                 // Seek of the sub-menus
153                 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
154 
155                 Collection<Page> listSubLevelMenuPages = PageHome.getChildPagesMinimalData( menuPage.getId( ) );
156                 int nSubLevelMenuIndex = 1;
157 
158                 for ( Page subLevelMenuPage : listSubLevelMenuPages )
159                 {
160                     if ( ( subLevelMenuPage.isVisible( request ) ) || ( nMode == PortalMenuService.MODE_ADMIN ) )
161                     {
162                         XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
163                         XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
164                         XmlUtil.addElement( strXml, XmlContent.TAG_SUBLEVEL_INDEX, nSubLevelMenuIndex );
165                         XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, subLevelMenuPage.getId( ) );
166                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, subLevelMenuPage.getName( ) );
167                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_DESCRIPTION, subLevelMenuPage.getDescription( ) );
168                         XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
169                         XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
170                     }
171                 }
172 
173                 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
174                 XmlUtil.endElement( strXml, XmlContent.TAG_MENU );
175                 nMenuIndex++;
176             }
177         }
178 
179         XmlUtil.endElement( strXml, XmlContent.TAG_MENU_LIST );
180 
181         StyleSheet xslSource;
182 
183         // Selection of the XSL stylesheet
184         switch( nMode )
185         {
186             case PortalMenuService.MODE_NORMAL:
187             case PortalMenuService.MODE_ADMIN:
188                 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_TREE, PortalMenuService.MODE_NORMAL );
189 
190                 break;
191 
192             default:
193                 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_TREE, nMode );
194 
195                 break;
196         }
197 
198         Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
199 
200         // Added in v1.3
201         // Add a path param for choose url to use in admin or normal mode
202         Map<String, String> mapParamRequest = new HashMap<>( );
203         PortalService.setXslPortalPath( mapParamRequest, nMode );
204 
205         XmlTransformerServicervice.html#XmlTransformerService">XmlTransformerService xmlTransformerService = new XmlTransformerService( );
206 
207         return xmlTransformerService.transformBySourceWithXslCache( strXml.toString( ), xslSource, mapParamRequest, outputProperties );
208     }
209 
210     /**
211      *
212      * @param nPageId
213      *            The page identifier
214      * @return The parent page identifier
215      */
216     private int getPageTree( int nPageId )
217     {
218         Page page = PageHome.getPage( nPageId );
219         int nParentPageId = page.getParentPageId( );
220 
221         if ( nParentPageId == 0 )
222         {
223             return nPageId;
224         }
225 
226         int nParentTree = nParentPageId;
227 
228         while ( nParentPageId != 0 )
229         {
230             nParentTree = nParentPageId;
231 
232             Page parentPage = PageHome.getPageWithoutImageContent( nParentPageId );
233             nParentPageId = parentPage.getParentPageId( );
234         }
235 
236         return nParentTree;
237     }
238 }