Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor the controller #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 25 additions & 50 deletions app/controllers/AccountController.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
<?php

use Illuminate\Http\Request;

class AccountController extends BaseController {

### Sign In
/* After submitting the sign-in form */
public function postSignIn() {
$validator = Validator::make(Input::all(),
array(
'username' => 'required',
'password' => 'required'
)
);
if($validator->fails()) {
// Redirect to the sign in page
return Redirect::route('account-sign-in')
->withErrors($validator)
->withInput(); // redirect the input

} else {

$remember = (Input::has('remember')) ? true : false;
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
), $remember);
}
public function postSignIn(Request $request) {

if($auth) {

return Redirect::intended('/');
$this->validate($request, [
'username' => 'required',
'password' => 'required'
]);

} else {

return Redirect::route('account-sign-in')
->with('global', 'Wrong Email or Wrong Password.');
}
if (Auth::attempt(['username' => $request->username, 'password' => $request->password], $request->remember)) {
return Redirect::intended('/');
}

return Redirect::route('account-sign-in')
->with('global', 'There is a problem. Have you activated your account?');
}

/* Submitting the Create User form (POST) */
public function postCreate() {
public function postCreate(Request $request) {
$inputs = $request->all();
$this->validate($request, [
'username' => 'required|max:20|min:3|unique:users',
'password' => 'required',
'password_again'=> 'required|same:password'
]);

$validator = Validator::make(Input::all(),
array(
'username' => 'required|max:20|min:3|unique:users',
Expand All @@ -50,27 +38,14 @@ public function postCreate() {
)
);

if($validator->fails()) {
return Redirect::route('account-create')
->withErrors($validator)
->withInput(); // fills the field with the old inputs what were correct

} else {
// create an account
$username = Input::get('username');
$password = Input::get('password');
$userdata = User::create(array(
'username' => $input['username'],
'password' => Hash::make($input['password']) // Changed the default column for Password
));

$userdata = User::create(array(
'username' => $username,
'password' => Hash::make($password) // Changed the default column for Password
));

if($userdata) {


return Redirect::route('account-sign-in')
->with('global', 'Your account has been created. We have sent you an email to activate your account');
}
if($userdata) {
return Redirect::route('account-sign-in')
->with('global', 'Your account has been created. We have sent you an email to activate your account');
}
}

Expand Down
69 changes: 31 additions & 38 deletions app/controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class BaseController extends Controller {

protected $filter_params = array();
protected $sort_params = array();
protected $filter_params = array();

protected $sort_params = array();

/**
* Setup the layout used by the controller.
Expand All @@ -19,47 +19,40 @@ protected function setupLayout()
}
}

/*
* Apply pagination based on Get parameters to index queries
*/
protected function paginateQuery(&$query) {
$start = 0;
$limit = 30;
if(Input::has('offset')) {
$start = intval(Input::get('offset'));
}
if(Input::has('limit')) {
$limit = intval(Input::get('limit'));
/*
* Apply pagination based on Get parameters to index queries
*/
protected function paginateQuery(&$query) {
$limit = 30;
$query->paginate($limit);
}

/*
* Apply filters based on Get parameters to index queries
*/
protected function filterQuery(&$query) {
foreach($this->filter_params as $filter) {
if(Input::has($filter)) {
$val = Input::get($filter);
$query->where($filter, '=', $val);
}
$query->skip($start)->take($limit);
}
}

/*
* Apply filters based on Get parameters to index queries
*/
protected function filterQuery(&$query) {
foreach($this->filter_params as $filter) {
if(Input::has($filter)) {
$val = Input::get($filter);
$query->where($filter, '=', $val);
}
}
}

protected function sortQuery(&$query) {
if(Input::has('sorts')) {
$sorts = explode(",", Input::get('sorts'));
foreach($sorts as $sort) {
$param = explode(':', $sort);
if(in_array($param[0], $this->sort_params)) {
if(isset($param[1]) && strtolower($param[1])=='desc') {
$query->orderBy($param[0], 'desc');
} else {
$query->orderBy($param[0], 'asc');
}
protected function sortQuery(&$query) {
if(Input::has('sorts')) {
$sorts = explode(",", Input::get('sorts'));
foreach($sorts as $sort) {
$param = explode(':', $sort);
if(in_array($param[0], $this->sort_params)) {
if(isset($param[1]) && strtolower($param[1])=='desc') {
$query->orderBy($param[0], 'desc');
} else {
$query->orderBy($param[0], 'asc');
}
}
}
}
}

}
23 changes: 10 additions & 13 deletions app/controllers/BooksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,17 @@ public function index()

$book_list = $book_list->get();

for($i=0; $i<count($book_list); $i++){
foreach ($book_list as $book) {
$id = $book->id;

$id = $book_list[$i]['book_id'];
$conditions = array(
'book_id' => $id,
'available_status' => 1
);
$book->total_books = Issue::where('book_id','=',$id)
->count();

$book_list[$i]['total_books'] = Issue::select()
->where('book_id','=',$id)
->count();

$book_list[$i]['avaliable'] = Issue::select()
->where($conditions)
->count();
$book->avaliable = Issue::where([
'book_id' => $id,
'available_status' => 1
])
->count();
}

return $book_list;
Expand Down Expand Up @@ -166,6 +162,7 @@ public function edit($id)
'return_time' => 0,
'book_issue_id' => $id,
);

$book_issue_log = Logs::where($conditions)
->take(1)
->get();
Expand Down
84 changes: 46 additions & 38 deletions app/controllers/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@ public function index()

$logs = $logs->get();

for($i=0; $i<count($logs); $i++){

$issue_id = $logs[$i]['book_issue_id'];
$student_id = $logs[$i]['student_id'];

// to get the name of the book from book issue id
foreach($logs as $log) {
$issue_id = $log->book_issue_id;
$student_id = $log->student_id;

// to get the name of the book from book issue id
$issue = Issue::find($issue_id);
$book_id = $issue->book_id;
$book = Books::find($book_id);
$logs[$i]['book_name'] = $book->title;
$log->book_name = $book->title;

// to get the name of the student from student id
$student = Student::find($student_id);
$logs[$i]['student_name'] = $student->first_name . ' ' . $student->last_name;
$logs->student_name = $student->first_name . ' ' . $student->last_name;

// change issue date and return date in human readable format
$logs[$i]['issued_at'] = date('d-M', $logs[$i]['issued_at']);
$logs[$i]['return_at'] = date('d-M', $logs[$i]['issued_at'] + 1209600);

$logs[$i]['issued_at'] = date('d-M', strtotime($log->issued_at));
$logs[$i]['return_at'] = date('d-M', strtotime($log->issued_at) + 1209600);
}

return $logs;
Expand All @@ -47,61 +45,71 @@ public function store()
$studentID = $data['studentID'];

$student = Student::find($studentID);

if($student == NULL){
throw new Exception('Invalid Student ID');
} else {
}

if($student != NULL) {
$approved = $student->approved;

if($approved == 0){
throw new Exception('Student still not approved by Admin Librarian');
} else {
}

if($approved != 0) {
$books_issued = $student->books_issued;
$category = $student->category;

$max_allowed = StudentCategories::where('cat_id', '=', $category)->firstOrFail()->max_allowed;

if($books_issued >= $max_allowed){
throw new Exception('Student cannot issue any more books');
} else {
}

if($books_issued <= $max_allowed) {
$book = Issue::find($bookID);
if($book == NULL){
throw new Exception('Invalid Book Issue ID');
} else {
}

if($book != null) {
$book_availability = $book->available_status;

if($book_availability != 1){
throw new Exception('Book not available for issue');
} else {
}

// book is to be issued
DB::transaction( function() use($bookID, $studentID) {
$log = new Logs;
// book is to be issued
DB::transaction( function() use($bookID, $studentID) {
$log = new Logs;

$log->book_issue_id = $bookID;
$log->student_id = $studentID;
$log->issue_by = Auth::id();
$log->issued_at = time();
$log->return_time = 0;
$log->book_issue_id = $bookID;
$log->student_id = $studentID;
$log->issue_by = Auth::id();
$log->issued_at = time();
$log->return_time = 0;

$log->save();
$log->save();

// changing the availability status
$book = Issue::find($bookID);
$book->available_status = 0;
$book->save();
// changing the availability status
$book = Issue::find($bookID);
$book->available_status = 0;
$book->save();

// increasing number of books issed by student
$student = Student::find($studentID);
$student->books_issued = $student->books_issued + 1;
$student->save();
});
// increasing number of books issed by student
$student = Student::find($studentID);
$student->books_issued = $student->books_issued + 1;
$student->save();
});

return 'Successfully Issued';

return 'Successfully Issued';
}
}
}
}
}

}

public function show($id)
Expand Down
6 changes: 3 additions & 3 deletions app/views/account/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<div class="module-body">
<div class="control-group">
<div class="controls row-fluid">
<input class="span12" type="text" placeholder="Username" name="username" value="{{ Input::old('login') }}">
@if($errors->has('login'))
{{ $errors->first('login')}}
<input class="span12" type="text" placeholder="Username" name="username" value="{{ Input::old('username') }}">
@if($errors->has('username'))
{{ $errors->first('username')}}
@endif
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/account/signin.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<div class="control-group">
<div class="controls row-fluid">
<input class="span12" type="text" name="username" placeholder="Username" value="{{ Input::old('login') }}" autofocus>
@if($errors->has('user_login'))
{{ $errors->first('login')}}
@if($errors->has('username'))
{{ $errors->first('username')}}
@endif
</div>
</div>
Expand Down
Loading