2015-02-12 21:22:32 -07:00
|
|
|
import Ember from 'ember';
|
2015-05-26 19:41:12 -05:00
|
|
|
import {request as ajax} from 'ic-ajax';
|
|
|
|
|
2015-11-18 11:58:26 +00:00
|
|
|
const {$, Controller, computed, inject} = Ember;
|
2015-10-28 11:36:45 +00:00
|
|
|
|
|
|
|
export default Controller.extend({
|
2014-04-08 00:01:46 +02:00
|
|
|
uploadButtonText: 'Import',
|
2014-07-28 22:41:45 +01:00
|
|
|
importErrors: '',
|
2015-08-10 09:43:49 -06:00
|
|
|
submitting: false,
|
2015-11-18 10:50:48 +00:00
|
|
|
showDeleteAllModal: false,
|
2015-05-25 21:10:50 -05:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
ghostPaths: inject.service('ghost-paths'),
|
|
|
|
notifications: inject.service(),
|
|
|
|
session: inject.service(),
|
|
|
|
feature: inject.controller(),
|
2015-05-25 21:10:50 -05:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
labsJSON: computed('model.labs', function () {
|
2015-01-01 19:09:55 +00:00
|
|
|
return JSON.parse(this.get('model.labs') || {});
|
|
|
|
}),
|
2014-06-23 14:24:37 +00:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
saveLabs(optionName, optionValue) {
|
|
|
|
let labsJSON = this.get('labsJSON');
|
2014-12-14 17:56:04 +00:00
|
|
|
|
|
|
|
// Set new value in the JSON object
|
|
|
|
labsJSON[optionName] = optionValue;
|
|
|
|
|
2015-01-01 19:09:55 +00:00
|
|
|
this.set('model.labs', JSON.stringify(labsJSON));
|
2014-12-14 17:56:04 +00:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
this.get('model').save().catch((errors) => {
|
|
|
|
this.showErrors(errors);
|
|
|
|
this.get('model').rollbackAttributes();
|
2014-12-14 17:56:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
usePublicAPI: computed('feature.publicAPI', {
|
|
|
|
get() {
|
2015-10-23 11:03:38 +02:00
|
|
|
return this.get('feature.publicAPI');
|
|
|
|
},
|
2015-10-28 11:36:45 +00:00
|
|
|
set(key, value) {
|
2015-10-23 11:03:38 +02:00
|
|
|
this.saveLabs('publicAPI', value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2014-04-08 00:01:46 +02:00
|
|
|
actions: {
|
2015-10-28 11:36:45 +00:00
|
|
|
onUpload(file) {
|
|
|
|
let formData = new FormData();
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
let currentUserId = this.get('session.user.id');
|
2014-06-23 14:24:37 +00:00
|
|
|
|
2014-04-08 00:01:46 +02:00
|
|
|
this.set('uploadButtonText', 'Importing');
|
2014-12-11 16:55:14 +00:00
|
|
|
this.set('importErrors', '');
|
2014-06-23 14:24:37 +00:00
|
|
|
|
|
|
|
formData.append('importfile', file);
|
|
|
|
|
2015-05-26 19:41:12 -05:00
|
|
|
ajax(this.get('ghostPaths.url').api('db'), {
|
2014-06-23 14:24:37 +00:00
|
|
|
type: 'POST',
|
|
|
|
data: formData,
|
|
|
|
dataType: 'json',
|
|
|
|
cache: false,
|
|
|
|
contentType: false,
|
|
|
|
processData: false
|
2015-10-28 11:36:45 +00:00
|
|
|
}).then(() => {
|
2015-01-08 13:42:45 +00:00
|
|
|
// Clear the store, so that all the new data gets fetched correctly.
|
2015-10-28 11:36:45 +00:00
|
|
|
this.store.unloadAll();
|
2015-07-03 13:27:36 +02:00
|
|
|
// Reload currentUser and set session
|
2015-10-28 11:36:45 +00:00
|
|
|
this.set('session.user', this.store.findRecord('user', currentUserId));
|
2015-06-18 22:56:18 +01:00
|
|
|
// TODO: keep as notification, add link to view content
|
2015-11-18 10:50:48 +00:00
|
|
|
notifications.showNotification('Import successful.', {key: 'import.upload.success'});
|
2015-10-28 11:36:45 +00:00
|
|
|
}).catch((response) => {
|
2014-07-28 22:41:45 +01:00
|
|
|
if (response && response.jqXHR && response.jqXHR.responseJSON && response.jqXHR.responseJSON.errors) {
|
2015-10-28 11:36:45 +00:00
|
|
|
this.set('importErrors', response.jqXHR.responseJSON.errors);
|
2014-07-28 22:41:45 +01:00
|
|
|
}
|
2014-10-24 21:09:50 +00:00
|
|
|
|
2015-10-07 15:44:23 +01:00
|
|
|
notifications.showAlert('Import Failed', {type: 'error', key: 'import.upload.failed'});
|
2015-10-28 11:36:45 +00:00
|
|
|
}).finally(() => {
|
|
|
|
this.set('uploadButtonText', 'Import');
|
2014-06-23 14:24:37 +00:00
|
|
|
});
|
2014-04-08 00:01:46 +02:00
|
|
|
},
|
2014-06-23 14:24:37 +00:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
exportData() {
|
|
|
|
let dbUrl = this.get('ghostPaths.url').api('db');
|
|
|
|
let accessToken = this.get('session.data.authenticated.access_token');
|
|
|
|
let downloadURL = `${dbUrl}?access_token=${accessToken}`;
|
|
|
|
let iframe = $('#iframeDownload');
|
2014-06-23 14:24:37 +00:00
|
|
|
|
2014-07-25 17:14:48 +02:00
|
|
|
if (iframe.length === 0) {
|
2014-10-24 21:09:50 +00:00
|
|
|
iframe = $('<iframe>', {id: 'iframeDownload'}).hide().appendTo('body');
|
2014-07-25 17:14:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iframe.attr('src', downloadURL);
|
2014-06-23 14:24:37 +00:00
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
sendTestEmail() {
|
|
|
|
let notifications = this.get('notifications');
|
2015-08-10 09:43:49 -06:00
|
|
|
|
|
|
|
this.toggleProperty('submitting');
|
2014-06-23 14:24:37 +00:00
|
|
|
|
2015-05-26 19:41:12 -05:00
|
|
|
ajax(this.get('ghostPaths.url').api('mail', 'test'), {
|
2014-07-01 11:39:01 +02:00
|
|
|
type: 'POST'
|
2015-10-28 11:36:45 +00:00
|
|
|
}).then(() => {
|
2015-10-07 15:44:23 +01:00
|
|
|
notifications.showAlert('Check your email for the test message.', {type: 'info', key: 'test-email.send.success'});
|
2015-10-28 11:36:45 +00:00
|
|
|
this.toggleProperty('submitting');
|
|
|
|
}).catch((error) => {
|
2014-08-10 18:52:40 -07:00
|
|
|
if (typeof error.jqXHR !== 'undefined') {
|
2015-10-07 15:44:23 +01:00
|
|
|
notifications.showAPIError(error, {key: 'test-email.send'});
|
2014-08-10 18:52:40 -07:00
|
|
|
} else {
|
2015-10-07 15:44:23 +01:00
|
|
|
notifications.showErrors(error, {key: 'test-email.send'});
|
2014-08-10 18:52:40 -07:00
|
|
|
}
|
2015-10-28 11:36:45 +00:00
|
|
|
this.toggleProperty('submitting');
|
2014-06-23 14:24:37 +00:00
|
|
|
});
|
2015-11-18 10:50:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleDeleteAllModal() {
|
|
|
|
this.toggleProperty('showDeleteAllModal');
|
2014-04-08 00:01:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|