2016-08-02 01:05:22 +02:00
|
|
|
polr.controller('AdminCtrl', function($scope, $compile) {
|
|
|
|
$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) {
|
|
|
|
var admin_users_table = $('#admin_users_table').DataTable($.extend({
|
|
|
|
"ajax": BASE_API_PATH + 'admin/get_admin_users',
|
|
|
|
|
|
|
|
"columns": [
|
|
|
|
{data: 'username', name: 'username'},
|
|
|
|
{data: 'email', name: 'email'},
|
|
|
|
{data: 'created_at', name: 'created_at'},
|
|
|
|
|
2016-10-03 23:13:14 +02:00
|
|
|
{data: 'toggle_active', name: 'toggle_active'},
|
2016-10-01 04:10:58 +02:00
|
|
|
{data: 'api_action', name: 'api_action'},
|
2016-10-03 23:13:14 +02:00
|
|
|
{data: 'change_role', name: 'change_role'},
|
2016-10-01 04:10:58 +02:00
|
|
|
{data: 'delete', name: 'delete'}
|
|
|
|
]
|
|
|
|
}, datatables_config));
|
|
|
|
}
|
|
|
|
if ($('#admin_links_table').length) {
|
|
|
|
var admin_links_table = $('#admin_links_table').DataTable($.extend({
|
|
|
|
"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'},
|
|
|
|
{data: 'disable', name: 'disable', orderable: false, searchable: false},
|
|
|
|
{data: 'delete', name: 'delete', orderable: false, searchable: false}
|
|
|
|
|
|
|
|
]
|
|
|
|
}, datatables_config));
|
|
|
|
}
|
|
|
|
|
2016-10-02 02:30:06 +02:00
|
|
|
var user_links_table = $('#user_links_table').DataTable($.extend({
|
|
|
|
"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-08-02 01:05:22 +02:00
|
|
|
el.text(msg);
|
|
|
|
el.parent().parent().slideUp();
|
|
|
|
};
|
|
|
|
|
2016-09-30 22:51:43 +02:00
|
|
|
$scope.toggleUserActiveStatus = function($event) {
|
|
|
|
var el = $($event.target);
|
|
|
|
var user_id = el.data('user-id');
|
|
|
|
|
|
|
|
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-01 23:54:22 +02:00
|
|
|
$scope.toggleNewUserBox = function($event) {
|
|
|
|
var el = $($event.target);
|
|
|
|
$('#add_user_box').toggle();
|
|
|
|
if (el.text() == 'Add New User') {
|
|
|
|
el.text('Cancel');
|
|
|
|
$('#new_user_name').focus();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
el.text('Add New User');
|
|
|
|
$scope.resetNewUserFields();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.resetNewUserFields = function() {
|
|
|
|
$('#new_user_name').val('');
|
|
|
|
$('#new_user_password').val('');
|
|
|
|
$('#new_user_email').val('');
|
|
|
|
$("#new_user_role").val($("#new_user_role option:first").val());
|
|
|
|
$('#new_user_status').text('');
|
|
|
|
$('#new_user_status').css('color', '#000');
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.checkNewUserFiels = function() {
|
|
|
|
var user_name = $('#new_user_name').val();
|
|
|
|
var user_password = $('#new_user_password').val();
|
|
|
|
var user_email = $('#new_user_email').val();
|
|
|
|
|
|
|
|
if (user_name.trim() == '' || user_password.trim() == '' || user_email.trim() == '') return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.addNewUser = function($event) {
|
|
|
|
const status_error1 = 'Fields cannot be empty !';
|
|
|
|
const status_error2 = 'Unknown Error !';
|
|
|
|
const status_ok = 'New User added !\nPlease refresh page to see all users.';
|
|
|
|
|
|
|
|
var user_name = $('#new_user_name').val();
|
|
|
|
var user_password = $('#new_user_password').val();
|
|
|
|
var user_email = $('#new_user_email').val();
|
|
|
|
var user_role = $('#new_user_role').val();
|
|
|
|
|
|
|
|
if (!$scope.checkNewUserFiels()) {
|
|
|
|
$('#new_user_status').text(status_error1);
|
|
|
|
$('#new_user_status').css('color', '#f00');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
apiCall('admin/add_new_user', {
|
|
|
|
'user_name': user_name,
|
|
|
|
'user_password': user_password,
|
|
|
|
'user_email': user_email,
|
|
|
|
'user_role': user_role,
|
|
|
|
}, function(result) {
|
|
|
|
if (result == 'OK') {
|
|
|
|
$('#new_user_status').text(status_ok).css('color', '#325d88').hide();
|
|
|
|
$('#new_user_status').fadeIn('normal', function() {
|
|
|
|
$(this).delay(3000).fadeOut('slow', function() {
|
|
|
|
$('#add_user_box').toggle();
|
|
|
|
$('#add_user_btn').text('Add New User');
|
|
|
|
$('#new_user_status').show();
|
|
|
|
$scope.resetNewUserFields();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$('#new_user_status').text(status_error2);
|
|
|
|
$('#new_user_status').css('color', '#f00');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Delete user
|
2016-08-10 02:38:31 +02:00
|
|
|
$scope.deleteUser = function($event) {
|
2016-08-02 01:05:22 +02:00
|
|
|
var el = $($event.target);
|
|
|
|
var user_id = el.data('user-id');
|
2016-10-02 00:00:02 +02:00
|
|
|
var user_name = el.data('user-name');
|
2016-09-29 22:57:51 +02:00
|
|
|
|
2016-10-02 00:00:02 +02:00
|
|
|
var confirmation = confirm("User '" + user_name + "' will be deleted.\nAre you sure?");
|
|
|
|
if (!confirmation) return;
|
2016-08-02 01:05:22 +02:00
|
|
|
|
|
|
|
apiCall('admin/delete_user', {
|
|
|
|
'user_id': user_id,
|
2016-08-10 02:38:31 +02:00
|
|
|
}, function(new_status) {
|
2016-08-02 01:05:22 +02:00
|
|
|
$scope.hideRow(el, 'Deleted!');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-02 02:38:35 +02:00
|
|
|
// Delete link
|
2016-08-10 02:38:31 +02:00
|
|
|
$scope.deleteLink = function($event, link_ending) {
|
2016-08-02 01:05:22 +02:00
|
|
|
var el = $($event.target);
|
|
|
|
|
|
|
|
apiCall('admin/delete_link', {
|
|
|
|
'link_ending': link_ending,
|
2016-08-10 02:38:31 +02:00
|
|
|
}, function(new_status) {
|
2016-08-02 01:05:22 +02:00
|
|
|
$scope.hideRow(el, 'Deleted!');
|
|
|
|
});
|
2016-10-01 04:10:58 +02:00
|
|
|
};
|
2016-08-02 01:05:22 +02:00
|
|
|
|
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-10-02 02:18:55 +02:00
|
|
|
$('a#api_info_btn_' + user_id).attr('data-api-active', 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
|
|
|
// Disable and enable links
|
2016-08-10 02:38:31 +02:00
|
|
|
$scope.toggleLink = function($event, link_ending) {
|
2016-08-02 01:05:22 +02:00
|
|
|
var el = $($event.target);
|
|
|
|
var curr_action = el.text();
|
|
|
|
|
|
|
|
apiCall('admin/toggle_link', {
|
|
|
|
'link_ending': link_ending,
|
2016-08-10 02:38:31 +02:00
|
|
|
}, function(next_action) {
|
2016-08-02 01:05:22 +02:00
|
|
|
toastr.success(curr_action + " was successful.", "Success");
|
|
|
|
if (next_action == 'Disable') {
|
|
|
|
el.removeClass('btn-success');
|
|
|
|
el.addClass('btn-danger');
|
2016-08-10 02:38:31 +02:00
|
|
|
} else {
|
2016-08-02 01:05:22 +02:00
|
|
|
el.removeClass('btn-danger');
|
|
|
|
el.addClass('btn-success');
|
|
|
|
}
|
|
|
|
|
|
|
|
el.text(next_action);
|
|
|
|
});
|
2016-08-10 02:38:31 +02:00
|
|
|
};
|
|
|
|
|
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-08-10 02:38:31 +02: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-10-02 02:18:55 +02:00
|
|
|
|
|
|
|
api_active = $('a#api_info_btn_' + user_id).attr('data-api-active');
|
2016-08-02 01:05:22 +02:00
|
|
|
|
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-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();
|
|
|
|
});
|
2016-10-01 20:52:24 +02:00
|
|
|
|
|
|
|
function changeUserRole(what) {
|
|
|
|
var user_id = what.attr('data-user-id');
|
|
|
|
var role = what.val();
|
|
|
|
|
|
|
|
apiCall('admin/change_user_role', {
|
|
|
|
'user_id': user_id,
|
|
|
|
'role': role,
|
|
|
|
}, function(result) {
|
|
|
|
if (result == 'OK') {
|
|
|
|
var parent_td = what.parent();
|
|
|
|
var add = '<div id="role_changed_' + user_id + '" style="display: none; color: #fff; background: #0a0; font-weight: bold; text-align: center;">Changed</div>';
|
|
|
|
parent_td.append(add);
|
|
|
|
$('#role_changed_' + user_id).fadeIn('normal', function() {
|
|
|
|
$(this).delay(1000).fadeOut('slow', function() {
|
|
|
|
$('#role_changed_' + user_id).remove();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|