2013-02-23 04:41:12 +08:00
|
|
|
/**
|
|
|
|
Our data model for interacting with site settings.
|
|
|
|
|
2013-03-02 01:45:25 +08:00
|
|
|
@class SiteSetting
|
2013-02-23 04:41:12 +08:00
|
|
|
@extends Discourse.Model
|
|
|
|
@namespace Discourse
|
|
|
|
@module Discourse
|
2013-03-02 01:45:25 +08:00
|
|
|
**/
|
2013-02-23 04:41:12 +08:00
|
|
|
Discourse.SiteSetting = Discourse.Model.extend({
|
2013-03-02 01:45:25 +08:00
|
|
|
|
2014-06-10 03:17:36 +08:00
|
|
|
validationMessage: null,
|
|
|
|
|
2013-03-02 01:45:25 +08:00
|
|
|
/**
|
|
|
|
Is the boolean setting true?
|
|
|
|
|
|
|
|
@property enabled
|
|
|
|
**/
|
|
|
|
enabled: function(key, value) {
|
|
|
|
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
// get the boolean value of the setting
|
|
|
|
if (this.blank('value')) return false;
|
|
|
|
return this.get('value') === 'true';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// set the boolean value of the setting
|
|
|
|
this.set('value', value ? 'true' : 'false');
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
|
|
|
|
2013-03-02 01:45:25 +08:00
|
|
|
}.property('value'),
|
|
|
|
|
2014-04-04 04:07:59 +08:00
|
|
|
/**
|
|
|
|
The name of the setting. Basically, underscores in the setting key are replaced with spaces.
|
|
|
|
|
|
|
|
@property settingName
|
|
|
|
**/
|
|
|
|
settingName: function() {
|
|
|
|
return this.get('setting').replace(/\_/g, ' ');
|
|
|
|
}.property('setting'),
|
|
|
|
|
2013-03-02 01:45:25 +08:00
|
|
|
/**
|
|
|
|
Has the user changed the setting? If so we should save it.
|
|
|
|
|
|
|
|
@property dirty
|
|
|
|
**/
|
|
|
|
dirty: function() {
|
|
|
|
return this.get('originalValue') !== this.get('value');
|
|
|
|
}.property('originalValue', 'value'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
Has the setting been overridden from its default value?
|
|
|
|
|
|
|
|
@property overridden
|
|
|
|
**/
|
|
|
|
overridden: function() {
|
|
|
|
var val = this.get('value');
|
|
|
|
var defaultVal = this.get('default');
|
|
|
|
|
|
|
|
if (val === null) val = '';
|
|
|
|
if (defaultVal === null) defaultVal = '';
|
|
|
|
|
|
|
|
return val.toString() !== defaultVal.toString();
|
|
|
|
}.property('value'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
Reset the setting to its original value.
|
|
|
|
|
|
|
|
@method resetValue
|
|
|
|
**/
|
2013-02-23 04:41:12 +08:00
|
|
|
resetValue: function() {
|
|
|
|
this.set('value', this.get('originalValue'));
|
2014-06-10 03:17:36 +08:00
|
|
|
this.set('validationMessage', null);
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
|
|
|
|
2013-03-02 01:45:25 +08:00
|
|
|
/**
|
|
|
|
Save the setting's value.
|
|
|
|
|
|
|
|
@method save
|
|
|
|
**/
|
2013-02-23 04:41:12 +08:00
|
|
|
save: function() {
|
|
|
|
// Update the setting
|
2014-06-10 03:17:36 +08:00
|
|
|
var self = this, data = {};
|
2014-01-06 20:03:53 +08:00
|
|
|
data[this.get('setting')] = this.get('value');
|
|
|
|
return Discourse.ajax("/admin/site_settings/" + this.get('setting'), {
|
|
|
|
data: data,
|
2013-04-04 04:06:55 +08:00
|
|
|
type: 'PUT'
|
|
|
|
}).then(function() {
|
2014-06-10 03:17:36 +08:00
|
|
|
self.set('originalValue', self.get('value'));
|
|
|
|
self.set('validationMessage', null);
|
|
|
|
}, function(e) {
|
|
|
|
if (e.responseJSON && e.responseJSON.errors) {
|
|
|
|
self.set('validationMessage', e.responseJSON.errors[0]);
|
|
|
|
} else {
|
|
|
|
self.set('validationMessage', I18n.t('generic_error'));
|
|
|
|
}
|
2013-02-23 04:41:12 +08:00
|
|
|
});
|
2013-06-11 23:39:55 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
validValues: function() {
|
2013-08-24 05:35:01 +08:00
|
|
|
var vals, setting;
|
2013-06-11 23:39:55 +08:00
|
|
|
vals = Em.A();
|
2013-08-24 05:35:01 +08:00
|
|
|
setting = this;
|
2013-06-12 02:23:52 +08:00
|
|
|
_.each(this.get('valid_values'), function(v) {
|
2013-08-24 05:35:01 +08:00
|
|
|
if (v.name && v.name.length > 0) {
|
|
|
|
if (setting.translate_names) {
|
|
|
|
vals.addObject({name: I18n.t(v.name), value: v.value});
|
|
|
|
} else {
|
|
|
|
vals.addObject(v);
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 23:39:55 +08:00
|
|
|
});
|
|
|
|
return vals;
|
2013-06-12 02:23:52 +08:00
|
|
|
}.property('valid_values'),
|
|
|
|
|
|
|
|
allowsNone: function() {
|
|
|
|
if ( _.indexOf(this.get('valid_values'), '') >= 0 ) return 'admin.site_settings.none';
|
2013-06-11 23:39:55 +08:00
|
|
|
}.property('valid_values')
|
2013-02-23 04:41:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
Discourse.SiteSetting.reopenClass({
|
2013-03-02 01:45:25 +08:00
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
findAll: function() {
|
2013-11-14 03:02:47 +08:00
|
|
|
return Discourse.ajax("/admin/site_settings").then(function (settings) {
|
|
|
|
// Group the results by category
|
|
|
|
var categoryNames = [],
|
|
|
|
categories = {},
|
|
|
|
result = Em.A();
|
2013-06-11 04:48:50 +08:00
|
|
|
_.each(settings.site_settings,function(s) {
|
2013-02-23 04:41:12 +08:00
|
|
|
s.originalValue = s.value;
|
2013-11-14 03:02:47 +08:00
|
|
|
if (!categoryNames.contains(s.category)) {
|
|
|
|
categoryNames.pushObject(s.category);
|
|
|
|
categories[s.category] = Em.A();
|
|
|
|
}
|
|
|
|
categories[s.category].pushObject(Discourse.SiteSetting.create(s));
|
|
|
|
});
|
|
|
|
_.each(categoryNames, function(n) {
|
2013-11-15 01:37:41 +08:00
|
|
|
result.pushObject({nameKey: n, name: I18n.t('admin.site_settings.categories.' + n), siteSettings: categories[n]});
|
2013-02-21 02:15:50 +08:00
|
|
|
});
|
2013-11-14 03:02:47 +08:00
|
|
|
return result;
|
2013-02-23 04:41:12 +08:00
|
|
|
});
|
2013-05-04 05:55:04 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
update: function(key, value) {
|
2013-05-08 01:30:12 +08:00
|
|
|
return Discourse.ajax("/admin/site_settings/" + key, {
|
2013-05-04 05:55:04 +08:00
|
|
|
type: 'PUT',
|
|
|
|
data: { value: value }
|
|
|
|
});
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
2013-11-14 03:02:47 +08:00
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
});
|
2013-02-21 02:15:50 +08:00
|
|
|
|
|
|
|
|