2022-03-19 13:54:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
2019-01-16 14:47:32 +00:00
|
|
|
|
|
|
|
use App\Item;
|
2019-01-18 15:21:50 +00:00
|
|
|
use App\Setting;
|
|
|
|
use Cache;
|
2022-03-19 13:54:32 +00:00
|
|
|
use Form;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2022-03-13 19:30:24 +00:00
|
|
|
use Yaml;
|
2019-01-16 14:47:32 +00:00
|
|
|
|
|
|
|
abstract class Search
|
|
|
|
{
|
2019-01-18 18:21:44 +00:00
|
|
|
/**
|
|
|
|
* List of all search providers
|
2022-03-19 13:54:32 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2019-01-18 18:21:44 +00:00
|
|
|
*/
|
2019-01-16 14:47:32 +00:00
|
|
|
public static function providers()
|
|
|
|
{
|
|
|
|
$providers = self::standardProviders();
|
2019-01-18 15:21:50 +00:00
|
|
|
$providers = $providers + self::appProviders();
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-03-13 19:30:24 +00:00
|
|
|
return collect($providers);
|
2019-01-16 14:47:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 18:21:44 +00:00
|
|
|
/**
|
|
|
|
* Gets details for a single provider
|
2022-03-19 13:54:32 +00:00
|
|
|
*
|
|
|
|
* @return object
|
2019-01-18 18:21:44 +00:00
|
|
|
*/
|
2019-01-16 14:47:32 +00:00
|
|
|
public static function providerDetails($provider)
|
|
|
|
{
|
|
|
|
$providers = self::providers();
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! isset($providers[$provider])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (object) $providers[$provider] ?? false;
|
2019-01-16 14:47:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 18:21:44 +00:00
|
|
|
/**
|
|
|
|
* Array of the standard providers
|
2022-03-19 13:54:32 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2019-01-18 18:21:44 +00:00
|
|
|
*/
|
2019-01-16 14:47:32 +00:00
|
|
|
public static function standardProviders()
|
|
|
|
{
|
2022-03-13 19:30:24 +00:00
|
|
|
// $providers = json_decode(file_get_contents(storage_path('app/searchproviders.json')));
|
|
|
|
// print_r($providers);
|
|
|
|
$providers = Yaml::parseFile(storage_path('app/searchproviders.yaml'));
|
|
|
|
$all = [];
|
2022-03-19 13:54:32 +00:00
|
|
|
foreach ($providers as $key => $provider) {
|
2022-03-13 19:30:24 +00:00
|
|
|
$all[$key] = $provider;
|
|
|
|
$all[$key]['type'] = 'standard';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $all;
|
2019-01-16 14:47:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 18:21:44 +00:00
|
|
|
/**
|
|
|
|
* Loops through users apps to see if app is a search provider, might be worth
|
|
|
|
* looking into caching this at some point
|
2022-03-19 13:54:32 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2019-01-18 18:21:44 +00:00
|
|
|
*/
|
2019-01-18 15:21:50 +00:00
|
|
|
public static function appProviders()
|
|
|
|
{
|
|
|
|
$providers = [];
|
|
|
|
$userapps = Item::all();
|
2022-03-19 13:54:32 +00:00
|
|
|
foreach ($userapps as $app) {
|
|
|
|
if (empty($app->class)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (($provider = Item::isSearchProvider($app->class)) !== false) {
|
2019-01-18 15:21:50 +00:00
|
|
|
$name = Item::nameFromClass($app->class);
|
2019-09-11 17:42:33 +00:00
|
|
|
$providers[$app->id] = [
|
2022-03-13 19:30:24 +00:00
|
|
|
'id' => $app->id,
|
2019-01-18 15:21:50 +00:00
|
|
|
'type' => $provider->type,
|
2019-01-18 18:21:44 +00:00
|
|
|
'class' => $app->class,
|
|
|
|
'url' => $app->url,
|
2022-03-13 19:30:24 +00:00
|
|
|
'name' => $app->title,
|
2019-09-11 17:42:33 +00:00
|
|
|
'colour' => $app->colour,
|
|
|
|
'icon' => $app->icon,
|
2022-03-19 13:54:32 +00:00
|
|
|
'description' => $app->description,
|
2019-01-18 15:21:50 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2019-01-18 15:21:50 +00:00
|
|
|
return $providers;
|
|
|
|
}
|
|
|
|
|
2019-01-18 18:21:44 +00:00
|
|
|
/**
|
|
|
|
* Outputs the search form
|
2022-03-19 13:54:32 +00:00
|
|
|
*
|
2019-01-18 15:21:50 +00:00
|
|
|
* @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));
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2019-01-18 15:21:50 +00:00
|
|
|
//die(var_dump($user_search_provider));
|
|
|
|
// return early if search isn't applicable
|
2022-03-19 13:54:32 +00:00
|
|
|
if ((bool) $homepage_search !== true) {
|
|
|
|
return $output;
|
|
|
|
}
|
2019-01-18 18:21:44 +00:00
|
|
|
$user_search_provider = $user_search_provider ?? 'none';
|
2019-01-18 15:21:50 +00:00
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if ((bool) $homepage_search && (bool) $search_provider) {
|
|
|
|
if ((bool) $user_search_provider) {
|
2019-01-18 15:21:50 +00:00
|
|
|
$name = 'app.options.'.$user_search_provider;
|
|
|
|
$provider = self::providerDetails($user_search_provider);
|
|
|
|
|
|
|
|
$output .= '<div class="searchform">';
|
2019-06-18 11:25:05 +00:00
|
|
|
$output .= '<form action="'.url('search').'"'.getLinkTargetAttribute().' method="get">';
|
2019-01-18 15:21:50 +00:00
|
|
|
$output .= '<div id="search-container" class="input-container">';
|
|
|
|
$output .= '<select name="provider">';
|
2022-03-19 13:54:32 +00:00
|
|
|
foreach (self::providers() as $key => $searchprovider) {
|
|
|
|
$selected = ((string) $key === (string) $user_search_provider) ? ' selected="selected"' : '';
|
2022-03-13 19:30:24 +00:00
|
|
|
$output .= '<option value="'.$key.'"'.$selected.'>'.$searchprovider['name'].'</option>';
|
2019-01-18 15:21:50 +00:00
|
|
|
}
|
|
|
|
$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>';
|
2019-06-18 11:25:05 +00:00
|
|
|
$output .= '</form>';
|
2019-01-18 15:21:50 +00:00
|
|
|
$output .= '</div>';
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2019-01-18 15:21:50 +00:00
|
|
|
return $output;
|
|
|
|
}
|
2019-09-11 17:42:33 +00:00
|
|
|
}
|