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
64 public class TreeMenuInclude implements PageInclude
65 {
66
67
68 private static final int PORTAL_COMPONENT_MENU_TREE = 7;
69
70
71 private static final String PROPERTY_ROOT_TREE = "lutece.root.tree";
72
73
74
75
76
77
78
79
80 public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
81 {
82 if ( request != null )
83 {
84 int nCurrentPageId;
85
86 try
87 {
88 nCurrentPageId = ( request.getParameter( Parameters.PAGE_ID ) == null )
89 ? PortalService.getRootPageId( ) : Integer.parseInt( request.getParameter( Parameters.PAGE_ID ) );
90 }
91 catch ( NumberFormatException nfe )
92 {
93 AppLogService.info( "TreeMenuInclude.fillTemplate() : " + nfe.getLocalizedMessage( ) );
94 nCurrentPageId = 0;
95 }
96
97 data.setTreeMenu( buildTreeMenuContent( nCurrentPageId, nMode, request ) );
98 rootModel.put( Markers.PAGE_TREE_MENU, ( data.getTreeMenu( ) == null ) ? "" : data.getTreeMenu( ) );
99 }
100 }
101
102
103
104
105
106
107
108
109
110 public String buildTreeMenuContent( int nIdPage, int nMode, HttpServletRequest request )
111 {
112 StringBuffer strXml = new StringBuffer( );
113
114 String strCurrentPageId = Integer.toString( nIdPage );
115
116 String strTreeOnRoot = AppPropertiesService.getProperty( PROPERTY_ROOT_TREE );
117 Collection<Page> listPagesMenu;
118
119
120 if ( strTreeOnRoot.equalsIgnoreCase( "true" ) )
121 {
122 listPagesMenu = PageHome.getChildPagesMinimalData( getPageTree( nIdPage ) );
123 }
124 else
125 {
126 listPagesMenu = PageHome.getChildPagesMinimalData( nIdPage );
127 }
128
129 strXml.append( XmlUtil.getXmlHeader( ) );
130 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU_LIST );
131
132 int nMenuIndex = 1;
133
134 for ( Page menuPage : listPagesMenu )
135 {
136 if ( ( menuPage.isVisible( request ) ) || ( nMode == PortalMenuService.MODE_ADMIN ) )
137 {
138 XmlUtil.beginElement( strXml, XmlContent.TAG_MENU );
139 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
140 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, menuPage.getId( ) );
141 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, menuPage.getName( ) );
142 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_DESCRIPTION, menuPage.getDescription( ) );
143 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
144
145
146 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
147
148 Collection<Page> listSubLevelMenuPages = PageHome.getChildPagesMinimalData( menuPage.getId( ) );
149 int nSubLevelMenuIndex = 1;
150
151 for ( Page subLevelMenuPage : listSubLevelMenuPages )
152 {
153 if ( ( subLevelMenuPage.isVisible( request ) ) || ( nMode == PortalMenuService.MODE_ADMIN ) )
154 {
155 XmlUtil.beginElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
156 XmlUtil.addElement( strXml, XmlContent.TAG_MENU_INDEX, nMenuIndex );
157 XmlUtil.addElement( strXml, XmlContent.TAG_SUBLEVEL_INDEX, nSubLevelMenuIndex );
158 XmlUtil.addElement( strXml, XmlContent.TAG_PAGE_ID, subLevelMenuPage.getId( ) );
159 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_NAME, subLevelMenuPage.getName( ) );
160 XmlUtil.addElementHtml( strXml, XmlContent.TAG_PAGE_DESCRIPTION,
161 subLevelMenuPage.getDescription( ) );
162 XmlUtil.addElementHtml( strXml, XmlContent.TAG_CURRENT_PAGE_ID, strCurrentPageId );
163 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU );
164 }
165 }
166
167 XmlUtil.endElement( strXml, XmlContent.TAG_SUBLEVEL_MENU_LIST );
168 XmlUtil.endElement( strXml, XmlContent.TAG_MENU );
169 nMenuIndex++;
170 }
171 }
172
173 XmlUtil.endElement( strXml, XmlContent.TAG_MENU_LIST );
174
175 StyleSheet xslSource;
176
177
178 switch ( nMode )
179 {
180 case PortalMenuService.MODE_NORMAL:
181 case PortalMenuService.MODE_ADMIN:
182 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_TREE, PortalMenuService.MODE_NORMAL );
183
184 break;
185
186 default:
187 xslSource = PortalComponentHome.getXsl( PORTAL_COMPONENT_MENU_TREE, nMode );
188
189 break;
190 }
191
192 Properties outputProperties = ModeHome.getOuputXslProperties( nMode );
193
194
195
196 Map<String, String> mapParamRequest = new HashMap<String, String>( );
197 PortalService.setXslPortalPath( mapParamRequest, nMode );
198
199 XmlTransformerService xmlTransformerService = new XmlTransformerService( );
200
201 return xmlTransformerService.transformBySourceWithXslCache( strXml.toString( ), xslSource, mapParamRequest,
202 outputProperties );
203 }
204
205
206
207
208
209
210 private int getPageTree( int nPageId )
211 {
212 Page page = PageHome.getPage( nPageId );
213 int nParentPageId = page.getParentPageId( );
214
215 if ( nParentPageId == 0 )
216 {
217 return nPageId;
218 }
219
220 int nParentTree = nParentPageId;
221
222
223 while ( nParentPageId != 0 )
224 {
225 nParentTree = nParentPageId;
226
227 Page parentPage = PageHome.getPageWithoutImageContent( nParentPageId );
228 nParentPageId = parentPage.getParentPageId( );
229 }
230
231 return nParentTree;
232 }
233 }