-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
SettingService.cfc
executable file
·333 lines (290 loc) · 9.34 KB
/
SettingService.cfc
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
/**
* ContentBox - A Modular Content Platform
* Copyright since 2012 by Ortus Solutions, Corp
* www.ortussolutions.com/products/contentbox
* ---
* Setting Service for contentbox
*/
component extends="cborm.models.VirtualEntityService" accessors="true" threadsafe singleton{
// DI properties
property name="cachebox" inject="cachebox";
property name="moduleSettings" inject="coldbox:setting:modules";
property name="appMapping" inject="coldbox:setting:appMapping";
property name="requestService" inject="coldbox:requestService";
/**
* The cache provider name. This is lazy-loaded upon request
*/
property name="cacheProviderName";
/**
* Constructor
*/
SettingService function init(){
// init it
super.init( entityName="cbSetting" );
variables.cacheProviderName = "";
return this;
}
/**
* Retrieve a multi-tenant settings cache key
*/
string function getSettingsCacheKey(){
return "cb-settings-#cgi.http_host#";
}
/**
* Check if the installer and dsn creator modules are present
*/
struct function isInstallationPresent(){
var results = { installer = false, dsncreator = false };
if( structKeyExists( moduleSettings, "contentbox-installer" ) AND
directoryExists( moduleSettings[ "contentbox-installer" ].path ) ){
results.installer = true;
}
if( structKeyExists( moduleSettings, "contentbox-dsncreator" ) AND
directoryExists( moduleSettings[ "contentbox-dsncreator" ].path ) ){
results.dsncreator = true;
}
return results;
}
/**
* Delete the installer module
*/
boolean function deleteInstaller(){
if( structKeyExists( moduleSettings, "contentbox-installer" ) AND
directoryExists( moduleSettings[ "contentbox-installer" ].path ) ){
directoryDelete( moduleSettings[ "contentbox-installer" ].path, true );
return true;
}
return false;
}
/**
* Delete the dsn creator module
*/
boolean function deleteDSNCreator(){
if( structKeyExists( moduleSettings, "contentbox-dsncreator" ) AND
directoryExists( moduleSettings[ "contentbox-dsncreator" ].path ) ){
directoryDelete( moduleSettings[ "contentbox-dsncreator" ].path, true );
return true;
}
return false;
}
/**
* Check if contentbox has been installed by checking if there are no settings and no cb_active ONLY
*/
boolean function isCBReady(){
var thisCount = newCriteria()
.isEq( "name", "cb_active" )
.count();
return( thisCount > 0 ? true : false );
}
/**
* Mark cb as ready to serve
*/
function activateCB(){
var s = new( properties={ name="cb_active", value="true" } );
save( s );
return this;
}
/**
* Get a setting
* @name The name of the seting
* @defaultValue The default value if setting not found.
*/
function getSetting( required name, defaultValue ){
var s = getAllSettings( asStruct=true );
if( structKeyExists( s, arguments.name ) ){
return s[ arguments.name ];
}
if( structKeyExists( arguments, "defaultValue" ) ){
return arguments.defaultValue;
}
throw(
message = "Setting #arguments.name# not found in settings collection",
detail = "Registered settings are: #structKeyList(s)#",
type = "contentbox.SettingService.SettingNotFound"
);
}
/**
* Get all settings
* @asStruct Indicator if we should return structs or array of objects
*
* @return struct or array of objects
*/
function getAllSettings( boolean asStruct=false ){
var cache = getSettingsCacheProvider();
var cacheKey = getSettingsCacheKey();
// retrieve all settings from cache
var settings = cache.get( getSettingsCacheKey() );
// found in cache?
if( isNull( settings ) ){
// not found, so query db
var settings = list( sortOrder="name" );
// cache them for 2 hours
cache.set( cacheKey, settings, 90 );
}
// convert to struct
if( arguments.asStruct ){
var s = {};
for( var x=1; x lte settings.recordcount; x++ ){
s[ settings.name[ x ] ] = settings.value[ x ];
}
return s;
}
return settings;
}
/**
* flush settings cache for current multi-tenant host
*/
SettingService function flushSettingsCache(){
getSettingsCacheProvider().clear( getSettingsCacheKey() );
return this;
}
/**
* Bulk saving of options using a memento structure of options
*/
any function bulkSave( struct memento ){
var settings = getAllSettings( asStruct=true );
var oOption = "";
var newOptions = [];
// iterate over settings
for(var key in settings){
// save only sent in setting keys
if( structKeyExists( memento, key ) ){
oOption = findWhere( { name=key } );
oOption.setValue( memento[ key ] );
arrayAppend( newOptions, oOption );
}
}
// save new settings and flush cache
saveAll( newOptions );
flushSettingsCache();
return this;
}
/**
* Build file browser settings structure
*/
struct function buildFileBrowserSettings(){
var cbSettings = getAllSettings(asStruct=true);
var settings = {
directoryRoot = expandPath( cbSettings.cb_media_directoryRoot ),
createFolders = cbSettings.cb_media_createFolders,
deleteStuff = cbSettings.cb_media_allowDelete,
allowDownload = cbSettings.cb_media_allowDownloads,
allowUploads = cbSettings.cb_media_allowUploads,
acceptMimeTypes = cbSettings.cb_media_acceptMimeTypes,
quickViewWidth = cbSettings.cb_media_quickViewWidth,
loadJQuery = false,
useMediaPath = true,
html5uploads = {
maxFileSize = cbSettings.cb_media_html5uploads_maxFileSize,
maxFiles = cbSettings.cb_media_html5uploads_maxFiles
}
};
// Base MediaPath
var mediaPath = ( len( AppMapping ) ? AppMapping : "" ) & "/";
if( findNoCase( "index.cfm", requestService.getContext().getSESBaseURL() ) ){
mediaPath = "index.cfm" & mediaPath;;
}
// add the entry point
var entryPoint = moduleSettings[ "contentbox-ui" ].entryPoint;
mediaPath &= ( len( entryPoint ) ? "#entryPoint#/" : "" ) & "__media";
// Store it
mediaPath = ( left( mediaPath,1 ) == '/' ? mediaPath : "/" & mediaPath );
settings.mediaPath =mediaPath;
return settings;
}
/**
* setting search returns struct with keys [settings,count]
*/
struct function search( search="", max=0, offset=0, sortOrder="name asc" ){
var results = {};
// criteria queries
var c = newCriteria();
// Search Criteria
if( len(arguments.search) ){
c.like( "name","%#arguments.search#%" );
}
// run criteria query and projections count
results.count = c.count( "settingID" );
results.settings = c.resultTransformer( c.DISTINCT_ROOT_ENTITY )
.list(offset=arguments.offset, max=arguments.max, sortOrder=sortOrder, asQuery=false);
return results;
}
/**
* Get all data prepared for export
*/
array function getAllForExport(){
var c = newCriteria();
return c.withProjections( property="settingID,name,value" )
.resultTransformer( c.ALIAS_TO_ENTITY_MAP )
.list( sortOrder="name" );
}
/**
* Import data from a ContentBox JSON file. Returns the import log
*/
string function importFromFile(required importFile, boolean override=false){
var data = fileRead( arguments.importFile );
var importLog = createObject( "java", "java.lang.StringBuilder" ).init( "Starting import with override = #arguments.override#...<br>" );
if( !isJSON( data ) ){
throw(message="Cannot import file as the contents is not JSON", type="InvalidImportFormat" );
}
// deserialize packet: Should be array of { settingID, name, value }
return importFromData( deserializeJSON( data ), arguments.override, importLog );
}
/**
* Import data from an array of structures of settings
*/
string function importFromData(required importData, boolean override=false, importLog){
var allSettings = [];
// iterate and import
for( var thisSetting in arguments.importData ){
var args = { name = thisSetting.name };
var oSetting = findWhere( criteria=args );
// if null, then create it
if( isNull( oSetting ) ){
var args = { name = thisSetting.name, value = javaCast( "string", thisSetting.value ) };
arrayAppend( allSettings, new( properties=args ) );
// logs
importLog.append( "New setting imported: #thisSetting.name#<br>" );
}
// else only override if true
else if( arguments.override ){
oSetting.setValue( javaCast( "string", thisSetting.value ) );
arrayAppend( allSettings, oSetting );
importLog.append( "Overriding setting: #thisSetting.name#<br>" );
}
else{
importLog.append( "Skipping setting: #thisSetting.name#<br>" );
}
}
// Save them?
if( arrayLen( allSettings ) ){
saveAll( allSettings );
importLog.append( "Saved all imported and overriden settings!" );
}
else{
importLog.append( "No settings imported as none where found or able to be overriden from the import file." );
}
return importLog.toString();
}
/**
* Get the cache provider object to be used for settings
* @return coldbox.system.cache.ICacheProvider
*/
function getSettingsCacheProvider(){
if( !len( variables.cacheProviderName ) ){
lock name="settingsCacheProviderName" timeout="10" throwOnTimeout="true"{
if( !len( variables.cacheProviderName ) ){
// query db for cache name
var cacheProvider = newCriteria()
.isEq( "name", "cb_site_settings_cache" )
.get();
// lazy load it.
variables.cacheProviderName = cacheprovider.getValue();
}
}
}
// Return the cache to use.
return cacheBox.getCache( variables.cacheProviderName );
}
/******************************** PRIVATE ************************************************/
}