-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataListing.cfc
More file actions
348 lines (295 loc) · 10.8 KB
/
DataListing.cfc
File metadata and controls
348 lines (295 loc) · 10.8 KB
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
/**
* A widget that lists information from the ContentStore
*/
component extends="contentbox.models.ui.BaseWidget" singleton{
DataListing function init(required any controller){
// super init
super.init( arguments.controller );
// Widget Properties
setName( "DataListing" );
setVersion( "1.1" );
setDescription( "A widget that renders ContentStore data." );
setAuthor( "Computer Know How" );
setAuthorURL( "http://www.compknowhow.com" );
setIcon( "list" );
setCategory( "Content" );
return this;
}
/**
* Renders a file list
* @category.label Category
* @category.hint The category of the items from the ContentStore
* @category.optionsUDF getCategoryList
*
* @fields.label Fields
* @fields.hint The fields to include in the listing
*
* @sortField.label Sort Field
* @sortField.hint The field to sort by intitially
*
* @sortOrder.label Sort Order
* @sortOrder.hint The order to sort by intitially
* @sortOrder.optionsUDF getSortOrders
*
* @groupLinks.label Add Group In Page Links?
* @groupLinks.hint Display in page group linking?
*
* @grouping.label Setup Grouping?
* @grouping.hint Group the list by the group custom field?
*
* @headerLevel.label Header Level
* @headerLevel.hint The header level of the groups (h1, h2, h3...)
* @headerLevel.optionsUDF getHeaderLevels
*
* @listingType.label Listing Type
* @listingType.hint Type of listing
* @listingType.optionsUDF getListingTypes
*
* @class.label Class
* @class.hint Class(es) to apply to the listing table or list
*/
any function renderIt(required string category, string fields = "title,content", string sortField = "title", string sortOrder = "asc", boolean groupLinks = false, boolean grouping = false, numeric headerLevel = 3, string listingType = "table", string class="") {
var event = getRequestContext();
var local.sortField = event.getValue("sortField",arguments.sortField);
var local.sortOrder = event.getValue("sortOrder",arguments.sortOrder);
var rString = "";
var listings = getListings(arguments.category,local.sortField,local.sortOrder);
if (arguments.grouping) {
var groups = getGroups(listings);
}
var iteration = 1;
var currentGroup = "";
var fieldsArray = listToArray(arguments.fields);
saveContent variable="rString"{
// group in page links
if(arguments.groupLinks and arguments.grouping) {
var groupCount = 0;
writeOutput('<div class="group-links" style="margin-bottom:20px;">');
for( var group in groups ) {
groupCount++;
writeOutput('<a href="#cb.linkSelf()###' & group & '">' & group & '</a>');
if(groupCount < arrayLen(groups)) {
writeOutput(' | ');
}
}
writeOutput('</div>');
}
// loop over results and display
for( var listing in listings ) {
if(arguments.grouping) {
if(listing.group neq currentGroup) {
if(iteration gt 1) {writeOutput('</' & arguments.listingType & '>');}
if(groupLinks) {
writeOutput('<a name="' & listing.group & '"></a>');
}
writeOutput('<h' & arguments.headerLevel & '>' & listing.group & '</h' & arguments.headerLevel & '>');
writeOutput('<' & arguments.listingType & ' class="' & arguments.class & '">');
currentGroup = listing.group;
}
} else {
if(iteration eq 1) {
if(len(arguments.class)) {
writeOutput('<' & arguments.listingType & ' class="' & arguments.class & '">');
} else {
writeOutput('<' & arguments.listingType & '>');
}
if(arguments.listingType eq 'table') {
writeOutput('<thead><tr>');
for( var field in fieldsArray ) {
var oppositeDirection = ( local.sortOrder == "asc" ? "desc" : "asc" );
var sortIcon = ( local.sortOrder == "asc" ? "fa-chevron-circle-down" : "fa-chevron-circle-up" );
var link = cb.linkSelf() & "?sortField=#field#&sortOrder=#oppositeDirection#";
if(local.sortField eq field) {
writeOutput('<th><a href="#link#">#REReplace( field , "\b(\S)(\S*)\b" , "\u\1\L\2" , "all" )#</a> <i class="fa #sortIcon#"></i></th>');
} else {
writeOutput('<th><a href="#link#">#REReplace( field , "\b(\S)(\S*)\b" , "\u\1\L\2" , "all" )#</a></th>');
}
}
writeOutput('</tr></thead>');
}
}
}
if(arguments.listingType eq 'ul') {
var fieldIndex = 1;
writeOutput('<li>');
for( var field in fieldsArray ) {
field = "listing." & field;
writeOutput('#evaluate(field)#');
if( fieldIndex lt arrayLen(fieldsArray) ) {
writeOutput(' - ');
}
fieldIndex++;
}
writeOutput('</li>');
} else if (arguments.listingType eq 'dl') {
var fieldIndex = 1;
for( var field in fieldsArray ) {
field = "listing." & field;
if( fieldIndex eq 1 ) {
writeOutput('<dt>#evaluate(field)#</dt>');
} else {
writeOutput('<dd>#evaluate(field)#</dd>');
}
fieldIndex++;
}
} else if (arguments.listingType eq 'table') {
writeOutput('<tr>');
for( var field in fieldsArray ) {
field = "listing." & field;
writeOutput('<td>#evaluate(field)#</td>');
}
writeOutput('</tr>');
} else if (arguments.listingType eq 'div' and arguments.class eq 'testimonials') {
writeOutput('<blockquote>');
var testimonial = "listing.content";
var author = "listing.author";
var company = "listing.company";
writeOutput('#evaluate(testimonial)#');
writeOutput('<cite><span class="author">#evaluate(author)#</span> #evaluate(company)#</cite>');
writeOutput('</blockquote>');
} else if (arguments.listingType eq 'div' and arguments.class eq 'accordion') {
var question = "listing.title";
var answer = "listing.content";
writeOutput('<div class="panel panel-default">');
writeOutput('<div class="panel-heading">');
writeOutput('<h4 class="panel-title"><a data-parent="##accordion" data-toggle="collapse" href="##collapse-#category#-#iteration#" class="collapse" style="display: block;">#evaluate(question)#</a></h4>');
writeOutput('</div>');
writeOutput('<div class="panel-collapse collapse" id="collapse-#category#-#iteration#">');
writeOutput('<div class="panel-body">');
writeOutput('<p>#evaluate(answer)#</p>');
writeOutput('</div>');
writeOutput('</div>');
writeOutput('</div>');
}
if(iteration eq arrayLen(listings)) {writeOutput('</' & arguments.listingType & '>');}
iteration++;
}
}
return rString;
}
/**
* Return an array of ContentStore listings, the @ignore annotation means the ContentBox widget editors do not use it only used internally.
* @cbignore
*/
array function getListings(required string category, required string sortField, required string sortOrder){
var listings = [];
// get content store data in the specified category
if(len(arguments.category)) {
var contentStoreData = contentStoreService.findPublishedContent( category=arguments.category );
} else {
var contentStoreData = contentStoreService.findPublishedContent();
}
for( var item in contentStoreData.content ) {
// get root details that we care about
var data = {
"title" = item.getTitle(),
"content" = item.getContent(),
"contentID" = item.getContentID(),
"slug" = item.getSlug(),
"group" = ""
};
// check that there are custom fields first
if( item.hasCustomField() ) {
// get custom field data as a struct and append it to our data
var custom = item.getCustomFieldsAsStruct();
structAppend( data, custom );
}
// add the item to our array; now we have a nice array of structs to use however
arrayAppend( listings, data );
}
return arrayOfStructsSort(listings, arguments.sortField, arguments.sortOrder);
}
/**
* Return an array of groups, the @ignore annotation means the ContentBox widget editors do not use it only used internally.
* @cbignore
*/
array function getGroups(listings){
var groups = [];
for( var listing in arguments.listings ) {
if(!arrayFind(groups, listing.group)) {
// add the group to our array
arrayAppend(groups, listing.group);
}
}
return groups;
}
/**
* Return an array of categories, the @ignore annotation means the ContentBox widget editors do not use it only used internally.
* @cbignore
*/
array function getCategoryList(){
var categories = categoryService.getAllNames();
arrayPrepend(categories,"");
return categories;
}
/**
* Return an array of listing types, the @ignore annotation means the ContentBox widget editors do not use it only used internally.
* @cbignore
*/
array function getSortOrders(){
return ["asc","desc"];
}
/**
* Return an array of listing types, the @ignore annotation means the ContentBox widget editors do not use it only used internally.
* @cbignore
*/
array function getHeaderLevels(){
return [1,2,3,4,5,6];
}
/**
* Return an array of listing types, the @ignore annotation means the ContentBox widget editors do not use it only used internally.
* @cbignore
*/
array function getListingTypes(){
return ["ul","dl","table","div"];
}
/**
* Sorts an array of structures based on a key in the structures, the @ignore annotation means the ContentBox widget editors do not use it only used internally.
* @cbignore
*
* @param aofS - Array of structures. (Required)
* @param key - Key to sort by. (Required)
* @param sortOrder - Order to sort by, asc or desc. (Optional)
* @param sortType - Text, textnocase, or numeric. (Optional)
* @param delim - Delimiter used for temporary data storage. Must not exist in data. Defaults to a period. (Optional)
*
* @return Returns a sorted array.
* @author Nathan Dintenfass (nathan@changemedia.com)
* @version 1, April 4, 2013
*/
function arrayOfStructsSort(required aOfS, required key){
//by default we'll use an ascending sort
var sortOrder = "asc";
//by default, we'll use a textnocase sort
var sortType = "textnocase";
//by default, use ascii character 30 as the delim
var delim = ".";
//make an array to hold the sort stuff
var sortArray = arraynew(1);
//make an array to return
var returnArray = arraynew(1);
//grab the number of elements in the array (used in the loops)
var count = arrayLen(aOfS);
//make a variable to use in the loop
var ii = 1;
//if there is a 3rd argument, set the sortOrder
if(arraylen(arguments) GT 2)
sortOrder = arguments[3];
//if there is a 4th argument, set the sortType
if(arraylen(arguments) GT 3)
sortType = arguments[4];
//if there is a 5th argument, set the delim
if(arraylen(arguments) GT 4)
delim = arguments[5];
//loop over the array of structs, building the sortArray
for(ii = 1; ii lte count; ii = ii + 1)
sortArray[ii] = aOfS[ii][key] & delim & ii;
//now sort the array
arraySort(sortArray,sortType,sortOrder);
//now build the return array
for(ii = 1; ii lte count; ii = ii + 1)
returnArray[ii] = aOfS[listLast(sortArray[ii],delim)];
//return the array
return returnArray;
}
}