phpvms/app/Http/Controllers/Admin/DashboardController.php

107 lines
3.1 KiB
PHP
Raw Normal View History

2017-06-11 09:27:24 +08:00
<?php
namespace App\Http\Controllers\Admin;
use App\Contracts\Controller;
2019-07-16 03:51:35 +08:00
use App\Facades\Utils;
use App\Repositories\NewsRepository;
use App\Repositories\PirepRepository;
use App\Repositories\UserRepository;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Laracasts\Flash\Flash;
use PragmaRX\Version\Package\Facade as Version;
use vierbergenlars\SemVer\version as semver;
class DashboardController extends Controller
2017-06-11 09:27:24 +08:00
{
2018-08-27 00:40:04 +08:00
private $newsRepo;
private $pirepRepo;
private $userRepo;
2017-12-04 00:55:01 +08:00
2018-02-21 12:33:09 +08:00
/**
* DashboardController constructor.
2018-08-27 00:40:04 +08:00
*
* @param NewsRepository $newsRepo
2018-02-21 12:33:09 +08:00
* @param PirepRepository $pirepRepo
* @param UserRepository $userRepo
2018-02-21 12:33:09 +08:00
*/
2017-12-04 00:55:01 +08:00
public function __construct(
NewsRepository $newsRepo,
PirepRepository $pirepRepo,
UserRepository $userRepo
2017-12-04 00:55:01 +08:00
) {
$this->newsRepo = $newsRepo;
2017-12-04 00:55:01 +08:00
$this->pirepRepo = $pirepRepo;
$this->userRepo = $userRepo;
2017-12-04 00:55:01 +08:00
}
2017-06-11 09:27:24 +08:00
/**
* Check if a new version is available by checking the VERSION file from
* S3 and then using the semver library to do the comparison. Just show
* a session flash file on this page that'll get cleared right away
2018-08-27 00:40:04 +08:00
*
2018-08-27 02:50:08 +08:00
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function checkNewVersion()
{
2018-02-12 23:47:12 +08:00
try {
$current_version = new semver(Version::compact());
$latest_version = new semver(Utils::downloadUrl(config('phpvms.version_file')));
if (semver::gt($latest_version, $current_version)) {
Flash::warning('New version '.$latest_version.' is available!');
2018-02-12 23:47:12 +08:00
}
} catch (\Exception $e) {
Log::error($e->getMessage());
Flash::warning('Could not contact phpVMS for version check');
}
}
/**
* Show the admin dashboard
2018-08-27 00:40:04 +08:00
*
2018-08-27 02:50:08 +08:00
* @param Request $request
2018-08-27 02:51:47 +08:00
*
2018-08-27 02:50:08 +08:00
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \GuzzleHttp\Exception\GuzzleException
2018-08-27 02:51:47 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2017-06-11 09:27:24 +08:00
*/
public function index(Request $request)
{
$this->checkNewVersion();
2017-08-19 01:02:32 +08:00
return view('admin.dashboard.index', [
'news' => $this->newsRepo->getLatest(),
2017-12-04 00:55:01 +08:00
'pending_pireps' => $this->pirepRepo->getPendingCount(),
'pending_users' => $this->userRepo->getPendingCount(),
]);
2017-06-11 09:27:24 +08:00
}
/**
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function news(Request $request)
{
if ($request->isMethod('post')) {
$attrs = $request->post();
$attrs['user_id'] = Auth::user()->id;
$this->newsRepo->create($attrs);
} elseif ($request->isMethod('delete')) {
$news_id = $request->input('news_id');
$this->newsRepo->delete($news_id);
}
return view('admin.dashboard.news', [
'news' => $this->newsRepo->getLatest(),
]);
}
2017-06-11 09:27:24 +08:00
}