Skip to content

Commit 32a7f53

Browse files
popularTags method and tags sidebar, fix search and enable in extension theme
1 parent 413b43e commit 32a7f53

File tree

5 files changed

+64
-21
lines changed

5 files changed

+64
-21
lines changed

app/LetsBlog.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ public static function search($q = '')
2626

2727
public static function related(Post $post, $limit = 6)
2828
{
29-
$q = Post::selectRaw("*, MATCH(title, body) AGAINST(?) AS score", [$post->title])->where('published_at', '<>', 'NULL')->search($post->title)->where('type', 'post')->where('status', 'active')->where('id', '<>', $post->id)->orderBy('score', 'desc')->with('tags', 'series')->limit($limit);
29+
switch (config('database.default')) {
30+
case 'sqlite':
31+
$q = Post::selectRaw("*, ABS(RANDOM()) AS score")->where('published_at', '<>', 'NULL')->search($post->title)->where('type', 'post')->where('status', 'active')->where('id', '<>', $post->id)->orderBy('score', 'desc')->with('tags', 'series')->limit($limit);
32+
break;
33+
34+
default:
35+
$q = Post::selectRaw("*, MATCH(title, body) AGAINST(?) AS score", [$post->title])->where('published_at', '<>', 'NULL')->search($post->title)->where('type', 'post')->where('status', 'active')->where('id', '<>', $post->id)->orderBy('score', 'desc')->with('tags', 'series')->limit($limit);
36+
37+
break;
38+
}
3039

3140
if ((int)$post->series_id > 0) {
3241
$q->where('series_id', '<>', $post->series_id);
@@ -78,9 +87,26 @@ public static function count()
7887
return Post::where('published_at', '<>', 'NULL')->where('type', 'post')->where('status', 'active')->count();
7988
}
8089

81-
public static function tags()
90+
public static function tags($limit=false)
91+
{
92+
$q = Tag::withCount('posts')->orderBy('slug', 'asc');
93+
94+
if( ! empty($limit)){
95+
$q = $q->limit($limit);
96+
}
97+
98+
return $q->get();
99+
}
100+
101+
public static function popularTags($limit=false)
82102
{
83-
return Tag::withCount('posts')->orderBy('slug', 'asc')->get();
103+
$q = Tag::withCount('posts')->orderBy('slug', 'asc')->has('posts', '>', 1);
104+
105+
if( ! empty($limit)){
106+
$q = $q->limit($limit);
107+
}
108+
109+
return $q->get();
84110
}
85111

86112
public static function publishedWhereTag($tag)

app/Models/Post.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ public function series()
3131

3232
public function scopeSearch($q, $search)
3333
{
34-
return $q->whereRaw("MATCH (`title`, `body`) AGAINST (?)", [$search]);
34+
switch (config('database.default')) {
35+
case 'sqlite':
36+
return $q->whereRaw("`title` LIKE '%{$search}%'");
37+
break;
38+
default:
39+
return $q->whereRaw("MATCH (`title`, `body`) AGAINST (?)", [$search]);
40+
break;
41+
}
3542
}
3643

3744
public function getUrlAttribute()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div class="panel panel-default">
2+
3+
<div class="panel-heading">
4+
<h1 class="panel-title">
5+
<a href="{{ route('tags') }}"><i class="fa fa-tags" aria-hidden="true"></i>&nbsp;&nbsp;{{ $title or 'Tags' }}:</a>
6+
</h1>
7+
</div>
8+
9+
<ul class="list-group">
10+
@foreach ($tags as $t)
11+
<a class="list-group-item" href="{{ $t->url }}">{{ $t->name }}
12+
<span class="badge">{{ $t->posts_count }}</span>
13+
</a>
14+
@endforeach
15+
</ul>
16+
17+
</div>

resources/views/themes/extension/layout.blade.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,17 @@
4040
<br class="hidden-md hidden-lg" style="clear:both;">
4141

4242
<div class="col-md-4 col-lg-3">
43+
44+
@include (lb_view('components.search'))
45+
46+
<br>
47+
4348
@section ('layout.sidebar')
49+
50+
@include (lb_view('components.tags'), ['title' => 'Popular Tags', 'tags' => LetsBlog::popularTags(10)])
51+
4452
@show
53+
4554
</div>
4655

4756
@endif

resources/views/themes/extension/post/show.blade.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,7 @@
6969
@endif
7070

7171
@if ($post->tags)
72-
73-
<div class="panel panel-default">
74-
75-
<div class="panel-heading">
76-
<h1 class="panel-title">
77-
<i class="fa fa-tags" aria-hidden="true"></i>&nbsp;&nbsp;Tags:
78-
</h1>
79-
</div>
80-
81-
<ul class="list-group">
82-
@foreach ($post->tags as $t)
83-
<a class="list-group-item" href="{{ $t->url }}">{{ $t->name }}</a>
84-
@endforeach
85-
</ul>
86-
87-
</div>
88-
72+
@include (lb_view('components.tags'), ['tags' => $post->tags])
8973
@endif
9074

9175
@parent

0 commit comments

Comments
 (0)