MainController.php 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<?php
/**
 * SPDX-FileCopyrightText: 2020 Carl Schwan <carl@carlschwan.eu>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace App\Controller;

use App\Model\AppData;
use App\Providers\ApplicationsProviderFile;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

use App\Services\CdnCache;

function startsWith($haystack, $needle)
{
     $length = strlen($needle);
     return (substr($haystack, 0, $length) === $needle);
}

/**
 * Class MainController
 * @package App\Controller
 */
class MainController extends AbstractController
{
    /**
     * @Route("/{_locale}", name="app_main_index", requirements={"_locale": "%app.locales%"}, defaults={"_locale": "en"})
     * @param CdnCache $cdn
     * @return Response
     */
    public function index(string $_locale, CdnCache $cdn): Response
    {
        $applicationsProvider = new ApplicationsProviderFile("index.json");

        return $this->render('main/index.html.twig', [
            'cdn' => $cdn,
            'categories' => $applicationsProvider->getAllCategories(),
            'locale' => $_locale
        ]);
    }

    /**
     * @Route("/{category}", defaults={"_locale": "en"})
     * @Route("/{_locale}/{category}", name="app_main_category_intl", requirements={"_locale": "%app.locales%"})
     * @param string $_locale
     * @param string $category
     * @param CdnCache $cdn
     * @return Response
     */
    public function category(string $_locale, string $category, CdnCache $cdn): Response
    {
        $applicationsProvider = new ApplicationsProviderFile("index.json");

        return $this->render('main/category.html.twig', [
            'cdn' => $cdn,
            'category' => $applicationsProvider->getApplicationsByCategory(ucfirst($category)),
            'locale' => $_locale
        ]);
    }

    /**
     * @Route("/{category}/{application}", requirements={"_locale": "%app.locales%"}, defaults={"_locale": "en"})
     * @Route("/{_locale}/{category}/{application}", name="app_main_application_intl", requirements={"_locale": "%app.locales%"})
     * @param string $_locale
     * @param string $category
     * @param string $application
     * @param CdnCache $cdn
     * @return Response
     */
    public function application(string $_locale, string $category, string $application, CdnCache $cdn): Response
    {
        $applicationObj = AppData::fromName($application);

        if (!($applicationObj || startsWith($application, 'org'))) {
            return $this->redirectToRoute('app_main_application_intl', ['_locale' => $_locale, 'category' => $category, 'application' => 'org.kde.' . $application]);
        } else if (!$applicationObj) {
            throw $this->createNotFoundException('The product does not exist');
        }

        return $this->render('main/application.html.twig', [
            'cdn' => $cdn,
            'application' => $applicationObj,
            'category' => $category,
            'locale' => $_locale
        ]);
    }

    /**
     * @Route("/{category}/{application}/development", requirements={"_locale": "%app.locales%"}, defaults={"_locale": "en"})
     * @Route("/{_locale}/{category}/{application}/development", name="app_main_application_dev_intl", requirements={"_locale": "%app.locales%"})
     * @param string $_locale
     * @param string $category
     * @param string $application
     * @param CdnCache $cdn
     * @return Response
     */
    public function applicationDevelopment(string $_locale, string $category, string $application, CdnCache $cdn): Response
    {
        $application = AppData::fromName($application);

        return $this->render('main/applicationDevelopment.html.twig', [
            'cdn' => $cdn,
            'application' => $application,
            'category' => $category,
            'locale' => $_locale
        ]);
    }
}