src/Cms/CoreBundle/Util/Controller.php line 139

Open in your IDE?
  1. <?php
  2. namespace Cms\CoreBundle\Util;
  3. use App\Controller\AbstractController;
  4. use Cms\CoreBundle\Model\Interfaces\Lockable\LockableInterface;
  5. use Cms\CoreBundle\Model\Scenes\DashboardScenes\AjaxScene;
  6. use Cms\CoreBundle\Model\Scenes\DashboardScenes\DocumentScene;
  7. use Cms\CoreBundle\Service\Locksmith;
  8. use Cms\CoreBundle\Service\SceneRenderer;
  9. use Cms\LogBundle\Service\LoggingService;
  10. use ReflectionClass;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /**
  15.  * Class Controller
  16.  * @package Cms\CoreBundle\Util\Controller
  17.  */
  18. abstract class Controller extends AbstractController
  19. {
  20.     /**
  21.      * @param LockableInterface $entity
  22.      * @param string|null $link
  23.      * @return Response|null
  24.      */
  25.     protected function trapLocked(LockableInterface $entity, ?string $link null): ?Response
  26.     {
  27.         $obtained $this->getLocksmith()->obtainLock($entity);
  28.         if ($obtained !== true) {
  29.             $scene = new DocumentScene(
  30.                 '@CmsCore/error.html.twig',
  31.                 array(
  32.                     'title' => 'Resource is Locked',
  33.                     'body' => sprintf(
  34.                         '<p>The user <strong>%s</strong> has locked this resource for modification. Please contact a system administrator for help if you believe this is an error.</p>',
  35.                         $entity->getLockedBy()->getEmail()
  36.                     ),
  37.                     'link' => ( ! empty($link)) ? $link '/_dashboard',
  38.                 )
  39.             );
  40.             $content $this->getSceneRenderer()->render($scene);
  41.             return new Response(
  42.                 $content,
  43.                 500,
  44.                 array(
  45.                     'Content-Type' => 'text/html',
  46.                 )
  47.             );
  48.         }
  49.         return null;
  50.     }
  51.     /**
  52.      * @param Request|null $request
  53.      * @return bool
  54.      */
  55.     protected function handleSubmission(Request $request null): bool
  56.     {
  57.         // if no request, get the current one
  58.         if ($request === null) {
  59.             // TODO: do we want the current or the master?
  60.             $request $this->getRequest();
  61.         }
  62.         // determine if it was submitted and if it was valid
  63.         return ($request->isMethod(Request::METHOD_POST));
  64.     }
  65.     /**
  66.      * @param Request|null $request
  67.      * @return bool
  68.      */
  69.     protected function handleBulk(Request $request null): bool
  70.     {
  71.         // if no request, get the current one
  72.         if ($request === null) {
  73.             // TODO: do we want the current or the master?
  74.             $request $this->getRequest();
  75.         }
  76.         // see if we have a process field given
  77.         return ($request->request->get('process'false) == true);
  78.     }
  79.     /**
  80.      * @param Request|null $request
  81.      * @return bool
  82.      */
  83.     protected function handleSecureDelete(Request $request null): bool
  84.     {
  85.         // if no request, get the current one
  86.         if ($request === null) {
  87.             // TODO: do we want the current or the master?
  88.             $request $this->getRequest();
  89.         }
  90.         // determine if it was submitted and if it was valid
  91.         if ($request->isMethod(Request::METHOD_POST)) {
  92.             // there should be a "secure" field
  93.             $secure $request->request->get('secure');
  94.             // check for value
  95.             if ($secure !== null && strtoupper(trim($secure)) === 'DELETE') {
  96.                 // good input
  97.                 return true;
  98.             }
  99.         }
  100.         // not good
  101.         return false;
  102.     }
  103.     /**
  104.      * @return array
  105.      */
  106.     protected static function routing(): array
  107.     {
  108.         $refl = new ReflectionClass(static::class);
  109.         $routes = [];
  110.         foreach ($refl->getConstants() as $key => $value) {
  111.             if (preg_match('/^ROUTES__(.+)/'$key$matches) === 1) {
  112.                 $routes[strtolower($matches[1])] = $value;
  113.             }
  114.         }
  115.         return $routes;
  116.     }
  117.     /**
  118.      * @param string|array|null $template
  119.      * @param array $parameters
  120.      * @return DocumentScene
  121.      */
  122.     public function view($template null, array $parameters = []): DocumentScene
  123.     {
  124.         // handle standard naming conventions
  125.         if (is_array($template)) {
  126.             $parameters $template;
  127.             $template null;
  128.         }
  129.         // get the route constants on our controller
  130.         if (isset($parameters['routes']) === false) {
  131.             $parameters['routes'] = [];
  132.         }
  133.         $refl = new ReflectionClass($this);
  134.         foreach ($refl->getConstants() as $key => $value) {
  135.             if (preg_match('/^ROUTES__.+/'$key) === 1) {
  136.                 $key strtolower(preg_replace('/^ROUTES__(.+)$/''$1'$key));
  137.                 $parameters['routes'][$key] = $value;
  138.             }
  139.         }
  140.         // track stuff
  141.         [$cls$func] = explode(
  142.             '::',
  143.             $this->getRequest()->attributes->get('_controller')
  144.         );
  145.         $func preg_replace('/Action$/'''$func);
  146.         if ($func === null || $cls === null) {
  147.             throw new \LogicException();
  148.         }
  149.         // fill in view based on standards if not given
  150.         if ($template === null || $template === '') {
  151.             $template $func;
  152.         }
  153.         // add stuff if not given
  154.         if (preg_match('/^(Cms|Platform|Products)\\\\(.+?)Bundle\\\\Controller\\\\(.+?)Controller$/'$cls$cpieces) !== 1) {
  155.             throw new \LogicException();
  156.         }
  157.         // TODO: this logic may need further modification due to the change of Twig naming patterns...
  158.         if ($template === $func) {
  159.             $template str_replace('\\''/'$cpieces[3]).'/'.$template;
  160.             $template '@'.$cpieces[1].str_replace(['\\''/'], [''''], $cpieces[2]).'/'.$template;
  161.         }
  162.         // TODO: this logic may need further modification due to the change of Twig naming patterns...
  163.         // assume html and twig if nothing is given
  164.         if (substr_count($template'.') === 0) {
  165.             $template .= '.html';
  166.         }
  167.         if (substr_count($template'.') === 1) {
  168.             $template .= '.twig';
  169.         }
  170.         // create the view with the view layer
  171.         return new DocumentScene(
  172.             $template,
  173.             $parameters
  174.         );
  175.     }
  176.     /**
  177.      * @param string|array|null $template
  178.      * @param array $parameters
  179.      * @return AjaxScene
  180.      * @throws \Exception
  181.      */
  182.     public function viewAjax($template null, array $parameters = []): AjaxScene
  183.     {
  184.         // handle standard naming conventions
  185.         if (is_array($template)) {
  186.             $parameters $template;
  187.             $template null;
  188.         }
  189.         // get the route constants on our controller
  190.         if (isset($parameters['routes']) === false) {
  191.             $parameters['routes'] = [];
  192.         }
  193.         $refl = new ReflectionClass($this);
  194.         foreach ($refl->getConstants() as $key => $value) {
  195.             if (preg_match('/^ROUTES__.+/'$key) === 1) {
  196.                 $key strtolower(preg_replace('/^ROUTES__(.+)$/''$1'$key));
  197.                 $parameters['routes'][$key] = $value;
  198.             }
  199.         }
  200.         // track stuff
  201.         [$cls$func] = explode(
  202.             '::',
  203.             $this->getRequest()->attributes->get('_controller')
  204.         );
  205.         $func preg_replace('/Action$/'''$func);
  206.         if ($func === null || $cls === null) {
  207.             throw new \LogicException();
  208.         }
  209.         // fill in view based on standards if not given
  210.         if ($template === null || $template === '') {
  211.             $template $func;
  212.         }
  213.         // add stuff if not given
  214.         if (preg_match('/^(Cms|Platform|Products)\\\\(.+?)Bundle\\\\Controller\\\\(.+?)Controller$/'$cls$cpieces) !== 1) {
  215.             throw new \LogicException();
  216.         }
  217.         // TODO: this logic may need further modification due to the change of Twig naming patterns...
  218.         if ($template === $func) {
  219.             $template str_replace('\\''/'$cpieces[3]).'/'.$template;
  220.             $template '@'.$cpieces[1].str_replace(['\\''/'], [''''], $cpieces[2]).'/'.$template;
  221.         }
  222.         // TODO: this logic may need further modification due to the change of Twig naming patterns...
  223.         // assume html and twig if nothing is given
  224.         if (substr_count($template'.') === 0) {
  225.             $template .= '.html';
  226.         }
  227.         if (substr_count($template'.') === 1) {
  228.             $template .= '.twig';
  229.         }
  230.         // create the view with the view layer
  231.         return new AjaxScene(
  232.             $template,
  233.             $parameters
  234.         );
  235.     }
  236.     /**
  237.      * {@inheritdoc}
  238.      */
  239.     public function redirectToRoute(string $route, array $parameters = [], int $status 302): RedirectResponse
  240.     {
  241.         if (isset($parameters['return'])) {
  242.             return $this->redirect($parameters['return'], $status);
  243.         }
  244.         return parent::redirectToRoute($route$parameters$status);
  245.     }
  246.     /**
  247.      * @param string $route
  248.      * @param array $parameters
  249.      * @param int $status
  250.      * @return RedirectResponse
  251.      */
  252.     public function redirectToRouteOrOverride(string $route, array $parameters = [], int $status 302): RedirectResponse
  253.     {
  254.         $request $this->getRequest();
  255.         if ($request->query->has('redirectTo')) {
  256.             return $this->redirect($request->query->get('redirectTo'), $status);
  257.         }
  258.         return $this->redirectToRoute($route$parameters$status);
  259.     }
  260.     /**
  261.      * @return Locksmith|object
  262.      */
  263.     protected function getLocksmith(): Locksmith
  264.     {
  265.         return $this->get(__METHOD__);
  266.     }
  267.     /**
  268.      * @return SceneRenderer|object
  269.      */
  270.     protected function getSceneRenderer(): SceneRenderer
  271.     {
  272.         return $this->get(__METHOD__);
  273.     }
  274.     /**
  275.      * @return LoggingService|object
  276.      */
  277.     protected function getActivityLogger(): LoggingService
  278.     {
  279.         return $this->get(__METHOD__);
  280.     }
  281. }