PHP code example of offline / oc-site-search-plugin
1. Go to this page and download the library: Download offline/oc-site-search-plugin library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
[searchResults]
resultsPerPage = 10
showProviderBadge = 1
noResultsMessage = "Your search returned no results."
visitPageMessage = "Visit page"
==
function onInit()
{
\Event::listen('offline.sitesearch.results', function ($results) {
// return $results->filter(...);
return $results->sortByDesc('model.custom_attribute');
});
}
==
{% component 'searchResults' %}
public function boot()
{
\Event::listen('offline.sitesearch.query', function ($query) {
// The controller is used to generate page URLs.
$controller = \Cms\Classes\Controller::getController() ?? new \Cms\Classes\Controller();
// Search your plugin's contents
$items = YourCustomDocumentModel
::where('title', 'like', "%${query}%")
->orWhere('content', 'like', "%${query}%")
->get();
// Now build a results array
$results = $items->map(function ($item) use ($query, $controller) {
// If the query is found in the title, set a relevance of 2
$relevance = mb_stripos($item->title, $query) !== false ? 2 : 1;
// Optional: Add an age penalty to older results. This makes sure that
// newer results are listed first.
// if ($relevance > 1 && $item->created_at) {
// $ageInDays = $item->created_at->diffInDays(\Illuminate\Support\Carbon::now());
// $relevance -= \OFFLINE\SiteSearch\Classes\Providers\ResultsProvider::agePenaltyForDays($ageInDays);
// }
return [
'title' => $item->title,
'text' => $item->content,
'url' => $controller->pageUrl('cms-page-file-name', ['slug' => $item->slug]),
'thumb' => optional($item->images)->first(), // Instance of System\Models\File
'relevance' => $relevance, // higher relevance results in a higher
// position in the results listing
// 'meta' => 'data', // optional, any other information you want
// to associate with this result
// 'model' => $item, // optional, pass along the original model
];
});
return [
'provider' => 'Document', // The badge to display for this result
'results' => $results,
];
});
}
public function boot()
{
Event::listen('offline.sitesearch.extend', function () {
return new DocumentsSearchProvider();
// or
// return [new DocumentsSearchProvider(), new FilesSearchProvider()];
});
}
use OFFLINE\SiteSearch\Classes\Providers\ResultsProvider;
class DocumentsSearchProvider extends ResultsProvider
{
public function search()
{
// Get your matching models
$matching = YourCustomDocumentModel::where('title', 'like', "%{$this->query}%")
->orWhere('content', 'like', "%{$this->query}%")
->get();
// Create a new Result for every match
foreach ($matching as $match) {
$result = $this->newResult();
$result->relevance = 1;
$result->title = $match->title;
$result->text = $match->description;
$result->url = $match->url;
$result->thumb = $match->image;
$result->model = $match;
$result->meta = [
'some_data' => $match->some_other_property,
];
// Add the results to the results collection
$this->addResult($result);
}
return $this;
}
public function displayName()
{
return 'My Result';
}
public function identifier()
{
return 'VendorName.PluginName';
}
}
// app/Provider.php
namespace App;
use Cms\Classes\Controller;
use System\Classes\AppBase;
use Tailor\Classes\Blueprint\EntryBlueprint;
use Tailor\Models\EntryRecord;
class Provider extends AppBase
{
// ...
/**
* Build the target URL for a OFFLINE.SiteSearch result.
*/
public static function resolveSiteSearchUrl(Controller $controller,
EntryRecord $record,
EntryBlueprint $blueprint): string
{
if ($blueprint->handle === 'SomethingSpecial') {
return '/special-url';
}
return $controller->pageUrl('my-custom-page', [
'slug' => $record->slug,
]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.