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.plugins.newsletter.web;
35
36 import fr.paris.lutece.plugins.newsletter.business.NewsLetter;
37 import fr.paris.lutece.plugins.newsletter.business.NewsLetterHome;
38 import fr.paris.lutece.plugins.newsletter.business.NewsLetterProperties;
39 import fr.paris.lutece.plugins.newsletter.business.NewsletterPropertiesHome;
40 import fr.paris.lutece.plugins.newsletter.service.NewsLetterArchiveService;
41 import fr.paris.lutece.plugins.newsletter.service.NewsLetterRegistrationService;
42 import fr.paris.lutece.plugins.newsletter.util.NewsLetterConstants;
43 import fr.paris.lutece.portal.service.captcha.CaptchaSecurityService;
44 import fr.paris.lutece.portal.service.i18n.I18nService;
45 import fr.paris.lutece.portal.service.message.SiteMessageException;
46 import fr.paris.lutece.portal.service.plugin.Plugin;
47 import fr.paris.lutece.portal.service.plugin.PluginService;
48 import fr.paris.lutece.portal.service.template.AppTemplateService;
49 import fr.paris.lutece.portal.web.xpages.XPage;
50 import fr.paris.lutece.portal.web.xpages.XPageApplication;
51 import fr.paris.lutece.util.html.HtmlTemplate;
52
53 import java.util.Collection;
54 import java.util.HashMap;
55 import java.util.Locale;
56 import java.util.Map;
57
58 import javax.servlet.http.HttpServletRequest;
59
60 import static fr.paris.lutece.portal.service.admin.AdminUserService.getLocale;
61
62
63
64
65 public class NewsLetterApp implements XPageApplication
66 {
67
68 private static final String TEMPLATE_XPAGE_NEWSLETTER = "skin/plugins/newsletter/page_newsletter.html";
69 private static final String TEMPLATE_XPAGE_TOS = "skin/plugins/newsletter/tos.html";
70
71
72 private static final String PROPERTY_PATHLABEL = "newsletter.pagePathLabel";
73 private static final String PROPERTY_PAGETITLE = "newsletter.pageTitle";
74
75
76 private static final String MARK_NEWSLETTERS_LIST = "newsletters_list";
77 private static final String MARK_PLUGIN = "plugin";
78 private static final String MARK_TOS = "tos";
79 private static final String MARK_CAPTCHA = "captcha";
80 private static final String MARK_IS_ACTIVE_CAPTCHA = "is_active_captcha";
81 private static final String MARK_PROPERTIES = "properties";
82
83
84 private static final String PARAMETER_VIEW_REQUIREMENT = "view_requirement";
85
86
87 private static final String JCAPTCHA_PLUGIN = "jcaptcha";
88
89
90 private CaptchaSecurityService _captchaService;
91
92
93
94
95
96
97
98
99 public void init( HttpServletRequest request, Plugin plugin )
100 {
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws SiteMessageException
117 {
118 String strAction = request.getParameter( NewsLetterConstants.PARAMETER_ACTION );
119 init( request, plugin );
120
121 XPage resultPage = null;
122
123 if ( strAction == null )
124 {
125 return getPage( request, plugin );
126 }
127
128 if ( strAction.equals( NewsLetterConstants.ACTION_REGISTER ) )
129 {
130 NewsLetterRegistrationService.getInstance( ).doSubscription( request );
131 }
132 else
133 if ( strAction.equals( NewsLetterConstants.ACTION_CONFIRM_UNSUBSCRIBE ) )
134 {
135 NewsLetterRegistrationService.getInstance( ).doConfirmUnSubscribe( request );
136 }
137 else
138 if ( strAction.equals( NewsLetterConstants.ACTION_UNSUBSCRIBE ) )
139 {
140 NewsLetterRegistrationService.getInstance( ).doUnSubscribe( request );
141 }
142 else
143 if ( strAction.equals( NewsLetterConstants.ACTION_CONFIRM_SUBSCRIBE ) )
144 {
145 NewsLetterRegistrationService.getInstance( ).doConfirmSubscribe( request );
146 }
147
148 resultPage = getPage( request, plugin );
149
150 return resultPage;
151 }
152
153
154
155
156
157
158
159
160
161
162 private XPage getPage( HttpServletRequest request, Plugin plugin )
163 {
164 XPage page = new XPage( );
165
166 if ( request.getParameter( PARAMETER_VIEW_REQUIREMENT ) != null )
167 {
168
169 page.setTitle( I18nService.getLocalizedString( PROPERTY_PATHLABEL, getLocale( request ) ) );
170 page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGETITLE, getLocale( request ) ) );
171 page.setContent( getRequirement( request, plugin ) );
172 }
173 else
174 {
175 page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PATHLABEL, getLocale( request ) ) );
176 page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGETITLE, getLocale( request ) ) );
177
178 HashMap<String, Object> model = new HashMap<String, Object>( );
179 Collection<NewsLetter> list = NewsLetterHome.findAll( plugin );
180 NewsLetterProperties properties = NewsletterPropertiesHome.find( plugin );
181 model.put( MARK_PROPERTIES, properties );
182 model.put( MARK_NEWSLETTERS_LIST, list );
183 model.put( MARK_TOS, properties.getTOS( ) );
184
185 model.put( MARK_PLUGIN, plugin );
186
187 boolean bIsCaptchaEnabled = PluginService.isPluginEnable( JCAPTCHA_PLUGIN );
188 model.put( MARK_IS_ACTIVE_CAPTCHA, bIsCaptchaEnabled );
189
190 if ( bIsCaptchaEnabled )
191 {
192 _captchaService = new CaptchaSecurityService( );
193 model.put( MARK_CAPTCHA, _captchaService.getHtmlCode( ) );
194 }
195 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_NEWSLETTER, getLocale( request ), model );
196 page.setContent( template.getHtml( ) );
197 }
198
199 return page;
200 }
201
202
203
204
205
206
207
208
209
210
211 public String getShowArchivePage( HttpServletRequest request ) throws SiteMessageException
212 {
213 return NewsLetterArchiveService.getInstance( ).getShowArchivePage( request );
214 }
215
216
217
218
219
220
221
222
223
224 public void doSubscription( HttpServletRequest request ) throws SiteMessageException
225 {
226 NewsLetterRegistrationService.getInstance( ).doSubscription( request );
227 }
228
229
230
231
232
233
234
235
236
237 public void doUnSubscribe( HttpServletRequest request ) throws SiteMessageException
238 {
239 NewsLetterRegistrationService.getInstance( ).doUnSubscribe( request );
240 }
241
242
243
244
245
246
247
248
249
250 public void doConfirmSubscribe( HttpServletRequest request ) throws SiteMessageException
251 {
252 NewsLetterRegistrationService.getInstance( ).doConfirmSubscribe( request );
253 }
254
255
256
257
258
259
260
261
262
263
264 private String getRequirement( HttpServletRequest request, Plugin plugin )
265 {
266 Map<String, Object> model = new HashMap<String, Object>( );
267 Locale locale = getLocale( request );
268
269 NewsLetterProperties properties = NewsletterPropertiesHome.find( plugin );
270 model.put( MARK_TOS, properties.getTOS( ) );
271
272 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_TOS, locale, model );
273
274 return template.getHtml( );
275 }
276
277 }