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.style.Theme;
37 import fr.paris.lutece.portal.service.content.PageData;
38 import fr.paris.lutece.portal.service.includes.PageInclude;
39 import fr.paris.lutece.portal.service.plugin.Plugin;
40 import fr.paris.lutece.portal.service.plugin.PluginService;
41 import fr.paris.lutece.portal.service.portal.PortalService;
42 import fr.paris.lutece.portal.service.template.AppTemplateService;
43 import fr.paris.lutece.portal.web.xpages.XPageApplicationEntry;
44 import fr.paris.lutece.util.html.HtmlTemplate;
45
46 import java.util.Collection;
47 import java.util.HashMap;
48 import java.util.Locale;
49 import java.util.Map;
50
51 import javax.servlet.http.HttpServletRequest;
52
53
54
55
56
57 public class LinksInclude implements PageInclude
58 {
59
60 private static final String PARAMETER_PAGE = "page";
61
62
63 private static final String MARK_FAVOURITE = "favourite";
64 private static final String MARK_PORTAL_NAME = "lutece_name";
65 private static final String MARK_PLUGIN_THEME_CSS = "plugin_theme";
66 private static final String MARK_PLUGINS_CSS_LINKS = "plugins_css_links";
67 private static final String MARK_PLUGINS_JAVASCRIPT_LINKS = "plugins_javascript_links";
68 private static final String MARK_PLUGIN_CSS_STYLESHEET = "plugin_css_stylesheet";
69 private static final String MARK_PLUGIN_JAVASCRIPT_FILE = "plugin_javascript_file";
70 private static final String MARK_CSS_PREFIX = "css_prefix";
71
72
73 private static final String TEMPLATE_PLUGIN_CSS_LINK = "skin/site/plugin_css_link.html";
74 private static final String TEMPLATE_PLUGIN_JAVASCRIPT_LINK = "skin/site/plugin_javascript_link.html";
75 private static final String PREFIX_PLUGINS_CSS = "css/plugins/";
76 private static final String ABSOLUTE_URL = "http://";
77
78
79
80
81
82
83
84
85 public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
86 {
87 if ( request != null )
88 {
89
90 String strFavourite = ( data.getFavourite( ) != null ) ? data.getFavourite( )
91 : PortalService.getSiteName( );
92 String strPortalName = PortalService.getSiteName( );
93 rootModel.put( MARK_FAVOURITE, strFavourite );
94 rootModel.put( MARK_PORTAL_NAME, strPortalName );
95
96 Locale locale = request.getLocale( );
97
98
99 Collection<Plugin> listPlugins = PluginService.getPluginList( );
100 listPlugins.add( PluginService.getCore( ) );
101
102 StringBuilder sbCssLinks = new StringBuilder( );
103 StringBuilder sbJsLinks = new StringBuilder( );
104
105 for ( Plugin plugin : listPlugins )
106 {
107 if ( plugin.isInstalled( ) )
108 {
109 String strPage = request.getParameter( PARAMETER_PAGE );
110 Theme xpageTheme = plugin.getXPageTheme( request );
111
112 if ( ( strPage != null ) && ( xpageTheme != null ) )
113 {
114 for ( XPageApplicationEntry entry : plugin.getApplications( ) )
115 {
116 if ( strPage.equals( entry.getId( ) ) )
117 {
118 rootModel.put( MARK_PLUGIN_THEME_CSS, xpageTheme );
119 }
120 }
121 }
122
123 boolean bXPage = isPluginXPage( strPage, plugin );
124
125 if ( plugin.isCssStylesheetsScopePortal( ) || ( bXPage && plugin.isCssStylesheetsScopeXPage( ) ) )
126 {
127 for ( String strCssStyleSheet : plugin.getCssStyleSheets( ) )
128 {
129 appendStyleSheet( sbCssLinks, strCssStyleSheet, locale );
130 }
131
132 for ( String strCssStyleSheet : plugin.getCssStyleSheets( nMode ) )
133 {
134 appendStyleSheet( sbCssLinks, strCssStyleSheet, locale );
135 }
136 }
137
138 if ( plugin.isJavascriptFilesScopePortal( ) || ( bXPage && plugin.isJavascriptFilesScopeXPage( ) ) )
139 {
140 for ( String strJavascriptFile : plugin.getJavascriptFiles( ) )
141 {
142 appendJavascriptFile( sbJsLinks, strJavascriptFile, locale );
143 }
144
145 for ( String strJavascriptFile : plugin.getJavascriptFiles( nMode ) )
146 {
147 appendJavascriptFile( sbJsLinks, strJavascriptFile, locale );
148 }
149 }
150 }
151 }
152
153 rootModel.put( MARK_PLUGINS_CSS_LINKS, sbCssLinks.toString( ) );
154 rootModel.put( MARK_PLUGINS_JAVASCRIPT_LINKS, sbJsLinks.toString( ) );
155 }
156 }
157
158
159
160
161
162
163
164 private void appendJavascriptFile( StringBuilder sbJsLinks, String strJavascriptFile, Locale locale )
165 {
166 Map<String, String> model = new HashMap<String, String>( 1 );
167 model.put( MARK_PLUGIN_JAVASCRIPT_FILE, strJavascriptFile );
168
169 HtmlTemplate tJs = AppTemplateService.getTemplate( TEMPLATE_PLUGIN_JAVASCRIPT_LINK, locale, model );
170 sbJsLinks.append( tJs.getHtml( ) );
171 }
172
173
174
175
176
177
178
179 private void appendStyleSheet( StringBuilder sbCssLinks, String strCssStyleSheet, Locale locale )
180 {
181 String strPrefix = ( strCssStyleSheet.startsWith( ABSOLUTE_URL ) ) ? "" : PREFIX_PLUGINS_CSS;
182
183 Map<String, String> model = new HashMap<String, String>( 2 );
184 model.put( MARK_PLUGIN_CSS_STYLESHEET, strCssStyleSheet );
185 model.put( MARK_CSS_PREFIX, strPrefix );
186
187 HtmlTemplate tCss = AppTemplateService.getTemplate( TEMPLATE_PLUGIN_CSS_LINK, locale, model );
188 sbCssLinks.append( tCss.getHtml( ) );
189 }
190
191
192
193
194
195
196
197 private boolean isPluginXPage( String strPage, Plugin plugin )
198 {
199 if ( ( strPage != null ) )
200 {
201 for ( XPageApplicationEntry app : plugin.getApplications( ) )
202 {
203 if ( strPage.equals( app.getId( ) ) )
204 {
205 return true;
206 }
207 }
208 }
209
210 return false;
211 }
212 }