Skip to content

Build admin layout #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function show(Article $article)
$article = Cache::remember('post-' . $article->id, now()->addHour(), fn () => $article);

abort_unless(
$article->isPublished() || ($user && $user->hasAnyRole(['admin', 'moderator'])),
$article->isPublished() || ($user && $article->isAuthoredBy($user)) || ($user && $user->hasAnyRole(['admin', 'moderator'])),
404
);

Expand Down
11 changes: 10 additions & 1 deletion app/Http/Controllers/Cpanel/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
namespace App\Http\Controllers\Cpanel;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\User;
use Illuminate\Support\Facades\Cache;

class DashboardController extends Controller
{
public function home()
{
return view('cpanel.dashboard');
$users = Cache::remember('new-members', now()->addHour(), fn () => User::verifiedUsers()->latest()->limit(15)->get());
$latestArticles = Cache::remember('last-posts', now()->addHour(), fn () => Article::latest()->limit(2)->get());

return view('cpanel.dashboard', [
'latestArticles' => $latestArticles,
'users' => $users,
]);
}
}
7 changes: 4 additions & 3 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public function register()
*
* @return void
*/
public function boot()
public function boot(): void
{
date_default_timezone_set('Africa/Douala');
setlocale(LC_TIME, 'fr_FR', 'fr', 'FR', 'French', 'fr_FR.UTF-8');
Carbon::setLocale('fr_FR');
setlocale(LC_TIME, 'French');
setlocale(LC_ALL, 'fr_FR.UTF-8');
Carbon::setLocale('fr');

$this->bootMacros();
$this->bootViewsComposer();
Expand Down
84 changes: 84 additions & 0 deletions app/Widgets/RecentNumbers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Widgets;

use App\Models\Article;
use App\Models\User;
use Arrilot\Widgets\AbstractWidget;
use CyrildeWit\EloquentViewable\Support\Period;

class RecentNumbers extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [];

/**
* The number of seconds before each reload.
*
* @var int|float
*/
public $reloadTimeout = 5400;

/**
* The number of minutes before cache expires.
* False means no caching at all.
*
* @var int|float|bool
*/
// public $cacheTime = 90;

/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
$lastMonth = now()->subMonth();
$countUsers = User::count();
$lastMonthRegistered = User::query()->whereBetween('created_at', [
$lastMonth->startOfMonth()->format('Y-m-d'),
$lastMonth->endOfMonth()->format('Y-m-d'),
])->count();
$currentMonthRegistered = User::query()->where('created_at', '>=', now()->startOfMonth())->count();
$difference = $currentMonthRegistered - $lastMonthRegistered;

$countArticles = Article::count();
$lastMonthArticles = Article::query()->whereBetween('created_at', [
$lastMonth->startOfMonth()->format('Y-m-d'),
$lastMonth->endOfMonth()->format('Y-m-d'),
])->count();
$currentMonthArticles = Article::query()->where('created_at', '>=', now()->startOfMonth())->count();
$differenceArticle = $currentMonthArticles - $lastMonthArticles;

$totalViews = views(Article::class)->count();
$lastMonthViews = views(Article::class)->period(Period::pastMonths(1))->count();
$currentViews = views(Article::class)->period(Period::create(now()->startOfMonth()))->count();
$differenceViews = $currentViews - $lastMonthViews;

return view('widgets.recent_numbers', [
'config' => $this->config,
'users' => [
'count' => $countUsers,
'increase' => $difference > 0,
'decreased' => $difference < 0,
'current' => max($difference, 0),
],
'articles' => [
'count' => $countArticles,
'increase' => $differenceArticle > 0,
'decreased' => $differenceArticle < 0,
'current' => max($differenceArticle, 0),
],
'views' => [
'count' => $totalViews,
'increase' => $difference > 0,
'decreased' => $difference < 0,
'current' => $differenceViews,
],
]);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"ext-fileinfo": "*",
"ext-json": "*",
"archtechx/laravel-seo": "^0.4.0",
"arrilot/laravel-widgets": "^3.13",
"blade-ui-kit/blade-heroicons": "^1.3",
"blade-ui-kit/blade-ui-kit": "^0.3",
"cyrildewit/eloquent-viewable": "^6.1",
Expand Down
70 changes: 68 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ public function unverified()
];
});
}

/**
* Indicate that the model's created_at should be last month.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function lastMonth()
{
return $this->state(function (array $attributes) {
return [
'created_at' => now()->subMonth(),
];
});
}
}
Loading