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.pagelinkservice.web;
35
36 import fr.paris.lutece.plugins.pagelinkservice.business.PageLinkService;
37 import fr.paris.lutece.plugins.pagelinkservice.business.PageLinkServiceHome;
38 import fr.paris.lutece.portal.business.page.Page;
39 import fr.paris.lutece.portal.business.page.PageHome;
40 import fr.paris.lutece.portal.business.user.AdminUser;
41 import fr.paris.lutece.portal.service.admin.AdminUserService;
42 import fr.paris.lutece.portal.service.message.AdminMessage;
43 import fr.paris.lutece.portal.service.message.AdminMessageService;
44 import fr.paris.lutece.portal.service.page.IPageService;
45 import fr.paris.lutece.portal.service.page.PageResourceIdService;
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.spring.SpringContextService;
49 import fr.paris.lutece.portal.service.template.AppTemplateService;
50 import fr.paris.lutece.portal.service.util.AppPathService;
51 import fr.paris.lutece.portal.service.util.AppPropertiesService;
52 import fr.paris.lutece.portal.web.constants.Messages;
53 import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
54 import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
55 import fr.paris.lutece.util.html.HtmlTemplate;
56 import fr.paris.lutece.util.url.UrlItem;
57
58 import org.apache.commons.lang3.StringEscapeUtils;
59
60 import java.util.ArrayList;
61 import java.util.Collection;
62 import java.util.HashMap;
63
64 import javax.servlet.http.HttpServletRequest;
65
66
67
68
69
70 public class PageLinkServiceJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
71 {
72
73
74 private static final String REGEX_PAGE_ID = "^[\\d]+$";
75
76
77 private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
78 private static final String PARAMETER_PAGE_NAME = "page_name";
79 private static final String PARAMETER_PAGE_ID = "page";
80 private static final String PARAMETER_PAGE_ID_URL = "page_id";
81 private static final String PARAMETER_ALT = "alt";
82 private static final String PARAMETER_TARGET = "target";
83 private static final String PARAMETER_NAME = "name";
84 private static final String PARAMETER_INPUT = "input";
85
86
87 private static final String PROPERTY_SEARCH_MESSAGE = "message.warning.resulsearch.empty";
88
89
90 private static final String MARK_PLUGIN_NAME = "plugin_name";
91 private static final String MARK_SEARCH_MESSAGE = "search_message";
92 private static final String MARK_PAGES_LIST = "pages_list";
93 private static final String MARK_LIST_PAGE = "list_page";
94 private static final String MARK_URL = "url";
95 private static final String MARK_TARGET = "target";
96 private static final String MARK_ALT = "alt";
97 private static final String MARK_NAME = "name";
98 private static final String MARK_INPUT = "input";
99
100
101 private static final String TEMPLATE_SELECTOR_PAGE = "admin/plugins/pagelinkservice/pagelinkservice_selector.html";
102 private static final String TEMPLATE_LINK = "admin/plugins/pagelinkservice/pagelinkservice_link.html";
103 private AdminUser _user;
104 private Plugin _plugin;
105 private String _input;
106 private IPageService _pageService = (IPageService) SpringContextService.getBean("pageService");
107
108
109
110
111
112
113
114
115
116
117 public String getInsertServiceSelectorUI( HttpServletRequest request )
118 {
119 init( request );
120
121 AdminUser user = AdminUserService.getAdminUser( request );
122 String strSearch = request.getParameter( PARAMETER_PAGE_NAME );
123 String strResultSearch = ( strSearch == null ) ? "" : strSearch;
124 String strPageName = replaceString( strResultSearch, "'", "''" );
125
126 HashMap model = getDefaultModel( );
127
128 Collection<PageLinkService> listPages = PageLinkServiceHome.getPageListbyName( strPageName );
129 Collection<PageLinkService> listPagesAuthorized = new ArrayList<PageLinkService>( );
130
131 for ( PageLinkService pageLinkService : listPages )
132 {
133 if ( _pageService.isAuthorizedAdminPage( pageLinkService.getIdPage( ),
134 PageResourceIdService.PERMISSION_VIEW, user ) )
135 {
136 listPagesAuthorized.add( pageLinkService );
137 }
138 }
139
140 model.put( MARK_PAGES_LIST, listPagesAuthorized );
141
142 StringBuilder strListPage = new StringBuilder( );
143
144 if ( listPages.isEmpty() )
145 {
146 model.put( MARK_SEARCH_MESSAGE, AppPropertiesService.getProperty( PROPERTY_SEARCH_MESSAGE ) );
147 }
148
149 String strBaseUrl = AppPathService.getBaseUrl( request );
150
151
152 model.put( MARK_URL, strBaseUrl );
153 model.put( MARK_LIST_PAGE, strListPage.toString( ) );
154
155 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_SELECTOR_PAGE, _user.getLocale( ), model );
156
157 return template.getHtml( );
158 }
159
160
161
162
163
164
165
166
167
168 private String replaceString( String str, String oldStr, String newStr )
169 {
170
171 String cleanString = str;
172
173 int index = 0;
174
175 while ( true )
176 {
177 index = cleanString.lastIndexOf( oldStr );
178
179 if ( ( index == 2 ) || ( index == -1 ) )
180 {
181 break;
182 }
183
184 cleanString = cleanString.substring( 0, index ) + newStr +
185 cleanString.substring( index + oldStr.length( ) );
186 }
187
188 return cleanString;
189 }
190
191
192
193
194
195
196
197 public String doInsertUrl( HttpServletRequest request )
198 {
199 init( request );
200
201 String strPageId = request.getParameter( PARAMETER_PAGE_ID );
202 String strTarget = request.getParameter( PARAMETER_TARGET );
203 String strAlt = request.getParameter( PARAMETER_ALT );
204 String strName = request.getParameter( PARAMETER_NAME );
205 HashMap<String, Object> model = new HashMap<String, Object>( );
206
207 Page page = null;
208
209 if ( ( strPageId == null ) || !strPageId.matches( REGEX_PAGE_ID ) )
210 {
211 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
212 }
213
214 page = PageHome.findByPrimaryKey( Integer.parseInt( strPageId ) );
215
216 UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
217 url.addParameter( PARAMETER_PAGE_ID_URL, page.getId( ) );
218 model.put( MARK_URL, url.getUrl( ) );
219 model.put( MARK_TARGET, strTarget );
220 model.put( MARK_ALT, strAlt );
221 model.put( MARK_NAME, ( strName.length( ) == 0 ) ? page.getName( ) : strName );
222
223 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_LINK, null, model );
224
225 return insertUrl( request, _input, StringEscapeUtils.escapeEcmaScript( template.getHtml( )) );
226 }
227
228 private void init( HttpServletRequest request )
229 {
230 String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
231 _user = AdminUserService.getAdminUser( request );
232 _plugin = PluginService.getPlugin( strPluginName );
233 _input = request.getParameter( PARAMETER_INPUT );
234 }
235
236 private HashMap getDefaultModel( )
237 {
238 HashMap model = new HashMap( );
239 model.put( MARK_PLUGIN_NAME, _plugin.getName( ) );
240 model.put( MARK_INPUT, _input );
241
242 return model;
243 }
244 }