2016-08-02 01:05:22 +02:00
|
|
|
polr.controller('AdminCtrl', function($scope, $compile) {
|
2016-12-02 23:07:59 +01:00
|
|
|
$scope.state = {
|
|
|
|
showNewUserWell: false
|
|
|
|
};
|
2016-12-03 01:06:40 +01:00
|
|
|
$scope.datatables = {};
|
2016-12-02 23:07:59 +01:00
|
|
|
|
2016-08-02 01:05:22 +02:00
|
|
|
$scope.syncHash = function() {
|
|
|
|
var url = document.location.toString();
|
|
|
|
if (url.match('#')) {
|
2016-08-10 02:38:31 +02:00
|
|
|
$('.admin-nav a[href=#' + url.split('#')[1] + ']').tab('show');
|
2016-08-02 01:05:22 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Initialise Datatables elements
|
2016-10-01 04:10:58 +02:00
|
|
|
$scope.initTables = function () {
|
|
|
|
var datatables_config = {
|
|
|
|
'autoWidth': false,
|
|
|
|
'processing': true,
|
|
|
|
'serverSide': true,
|
|
|
|
|
|
|
|
'drawCallback': function () {
|
|
|
|
// Compile Angular bindings on each draw
|
|
|
|
$compile($(this))($scope);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($('#admin_users_table').length) {
|
2016-12-03 01:06:40 +01:00
|
|
|
$scope.datatables['admin_users_table'] = $('#admin_users_table').DataTable($.extend({
|
2016-10-01 04:10:58 +02:00
|
|
|
"ajax": BASE_API_PATH + 'admin/get_admin_users',
|
|
|
|
|
|
|
|
"columns": [
|
2016-10-03 23:30:17 +02:00
|
|
|
{className: 'wrap-text', data: 'username', name: 'username'},
|
|
|
|
{className: 'wrap-text', data: 'email', name: 'email'},
|
2016-10-01 04:10:58 +02:00
|
|
|
{data: 'created_at', name: 'created_at'},
|
|
|
|
|
2016-10-03 23:13:14 +02:00
|
|
|
{data: 'toggle_active', name: 'toggle_active'},
|
2016-10-03 23:38:05 +02:00
|
|
|
{data: 'api_action', name: 'api_action', orderable: false, searchable: false},
|
2016-10-03 23:13:14 +02:00
|
|
|
{data: 'change_role', name: 'change_role'},
|
2016-10-03 23:38:05 +02:00
|
|
|
{data: 'delete', name: 'delete', orderable: false, searchable: false}
|
2016-10-01 04:10:58 +02:00
|
|
|
]
|
|
|
|
}, datatables_config));
|
|
|
|
}
|
|
|
|
if ($('#admin_links_table').length) {
|
2016-12-03 01:06:40 +01:00
|
|
|
$scope.datatables['admin_links_table'] = $('#admin_links_table').DataTable($.extend({
|
2016-10-01 04:10:58 +02:00
|
|
|
"ajax": BASE_API_PATH + 'admin/get_admin_links',
|
|
|
|
|
|
|
|
"columns": [
|
|
|
|
{className: 'wrap-text', data: 'short_url', name: 'short_url'},
|
|
|
|
{className: 'wrap-text', data: 'long_url', name: 'long_url'},
|
|
|
|
{data: 'clicks', name: 'clicks'},
|
|
|
|
{data: 'created_at', name: 'created_at'},
|
|
|
|
{data: 'creator', name: 'creator'},
|
2016-10-03 23:38:05 +02:00
|
|
|
|
2016-10-01 04:10:58 +02:00
|
|
|
{data: 'disable', name: 'disable', orderable: false, searchable: false},
|
|
|
|
{data: 'delete', name: 'delete', orderable: false, searchable: false}
|
|
|
|
|
|
|
|
]
|
|
|
|
}, datatables_config));
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:06:40 +01:00
|
|
|
$scope.datatables['user_links_table'] = $('#user_links_table').DataTable($.extend({
|
2016-10-02 02:30:06 +02:00
|
|
|
"ajax": BASE_API_PATH + 'admin/get_user_links',
|
|
|
|
|
|
|
|
"columns": [
|
|
|
|
{className: 'wrap-text', data: 'short_url', name: 'short_url'},
|
|
|
|
{className: 'wrap-text', data: 'long_url', name: 'long_url'},
|
|
|
|
{data: 'clicks', name: 'clicks'},
|
|
|
|
{data: 'created_at', name: 'created_at'}
|
|
|
|
]
|
|
|
|
}, datatables_config));
|
2016-10-01 04:10:58 +02:00
|
|
|
};
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Append modals to Angular root
|
2016-08-10 02:38:31 +02:00
|
|
|
$scope.appendModal = function(html, id) {
|
2016-08-02 01:05:22 +02:00
|
|
|
id = esc_selector(id);
|
|
|
|
|
|
|
|
$(".ng-root").append(html);
|
|
|
|
var modal_ele = $("#" + id);
|
|
|
|
|
|
|
|
modal_ele.append(html);
|
|
|
|
modal_ele.modal();
|
|
|
|
$compile(modal_ele)($scope);
|
|
|
|
|
2016-08-10 02:38:31 +02:00
|
|
|
$("body").delegate("#" + id, "hidden.bs.modal", function() {
|
2016-08-02 01:05:22 +02:00
|
|
|
modal_ele.remove();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Hide table rows
|
2016-08-10 02:38:31 +02:00
|
|
|
$scope.hideRow = function(el, msg) {
|
2016-12-03 01:06:40 +01:00
|
|
|
var row = el.parent().parent();
|
|
|
|
toastr.success(msg, "Success");
|
|
|
|
row.fadeOut('slow');
|
2016-08-02 01:05:22 +02:00
|
|
|
};
|
|
|
|
|
2016-10-13 21:19:01 +02:00
|
|
|
/*
|
|
|
|
User Management
|
|
|
|
*/
|
2016-12-03 01:29:25 +01:00
|
|
|
$scope.toggleUserActiveStatus = function($event, user_id) {
|
2016-09-30 22:51:43 +02:00
|
|
|
var el = $($event.target);
|
|
|
|
|
|
|
|
apiCall('admin/toggle_user_active', {
|
|
|
|
'user_id': user_id,
|
|
|
|
}, function(new_status) {
|
2016-10-03 23:13:14 +02:00
|
|
|
var text = (new_status == 1) ? 'Active' : 'Inactive';
|
|
|
|
el.text(text);
|
2016-10-02 00:00:02 +02:00
|
|
|
if (el.hasClass('btn-success')) {
|
|
|
|
el.removeClass('btn-success').addClass('btn-danger');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
el.removeClass('btn-danger').addClass('btn-success');
|
|
|
|
}
|
2016-09-30 22:51:43 +02:00
|
|
|
});
|
2016-10-02 00:00:02 +02:00
|
|
|
}
|
2016-09-30 22:51:43 +02:00
|
|
|
|
2016-10-13 21:19:01 +02:00
|
|
|
$scope.checkNewUserFields = function() {
|
|
|
|
var response = true;
|
2016-10-01 23:54:22 +02:00
|
|
|
|
2016-10-13 21:19:01 +02:00
|
|
|
$('.new-user-fields input').each(function () {
|
|
|
|
if ($(this).val().trim() == '' || response == false) {
|
|
|
|
response = false;
|
|
|
|
}
|
|
|
|
});
|
2016-10-01 23:54:22 +02:00
|
|
|
|
2016-10-13 21:19:01 +02:00
|
|
|
return response;
|
2016-10-01 23:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$scope.addNewUser = function($event) {
|
2016-12-02 23:07:59 +01:00
|
|
|
// Create a new user
|
2016-12-03 02:16:07 +01:00
|
|
|
// FIXME could use Angular models in the future
|
|
|
|
// instead of relying on .val()
|
2016-10-01 23:54:22 +02:00
|
|
|
|
2016-12-02 23:07:59 +01:00
|
|
|
var username = $('#new-username').val();
|
|
|
|
var user_password = $('#new-user-password').val();
|
|
|
|
var user_email = $('#new-user-email').val();
|
|
|
|
var user_role = $('#new-user-role').val();
|
2016-10-01 23:54:22 +02:00
|
|
|
|
2016-10-13 21:19:01 +02:00
|
|
|
if (!$scope.checkNewUserFields()) {
|
2016-12-03 02:00:20 +01:00
|
|
|
toastr.error("Fields cannot be empty.", "Error");
|
2016-12-02 23:07:59 +01:00
|
|
|
return false;
|
2016-10-01 23:54:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
apiCall('admin/add_new_user', {
|
2016-12-02 23:07:59 +01:00
|
|
|
'username': username,
|
2016-10-01 23:54:22 +02:00
|
|
|
'user_password': user_password,
|
|
|
|
'user_email': user_email,
|
|
|
|
'user_role': user_role,
|
|
|
|
}, function(result) {
|
2016-12-03 01:06:40 +01:00
|
|
|
toastr.success("User " + username + " successfully created.", "Success");
|
|
|
|
$('#new-user-form').clearForm();
|
|
|
|
$scope.datatables['admin_users_table'].ajax.reload();
|
2016-12-02 23:07:59 +01:00
|
|
|
}, function () {
|
2016-12-03 02:00:20 +01:00
|
|
|
toastr.error("An error occured while creating the user.", "Error");
|
2016-10-01 23:54:22 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Delete user
|
2016-12-02 23:07:59 +01:00
|
|
|
$scope.deleteUser = function($event, user_id) {
|
2016-08-02 01:05:22 +02:00
|
|
|
var el = $($event.target);
|
|
|
|
|
|
|
|
apiCall('admin/delete_user', {
|
|
|
|
'user_id': user_id,
|
2016-08-10 02:38:31 +02:00
|
|
|
}, function(new_status) {
|
2016-12-03 01:06:40 +01:00
|
|
|
$scope.hideRow(el, 'User successfully deleted.');
|
2016-08-02 01:05:22 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-12-03 00:38:28 +01:00
|
|
|
$scope.changeUserRole = function(role, user_id) {
|
2016-12-02 23:07:59 +01:00
|
|
|
apiCall('admin/change_user_role', {
|
|
|
|
'user_id': user_id,
|
|
|
|
'role': role,
|
|
|
|
}, function(result) {
|
|
|
|
toastr.success("User role successfully changed.", "Success");
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Generate new API key for user_id
|
2016-08-02 01:05:22 +02:00
|
|
|
$scope.generateNewAPIKey = function($event, user_id, is_dev_tab) {
|
|
|
|
var el = $($event.target);
|
|
|
|
var status_display_elem = el.prevAll('.status-display');
|
|
|
|
|
|
|
|
if (is_dev_tab) {
|
|
|
|
status_display_elem = el.parent().prev().children();
|
|
|
|
}
|
|
|
|
|
|
|
|
apiCall('admin/generate_new_api_key', {
|
|
|
|
'user_id': user_id,
|
2016-08-10 02:38:31 +02:00
|
|
|
}, function(new_status) {
|
2016-08-02 01:05:22 +02:00
|
|
|
if (status_display_elem.is('input')) {
|
|
|
|
status_display_elem.val(new_status);
|
2016-08-10 02:38:31 +02:00
|
|
|
} else {
|
2016-08-02 01:05:22 +02:00
|
|
|
status_display_elem.text(new_status);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Toggle API access status
|
2016-08-02 01:05:22 +02:00
|
|
|
$scope.toggleAPIStatus = function($event, user_id) {
|
|
|
|
var el = $($event.target);
|
|
|
|
var status_display_elem = el.prevAll('.status-display');
|
|
|
|
|
|
|
|
apiCall('admin/toggle_api_active', {
|
|
|
|
'user_id': user_id,
|
2016-08-10 02:38:31 +02:00
|
|
|
}, function(new_status) {
|
2016-08-02 01:05:22 +02:00
|
|
|
new_status = res_value_to_text(new_status);
|
|
|
|
status_display_elem.text(new_status);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Update user API quotas
|
2016-08-10 02:38:31 +02:00
|
|
|
$scope.updateAPIQuota = function($event, user_id) {
|
|
|
|
var el = $($event.target);
|
|
|
|
var new_quota = el.prevAll('.api-quota').val();
|
|
|
|
|
|
|
|
apiCall('admin/edit_api_quota', {
|
|
|
|
'user_id': user_id,
|
|
|
|
'new_quota': parseInt(new_quota)
|
|
|
|
}, function(next_action) {
|
|
|
|
toastr.success("Quota successfully changed.", "Success");
|
|
|
|
});
|
2016-10-01 04:10:58 +02:00
|
|
|
};
|
2016-08-10 02:38:31 +02:00
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Open user API settings menu
|
2016-12-03 01:29:25 +01:00
|
|
|
$scope.openAPIModal = function($event, username, api_key, api_active, api_quota, user_id) {
|
2016-08-02 01:05:22 +02:00
|
|
|
var el = $($event.target);
|
2016-08-15 08:07:36 +02:00
|
|
|
var markup = $('#api-modal-template').html();
|
|
|
|
|
2016-08-02 01:05:22 +02:00
|
|
|
var modal_id = "api-modal-" + username;
|
|
|
|
var modal_context = {
|
|
|
|
id: modal_id,
|
|
|
|
api_key: api_key,
|
|
|
|
api_active: parseInt(api_active),
|
|
|
|
api_quota: api_quota,
|
|
|
|
user_id: user_id,
|
|
|
|
title: "API Information for " + username,
|
|
|
|
body: markup
|
|
|
|
};
|
|
|
|
var mt_html = $scope.modal_template(modal_context);
|
|
|
|
var compiled_mt = Handlebars.compile(mt_html);
|
|
|
|
mt_html = compiled_mt(modal_context);
|
|
|
|
$scope.appendModal(mt_html, modal_id);
|
2016-10-01 04:10:58 +02:00
|
|
|
};
|
2016-08-02 01:05:22 +02:00
|
|
|
|
2016-10-13 21:19:01 +02:00
|
|
|
/*
|
|
|
|
Link Management
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Delete link
|
|
|
|
$scope.deleteLink = function($event, link_ending) {
|
|
|
|
var el = $($event.target);
|
|
|
|
|
|
|
|
apiCall('admin/delete_link', {
|
|
|
|
'link_ending': link_ending,
|
|
|
|
}, function(new_status) {
|
2016-12-03 01:06:40 +01:00
|
|
|
$scope.hideRow(el, 'Link successfully deleted.');
|
2016-10-13 21:19:01 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Disable and enable links
|
|
|
|
$scope.toggleLink = function($event, link_ending) {
|
|
|
|
var el = $($event.target);
|
|
|
|
var curr_action = el.text();
|
|
|
|
|
|
|
|
apiCall('admin/toggle_link', {
|
|
|
|
'link_ending': link_ending,
|
|
|
|
}, function(next_action) {
|
|
|
|
toastr.success(curr_action + " was successful.", "Success");
|
|
|
|
if (next_action == 'Disable') {
|
|
|
|
el.removeClass('btn-success');
|
|
|
|
el.addClass('btn-danger');
|
|
|
|
} else {
|
|
|
|
el.removeClass('btn-danger');
|
|
|
|
el.addClass('btn-success');
|
|
|
|
}
|
|
|
|
|
|
|
|
el.text(next_action);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Initialisation
|
|
|
|
*/
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Initialise AdminCtrl
|
2016-08-10 02:38:31 +02:00
|
|
|
$scope.init = function() {
|
|
|
|
var modal_source = $("#modal-template").html();
|
2016-08-02 01:05:22 +02:00
|
|
|
$scope.modal_template = Handlebars.compile(modal_source);
|
|
|
|
|
2016-08-10 02:38:31 +02:00
|
|
|
$('.admin-nav a').click(function(e) {
|
2016-08-02 01:05:22 +02:00
|
|
|
e.preventDefault();
|
|
|
|
$(this).tab('show');
|
|
|
|
});
|
|
|
|
$scope.syncHash();
|
|
|
|
|
2016-08-10 02:38:31 +02:00
|
|
|
$(window).on('hashchange', function() {
|
2016-08-02 01:05:22 +02:00
|
|
|
$scope.syncHash();
|
|
|
|
});
|
|
|
|
|
|
|
|
$("a[href^=#]").on("click", function(e) {
|
|
|
|
history.pushState({}, '', this.href);
|
|
|
|
});
|
2016-10-01 04:10:58 +02:00
|
|
|
|
|
|
|
$scope.initTables();
|
|
|
|
};
|
2016-08-02 01:05:22 +02:00
|
|
|
|
|
|
|
$scope.init();
|
|
|
|
});
|