<?php
declare(strict_types=1);
/*
* This file is part of the PEC Platform Bundle.
*
* (c) PEC project engineers & consultants
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pec\Bundle\PlatformBundle\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* This controllers offers some common actions including access to the user preference service
*/
class DefaultController extends BaseController {
/**
* Default start action which redirects the user to the configured homepage route
*
* @return RedirectResponse
*/
public function indexAction(): RedirectResponse {
return $this->redirectToRoute($this->getParameter('pec_platform.homepage_route'));
}
/**
* This method should be called if the state of the sidebar is changed by the user
*
* @param Request $request
* @param int $state
* @return JsonResponse
*/
public function toggleSidebarAction(Request $request, int $state = 1): JsonResponse {
$this->getPreferenceService()->setPreference($this->getUser(), 'pec_platform_show_sidebar', $state);
$request->getSession()->set('pec_platform_show_sidebar', $state);
return $this->createJsonSuccessMessage('sidebar state changed');
}
/**
* Returns the request user preference as a json object
*
* @param string $key
* @return JsonResponse
*/
public function getUserPreferenceAction(string $key): JsonResponse {
$value = $this->getPreferenceService()->getPreference($this->getUser(), $key, null);
return $this->createJsonSuccessObject($value);
}
/**
* Sets a user preference
*
* @param Request $request
* @param string $key
* @return JsonResponse
*/
public function setUserPreferenceAction(Request $request, string $key): JsonResponse {
$jsonValue = $request->request->get('value', null);
$this->getPreferenceService()->setPreference($this->getUser(), $key, json_decode($jsonValue, false));
return $this->createJsonSuccessMessage('done');
}
/**
* Returns the request user preference as a plain text string
*
* @param string $key
* @return Response
*/
public function renderUserPreferenceAction(string $key): Response {
$value = $this->getPreferenceService()->getPreference($this->getUser(), $key, null);
$response = new Response();
$response->setContent($value);
return $response;
}
/**
* Answers with a pong!
*
* @return JsonResponse
*
*/
public function pingAction(): JsonResponse {
return $this->createJsonSuccessMessage('pong');
}
/**
* Returns all flashes from the current flash bag
*
* @param Request $request
* @return JsonResponse
*/
public function getFlashesAction(Request $request): JsonResponse {
return new JsonResponse([
'flashes' => $request->getSession()->all(),
]);
}
}