-
Notifications
You must be signed in to change notification settings - Fork 460
/
UtilHTML.java
492 lines (386 loc) · 16.3 KB
/
UtilHTML.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package com.dotmarketing.util;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.beans.Inode;
import com.dotmarketing.beans.WebAsset;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.IdentifierAPI;
import com.dotmarketing.business.Versionable;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.factories.InodeFactory;
import com.dotmarketing.factories.WebAssetFactory;
import com.dotmarketing.portlets.categories.business.CategoryAPI;
import com.dotmarketing.portlets.categories.model.Category;
import com.dotmarketing.portlets.containers.model.Container;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.files.model.File;
import com.dotmarketing.portlets.languagesmanager.business.LanguageAPI;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.portlets.links.model.Link;
import com.dotmarketing.portlets.templates.model.Template;
import com.liferay.portal.model.User;
public class UtilHTML {
private static CategoryAPI categoryAPI = APILocator.getCategoryAPI();
private static LanguageAPI langAPI = APILocator.getLanguageAPI();
private static IdentifierAPI identifierAPI = APILocator.getIdentifierAPI();
private static HostAPI hostAPI = APILocator.getHostAPI();
public static String convertContentsToHTML(ArrayList<Contentlet> contents){
StringBuffer buffy = new StringBuffer();
for (Contentlet contentlet : contents) {
// contentlet.get
}
return buffy.toString();
}
public static String getStatusIcons(Versionable v) throws DotStateException, DotDataException, DotSecurityException{
StringBuffer buf = new StringBuffer();
if(!v.isLive() && v.isWorking() && (!v.isArchived() && v.isWorking())){
buf.append("<span class='workingIcon'></span>");
}else{
buf.append("<span class='greyDotIcon' style='opacity:.4'></span>");
}
if(v.isArchived()){
buf.append("<span class='archivedIcon'></span>");
}else if(APILocator.getVersionableAPI().hasLiveVersion(v)){
buf.append("<span class='liveIcon'></span>");
}else{
buf.append("<span class='greyDotIcon' style='opacity:.4'></span>");
}
if(v.isLocked()){
buf.append("<span class='lockIcon'></span>");
}
return buf.toString();
}
public static String getVersionStatusIcons(Versionable v) throws DotStateException, DotDataException, DotSecurityException{
StringBuffer buf = new StringBuffer();
if(v.isWorking()){
buf.append("<span class='workingIcon'></span>");
}
if(v.isLive()){
buf.append("<span class='liveIcon'></span>");
}
return buf.toString();
}
public static String getSelectCategories(Inode parent, int level, Inode current, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
level++;
Logger.debug(UtilHTML.class, "getSelectCategories!!!");
List<Category> categories = (parent == null) ? categoryAPI.findTopLevelCategories(user, respectFrontendRoles) : categoryAPI
.getChildren(parent, user, respectFrontendRoles);
StringBuffer strHTML = new StringBuffer();
Iterator<Category> m = categories.iterator();
while (m.hasNext()) {
Category cat = (Category) m.next();
boolean canUse = categoryAPI.canUseCategory(cat, user, respectFrontendRoles);
if(canUse){
if (level == 1) {
strHTML.append("<option value=\"" + cat.getInode() + "\" class='topCat' ");
} else {
strHTML.append("<option value=\"" + cat.getInode() + "\"");
}
if (current != null) {
Iterator<Category> l = categoryAPI.getParents(current, user, respectFrontendRoles).iterator();
while (l.hasNext()) {
Category disCat = (Category) l.next();
if (cat.getInode().equalsIgnoreCase(disCat.getInode())) {
strHTML.append(" selected");
}
}
}
strHTML.append(">");
for (int k = 0; k < (level - 1); k++) {
strHTML.append(" ");
}
strHTML.append("+ ");
strHTML.append(cat.getCategoryName() + "</option>\n");
if (categoryAPI.getChildren(cat, user, respectFrontendRoles).size() > 0) {
strHTML.append(getSelectCategories(cat, level, current, user, respectFrontendRoles));
}
}
}
return strHTML.toString();
}
public static String getSelectLanguages(Contentlet contentlet) {
List<Language> languages = langAPI.getLanguages();
StringBuffer strHTML = new StringBuffer();
Iterator<Language> m = languages.iterator();
while (m.hasNext()) {
Language language = (Language) m.next();
strHTML.append("<option value=\"" + language.getId() + "\"");
if (language.getId() == contentlet.getLanguageId()) {
strHTML.append(" selected");
}
strHTML.append(">");
strHTML.append(language.getLanguage() + " (" + language.getCountry() + ")</option>\n");
}
return strHTML.toString();
}
public static String getSelectCategories(Inode parent, int level, String currentCats, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
CategoryAPI catAPI = APILocator.getCategoryAPI();
level++;
Logger.debug(UtilHTML.class, "getSelectCategories!!!");
List<Category> categories = (parent == null) ? categoryAPI.findTopLevelCategories(user, respectFrontendRoles) : categoryAPI
.getChildren(parent, user, respectFrontendRoles);
StringBuffer strHTML = new StringBuffer();
Iterator<Category> m = categories.iterator();
while (m.hasNext()) {
Category cat = (Category) m.next();
boolean canUse = catAPI.canUseCategory(cat, user, respectFrontendRoles);
if(canUse){
if (level == 1) {
strHTML.append("<option value=\"" + cat.getInode() + "\" class='topCat' ");
} else {
strHTML.append("<option value=\"" + cat.getInode() + "\"");
}
if (currentCats.indexOf(cat.getInode() + ",") != -1) {
Logger.debug(UtilHTML.class, "found the same objects!!!!");
strHTML.append(" selected");
}
strHTML.append(">");
for (int k = 0; k < (level - 1); k++) {
strHTML.append(" ");
}
if (level != 0)
strHTML.append("+ ");
strHTML.append(cat.getCategoryName() + "</option>\n");
if (categoryAPI.getChildren(cat, user, respectFrontendRoles).size() > 0) {
strHTML.append(getSelectCategories(cat, level, currentCats, user, respectFrontendRoles));
}
}
}
return strHTML.toString();
}
public static String getAssetIcon(WebAsset webasset) {
String imgSrc = "";
if (webasset instanceof Container) {
imgSrc = "/html/images/icons/layout-select-content.png";
} else if (webasset instanceof Template) {
imgSrc = "/html/images/icons/layout-header-3-mix.png";
} else if (webasset instanceof Link) {
imgSrc = "/html/images/icons/chain.png";
} else if (webasset instanceof File) {
imgSrc = "/icon?i=" + ((File) webasset).getFileName();
} else if (webasset instanceof com.dotmarketing.portlets.htmlpages.model.HTMLPage) {
imgSrc = "/html/images/icons/blog-blue.png";
}
return "<img src=\"" + imgSrc + "\" width=16 height=16 />";
}
public static String getAssetIcon(Contentlet con){
return "<img src=\"/html/images/icons/document-text-image.png\" width=16 height=16 />";
}
public static String getSelectCategoriesTextMode(Inode parent, int level, String currentCats,User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
CategoryAPI catAPI = APILocator.getCategoryAPI();
level++;
Logger.debug(UtilHTML.class, "getSelectCategories!!!");
List<Category> categories = (parent == null) ? categoryAPI.findTopLevelCategories(user, respectFrontendRoles) :
categoryAPI.getChildren(parent, user, respectFrontendRoles);
StringBuffer strHTML = new StringBuffer();
Iterator<Category> m = categories.iterator();
while (m.hasNext()) {
Category cat = (Category) m.next();
boolean canUse = catAPI.canUseCategory(cat, user, respectFrontendRoles);
if(canUse){
if (currentCats.indexOf(cat.getInode() + ",") != -1) {
Logger.debug(UtilHTML.class, "found the same objects!!!!");
strHTML.append("<tr><td>");
strHTML.append(cat.getCategoryName());
strHTML.append("</td></tr>");
}
if (categoryAPI.getChildren(cat, user, respectFrontendRoles).size() > 0) {
strHTML.append(getSelectCategoriesTextMode(cat, level, currentCats, user, respectFrontendRoles));
}
}
}
return strHTML.toString();
}
public static String getSelectCategories(Inode parent, int level, String[] currentCats, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
StringBuffer buffer = new StringBuffer();
if (currentCats != null) {
for (int i = 0; i < currentCats.length; i++) {
buffer.append(currentCats[i] + ",");
}
}
return getSelectCategories(parent, level, buffer.toString(), user, respectFrontendRoles);
}
public static String getSelectCategoriesTextMode(Inode parent, int level, String[] currentCats,User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
StringBuffer buffer = new StringBuffer();
if (currentCats != null) {
for (int i = 0; i < currentCats.length; i++) {
buffer.append(currentCats[i] + ",");
}
}
return getSelectCategoriesTextMode(parent, level, buffer.toString(), user, respectFrontendRoles);
}
public static List<Template> getSelectTemplates(Host host, User user) {
List<Template> result = new ArrayList<Template>();
String condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse();
// Gets roles
try {
List<WebAsset> templates = WebAssetFactory.getAssetsWorkingWithPermission(Template.class, 100000, 0, "title", null, user);
Iterator<WebAsset> templatesIter = templates.iterator();
Template template;
if (host == null) {
while (templatesIter.hasNext()) {
template = (Template) templatesIter.next();
result.add(template);
}
} else {
Identifier identifier;
Host systemHost = hostAPI.findSystemHost(user, false);
while (templatesIter.hasNext()) {
template = (Template) templatesIter.next();
identifier = identifierAPI.findFromInode(template.getIdentifier());
if (identifier.getHostId().equals(host.getIdentifier()) || !UtilMethods.isSet(identifier.getHostId()) || ((systemHost != null) && identifier.getHostId().equals(systemHost.getIdentifier()))) {
result.add(template);
}
}
}
} catch (Exception e) {
}
return result;
}
public static String getSelectCategories(Inode current, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
return getSelectCategories(null, 0, current, user, respectFrontendRoles);
}
public static String getSelectCategories(String x, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
Category current = categoryAPI.find(x, user, respectFrontendRoles);
return getSelectCategories(null, 0, current, user, respectFrontendRoles);
}
public static String getSelectCategoriesByParent(Inode parent, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
Inode current = new Inode();
Category parentCat = (Category) parent;
StringBuffer strHTML = new StringBuffer();
strHTML.append("<option value=\"" + parent.getInode() + "\" class='topCat'>");
strHTML.append(parentCat.getCategoryName() + "</option>\n");
strHTML.append(getSelectCategories(parent, 1, current, user, respectFrontendRoles));
return strHTML.toString();
}
public static String getSelectCategories(Inode parent, int level, Inode current, int maxlevel,
User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
ArrayList idList = new ArrayList();
if (current instanceof Category) {
idList.add(current.getInode());
} else {
List cats = InodeFactory.getParentsOfClass(current, Category.class);
Iterator it = cats.iterator();
while (it.hasNext()) {
Category cat = (Category) it.next();
idList.add(cat.getInode());
}
}
String[] catsArr = (String[]) idList.toArray(new String[0]);
return getSelectCategories(parent, level, catsArr, maxlevel, user, respectFrontendRoles);
}
public static String getSelectCategories(Inode parent, int level, String[] selectedCategoriesIds, int maxlevel,
User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
level++;
List categories = (parent == null) ? categoryAPI.findTopLevelCategories(user, respectFrontendRoles) :
categoryAPI.getChildren(parent, user, respectFrontendRoles);
StringBuffer strHTML = new StringBuffer();
Iterator m = categories.iterator();
while (m.hasNext()) {
Category cat = (Category) m.next();
if (level == 1) {
strHTML.append("<option value=\"" + cat.getInode() + "\" class=\"topCat\" ");
} else {
strHTML.append("<option value=\"" + cat.getInode() + "\"");
}
if (selectedCategoriesIds != null) {
for (int i = 0; i < selectedCategoriesIds.length; i++) {
String id = selectedCategoriesIds[i];
if (cat.getInode().equalsIgnoreCase(id)) {
strHTML.append(" selected");
}
}
}
strHTML.append(">");
for (int k = 0; k < (level - 1); k++) {
strHTML.append(" ");
}
strHTML.append("+ ");
strHTML.append(cat.getCategoryName() + "</option>");
if (level <= maxlevel && categoryAPI.getChildren(cat, user, respectFrontendRoles).size() > 0) {
strHTML.append(getSelectCategories(cat, level, selectedCategoriesIds, maxlevel, user, respectFrontendRoles));
}
}
return strHTML.toString();
}
public static String getSelectCategories(Inode parent, int level, Identifier current, User user, boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
level++;
Logger.debug(UtilHTML.class, "getSelectCategories!!!");
List<Category> categories = (parent == null) ? categoryAPI.findTopLevelCategories(user, respectFrontendRoles) : categoryAPI
.getChildren(parent, user, respectFrontendRoles);
StringBuffer strHTML = new StringBuffer();
Iterator<Category> m = categories.iterator();
while (m.hasNext()) {
Category cat = (Category) m.next();
boolean canUse = categoryAPI.canUseCategory(cat, user, respectFrontendRoles);
if(canUse){
if (level == 1) {
strHTML.append("<option value=\"" + cat.getInode() + "\" class='topCat' ");
} else {
strHTML.append("<option value=\"" + cat.getInode() + "\"");
}
if (current != null) {
Iterator<Category> l = categoryAPI.getParents(current, user, respectFrontendRoles).iterator();
while (l.hasNext()) {
Category disCat = (Category) l.next();
if (cat.getInode().equalsIgnoreCase(disCat.getInode())) {
strHTML.append(" selected");
}
}
}
strHTML.append(">");
for (int k = 0; k < (level - 1); k++) {
strHTML.append(" ");
}
strHTML.append("+ ");
strHTML.append(cat.getCategoryName() + "</option>\n");
if (categoryAPI.getChildren(cat, user, respectFrontendRoles).size() > 0) {
strHTML.append(getSelectCategories(cat, level, current, user, respectFrontendRoles));
}
}
}
return strHTML.toString();
}
public static String htmlEncode(String data)
{
final StringBuffer buf = new StringBuffer();
if(UtilMethods.isSet(data))
{
final char[] chars = data.toCharArray();
for (int i = 0; i < chars.length; i++)
{
if (chars[i] == ';')
buf.append(";");
else
buf.append("&#" + (int) chars[i]+";");
}
}
return buf.toString();
}
public static String escapeHTMLSpecialChars (String data){
return UtilMethods.escapeHTMLSpecialChars (data);
}
/**
* This method escape the double quotes into html representacion
* @param fixme The text where the double quates exists
* @return String
* @author Oswaldo Gallango
* @version 1.0
*/
public static String escapeDoubleQuotesToHTMLEspecialChars(String fixme) {
fixme = fixme.replaceAll("\"", """);
return fixme;
}
public static CategoryAPI getCategoryAPI() {
return categoryAPI;
}
public static void setCategoryAPI(CategoryAPI categoryAPI) {
UtilHTML.categoryAPI = categoryAPI;
}
}