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.style;
35
36 import fr.paris.lutece.portal.business.style.Theme;
37 import fr.paris.lutece.portal.business.style.ThemeHome;
38 import fr.paris.lutece.portal.service.message.AdminMessage;
39 import fr.paris.lutece.portal.service.message.AdminMessageService;
40 import fr.paris.lutece.portal.service.portal.ThemesService;
41 import fr.paris.lutece.portal.service.template.AppTemplateService;
42 import fr.paris.lutece.portal.service.util.AppPathService;
43 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
44 import fr.paris.lutece.portal.web.constants.Messages;
45 import fr.paris.lutece.util.html.HtmlTemplate;
46 import fr.paris.lutece.util.http.SecurityUtil;
47
48 import java.util.HashMap;
49
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52
53
54
55
56
57 public class ThemesJspBean extends AdminFeaturesPageJspBean
58 {
59
60
61
62
63 public static final String RIGHT_MANAGE_THEMES = "CORE_THEMES_MANAGEMENT";
64
65
66
67
68 private static final long serialVersionUID = -8201123021058332937L;
69
70
71 private static final String TEMPLATE_MANAGE_THEMES = "admin/style/manage_themes.html";
72 private static final String TEMPLATE_CREATE_THEME = "admin/style/create_theme.html";
73 private static final String TEMPLATE_MODIFY_THEME = "admin/style/modify_theme.html";
74
75
76 private static final String MARK_THEMES_LIST = "themes_list";
77 private static final String MARK_THEME = "theme_default";
78
79
80 private static final String PARAMETER_THEME = "theme";
81 private static final String PARAMETER_URL = "url";
82 private static final String BASE_URL = "base_url";
83 private static final String THEME_LICENCE = "theme_licence";
84 private static final String THEME_VERSION = "theme_version";
85 private static final String THEME_AUTHOR_URL = "theme_author_url";
86 private static final String THEME_AUTHOR = "theme_author";
87 private static final String PATH_CSS = "path_css";
88 private static final String PATH_JS = "path_js";
89 private static final String PATH_IMAGES = "path_images";
90 private static final String THEME_DESCRIPTION = "theme_description";
91 private static final String CODE_THEME = "code_theme";
92
93
94
95
96
97
98
99 public String getManageThemes( HttpServletRequest request )
100 {
101 HashMap<String, Object> model = new HashMap<String, Object>( );
102
103 model.put( MARK_THEMES_LIST, ThemeHome.getThemesList( ) );
104 model.put( MARK_THEME, ThemesService.getGlobalThemeObject( ) );
105 model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
106
107 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_THEMES, getLocale( ), model );
108
109 return getAdminPage( template.getHtml( ) );
110 }
111
112
113
114
115
116
117
118 public String doModifyGlobalTheme( HttpServletRequest request )
119 {
120 String strTheme = request.getParameter( PARAMETER_THEME );
121 ThemesService.setGlobalTheme( strTheme );
122
123 return this.getHomeUrl( request );
124 }
125
126
127
128
129
130
131
132
133 public String doModifyUserTheme( HttpServletRequest request, HttpServletResponse response )
134 {
135 String strTheme = request.getParameter( PARAMETER_THEME );
136 String strForwardUrl = request.getParameter( PARAMETER_URL );
137
138 if ( !SecurityUtil.containsCleanParameters( request ) )
139 {
140 return AppPathService.getBaseUrl( request );
141 }
142
143 ThemesService.setUserTheme( request, response, strTheme );
144
145 return strForwardUrl;
146 }
147
148
149
150
151
152
153 public String getCreateTheme( HttpServletRequest request )
154 {
155 HashMap<String, Object> model = new HashMap<String, Object>( );
156 model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
157
158 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_THEME, getLocale( ), model );
159
160 return getAdminPage( template.getHtml( ) );
161 }
162
163
164
165
166
167
168 public String getModifyTheme( HttpServletRequest request )
169 {
170 Theme themeToModify = ThemeHome.findByPrimaryKey( request.getParameter( CODE_THEME ) );
171
172 HashMap<String, Object> model = new HashMap<String, Object>( );
173 model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
174 model.put( PARAMETER_THEME, themeToModify );
175
176 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_THEME, getLocale( ), model );
177
178 return getAdminPage( template.getHtml( ) );
179 }
180
181
182
183
184
185
186 public String doModifyTheme( HttpServletRequest request )
187 {
188 Theme theme = getThemeFromRequest( request );
189
190
191 if ( isMissingFields( request ) )
192 {
193 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
194 }
195
196 ThemeHome.update( theme );
197
198 return this.getHomeUrl( request );
199 }
200
201
202
203
204
205
206 public String doCreateTheme( HttpServletRequest request )
207 {
208 Theme theme = getThemeFromRequest( request );
209
210
211 if ( isMissingFields( request ) )
212 {
213 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
214 }
215
216 ThemeHome.create( theme );
217
218 return this.getHomeUrl( request );
219 }
220
221
222
223
224
225
226 private Theme getThemeFromRequest( HttpServletRequest request )
227 {
228 Theme theme = new Theme( );
229
230 theme.setCodeTheme( request.getParameter( CODE_THEME ) );
231 theme.setThemeDescription( request.getParameter( THEME_DESCRIPTION ) );
232 theme.setPathImages( request.getParameter( PATH_IMAGES ) );
233 theme.setPathCss( request.getParameter( PATH_CSS ) );
234 theme.setPathJs( request.getParameter( PATH_JS ) );
235 theme.setThemeAuthor( request.getParameter( THEME_AUTHOR ) );
236 theme.setThemeAuthorUrl( request.getParameter( THEME_AUTHOR_URL ) );
237 theme.setThemeVersion( request.getParameter( THEME_VERSION ) );
238 theme.setThemeLicence( request.getParameter( THEME_LICENCE ) );
239
240 return theme;
241 }
242
243
244
245
246
247 private boolean isMissingFields( HttpServletRequest request )
248 {
249 return request.getParameter( CODE_THEME ).equals( "" ) ||
250 request.getParameter( THEME_DESCRIPTION ).equals( "" ) || request.getParameter( PATH_IMAGES ).equals( "" ) ||
251 request.getParameter( PATH_CSS ).equals( "" ) || request.getParameter( PATH_JS ).equals( "" );
252 }
253 }