- @unless ($posts->isEmpty() && $stickies->isEmpty())
-
- @unless ($posts->isEmpty())
-
-
Posts
-
- @each($theme.'.partial.post', $posts, 'post')
- @else
- @include($theme.'.partial.empty')
- @endunless
+
+ @unless ($posts->isEmpty())
+
+
Posts
-
- @include($theme.'.partial.sidebar')
-
- @else
- @include($theme.'.partial.empty')
- @endunless
+ @each($theme.'.partial.post', $posts, 'post')
+ @else
+ @include($theme.'.partial.empty')
+ @endunless
+
+
+ @include($theme.'.partial.sidebar')
+
diff --git a/resources/views/themes/zeus/partial/empty.blade.php b/resources/views/themes/zeus/partial/empty.blade.php
index aa2b820..9e96e24 100644
--- a/resources/views/themes/zeus/partial/empty.blade.php
+++ b/resources/views/themes/zeus/partial/empty.blade.php
@@ -1,4 +1,4 @@
- {{ __('No posts yet') }}!
+ {{ __('No posts found') }}!
diff --git a/resources/views/themes/zeus/partial/sidebar.blade.php b/resources/views/themes/zeus/partial/sidebar.blade.php
index acbf366..04f2f19 100644
--- a/resources/views/themes/zeus/partial/sidebar.blade.php
+++ b/resources/views/themes/zeus/partial/sidebar.blade.php
@@ -1,4 +1,5 @@
{{--@include($theme.'.partial.authors')--}}
+@include($theme.'.partial.sidebar.search')
@include($theme.'.partial.sidebar.categories')
@include($theme.'.partial.sidebar.recent')
@include($theme.'.partial.sidebar.pages')
diff --git a/resources/views/themes/zeus/partial/sidebar/search.blade.php b/resources/views/themes/zeus/partial/sidebar/search.blade.php
new file mode 100644
index 0000000..13d398f
--- /dev/null
+++ b/resources/views/themes/zeus/partial/sidebar/search.blade.php
@@ -0,0 +1,8 @@
+
diff --git a/src/Http/Livewire/Posts.php b/src/Http/Livewire/Posts.php
index 1e7e578..2fc087d 100644
--- a/src/Http/Livewire/Posts.php
+++ b/src/Http/Livewire/Posts.php
@@ -2,6 +2,9 @@
namespace LaraZeus\Sky\Http\Livewire;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Support\Facades\DB;
use LaraZeus\Sky\Models\Post;
use LaraZeus\Sky\Models\Tag;
use Livewire\Component;
@@ -10,28 +13,84 @@ class Posts extends Component
{
public function render()
{
- return view(app('theme').'.home')
- ->with([
- 'posts' => Post::NotSticky()
- ->orderBy('published_at', 'desc')
- ->get(),
+ $search = strtolower(request()->get('search'));
- 'pages' => Post::page()
- ->orderBy('published_at', 'desc')
- ->whereNull('parent_id')
- ->get(),
+ $posts = Post::NotSticky();
+ $posts = $this->applySearch($posts, $search);
+ $posts = $posts
+ ->orderBy('published_at', 'desc')
+ ->get();
- 'tags' => Tag::withCount('postsPublished')
- ->where('type', 'category')
- ->get(), // $this->tag->postsPublished
+ $pages = Post::page();
+ $pages = $this->applySearch($pages, $search);
+ $pages = $pages
+ ->orderBy('published_at', 'desc')
+ ->whereNull('parent_id')
+ ->get();
- 'stickies' => Post::sticky()->get(),
+ $pages = $this->highlightSearchResults($pages, $search);
+ $posts = $this->highlightSearchResults($posts, $search);
- 'recent' => Post::posts()
- ->take(config('zeus-sky.site_recent_count', 5))
+ $recent = Post::posts()
+ ->limit(config('zeus-sky.site_recent_count', 5))
->orderBy('published_at', 'desc')
- ->get(),
+ ->get();
+
+ return view(app('theme').'.home')
+ ->with([
+ 'posts' => $posts,
+ 'pages' => $pages,
+ 'recent' => $recent,
+ 'tags' => Tag::withCount('postsPublished')->where('type', 'category')->get(),
+ 'stickies' => Post::sticky()->get(),
])
->layout(config('zeus-sky.layout'));
}
+
+ private function applySearch(Builder $query, string $search): Builder
+ {
+ if ($search) {
+ return $query->where(function ($query) use ($search) {
+ foreach (['title', 'slug', 'content', 'description'] as $attribute) {
+ $query->orWhere(DB::raw("lower($attribute)"), 'like', "%$search%");
+ }
+
+ return $query;
+ });
+ }
+
+ return $query;
+ }
+
+ private function highlightSearchResults(Collection $posts, ?string $search = null): Collection
+ {
+ if (! $search) {
+ return $posts;
+ }
+
+ foreach ($posts as $i => $post) {
+ $posts[$i]->content = static::hl($post->content, [$search]);
+ }
+
+ return $posts;
+ }
+
+ /**
+ * Credits for this code goes to link below.
+ *
+ * @link https://stackoverflow.com/questions/8564578/php-search-text-highlight-function
+ */
+ public static function hl(string $inp, array $words): string
+ {
+ $class = config('zeus-sky.search_result_highlight_css_class', 'highlight');
+ $replace = array_flip(array_flip($words));
+ $pattern = [];
+
+ foreach ($replace as $k => $fword) {
+ $pattern[] = '/\b('.$fword.')(?!>)\b/i';
+ $replace[$k] = sprintf('
$1', $class);
+ }
+
+ return preg_replace($pattern, $replace, $inp);
+ }
}