Merge branch 'search' into dev
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
<?php
|
||||
namespace App\Handlers\Search;
|
||||
|
||||
use App\Handlers\Contracts\SearchHandler;
|
||||
use App\Models\Content as ContentModel;
|
||||
|
||||
class Content implements SearchHandler
|
||||
class Content extends Search
|
||||
{
|
||||
/**
|
||||
* Search for given patttern/term
|
||||
@@ -15,14 +14,14 @@ class Content implements SearchHandler
|
||||
*/
|
||||
public function search($term)
|
||||
{
|
||||
$token = '%' . $term . '%';
|
||||
$token = $this->tokenize($term);
|
||||
|
||||
return ContentModel::where('content', 'LIKE', $token)
|
||||
->orWhere('meta_description', 'LIKE', $token)
|
||||
->orWhere('meta_keywords', 'LIKE', $token)
|
||||
->orWhere('title', 'LIKE', $token)
|
||||
->orWhere('url', 'LIKE', $token)
|
||||
->get();
|
||||
->orWhere('meta_description', 'LIKE', $token)
|
||||
->orWhere('meta_keywords', 'LIKE', $token)
|
||||
->orWhere('title', 'LIKE', $token)
|
||||
->orWhere('url', 'LIKE', $token)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getId()
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace App\Handlers\Search;
|
||||
|
||||
use App\Models\File;
|
||||
|
||||
class Files extends Search
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return "files";
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return trans('search.files');
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for given patttern/term
|
||||
*
|
||||
* @param string $term searched word
|
||||
*
|
||||
* @return array list of found objects
|
||||
*/
|
||||
public function search($term)
|
||||
{
|
||||
$token = $this->tokenize($term);
|
||||
|
||||
return File::where('original_filename', 'LIKE', $token)
|
||||
->orWhere('keywords', 'LIKE', $token)
|
||||
->orWhere('description', 'LIKE', $token)
|
||||
->orWhere('mime_type', 'LIKE', $token)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace App\Handlers\Search\Presenters;
|
||||
|
||||
use App\Handlers\Contracts\SearchPresenterHandler;
|
||||
|
||||
class AdminFiles implements SearchPresenterHandler
|
||||
{
|
||||
/**
|
||||
* Method for rendering list of found results
|
||||
*
|
||||
* @param $foundItems
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function render($foundItems)
|
||||
{
|
||||
return view('admin.fileManager.filesTiles', ['files' => $foundItems]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace App\Handlers\Search;
|
||||
|
||||
use App\Handlers\Contracts\SearchHandler;
|
||||
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
||||
|
||||
abstract class Search implements SearchHandler
|
||||
{
|
||||
/**
|
||||
* Tokenize string for usage in LIKE clausules
|
||||
*
|
||||
* @param $term
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function tokenize($term)
|
||||
{
|
||||
$term = trim($term);
|
||||
|
||||
if (empty($term)) {
|
||||
throw new InvalidArgumentException(trans('search.emptyString'));
|
||||
}
|
||||
|
||||
return '%' . str_replace(' ', '%', $term) . '%';
|
||||
}
|
||||
}
|
||||
@@ -2,26 +2,49 @@
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Handlers\Contracts\SearchHandler;
|
||||
use App\Handlers\Contracts\SearchPresenterHandler;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Input;
|
||||
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
||||
|
||||
class Search extends Controller
|
||||
{
|
||||
/**
|
||||
* Create form for searching
|
||||
*
|
||||
* @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function getSearch()
|
||||
{
|
||||
|
||||
$results = [];
|
||||
$term = Input::get('search');
|
||||
|
||||
if (!is_null($term)) {
|
||||
$results = $this->performSearch($term);
|
||||
try {
|
||||
$results = $this->performSearch($term);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
// user entered empty string (or something like empty string)
|
||||
return redirect()
|
||||
->route('search')
|
||||
->with('error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return view('admin.search.page', ['search' => $term, 'results' => $results]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method used for performing search.
|
||||
*
|
||||
* This method iterates over all registered search engines and returns results for each engine apart.
|
||||
*
|
||||
* @param string $term search text
|
||||
*
|
||||
* @return array array of results. One item per each engine
|
||||
*/
|
||||
protected function performSearch($term)
|
||||
{
|
||||
$engines = App::make('registerSearchEngine')
|
||||
@@ -29,10 +52,17 @@ class Search extends Controller
|
||||
|
||||
$resultsParts = [];
|
||||
foreach ($engines as $engine) {
|
||||
/**
|
||||
* @var SearchHandler $search
|
||||
* @var SearchPresenterHandler $presenter
|
||||
*/
|
||||
|
||||
list ($search, $presenter) = $engine;
|
||||
|
||||
// try to find given term
|
||||
$results = $search->search($term);
|
||||
|
||||
// create result object
|
||||
$resultsParts[$search->getId()] = [
|
||||
'name' => $search->getName(),
|
||||
'content' => $presenter->render($results),
|
||||
|
||||
@@ -7,7 +7,9 @@ use App\Handlers\Registrators\SettingsExtensionRegistrator;
|
||||
use App\Handlers\Registrators\TemplateRegistrator;
|
||||
use App\Handlers\Registrators\WidgetRegistrator;
|
||||
use App\Handlers\Search\Content;
|
||||
use App\Handlers\Search\Files;
|
||||
use App\Handlers\Search\Presenters\AdminContent as AdminContentPresenter;
|
||||
use App\Handlers\Search\Presenters\AdminFiles;
|
||||
use App\Widgets\HtmlCode\HtmlCode;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
@@ -108,5 +110,6 @@ class AppServiceProvider extends ServiceProvider
|
||||
$registrator = $this->app->make('registerSearchEngine');
|
||||
|
||||
$registrator->register(new Content(), new AdminContentPresenter());
|
||||
$registrator->register(new Files(), new AdminFiles());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user