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

Added search feature on Donations and Needs page #183

Open
wants to merge 3 commits into
base: dev
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
15 changes: 15 additions & 0 deletions app/Http/Controllers/DonationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Validator;
use Illuminate\Support\Facades\Input;

class DonationController extends Controller
{
Expand Down Expand Up @@ -162,4 +163,18 @@ public function post(Request $request) {

return json_encode($response, JSON_UNESCAPED_UNICODE);
}

/**
* Search Donations
*
* @return type
*/
public function search(){

$search = Input::get('search');
$donations = $this->donation->searchDonations($search);
Input::flash();
return view('/frontend/donations/index')->with(['donations' => ($donations) ? $donations : array()]);

}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function emergency()
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function searchDonationsOrNeeds(){
DB::enableQueryLog();

$search = Input::get('search');
$searchFor = Input::get('type');
$donations = array();
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/NeedsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Validator;
use Illuminate\Support\Facades\Input;

class NeedsController extends Controller
{
Expand Down Expand Up @@ -147,5 +148,19 @@ public function post(Request $request) {

return json_encode($response, JSON_UNESCAPED_UNICODE);
}

/**
* Search needs
*
* @return type
*/
public function search(){

$search = Input::get('search');
$needs = $this->need->searchNeeds($search);
Input::flash();
return view('/frontend/needs/index')->with(['needs' => ($needs) ? $needs : array()]);

}
}

2 changes: 1 addition & 1 deletion app/Repositories/DonationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function searchDonations($search){
$query = $query->where(DB::Raw('CONCAT_WS(" ",name,telephone,address,city,donation,information) '),"like","%$searchPart%");
}
}
return $query->get();
return $query->orderBy('id', 'desc')->paginate(\Config::get('rf_settings.donations_page_pagination'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\Config::get can be replaced with config() calls

} catch (\Exception $e) {
Log::error($e->getMessage());
return false;
Expand Down
4 changes: 2 additions & 2 deletions app/Repositories/NeedsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function findNeed($id)
/**
*
* @param type $search
* @return boolean
* @return Array
*/
public function searchNeeds($search){

Expand All @@ -83,7 +83,7 @@ public function searchNeeds($search){
$query = $query->where(DB::Raw('CONCAT_WS(" ",name,telephone,address,city,needs)'),"like","%$searchPart%");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This query might be open to SQL injection. Since the calls to searchNeeds with the parameter is passed to a DB::raw which doesn't use prepared statements nor sanitises the input.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nadeeravista could you please have a look on @gayanhewa's comment as well as fix conflicts?

}
}
return $query->get();
return $query->orderBy('id', 'desc')->paginate(\Config::get('rf_settings.needs_page_pagination'));
} catch (\Exception $e) {
Log::error($e->getMessage());
return false;
Expand Down
23 changes: 23 additions & 0 deletions config/rf_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


return [
/*
|--------------------------------------------------------------------------
| Home page pagination for both donations and needs
|--------------------------------------------------------------------------
*/
'home_page_pagination' => '10',
/*
|--------------------------------------------------------------------------
| Neees page pagination
|--------------------------------------------------------------------------
*/
'needs_page_pagination'=> '10',
/*
|--------------------------------------------------------------------------
| Donations page paginations
|--------------------------------------------------------------------------
*/
'donations_page_pagination'=> '10',
];
18 changes: 15 additions & 3 deletions resources/views/frontend/donations/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

@section('content')
<div class="container main-container">
<div class="row">
<div class="col-md-12">
<p style="float: right;">
<a href="/donations/add"><button type="button" class="btn btn-primary btn-hg btn-mobile-block">මෙතනින් ආධාර එකතු කරන්න</button></a>
</p>
</div>
</div>
<div class="well hero">
{!! Form::open(['url' => 'donations/search','id'=>'reports']) !!}
<div class="row">
<div class="col-lg-10"><input name="search" type="text" class="form-control" placeholder="ඔබී සෙවුම මෙහි ඇතුලත් කරන්න." value="{{ Input::old('search')}}"></div>
<div class="col-lg-2"><button class="btn btn-primary btn-hg btn-mobile-block" type="submit" type="button">සොයන්න</button></div>
</div>
{!! Form::close() !!}
</div>
<div class="row">
<div class="col-md-12">
@if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
@endif
<p style="float: right;">
<a href="/donations/add"><button type="button" class="btn btn-primary btn-hg btn-mobile-block">මෙතනින් ආධාර එකතු කරන්න</button></a>
</p>
<table class="table table-responsive" id="donations-table">
<thead>
<tr>
Expand Down
2 changes: 0 additions & 2 deletions resources/views/frontend/home.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
</div>
<div class="well hero">
{!! Form::open(['url' => 'search-donations-needs','id'=>'reports']) !!}
<!-- Example single danger button -->

<div class="row">
<div class="col-sm-2">{!! Form::select('type', array('all'=>'සියලු', 'donations' => 'ආධාර', 'needs' => 'අවශ්‍යතා'), '1', array("class"=>"btn btn-lg btn-primary selectpicker")) !!}</div>
<div class="col-sm-8"><input name="search" type="text" class="form-control" placeholder="ඔබී සෙවුම මෙහි ඇතුලත් කරන්න." value="{{ Input::old('search')}}"></div>
Expand Down
15 changes: 15 additions & 0 deletions resources/views/frontend/needs/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

@section('content')
<div class="container main-container">
<div class="row">
<div class="col-md-12">
<p style="float: right;">
<a href="/needs/add"><button type="button" class="btn btn-primary btn-hg btn-mobile-block">මෙතනින් අවශ්‍යතා එකතු කරන්න</button></a>
</p>
</div>
</div>
<div class="well hero">
{!! Form::open(['url' => 'needs/search','id'=>'reports']) !!}
<div class="row">
<div class="col-lg-10"><input name="search" type="text" class="form-control" placeholder="ඔබී සෙවුම මෙහි ඇතුලත් කරන්න." value="{{ Input::old('search')}}"></div>
<div class="col-lg-2"><button class="btn btn-primary btn-hg btn-mobile-block" type="submit" type="button">සොයන්න</button></div>
</div>
{!! Form::close() !!}
</div>
<div class="row">
<div class="col-md-12">
@if (session('message'))
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
Route::post('/donations/add', 'DonationController@save');
Route::get('/donations/show/{id}', 'DonationController@show');
Route::get('/online-donations', 'DonationController@showOnlineDonations');
Route::post('/donations/search', 'DonationController@search');

Route::get('/needs', 'NeedsController@index');
Route::get('/needs/add', 'NeedsController@add');
Route::post('/needs/add', 'NeedsController@save');
Route::get('/needs/show/{id}', 'NeedsController@show');
Route::post('/needs/search', 'NeedsController@search');

Route::get('/emergency-contacts', 'HomeController@emergency');
Route::get('/twitter-feed', 'FeedsController@index');
Expand Down