2020-06-05 15:54:40 +02:00
|
|
|
<?php
|
2020-08-04 13:25:22 +02:00
|
|
|
|
2021-06-08 11:52:11 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-10 17:00:12 +02:00
|
|
|
/**
|
2022-02-19 17:06:11 +01:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-06-10 17:00:12 +02:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-06-05 15:54:40 +02:00
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
use App\Models\PodcastModel;
|
2023-03-17 15:51:43 +01:00
|
|
|
use CodeIgniter\Database\Exceptions\DatabaseException;
|
2021-05-14 19:59:35 +02:00
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2023-03-17 15:51:43 +01:00
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
2023-08-27 15:26:06 +02:00
|
|
|
use Config\Cache;
|
2023-03-17 15:51:43 +01:00
|
|
|
use Modules\Media\FileManagers\FileManagerInterface;
|
2020-06-05 15:54:40 +02:00
|
|
|
|
2021-05-12 16:00:25 +02:00
|
|
|
class HomeController extends BaseController
|
2020-06-05 15:54:40 +02:00
|
|
|
{
|
2021-05-19 18:35:13 +02:00
|
|
|
public function index(): RedirectResponse | string
|
2020-06-10 17:00:12 +02:00
|
|
|
{
|
2022-10-19 13:35:08 +02:00
|
|
|
$sortOptions = ['activity', 'created_desc', 'created_asc'];
|
|
|
|
$sortBy = in_array($this->request->getGet('sort'), $sortOptions, true) ? $this->request->getGet(
|
|
|
|
'sort'
|
|
|
|
) : 'activity';
|
|
|
|
|
|
|
|
$allPodcasts = (new PodcastModel())->getAllPodcasts($sortBy);
|
|
|
|
|
2020-06-26 16:34:52 +02:00
|
|
|
// check if there's only one podcast to redirect user to it
|
2021-05-14 19:59:35 +02:00
|
|
|
if (count($allPodcasts) === 1) {
|
2021-07-26 15:10:46 +02:00
|
|
|
return redirect()->route('podcast-activity', [$allPodcasts[0]->handle]);
|
2020-06-26 16:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// default behavior: list all podcasts on home page
|
2021-05-19 18:35:13 +02:00
|
|
|
$data = [
|
2021-11-12 17:31:35 +01:00
|
|
|
'metatags' => get_home_metatags(),
|
2021-05-19 18:35:13 +02:00
|
|
|
'podcasts' => $allPodcasts,
|
2023-06-12 16:47:38 +02:00
|
|
|
'sortBy' => $sortBy,
|
2021-05-19 18:35:13 +02:00
|
|
|
];
|
2021-09-02 18:34:25 +02:00
|
|
|
|
2020-06-10 17:00:12 +02:00
|
|
|
return view('home', $data);
|
|
|
|
}
|
2023-03-17 15:51:43 +01:00
|
|
|
|
|
|
|
public function health(): ResponseInterface
|
|
|
|
{
|
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
db_connect();
|
|
|
|
} catch (DatabaseException) {
|
|
|
|
$errors[] = 'Unable to connect to the database.';
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Can Castopod connect to the cache handler
|
2023-08-27 15:26:06 +02:00
|
|
|
if (config(Cache::class)->handler !== 'dummy' && cache()->getCacheInfo() === null) {
|
2023-03-17 15:51:43 +01:00
|
|
|
$errors[] = 'Unable connect to the cache handler.';
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Can Castopod write to storage?
|
|
|
|
|
|
|
|
/** @var FileManagerInterface $fileManager */
|
|
|
|
$fileManager = service('file_manager', false);
|
|
|
|
|
|
|
|
if (! $fileManager->isHealthy()) {
|
|
|
|
$errors[] = 'Problem with file manager.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($errors !== []) {
|
2023-03-23 12:59:51 +01:00
|
|
|
return $this->response->setStatusCode(503)
|
2023-03-17 15:51:43 +01:00
|
|
|
->setJSON([
|
2023-06-12 16:47:38 +02:00
|
|
|
'code' => 503,
|
2023-03-17 15:51:43 +01:00
|
|
|
'errors' => $errors,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response->setStatusCode(200)
|
|
|
|
->setJSON([
|
2023-06-12 16:47:38 +02:00
|
|
|
'code' => 200,
|
2023-03-17 15:51:43 +01:00
|
|
|
'message' => '✨ All good!',
|
|
|
|
]);
|
|
|
|
}
|
2020-06-05 15:54:40 +02:00
|
|
|
}
|