Skip to content

Commit 1ba8c0f

Browse files
authored
Merge pull request #49 from laravelcm/build-admin-layout
Build admin layout
2 parents a41c2df + 345e350 commit 1ba8c0f

24 files changed

+444
-38
lines changed

app/Http/Controllers/ArticlesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function show(Article $article)
3030
$article = Cache::remember('post-' . $article->id, now()->addHour(), fn () => $article);
3131

3232
abort_unless(
33-
$article->isPublished() || ($user && $user->hasAnyRole(['admin', 'moderator'])),
33+
$article->isPublished() || ($user && $article->isAuthoredBy($user)) || ($user && $user->hasAnyRole(['admin', 'moderator'])),
3434
404
3535
);
3636

app/Http/Controllers/Cpanel/DashboardController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
namespace App\Http\Controllers\Cpanel;
44

55
use App\Http\Controllers\Controller;
6+
use App\Models\Article;
7+
use App\Models\User;
8+
use Illuminate\Support\Facades\Cache;
69

710
class DashboardController extends Controller
811
{
912
public function home()
1013
{
11-
return view('cpanel.dashboard');
14+
$users = Cache::remember('new-members', now()->addHour(), fn () => User::verifiedUsers()->latest()->limit(15)->get());
15+
$latestArticles = Cache::remember('last-posts', now()->addHour(), fn () => Article::latest()->limit(2)->get());
16+
17+
return view('cpanel.dashboard', [
18+
'latestArticles' => $latestArticles,
19+
'users' => $users,
20+
]);
1221
}
1322
}

app/Providers/AppServiceProvider.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ public function register()
3838
*
3939
* @return void
4040
*/
41-
public function boot()
41+
public function boot(): void
4242
{
4343
date_default_timezone_set('Africa/Douala');
44-
setlocale(LC_TIME, 'fr_FR', 'fr', 'FR', 'French', 'fr_FR.UTF-8');
45-
Carbon::setLocale('fr_FR');
44+
setlocale(LC_TIME, 'French');
45+
setlocale(LC_ALL, 'fr_FR.UTF-8');
46+
Carbon::setLocale('fr');
4647

4748
$this->bootMacros();
4849
$this->bootViewsComposer();

app/Widgets/RecentNumbers.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace App\Widgets;
4+
5+
use App\Models\Article;
6+
use App\Models\User;
7+
use Arrilot\Widgets\AbstractWidget;
8+
use CyrildeWit\EloquentViewable\Support\Period;
9+
10+
class RecentNumbers extends AbstractWidget
11+
{
12+
/**
13+
* The configuration array.
14+
*
15+
* @var array
16+
*/
17+
protected $config = [];
18+
19+
/**
20+
* The number of seconds before each reload.
21+
*
22+
* @var int|float
23+
*/
24+
public $reloadTimeout = 5400;
25+
26+
/**
27+
* The number of minutes before cache expires.
28+
* False means no caching at all.
29+
*
30+
* @var int|float|bool
31+
*/
32+
// public $cacheTime = 90;
33+
34+
/**
35+
* Treat this method as a controller action.
36+
* Return view() or other content to display.
37+
*/
38+
public function run()
39+
{
40+
$lastMonth = now()->subMonth();
41+
$countUsers = User::count();
42+
$lastMonthRegistered = User::query()->whereBetween('created_at', [
43+
$lastMonth->startOfMonth()->format('Y-m-d'),
44+
$lastMonth->endOfMonth()->format('Y-m-d'),
45+
])->count();
46+
$currentMonthRegistered = User::query()->where('created_at', '>=', now()->startOfMonth())->count();
47+
$difference = $currentMonthRegistered - $lastMonthRegistered;
48+
49+
$countArticles = Article::count();
50+
$lastMonthArticles = Article::query()->whereBetween('created_at', [
51+
$lastMonth->startOfMonth()->format('Y-m-d'),
52+
$lastMonth->endOfMonth()->format('Y-m-d'),
53+
])->count();
54+
$currentMonthArticles = Article::query()->where('created_at', '>=', now()->startOfMonth())->count();
55+
$differenceArticle = $currentMonthArticles - $lastMonthArticles;
56+
57+
$totalViews = views(Article::class)->count();
58+
$lastMonthViews = views(Article::class)->period(Period::pastMonths(1))->count();
59+
$currentViews = views(Article::class)->period(Period::create(now()->startOfMonth()))->count();
60+
$differenceViews = $currentViews - $lastMonthViews;
61+
62+
return view('widgets.recent_numbers', [
63+
'config' => $this->config,
64+
'users' => [
65+
'count' => $countUsers,
66+
'increase' => $difference > 0,
67+
'decreased' => $difference < 0,
68+
'current' => max($difference, 0),
69+
],
70+
'articles' => [
71+
'count' => $countArticles,
72+
'increase' => $differenceArticle > 0,
73+
'decreased' => $differenceArticle < 0,
74+
'current' => max($differenceArticle, 0),
75+
],
76+
'views' => [
77+
'count' => $totalViews,
78+
'increase' => $difference > 0,
79+
'decreased' => $difference < 0,
80+
'current' => $differenceViews,
81+
],
82+
]);
83+
}
84+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"ext-fileinfo": "*",
1010
"ext-json": "*",
1111
"archtechx/laravel-seo": "^0.4.0",
12+
"arrilot/laravel-widgets": "^3.13",
1213
"blade-ui-kit/blade-heroicons": "^1.3",
1314
"blade-ui-kit/blade-ui-kit": "^0.3",
1415
"cyrildewit/eloquent-viewable": "^6.1",

composer.lock

Lines changed: 68 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database/factories/UserFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ public function unverified()
4545
];
4646
});
4747
}
48+
49+
/**
50+
* Indicate that the model's created_at should be last month.
51+
*
52+
* @return \Illuminate\Database\Eloquent\Factories\Factory
53+
*/
54+
public function lastMonth()
55+
{
56+
return $this->state(function (array $attributes) {
57+
return [
58+
'created_at' => now()->subMonth(),
59+
];
60+
});
61+
}
4862
}

0 commit comments

Comments
 (0)