Skip to content

Commit

Permalink
Setup code and admin panel impls
Browse files Browse the repository at this point in the history
  • Loading branch information
cydrobolt committed Dec 12, 2015
1 parent 6a930f9 commit 8ddc8ca
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 47 deletions.
70 changes: 33 additions & 37 deletions app/Http/Controllers/SetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,46 @@
use Illuminate\Http\Redirect;
use Illuminate\Console\Application\Artisan;


protected function parseExitCode($exitCode) {
if ($exitCode == 0) {
return true;
}
else {
return false;
class SetupController extends Controller {
protected function parseExitCode($exitCode) {
if ($exitCode == 0) {
return true;
}
else {
return false;
}
}
}

private function setupAlreadyRan() {
return view('error', [
'message' => 'Sorry, but you have already ran the set up script previously.'
]);
}
private function setupAlreadyRan() {
return view('error', [
'message' => 'Sorry, but you have already ran the set up script previously.'
]);
}

private function resetDatabase() {
$exitCode = Artisan::call('migrate:refresh', [
'--force' => true,
]);
return $this->parseExitCode($exitCode);
}
private function resetDatabase() {
$exitCode = Artisan::call('migrate:refresh', [
'--force' => true,
]);
return $this->parseExitCode($exitCode);
}

private function createDatabase() {
$exitCode = Artisan::call('migrate');
return $this->parseExitCode($exitCode);
}
private function createDatabase() {
$exitCode = Artisan::call('migrate');
return $this->parseExitCode($exitCode);
}

public static function displaySetupPage(Request $request) {
if (!env('POLR_SETUP_RAN')) {
// first time running setup script
public static function displaySetupPage(Request $request) {
if (env('POLR_SETUP_RAN')) {
return $this->setupAlreadyRan();
}

return view('setup');
}
else {
// has previously ran setup script
return $this->setupAlreadyRan();
}
}

public static function performSetup(Request $request) {
if (!env('POLR_SETUP_RAN')) {

}
else {
return $this->setupAlreadyRan();
public static function performSetup(Request $request) {
if (env('POLR_SETUP_RAN')) {
return $this->setupAlreadyRan();
}

}
}
2 changes: 1 addition & 1 deletion app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$app->get('/admin', ['as' => 'admin', 'uses' => 'AdminController@displayAdminPage']);

$app->get('/setup', ['as' => 'setup', 'uses' => 'SetupController@displaySetupPage']);
$app->post('/setup', ['as' => 'psetup', 'uses' => 'SetupController@performSetup'])
$app->post('/setup', ['as' => 'psetup', 'uses' => 'SetupController@performSetup']);

$app->get('/{short_url}', ['uses' => 'LinkController@performRedirect']);
$app->get('/{short_url}/{secret_key}', ['uses' => 'LinkController@performRedirect']);
Expand Down
13 changes: 13 additions & 0 deletions public/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@
.change-password {
width: 150px;
}

.change-password-btn {
margin-top: 15px;
}

.password-box {
margin-top: 10px;
margin-bottom: 10px;
}

.hidden-metadata {
display: none;
}
11 changes: 11 additions & 0 deletions public/css/default-bootstrap.min.css

Large diffs are not rendered by default.

82 changes: 75 additions & 7 deletions public/js/admin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,77 @@
$('.admin-nav a').click(function (e) {
e.preventDefault();
$(this).tab('show');
});
function syncHash() {
var url = document.location.toString();
if (url.match('#')) {
$('.admin-nav a[href=#'+url.split('#')[1]+']').tab('show') ;
}
}

var url = document.location.toString();
if (url.match('#')) {
$('.admin-nav a[href=#'+url.split('#')[1]+']').tab('show') ;
function appendModal(html, id) {
$('body').append(html);
var modal_ele = $('#' + id);
modal_ele.append(html);
modal_ele.modal();
$( "body" ).delegate( "#" + id, "hidden.bs.modal", function () {
modal_ele.remove();
});
}

$(function () {
var modal_source = $("#modal-template").html();
var modal_template = Handlebars.compile(modal_source);

$('.admin-nav a').click(function (e) {
e.preventDefault();
$(this).tab('show');
});
syncHash();

$(window).on('hashchange',function(){
syncHash();
});

$('.activate-api-modal').click(function () {
var te = $(this);
var username = te.data('username');
var api_key = te.data('api-key');
var api_active = te.data('api-active');
var api_quota = te.data('api-quota');

var markup = `
<div>
<p>
<span>API Active</span>:
{{#if api_active}}
True
{{else}}
False
{{/if}}
- <a href='#' class='btn btn-xs btn-success'>Active (click to toggle)</a>
</p>
<p>
<span>API Key: <code>{{api_key}}</code></span>
</p>
<p>
<span>API Quota: <code>{{api_quota}}</code></span>
</p>
</div>
`;
var modal_id = "api-modal-" + username;
var modal_context = {
id: modal_id,
api_key: api_key,
api_active: api_active,
api_quota: api_quota,
title: "API Information for " + username,
body: markup
};
var mt_html = modal_template(modal_context);
var compiled_mt = Handlebars.compile(mt_html);
mt_html = compiled_mt(modal_context);
appendModal(mt_html, modal_id);
});

$('.activate-edit-modal').click(function () {
// activate modal
});

});
29 changes: 29 additions & 0 deletions public/js/handlebars-v4.0.5.min.js

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion resources/views/admin.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,35 @@
])

{!! $user_links->fragment('links')->render() !!}
{{-- Add search functions --}}
</div>

<div role="tabpanel" class="tab-pane" id="settings">
<h3>Change Password</h3>
<form action='/admin/action/change_password' method='POST'>
Old Password: <input class="form-control password-box" type='password' name='current_password' />
New Password: <input class="form-control password-box" type='password' name='new_password' />
<input type='submit' class='btn btn-success'/>
<input type='submit' class='btn btn-success change-password-btn'/>
</form>
</div>

@if ($role == 'admin')
<div role="tabpanel" class="tab-pane" id="admin">
<h3>Links</h3>

@include('snippets.link_table', [
'links' => $admin_links
])

{!! $admin_links->fragment('admin')->render() !!}

<h3>Users</h3>
@include('snippets.user_table', [
'users' => $admin_users
])

{!! $admin_users->fragment('admin')->render() !!}

</div>
@endif
</div>
Expand All @@ -55,5 +67,10 @@
@endsection

@section('js')
{{-- Include modal templates --}}
@include('snippets.modals')

{{-- Include extra JS --}}
<script src='/js/handlebars-v4.0.5.min.js'></script>
<script src='/js/admin.js'></script>
@endsection
32 changes: 32 additions & 0 deletions resources/views/layouts/minimal.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
Polr, a minimalist URL shortening platform.
Copyright (C) 2013-2015 Chaoyi Zha
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-->

<!DOCTYPE html>
<html>
<head>
<title>Polr @yield('title')</title>
@yield('css')
</head>
<body>
<div class='container'>
@yield('content')
</div>
@yield('js')
</body>
</html>
16 changes: 16 additions & 0 deletions resources/views/setup.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@extends('layouts.minimal')

@section('title')
Setup
@endsection

@section('css')
<link rel='stylesheet' href='/css/default-bootstrap.min.css' />
@endsection

@section('content')
<img src='/img/logo.png' />
<h1>Setup</h1>


@endsection
2 changes: 1 addition & 1 deletion resources/views/snippets/link_table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@foreach ($links as $link)
<tr>
<td>{{$link->short_url}}</td>
<!-- TODO truncate long link -->
{{-- TODO truncate long link --}}
<td>{{$link->long_url}}</td>
<td>{{$link->clicks}}</td>
<td>{{$link->created_at}}</td>
Expand Down
23 changes: 23 additions & 0 deletions resources/views/snippets/modals.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script id="modal-template" type="text/x-handlebars-template">

<div class="modal fade" id="@{{id}}" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title">@{{title}}</h4>
</div>
<div class="modal-body">
@{{{body}}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
</div>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->

</script>
40 changes: 40 additions & 0 deletions resources/views/snippets/user_table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<table class="table table-hover">
<tr>
<th>Username</th>
<th>Email</th>
<th>Created At</th>
<th>Activated</th>
<th>API</th>
<th>More</th>
</tr>
@foreach ($users as $user)
<tr>
<td>{{$user->username}}</td>
<td>{{$user->email}}</td>
<td>{{$user->created_at}}</td>
<td>{{$user->active}}</td>
{{-- <td>Active: {{$user->api_active}}</td> --}}
<td>
<a href='#'
class='activate-api-modal btn btn-sm btn-info'

data-api-active='{{$user->api_active}}'
data-api-key='{{$user->api_key}}'
data-api-quota='{{$user->api_quota}}'
data-username='{{$user->username}}'>
API info
</a>
</td>

<td>
<a href='#'
class='activate-edit-modal btn btn-sm btn-success'

data-username='{{$user->username}}'>
Edit
</a>
</td>

</tr>
@endforeach
</table>

0 comments on commit 8ddc8ca

Please sign in to comment.