2020-06-05 13:54:40 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-06-10 15:00:12 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-06-05 13:54:40 +00:00
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
use App\Models\PodcastModel;
|
2023-03-17 14:51:43 +00:00
|
|
|
use CodeIgniter\Database\Exceptions\DatabaseException;
|
2021-05-14 17:59:35 +00:00
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2023-03-17 14:51:43 +00:00
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
|
|
use Modules\Media\FileManagers\FileManagerInterface;
|
2020-06-05 13:54:40 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
class HomeController extends BaseController
|
2020-06-05 13:54:40 +00:00
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
public function index(): RedirectResponse | string
|
2020-06-10 15:00:12 +00:00
|
|
|
{
|
2022-10-19 11:35:08 +00: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 14:34:52 +00:00
|
|
|
// check if there's only one podcast to redirect user to it
|
2021-05-14 17:59:35 +00:00
|
|
|
if (count($allPodcasts) === 1) {
|
2021-07-26 13:10:46 +00:00
|
|
|
return redirect()->route('podcast-activity', [$allPodcasts[0]->handle]);
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// default behavior: list all podcasts on home page
|
2021-05-19 16:35:13 +00:00
|
|
|
$data = [
|
2021-11-12 16:31:35 +00:00
|
|
|
'metatags' => get_home_metatags(),
|
2021-05-19 16:35:13 +00:00
|
|
|
'podcasts' => $allPodcasts,
|
2023-06-12 14:47:38 +00:00
|
|
|
'sortBy' => $sortBy,
|
2021-05-19 16:35:13 +00:00
|
|
|
];
|
2021-09-02 16:34:25 +00:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
return view('home', $data);
|
|
|
|
}
|
2023-03-17 14:51:43 +00: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
|
2024-04-28 16:39:01 +00:00
|
|
|
if (config('Cache')->handler !== 'dummy' && cache()->getCacheInfo() === null) {
|
2023-03-17 14:51:43 +00: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 11:59:51 +00:00
|
|
|
return $this->response->setStatusCode(503)
|
2023-03-17 14:51:43 +00:00
|
|
|
->setJSON([
|
2023-06-12 14:47:38 +00:00
|
|
|
'code' => 503,
|
2023-03-17 14:51:43 +00:00
|
|
|
'errors' => $errors,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response->setStatusCode(200)
|
|
|
|
->setJSON([
|
2023-06-12 14:47:38 +00:00
|
|
|
'code' => 200,
|
2023-03-17 14:51:43 +00:00
|
|
|
'message' => '✨ All good!',
|
|
|
|
]);
|
|
|
|
}
|
2020-06-05 13:54:40 +00:00
|
|
|
}
|