KodeStar
6 years ago
committed by
GitHub
13 changed files with 207 additions and 58 deletions
@ -0,0 +1,28 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers; |
||||
|
|
||||
|
use Illuminate\Http\Request; |
||||
|
use App\Http\Controllers\Controller; |
||||
|
use App\Search; |
||||
|
|
||||
|
class SearchController extends Controller |
||||
|
{ |
||||
|
public function index(Request $request) |
||||
|
{ |
||||
|
$requestprovider = $request->input('provider'); |
||||
|
$query = $request->input('q'); |
||||
|
|
||||
|
$provider = Search::providerDetails($requestprovider); |
||||
|
|
||||
|
if($provider->type == 'standard') { |
||||
|
return redirect($provider->url.'?'.$provider->var.'='.urlencode($query)); |
||||
|
} elseif($provider->type == 'external') { |
||||
|
$class = new $provider->class; |
||||
|
//print_r($provider); |
||||
|
return $class->getResults($query, $provider); |
||||
|
} |
||||
|
|
||||
|
//print_r($provider); |
||||
|
} |
||||
|
} |
@ -0,0 +1,135 @@ |
|||||
|
<?php namespace App; |
||||
|
|
||||
|
use GuzzleHttp\Exception\GuzzleException; |
||||
|
use GuzzleHttp\Client; |
||||
|
use App\Item; |
||||
|
use App\Setting; |
||||
|
use Form; |
||||
|
use Cache; |
||||
|
|
||||
|
abstract class Search |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* List of all search providers |
||||
|
* |
||||
|
* @return Array |
||||
|
*/ |
||||
|
public static function providers() |
||||
|
{ |
||||
|
$providers = self::standardProviders(); |
||||
|
$providers = $providers + self::appProviders(); |
||||
|
return $providers; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Gets details for a single provider |
||||
|
* |
||||
|
* @return Object |
||||
|
*/ |
||||
|
public static function providerDetails($provider) |
||||
|
{ |
||||
|
$providers = self::providers(); |
||||
|
if(!isset($providers[$provider])) return false; |
||||
|
return (object)$providers[$provider] ?? false; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Array of the standard providers |
||||
|
* |
||||
|
* @return Array |
||||
|
*/ |
||||
|
public static function standardProviders() |
||||
|
{ |
||||
|
return [ |
||||
|
'google' => [ |
||||
|
'url' => 'https://www.google.com/search', |
||||
|
'var' => 'q', |
||||
|
'method' => 'get', |
||||
|
'type' => 'standard', |
||||
|
], |
||||
|
'ddg' => [ |
||||
|
'url' => 'https://duckduckgo.com/', |
||||
|
'var' => 'q', |
||||
|
'method' => 'get', |
||||
|
'type' => 'standard', |
||||
|
], |
||||
|
'bing' => [ |
||||
|
'url' => 'https://www.bing.com/search', |
||||
|
'var' => 'q', |
||||
|
'method' => 'get', |
||||
|
'type' => 'standard', |
||||
|
], |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Loops through users apps to see if app is a search provider, might be worth |
||||
|
* looking into caching this at some point |
||||
|
* |
||||
|
* @return Array |
||||
|
*/ |
||||
|
public static function appProviders() |
||||
|
{ |
||||
|
$providers = []; |
||||
|
$userapps = Item::all(); |
||||
|
foreach($userapps as $app) { |
||||
|
if(empty($app->class)) continue; |
||||
|
if(($provider = Item::isSearchProvider($app->class)) !== false) { |
||||
|
$name = Item::nameFromClass($app->class); |
||||
|
$providers[strtolower($name)] = [ |
||||
|
'type' => $provider->type, |
||||
|
'class' => $app->class, |
||||
|
'url' => $app->url, |
||||
|
]; |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
return $providers; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Outputs the search form |
||||
|
* |
||||
|
* @return html |
||||
|
*/ |
||||
|
public static function form() |
||||
|
{ |
||||
|
$output = ''; |
||||
|
$homepage_search = Setting::fetch('homepage_search'); |
||||
|
$search_provider = Setting::where('key', '=', 'search_provider')->first(); |
||||
|
$user_search_provider = Setting::fetch('search_provider'); |
||||
|
//die(print_r($search_provider)); |
||||
|
|
||||
|
//die(var_dump($user_search_provider)); |
||||
|
// return early if search isn't applicable |
||||
|
if((bool)$homepage_search !== true) return $output; |
||||
|
$user_search_provider = $user_search_provider ?? 'none'; |
||||
|
|
||||
|
if((bool)$homepage_search && (bool)$search_provider) { |
||||
|
|
||||
|
if((bool)$user_search_provider) { |
||||
|
$name = 'app.options.'.$user_search_provider; |
||||
|
$provider = self::providerDetails($user_search_provider); |
||||
|
|
||||
|
$output .= '<div class="searchform">'; |
||||
|
$output .= Form::open(['url' => 'search', 'method' => 'get']); |
||||
|
$output .= '<div id="search-container" class="input-container">'; |
||||
|
$output .= '<select name="provider">'; |
||||
|
foreach(self::providers() as $key => $searchprovider) { |
||||
|
$selected = ($key === $user_search_provider) ? ' selected="selected"' : ''; |
||||
|
$output .= '<option value="'.$key.'"'.$selected.'>'.__('app.options.'.$key).'</option>'; |
||||
|
} |
||||
|
$output .= '</select>'; |
||||
|
$output .= Form::text('q', null, ['class' => 'homesearch', 'autofocus' => 'autofocus', 'placeholder' => __('app.settings.search').'...']); |
||||
|
$output .= '<button type="submit">'.ucwords(__('app.settings.search')).'</button>'; |
||||
|
$output .= '</div>'; |
||||
|
$output .= Form::close(); |
||||
|
$output .= '</div>'; |
||||
|
} |
||||
|
} |
||||
|
return $output; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
<?php namespace App; |
||||
|
|
||||
|
use GuzzleHttp\Exception\GuzzleException; |
||||
|
use GuzzleHttp\Client; |
||||
|
|
||||
|
interface SearchInterface |
||||
|
{ |
||||
|
public function getResults($query, $providerdetails); |
||||
|
|
||||
|
} |
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@ |
|||||
{ |
{ |
||||
"/css/app.css": "/css/app.css?id=bfa06f05dc25570a993d", |
"/css/app.css": "/css/app.css?id=c067b0b68e4516e903e1", |
||||
"/js/app.js": "/js/app.js?id=0db2e72b5cd42d83e306" |
"/js/app.js": "/js/app.js?id=8dc4a6ea723d0df7469d" |
||||
} |
} |
||||
|
@ -1 +1 @@ |
|||||
{!! App\Setting::search() !!} |
{!! App\Search::form() !!} |
||||
|
Loading…
Reference in new issue