MainController.php 4.98 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
        $applicationsProvider = new ApplicationsProviderFile("index.json");

        return $this->render('main/index.html.twig', [
            'categories' => $applicationsProvider->getAllCategories(),
Carl Schwan's avatar
Carl Schwan committed
44 45
            'locale' => $_locale,
            'translations' => $this->getParameter('app.translations'),
46 47 48 49 50
        ]);
    }

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

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

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

Carl Schwan's avatar
Carl Schwan committed
102 103 104 105 106 107 108 109
        if (startsWith($application, 'org.kde')) {
            return $this->redirectToRoute('app_main_application_intl', [
                '_locale' => $_locale,
                'application' => substr($application, 8),
            ]);
        } 

        $applicationObj = AppData::fromName('org.kde.' . $application);
110

Carl Schwan's avatar
Carl Schwan committed
111
        if (!$applicationObj) {
112 113 114 115 116
            throw $this->createNotFoundException('The product does not exist');
        }

        return $this->render('main/application.html.twig', [
            'application' => $applicationObj,
Carl Schwan's avatar
Carl Schwan committed
117 118
            'locale' => $_locale,
            'translations' => $this->getParameter('app.translations'),
119 120
        ]);
    }
Carl Schwan's avatar
Carl Schwan committed
121 122 123 124 125 126 127 128 129 130 131 132

    /**
     * @Route("/{category}/{application}", requirements={"_locale": "%app.locales%"}, defaults={"_locale": "en"})
     * @Route("/{_locale}/{category}/{application}", name="riuehiiuhiu", requirements={"_locale": "%app.locales%"})
     */
    public function redirectOldCategory(Request $request, string $_locale, string $category, string $application): Response
    {
        return $this->redirectToRoute('app_main_application_intl', [
            '_locale' => $_locale,
            'application' => $application,
        ]);
    }
133
}