1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-09 19:52:28 +01:00

Create AdminPaginationController for Datatables pagination

This commit is contained in:
Chaoyi Zha 2016-10-01 20:38:35 -04:00
parent 8ba2826a7e
commit f25b418aae
4 changed files with 96 additions and 72 deletions

View File

@ -10,77 +10,11 @@ use App\Helpers\UserHelper;
class AdminController extends Controller {
/**
* Show the admin panel, and process admin AJAX requests.
* Show the admin panel, and process setting changes.
*
* @return Response
*/
public function paginateAdminUsers(Request $request) {
self::ensureAdmin();
$admin_users = User::select(['username', 'email', 'created_at', 'active', 'api_key', 'api_active', 'api_quota', 'id']);
return Datatables::of($admin_users)
->addColumn('api_action', function ($user) {
// Add "API Info" action button
return '<a class="activate-api-modal btn btn-sm btn-info"
ng-click="openAPIModal($event, \'' . $user->username . '\', \'' . $user->api_key . '\', \'' . $user->api_active . '\', \'' . $user->api_quota . '\', \'' . $user->id . '\')">
API info
</a>';
})
->addColumn('delete', function ($user) {
// Add "Delete" action button
$btn_class = '';
if (session('username') == $user->username) {
$btn_class = 'disabled';
}
return '<a ng-click="deleteUser($event)" class="btn btn-sm btn-danger ' . $btn_class . '"
data-user-id="' . $user->id . '">
Delete
</a>';
})
->make(true);
}
public function paginateAdminLinks(Request $request) {
self::ensureAdmin();
$admin_links = Link::select(['short_url', 'long_url', 'clicks', 'created_at', 'creator', 'is_disabled']);
return Datatables::of($admin_links)
->addColumn('disable', function ($link) {
// Add "Disable/Enable" action buttons
$btn_class = 'btn-danger';
$btn_text = 'Disable';
if ($link->is_disabled) {
$btn_class = 'btn-success';
$btn_text = 'Enable';
}
return '<a ng-click="toggleLink($event, \'' . $link->short_url . '\')" class="btn btn-sm ' . $btn_class . '">
' . $btn_text . '
</a>';
})
->addColumn('delete', function ($link) {
// Add "Delete" action button
return '<a ng-click="deleteLink($event, \'' . $link->short_url . '\')"
class="btn btn-sm btn-warning delete-link">
Delete
</a>';
})
->make(true);
}
public function paginateUserLinks(Request $request) {
self::ensureLoggedIn();
$username = session('username');
$user_links = Link::where('creator', $username)
->select(['short_url', 'long_url', 'clicks', 'created_at']);
return Datatables::of($user_links)
->make(true);
}
public function displayAdminPage(Request $request) {
if (!$this->isLoggedIn()) {
return redirect(route('login'))->with('error', 'Please login to access your dashboard.');

View File

@ -0,0 +1,81 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Yajra\Datatables\Facades\Datatables;
use App\Models\Link;
use App\Models\User;
class AdminPaginationController extends Controller {
/**
* Process AJAX Datatables pagination queries from the admin panel.
*
* @return Response
*/
public function paginateAdminUsers(Request $request) {
self::ensureAdmin();
$admin_users = User::select(['username', 'email', 'created_at', 'active', 'api_key', 'api_active', 'api_quota', 'id']);
return Datatables::of($admin_users)
->addColumn('api_action', function ($user) {
// Add "API Info" action button
return '<a class="activate-api-modal btn btn-sm btn-info"
ng-click="openAPIModal($event, \'' . $user->username . '\', \'' . $user->api_key . '\', \'' . $user->api_active . '\', \'' . $user->api_quota . '\', \'' . $user->id . '\')">
API info
</a>';
})
->addColumn('delete', function ($user) {
// Add "Delete" action button
$btn_class = '';
if (session('username') == $user->username) {
$btn_class = 'disabled';
}
return '<a ng-click="deleteUser($event)" class="btn btn-sm btn-danger ' . $btn_class . '"
data-user-id="' . $user->id . '">
Delete
</a>';
})
->make(true);
}
public function paginateAdminLinks(Request $request) {
self::ensureAdmin();
$admin_links = Link::select(['short_url', 'long_url', 'clicks', 'created_at', 'creator', 'is_disabled']);
return Datatables::of($admin_links)
->addColumn('disable', function ($link) {
// Add "Disable/Enable" action buttons
$btn_class = 'btn-danger';
$btn_text = 'Disable';
if ($link->is_disabled) {
$btn_class = 'btn-success';
$btn_text = 'Enable';
}
return '<a ng-click="toggleLink($event, \'' . $link->short_url . '\')" class="btn btn-sm ' . $btn_class . '">
' . $btn_text . '
</a>';
})
->addColumn('delete', function ($link) {
// Add "Delete" action button
return '<a ng-click="deleteLink($event, \'' . $link->short_url . '\')"
class="btn btn-sm btn-warning delete-link">
Delete
</a>';
})
->make(true);
}
public function paginateUserLinks(Request $request) {
self::ensureLoggedIn();
$username = session('username');
$user_links = Link::where('creator', $username)
->select(['short_url', 'long_url', 'clicks', 'created_at']);
return Datatables::of($user_links)
->make(true);
}
}

View File

@ -48,9 +48,9 @@ $app->group(['prefix' => '/api/v2', 'namespace' => 'App\Http\Controllers'], func
$app->post('admin/toggle_link', ['as' => 'api_toggle_link', 'uses' => 'AjaxController@toggleLink']);
$app->post('admin/delete_link', ['as' => 'api_delete_link', 'uses' => 'AjaxController@deleteLink']);
$app->get('admin/get_admin_users', ['as' => 'api_get_admin_users', 'uses' => 'AdminController@paginateAdminUsers']);
$app->get('admin/get_admin_links', ['as' => 'api_get_admin_links', 'uses' => 'AdminController@paginateAdminLinks']);
$app->get('admin/get_user_links', ['as' => 'api_get_user_links', 'uses' => 'AdminController@paginateUserLinks']);
$app->get('admin/get_admin_users', ['as' => 'api_get_admin_users', 'uses' => 'AdminPaginationController@paginateAdminUsers']);
$app->get('admin/get_admin_links', ['as' => 'api_get_admin_links', 'uses' => 'AdminPaginationController@paginateAdminLinks']);
$app->get('admin/get_user_links', ['as' => 'api_get_user_links', 'uses' => 'AdminPaginationController@paginateUserLinks']);
/* API shorten endpoints */

View File

@ -6,8 +6,8 @@ polr.controller('AdminCtrl', function($scope, $compile) {
}
};
// Initialise Datatables elements
$scope.initTables = function () {
// Initialise Datatables elements
var datatables_config = {
'autoWidth': false,
'processing': true,
@ -63,6 +63,7 @@ polr.controller('AdminCtrl', function($scope, $compile) {
}, datatables_config));
};
// Append modals to Angular root
$scope.appendModal = function(html, id) {
id = esc_selector(id);
@ -78,11 +79,13 @@ polr.controller('AdminCtrl', function($scope, $compile) {
});
};
// Hide table rows
$scope.hideRow = function(el, msg) {
el.text(msg);
el.parent().parent().slideUp();
};
// Delete user
$scope.deleteUser = function($event) {
var el = $($event.target);
var user_id = el.data('user-id');
@ -94,6 +97,7 @@ polr.controller('AdminCtrl', function($scope, $compile) {
});
};
// Delete link
$scope.deleteLink = function($event, link_ending) {
var el = $($event.target);
@ -104,6 +108,7 @@ polr.controller('AdminCtrl', function($scope, $compile) {
});
};
// Generate new API key for user_id
$scope.generateNewAPIKey = function($event, user_id, is_dev_tab) {
var el = $($event.target);
var status_display_elem = el.prevAll('.status-display');
@ -123,6 +128,7 @@ polr.controller('AdminCtrl', function($scope, $compile) {
});
};
// Toggle API access status
$scope.toggleAPIStatus = function($event, user_id) {
var el = $($event.target);
var status_display_elem = el.prevAll('.status-display');
@ -135,6 +141,7 @@ polr.controller('AdminCtrl', function($scope, $compile) {
});
};
// Disable and enable links
$scope.toggleLink = function($event, link_ending) {
var el = $($event.target);
var curr_action = el.text();
@ -155,6 +162,7 @@ polr.controller('AdminCtrl', function($scope, $compile) {
});
};
// Update user API quotas
$scope.updateAPIQuota = function($event, user_id) {
var el = $($event.target);
var new_quota = el.prevAll('.api-quota').val();
@ -167,7 +175,7 @@ polr.controller('AdminCtrl', function($scope, $compile) {
});
};
// Open user API settings menu
$scope.openAPIModal = function($event, username, api_key, api_active, api_quota, user_id) {
var el = $($event.target);
@ -189,6 +197,7 @@ polr.controller('AdminCtrl', function($scope, $compile) {
$scope.appendModal(mt_html, modal_id);
};
// Initialise AdminCtrl
$scope.init = function() {
var modal_source = $("#modal-template").html();
$scope.modal_template = Handlebars.compile(modal_source);