MainController.php 5.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?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;
Carl Schwan's avatar
Carl Schwan committed
13
use Symfony\Component\HttpFoundation\Request;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

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"})
Carl Schwan's avatar
Carl Schwan committed
31 32
     * @param Request $request
     * @param string $_locale
33 34
     * @return Response
     */
Carl Schwan's avatar
Carl Schwan committed
35
    public function index(Request $request, string $_locale): Response
36
    {
Carl Schwan's avatar
Carl Schwan committed
37 38 39
        if ($request->query->get('site_locale')) {
            return $this->redirectToRoute('app_main_index', ['_locale' => $request->query->get('site_locale')]);
        }
40 41 42 43 44 45 46 47 48 49 50
        $applicationsProvider = new ApplicationsProviderFile("index.json");

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

    /**
     * @Route("/{category}", defaults={"_locale": "en"})
     * @Route("/{_locale}/{category}", name="app_main_category_intl", requirements={"_locale": "%app.locales%"})
Carl Schwan's avatar
Carl Schwan committed
51
     * @param Request $request
52 53 54 55
     * @param string $_locale
     * @param string $category
     * @return Response
     */
Carl Schwan's avatar
Carl Schwan committed
56
    public function category(Request $request, string $_locale, string $category): Response
57
    {
Carl Schwan's avatar
Carl Schwan committed
58 59 60
        if ($request->query->get('site_locale')) {
            return $this->redirectToRoute('app_main_category_intl', ['_locale' => $request->query->get('site_locale'), 'category' => $category]);
        }
61
        $applicationsProvider = new ApplicationsProviderFile("index.json");
Carl Schwan's avatar
Carl Schwan committed
62
        $categoryObject = $applicationsProvider->getApplicationsByCategory(ucfirst($category));
63

Carl Schwan's avatar
Carl Schwan committed
64 65 66 67 68 69 70 71 72 73 74
        if ($categoryObject) {
            return $this->render('main/category.html.twig', [
                'category' => $categoryObject,
                'locale' => $_locale
            ]);
        }
        $applicationObj = AppData::fromBinary($category);
        return $this->redirectToRoute('app_main_application_intl', [
            '_locale' => $_locale,
            'category' => strtolower($applicationObj->getPrimaryCategory()),
            'application' => $applicationObj->getId(),
75 76 77 78 79 80
        ]);
    }

    /**
     * @Route("/{category}/{application}", requirements={"_locale": "%app.locales%"}, defaults={"_locale": "en"})
     * @Route("/{_locale}/{category}/{application}", name="app_main_application_intl", requirements={"_locale": "%app.locales%"})
Carl Schwan's avatar
Carl Schwan committed
81
     * @param Request $request
82 83 84 85 86
     * @param string $_locale
     * @param string $category
     * @param string $application
     * @return Response
     */
Carl Schwan's avatar
Carl Schwan committed
87
    public function application(Request $request, string $_locale, string $category, string $application): Response
88
    {
Carl Schwan's avatar
Carl Schwan committed
89 90 91 92
        if ($request->query->get('site_locale')) {
            return $this->redirectToRoute('app_main_application_intl', ['_locale' => $request->query->get('site_locale'), 'category' => $category, 'application' => $application]);
        }

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        $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', [
            '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%"})
Carl Schwan's avatar
Carl Schwan committed
111
     * @param Request $request
112 113 114 115 116
     * @param string $_locale
     * @param string $category
     * @param string $application
     * @return Response
     */
Carl Schwan's avatar
Carl Schwan committed
117
    public function applicationDevelopment(Request $request, string $_locale, string $category, string $application): Response
118
    {
Carl Schwan's avatar
Carl Schwan committed
119 120 121
        if ($request->query->get('site_locale')) {
            return $this->redirectToRoute('app_main_application_dev_intl', ['_locale' => $request->query->get('site_locale'), 'category' => $category, 'application' => $application]);
        }
122 123 124 125 126 127 128 129 130
        $application = AppData::fromName($application);

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