2017-06-11 09:27:24 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Controller;
|
2019-08-07 05:48:00 +08:00
|
|
|
use App\Repositories\KvpRepository;
|
2019-11-24 01:58:48 +08:00
|
|
|
use App\Repositories\NewsRepository;
|
2017-12-23 04:48:15 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
|
|
|
use App\Repositories\UserRepository;
|
2019-12-10 04:26:30 +08:00
|
|
|
use App\Services\CronService;
|
2019-11-20 23:16:01 +08:00
|
|
|
use App\Services\NewsService;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-07-18 22:51:05 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Laracasts\Flash\Flash;
|
2018-01-20 07:44:17 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class DashboardController extends Controller
|
2017-06-11 09:27:24 +08:00
|
|
|
{
|
2019-12-10 04:26:30 +08:00
|
|
|
private $cronSvc;
|
2019-08-07 05:48:00 +08:00
|
|
|
private $kvpRepo;
|
2019-11-24 01:58:48 +08:00
|
|
|
private $newsRepo;
|
2019-11-20 23:16:01 +08:00
|
|
|
private $newsSvc;
|
2018-08-27 00:40:04 +08:00
|
|
|
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
|
|
|
*
|
2019-12-10 04:26:30 +08:00
|
|
|
* @param CronService $cronSvc
|
2019-08-07 05:48:00 +08:00
|
|
|
* @param KvpRepository $kvpRepo
|
2019-11-24 01:58:48 +08:00
|
|
|
* @param NewsRepository $newsRepo
|
|
|
|
* @param NewsService $newsSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param PirepRepository $pirepRepo
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param UserRepository $userRepo
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-12-04 00:55:01 +08:00
|
|
|
public function __construct(
|
2019-12-10 04:26:30 +08:00
|
|
|
CronService $cronSvc,
|
2019-08-07 05:48:00 +08:00
|
|
|
KvpRepository $kvpRepo,
|
2019-11-24 01:58:48 +08:00
|
|
|
NewsRepository $newsRepo,
|
2019-11-20 23:16:01 +08:00
|
|
|
NewsService $newsSvc,
|
2017-12-23 04:48:15 +08:00
|
|
|
PirepRepository $pirepRepo,
|
|
|
|
UserRepository $userRepo
|
2017-12-04 00:55:01 +08:00
|
|
|
) {
|
2019-12-10 04:26:30 +08:00
|
|
|
$this->cronSvc = $cronSvc;
|
2019-08-07 05:48:00 +08:00
|
|
|
$this->kvpRepo = $kvpRepo;
|
2019-11-24 01:58:48 +08:00
|
|
|
$this->newsRepo = $newsRepo;
|
|
|
|
$this->newsSvc = $newsSvc;
|
2017-12-04 00:55:01 +08:00
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-12-23 04:48:15 +08:00
|
|
|
$this->userRepo = $userRepo;
|
2017-12-04 00:55:01 +08:00
|
|
|
}
|
|
|
|
|
2017-06-11 09:27:24 +08:00
|
|
|
/**
|
2018-01-20 07:44:17 +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
|
2018-01-20 07:44:17 +08:00
|
|
|
*/
|
|
|
|
protected function checkNewVersion()
|
|
|
|
{
|
2018-02-12 23:47:12 +08:00
|
|
|
try {
|
2019-08-07 05:48:00 +08:00
|
|
|
if ($this->kvpRepo->get('new_version_available', false) === true) {
|
|
|
|
$latest_version = $this->kvpRepo->get('latest_version_tag');
|
2018-03-20 09:50:40 +08:00
|
|
|
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');
|
2018-01-20 07:44:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2018-01-20 07:44:17 +08:00
|
|
|
$this->checkNewVersion();
|
|
|
|
|
2017-08-19 01:02:32 +08:00
|
|
|
return view('admin.dashboard.index', [
|
2019-12-10 04:26:30 +08:00
|
|
|
'news' => $this->newsRepo->getLatest(),
|
|
|
|
'pending_pireps' => $this->pirepRepo->getPendingCount(),
|
|
|
|
'pending_users' => $this->userRepo->getPendingCount(),
|
|
|
|
'cron_problem_exists' => $this->cronSvc->cronProblemExists(),
|
2017-07-23 10:31:26 +08:00
|
|
|
]);
|
2017-06-11 09:27:24 +08:00
|
|
|
}
|
2018-01-09 06:22:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-09 06:22:26 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2018-01-09 06:22:26 +08:00
|
|
|
*/
|
|
|
|
public function news(Request $request)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($request->isMethod('post')) {
|
2018-01-09 06:22:26 +08:00
|
|
|
$attrs = $request->post();
|
|
|
|
$attrs['user_id'] = Auth::user()->id;
|
|
|
|
|
2019-11-20 23:16:01 +08:00
|
|
|
$this->newsSvc->addNews($attrs);
|
2018-01-09 06:22:26 +08:00
|
|
|
} elseif ($request->isMethod('delete')) {
|
2019-11-20 23:16:01 +08:00
|
|
|
$id = $request->input('news_id');
|
|
|
|
$this->newsSvc->deleteNews($id);
|
2018-01-09 06:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('admin.dashboard.news', [
|
|
|
|
'news' => $this->newsRepo->getLatest(),
|
|
|
|
]);
|
|
|
|
}
|
2017-06-11 09:27:24 +08:00
|
|
|
}
|