vendor/pec-platform/platform-bundle/Controller/DefaultController.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the PEC Platform Bundle.
  5.  *
  6.  * (c) PEC project engineers &amp; consultants
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Pec\Bundle\PlatformBundle\Controller;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. /**
  17.  * This controllers offers some common actions including access to the user preference service
  18.  */
  19. class DefaultController extends BaseController {
  20.     /**
  21.      * Default start action which redirects the user to the configured homepage route
  22.      *
  23.      * @return RedirectResponse
  24.      */
  25.     public function indexAction(): RedirectResponse {
  26.         return $this->redirectToRoute($this->getParameter('pec_platform.homepage_route'));
  27.     }
  28.     /**
  29.      * This method should be called if the state of the sidebar is changed by the user
  30.      *
  31.      * @param Request $request
  32.      * @param int     $state
  33.      * @return JsonResponse
  34.      */
  35.     public function toggleSidebarAction(Request $requestint $state 1): JsonResponse {
  36.         $this->getPreferenceService()->setPreference($this->getUser(), 'pec_platform_show_sidebar'$state);
  37.         $request->getSession()->set('pec_platform_show_sidebar'$state);
  38.         return $this->createJsonSuccessMessage('sidebar state changed');
  39.     }
  40.     /**
  41.      * Returns the request user preference as a json object
  42.      *
  43.      * @param string $key
  44.      * @return JsonResponse
  45.      */
  46.     public function getUserPreferenceAction(string $key): JsonResponse {
  47.         $value $this->getPreferenceService()->getPreference($this->getUser(), $keynull);
  48.         return $this->createJsonSuccessObject($value);
  49.     }
  50.     /**
  51.      * Sets a user preference
  52.      *
  53.      * @param Request $request
  54.      * @param string  $key
  55.      * @return JsonResponse
  56.      */
  57.     public function setUserPreferenceAction(Request $requeststring $key): JsonResponse {
  58.         $jsonValue $request->request->get('value'null);
  59.         $this->getPreferenceService()->setPreference($this->getUser(), $keyjson_decode($jsonValuefalse));
  60.         return $this->createJsonSuccessMessage('done');
  61.     }
  62.     /**
  63.      * Returns the request user preference as a plain text string
  64.      *
  65.      * @param string $key
  66.      * @return Response
  67.      */
  68.     public function renderUserPreferenceAction(string $key): Response {
  69.         $value $this->getPreferenceService()->getPreference($this->getUser(), $keynull);
  70.         $response = new Response();
  71.         $response->setContent($value);
  72.         return $response;
  73.     }
  74.     /**
  75.      * Answers with a pong!
  76.      *
  77.      * @return JsonResponse
  78.      *
  79.      */
  80.     public function pingAction(): JsonResponse {
  81.         return $this->createJsonSuccessMessage('pong');
  82.     }
  83.     /**
  84.      * Returns all flashes from the current flash bag
  85.      *
  86.      * @param Request $request
  87.      * @return JsonResponse
  88.      */
  89.     public function getFlashesAction(Request $request): JsonResponse {
  90.         return new JsonResponse([
  91.             'flashes' => $request->getSession()->all(),
  92.         ]);
  93.     }
  94. }