1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
62
63 public class TreeMenuInclude implements PageInclude
64 {
65
66
67 private static final int PORTAL_COMPONENT_MENU_TREE = 7;
68
69
70 private static final String PROPERTY_ROOT_TREE = "lutece.root.tree";
71
72
73
74
75
76
77
78
79
80
81
82
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
108
109
110
111
112
113
114
115
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
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
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
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
201
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
213
214
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 }