2014-03-05 07:52:20 -05:00
|
|
|
/**
|
|
|
|
This controller supports the interface for dealing with badges.
|
|
|
|
|
|
|
|
@class AdminBadgesController
|
|
|
|
@extends Ember.ArrayController
|
|
|
|
@namespace Discourse
|
|
|
|
@module Discourse
|
|
|
|
**/
|
2014-07-22 23:20:45 -04:00
|
|
|
export default Ember.ArrayController.extend({
|
2014-07-27 04:22:01 -04:00
|
|
|
needs: ['modal'],
|
2014-07-22 23:20:45 -04:00
|
|
|
itemController: 'admin-badge',
|
2014-05-19 01:20:57 -04:00
|
|
|
queryParams: ['badgeId'],
|
2014-05-21 03:22:42 -04:00
|
|
|
badgeId: Em.computed.alias('selectedId'),
|
2014-05-19 01:20:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
ID of the currently selected badge.
|
|
|
|
|
2014-05-21 03:22:42 -04:00
|
|
|
@property selectedId
|
2014-05-19 01:20:57 -04:00
|
|
|
@type {Integer}
|
|
|
|
**/
|
2014-05-21 03:22:42 -04:00
|
|
|
selectedId: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
Badge that is currently selected.
|
2014-05-23 22:33:46 -04:00
|
|
|
|
2014-05-21 03:22:42 -04:00
|
|
|
@property selectedItem
|
|
|
|
@type {Discourse.Badge}
|
|
|
|
**/
|
|
|
|
selectedItem: function() {
|
2014-05-23 22:33:46 -04:00
|
|
|
if (this.get('selectedId') === undefined || this.get('selectedId') === "undefined") {
|
|
|
|
// New Badge
|
|
|
|
return this.get('newBadge');
|
|
|
|
} else {
|
|
|
|
// Existing Badge
|
|
|
|
var selectedId = parseInt(this.get('selectedId'));
|
|
|
|
return this.get('model').filter(function(badge) {
|
|
|
|
return parseInt(badge.get('id')) === selectedId;
|
|
|
|
})[0];
|
|
|
|
}
|
|
|
|
}.property('selectedId', 'newBadge'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
Unsaved badge, if one exists.
|
|
|
|
|
|
|
|
@property newBadge
|
|
|
|
@type {Discourse.Badge}
|
|
|
|
**/
|
|
|
|
newBadge: function() {
|
2014-05-21 03:22:42 -04:00
|
|
|
return this.get('model').filter(function(badge) {
|
2014-05-23 22:33:46 -04:00
|
|
|
return badge.get('id') === undefined;
|
2014-05-21 03:22:42 -04:00
|
|
|
})[0];
|
2014-05-23 22:33:46 -04:00
|
|
|
}.property('model.@each.id'),
|
|
|
|
|
|
|
|
/**
|
|
|
|
Whether a new unsaved badge exists.
|
|
|
|
|
|
|
|
@property newBadgeExists
|
|
|
|
@type {Discourse.Badge}
|
|
|
|
**/
|
|
|
|
newBadgeExists: Em.computed.notEmpty('newBadge'),
|
2014-03-05 07:52:20 -05:00
|
|
|
|
|
|
|
/**
|
2014-05-19 00:29:12 -04:00
|
|
|
We don't allow setting a description if a translation for the given badge
|
|
|
|
name exists.
|
2014-03-05 07:52:20 -05:00
|
|
|
|
|
|
|
@property canEditDescription
|
|
|
|
@type {Boolean}
|
|
|
|
**/
|
|
|
|
canEditDescription: Em.computed.none('selectedItem.translatedDescription'),
|
|
|
|
|
2014-03-19 01:13:38 -04:00
|
|
|
/**
|
|
|
|
Disable saving if the currently selected item is being saved.
|
|
|
|
|
|
|
|
@property disableSave
|
|
|
|
@type {Boolean}
|
|
|
|
**/
|
|
|
|
disableSave: Em.computed.alias('selectedItem.saving'),
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
actions: {
|
|
|
|
|
|
|
|
/**
|
|
|
|
Create a new badge and select it.
|
|
|
|
|
|
|
|
@method newBadge
|
|
|
|
**/
|
2014-05-23 22:33:46 -04:00
|
|
|
createNewBadge: function() {
|
2014-03-05 07:52:20 -05:00
|
|
|
var badge = Discourse.Badge.create({
|
|
|
|
name: I18n.t('admin.badges.new_badge')
|
|
|
|
});
|
|
|
|
this.pushObject(badge);
|
|
|
|
this.send('selectBadge', badge);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Select a particular badge.
|
|
|
|
|
|
|
|
@method selectBadge
|
|
|
|
@param {Discourse.Badge} badge The badge to be selected
|
|
|
|
**/
|
|
|
|
selectBadge: function(badge) {
|
2014-05-21 03:22:42 -04:00
|
|
|
this.set('selectedId', badge.get('id'));
|
2014-03-05 07:52:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Save the selected badge.
|
|
|
|
|
|
|
|
@method save
|
|
|
|
**/
|
|
|
|
save: function() {
|
2014-03-19 01:13:38 -04:00
|
|
|
if (!this.get('disableSave')) {
|
2014-07-24 04:28:09 -04:00
|
|
|
var fields = ['allow_title', 'multiple_grant',
|
|
|
|
'listable', 'auto_revoke',
|
|
|
|
'enabled', 'show_posts',
|
|
|
|
'target_posts', 'name', 'description',
|
|
|
|
'icon', 'query', 'badge_grouping_id',
|
FEATURE: Badge query validation, preview results, and EXPLAIN
Upon saving a badge or requesting a badge result preview,
BadgeGranter.contract_checks! will examine the provided badge SQL for
some contractual obligations - namely, the returned columns and use of
trigger parameters.
Saving the badge is wrapped in a transaction to make this easier, by
raising ActiveRecord::Rollback on a detected violation.
On the client, a modal view is added for the badge query sample run
results, named admin-badge-preview.
The preview action is moved up to the route.
The save action, on failure, triggers a 'saveError' action (also in the
route).
The preview action gains a new parameter, 'explain', which will give the
output of an EXPLAIN query for the badge sql, which can be used by forum
admins to estimate the cost of their badge queries.
The preview link is replaced by two links, one which omits (false) and
includes (true) the EXPLAIN query.
The Badge.save() method is amended to propogate errors.
Badge::Trigger gets some utility methods for use in the
BadgeGranter.contract_checks! method.
Additionally, extra checks outside of BadgeGranter.contract_checks! are
added in the preview() method, to cover cases of null granted_at
columns.
An uninitialized variable path is removed in the backfill() method.
TODO - it would be nice to be able to get the actual names of all
columns the provided query returns, so we could give more errors
2014-08-25 18:17:29 -04:00
|
|
|
'trigger', 'badge_type_id'],
|
|
|
|
self = this;
|
2014-07-24 04:28:09 -04:00
|
|
|
|
FEATURE: Badge query validation, preview results, and EXPLAIN
Upon saving a badge or requesting a badge result preview,
BadgeGranter.contract_checks! will examine the provided badge SQL for
some contractual obligations - namely, the returned columns and use of
trigger parameters.
Saving the badge is wrapped in a transaction to make this easier, by
raising ActiveRecord::Rollback on a detected violation.
On the client, a modal view is added for the badge query sample run
results, named admin-badge-preview.
The preview action is moved up to the route.
The save action, on failure, triggers a 'saveError' action (also in the
route).
The preview action gains a new parameter, 'explain', which will give the
output of an EXPLAIN query for the badge sql, which can be used by forum
admins to estimate the cost of their badge queries.
The preview link is replaced by two links, one which omits (false) and
includes (true) the EXPLAIN query.
The Badge.save() method is amended to propogate errors.
Badge::Trigger gets some utility methods for use in the
BadgeGranter.contract_checks! method.
Additionally, extra checks outside of BadgeGranter.contract_checks! are
added in the preview() method, to cover cases of null granted_at
columns.
An uninitialized variable path is removed in the backfill() method.
TODO - it would be nice to be able to get the actual names of all
columns the provided query returns, so we could give more errors
2014-08-25 18:17:29 -04:00
|
|
|
if (this.get('selectedItem.system')){
|
2014-07-24 04:28:09 -04:00
|
|
|
var protectedFields = this.get('protectedSystemFields');
|
|
|
|
fields = _.filter(fields, function(f){
|
|
|
|
return !_.include(protectedFields,f);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
FEATURE: Badge query validation, preview results, and EXPLAIN
Upon saving a badge or requesting a badge result preview,
BadgeGranter.contract_checks! will examine the provided badge SQL for
some contractual obligations - namely, the returned columns and use of
trigger parameters.
Saving the badge is wrapped in a transaction to make this easier, by
raising ActiveRecord::Rollback on a detected violation.
On the client, a modal view is added for the badge query sample run
results, named admin-badge-preview.
The preview action is moved up to the route.
The save action, on failure, triggers a 'saveError' action (also in the
route).
The preview action gains a new parameter, 'explain', which will give the
output of an EXPLAIN query for the badge sql, which can be used by forum
admins to estimate the cost of their badge queries.
The preview link is replaced by two links, one which omits (false) and
includes (true) the EXPLAIN query.
The Badge.save() method is amended to propogate errors.
Badge::Trigger gets some utility methods for use in the
BadgeGranter.contract_checks! method.
Additionally, extra checks outside of BadgeGranter.contract_checks! are
added in the preview() method, to cover cases of null granted_at
columns.
An uninitialized variable path is removed in the backfill() method.
TODO - it would be nice to be able to get the actual names of all
columns the provided query returns, so we could give more errors
2014-08-25 18:17:29 -04:00
|
|
|
this.get('selectedItem').save(fields).catch(function(error) {
|
|
|
|
// this shows the admin-badge-preview modal with the error
|
|
|
|
// kinda weird, but it consolidates the display logic for badge errors
|
|
|
|
self.send('saveError', error);
|
|
|
|
});
|
2014-03-19 01:13:38 -04:00
|
|
|
}
|
2014-03-05 07:52:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Confirm before destroying the selected badge.
|
|
|
|
|
|
|
|
@method destroy
|
|
|
|
**/
|
|
|
|
destroy: function() {
|
2014-05-19 01:20:57 -04:00
|
|
|
// Delete immediately if the selected badge is new.
|
|
|
|
if (!this.get('selectedItem.id')) {
|
|
|
|
this.get('model').removeObject(this.get('selectedItem'));
|
2014-05-21 03:22:42 -04:00
|
|
|
this.set('selectedId', null);
|
2014-05-19 01:20:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
var self = this;
|
|
|
|
return bootbox.confirm(I18n.t("admin.badges.delete_confirm"), I18n.t("no_value"), I18n.t("yes_value"), function(result) {
|
|
|
|
if (result) {
|
|
|
|
var selected = self.get('selectedItem');
|
|
|
|
selected.destroy().then(function() {
|
|
|
|
// Success.
|
2014-05-21 03:22:42 -04:00
|
|
|
self.set('selectedId', null);
|
2014-03-05 07:52:20 -05:00
|
|
|
self.get('model').removeObject(selected);
|
|
|
|
}, function() {
|
|
|
|
// Failure.
|
|
|
|
bootbox.alert(I18n.t('generic_error'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|