X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/96321e96c4762a30d6a98c97d227ca3a127cab3f..1e03b9bca33c3457074335594854a508592e5657:/Controller/AbstractController.php diff --git a/Controller/AbstractController.php b/Controller/AbstractController.php index f8a0b99..fe07f11 100644 --- a/Controller/AbstractController.php +++ b/Controller/AbstractController.php @@ -1,418 +1,359 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace Rapsys\AirBundle\Controller; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\Persistence\ManagerRegistry; +use Psr\Log\LoggerInterface; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController; +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; +use Symfony\Component\Asset\PackageInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Symfony\Component\Filesystem\Filesystem; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BaseAbstractController; -use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait; +use Symfony\Component\Form\FormFactoryInterface; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; +use Symfony\Contracts\Translation\TranslatorInterface; +use Symfony\Contracts\Service\ServiceSubscriberInterface; +use Rapsys\AirBundle\Entity\Dance; +use Rapsys\AirBundle\Entity\Location; use Rapsys\AirBundle\Entity\Slot; +use Rapsys\AirBundle\Entity\User; +use Rapsys\AirBundle\RapsysAirBundle; -use Rapsys\UserBundle\Utils\Slugger; +use Rapsys\PackBundle\Util\FacebookUtil; +use Rapsys\PackBundle\Util\ImageUtil; +use Rapsys\PackBundle\Util\MapUtil; +use Rapsys\PackBundle\Util\SluggerUtil; /** - * * Provides common features needed in controllers. * - * @todo: pass request_stack, router, slugger_util, path_package as constructor argument ? - * * {@inheritdoc} */ -abstract class AbstractController extends BaseAbstractController { - use ControllerTrait { - //Rename render as baseRender - render as protected baseRender; - } - - /** - * Return the facebook image - * - * @desc Generate image in jpeg format or load it from cache - * - * @param string $pathInfo The request path info - * @param array $parameters The image parameters - * @return array The image array - */ - protected function getFacebookImage(string $pathInfo, array $parameters = []): array { - //Get asset package - //XXX: require asset package to be public - $package = $this->container->get('rapsys_pack.path_package'); - - //Set texts - $texts = $parameters['texts'] ?? []; - - //Set default source - $source = $parameters['source'] ?? 'png/facebook.png'; - - //Set default source - $updated = $parameters['updated'] ?? strtotime('last week'); - - //Set source path - $src = $this->config['path']['public'].'/'.$source; - - //Set cache path - //XXX: remove extension and store as png anyway - $cache = $this->config['path']['cache'].'/facebook/'.substr($source, 0, strrpos($source, '.')).'.'.$this->config['facebook']['width'].'x'.$this->config['facebook']['height'].'.png'; - - //Set destination path - //XXX: format /facebook.jpeg - //XXX: was /facebook//..jpeg - $dest = $this->config['path']['public'].'/facebook'.$pathInfo.'.jpeg'; - - //With up to date generated image - if ( - is_file($dest) && - ($stat = stat($dest)) && - $stat['mtime'] >= $updated - ) { - //Get image size - list ($width, $height) = getimagesize($dest); - - //Iterate each text - foreach($texts as $text => $data) { - //With canonical text - if (!empty($data['canonical'])) { - //Prevent canonical to finish in alt - unset($texts[$text]); - } - } - - //Return image data - return [ - 'og:image' => $package->getAbsoluteUrl('@RapsysAir/facebook/'.$stat['mtime'].$pathInfo.'.jpeg'), - 'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))), - 'og:image:height' => $height, - 'og:image:width' => $width - ]; - //With image candidate - } elseif (is_file($src)) { - //Create image object - $image = new \Imagick(); - - //With cache image - if (is_file($cache)) { - //Read image - $image->readImage($cache); - //Without we generate it - } else { - //Check target directory - if (!is_dir($dir = dirname($cache))) { - //Create filesystem object - $filesystem = new Filesystem(); - - try { - //Create dir - //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) - $filesystem->mkdir($dir, 0775); - } catch (IOExceptionInterface $e) { - //Throw error - throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e); - } - } - - //Read image - $image->readImage($src); - - //Crop using aspect ratio - //XXX: for better result upload image directly in aspect ratio :) - $image->cropThumbnailImage($this->config['facebook']['width'], $this->config['facebook']['height']); +abstract class AbstractController extends BaseAbstractController implements ServiceSubscriberInterface { + ///AuthorizationCheckerInterface instance + protected $checker; - //Strip image exif data and properties - $image->stripImage(); + ///Config array + protected $config; - //Save cache image - if (!$image->writeImage($cache)) { - //Throw error - throw new \Exception(sprintf('Unable to write image "%s"', $cache)); - } - } - //Check target directory - if (!is_dir($dir = dirname($dest))) { - //Create filesystem object - $filesystem = new Filesystem(); - - try { - //Create dir - //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) - $filesystem->mkdir($dir, 0775); - } catch (IOExceptionInterface $e) { - //Throw error - throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e); - } - } + ///ContainerInterface instance + protected $container; - //Get image width - $width = $image->getImageWidth(); + ///Context array + protected $context; - //Get image height - $height = $image->getImageHeight(); + ///AccessDecisionManagerInterface instance + protected $decision; - //Create draw - $draw = new \ImagickDraw(); + ///ManagerRegistry instance + protected $doctrine; - //Set stroke antialias - $draw->setStrokeAntialias(true); + ///FacebookUtil instance + protected $facebook; - //Set text antialias - $draw->setTextAntialias(true); + ///FormFactoryInterface instance + protected $factory; - //Set font aliases - $fonts = [ - 'irishgrover' => $this->config['path']['public'].'/ttf/irishgrover.v10.ttf', - 'droidsans' => $this->config['path']['public'].'/ttf/droidsans.regular.ttf', - 'dejavusans' => $this->config['path']['public'].'/ttf/dejavusans.2.37.ttf', - 'labelleaurore' => $this->config['path']['public'].'/ttf/labelleaurore.v10.ttf' - ]; + ///Image util instance + protected $image; - //Set align aliases - $aligns = [ - 'left' => \Imagick::ALIGN_LEFT, - 'center' => \Imagick::ALIGN_CENTER, - 'right' => \Imagick::ALIGN_RIGHT - ]; + ///Locale string + protected $locale; - //Set default font - $defaultFont = 'dejavusans'; + ///MailerInterface instance + protected $mailer; - //Set default align - $defaultAlign = 'center'; + ///EntityManagerInterface instance + protected $manager; - //Set default size - $defaultSize = 60; + ///Map util instance + protected $map; - //Set default stroke - $defaultStroke = '#00c3f9'; + ///Modified DateTime + protected $modified; - //Set default width - $defaultWidth = 15; + ///PackageInterface instance + protected $package; - //Set default fill - $defaultFill = 'white'; + ///DatePeriod instance + protected $period; - //Init counter - $i = 1; + ///Request instance + protected $request; - //Set text count - $count = count($texts); + ///Route string + protected $route; - //Draw each text stroke - foreach($texts as $text => $data) { - //Set font - $draw->setFont($fonts[$data['font']??$defaultFont]); + ///Route params array + protected $routeParams; - //Set font size - $draw->setFontSize($data['size']??$defaultSize); + ///Router instance + protected $router; - //Set stroke width - $draw->setStrokeWidth($data['width']??$defaultWidth); + ///Slugger util instance + protected $slugger; - //Set text alignment - $draw->setTextAlignment($align = ($aligns[$data['align']??$defaultAlign])); - - //Get font metrics - $metrics = $image->queryFontMetrics($draw, $text); - - //Without y - if (empty($data['y'])) { - //Position verticaly each text evenly - $texts[$text]['y'] = $data['y'] = (($height + 100) / (count($texts) + 1) * $i) - 50; - } - - //Without x - if (empty($data['x'])) { - if ($align == \Imagick::ALIGN_CENTER) { - $texts[$text]['x'] = $data['x'] = $width/2; - } elseif ($align == \Imagick::ALIGN_LEFT) { - $texts[$text]['x'] = $data['x'] = 50; - } elseif ($align == \Imagick::ALIGN_RIGHT) { - $texts[$text]['x'] = $data['x'] = $width - 50; - } - } + ///RequestStack instance + protected $stack; - //Center verticaly - //XXX: add ascender part then center it back by half of textHeight - //TODO: maybe add a boundingbox ??? - $texts[$text]['y'] = $data['y'] += $metrics['ascender'] - $metrics['textHeight']/2; + ///Translator instance + protected $translator; - //Set stroke color - $draw->setStrokeColor(new \ImagickPixel($data['stroke']??$defaultStroke)); + /** + * Abstract constructor + * + * @param AuthorizationCheckerInterface $checker The container instance + * @param ContainerInterface $container The container instance + * @param AccessDecisionManagerInterface $decision The decision instance + * @param ManagerRegistry $doctrine The doctrine instance + * @param FacebookUtil $facebook The facebook instance + * @param FormFactoryInterface $factory The factory instance + * @param ImageUtil $image The image instance + * @param MailerInterface $mailer The mailer instance + * @param EntityManagerInterface $manager The manager instance + * @param MapUtil $map The map instance + * @param PackageInterface $package The package instance + * @param RouterInterface $router The router instance + * @param SluggerUtil $slugger The slugger instance + * @param RequestStack $stack The stack instance + * @param TranslatorInterface $translator The translator instance + * + * @TODO move all that stuff to setSlugger('@slugger') setters with a calls: [ setSlugger: [ '@slugger' ] ] to unbload classes ??? + * @TODO add a calls: [ ..., prepare: ['@???'] ] that do all the logic that can't be done in constructor because various things are not available + */ + public function __construct(AuthorizationCheckerInterface $checker, ContainerInterface $container, AccessDecisionManagerInterface $decision, ManagerRegistry $doctrine, FacebookUtil $facebook, FormFactoryInterface $factory, ImageUtil $image, MailerInterface $mailer, EntityManagerInterface $manager, MapUtil $map, PackageInterface $package, RouterInterface $router, SluggerUtil $slugger, RequestStack $stack, TranslatorInterface $translator) { + //Set checker + $this->checker = $checker; - //Set fill color - $draw->setFillColor(new \ImagickPixel($data['stroke']??$defaultStroke)); + //Retrieve config + $this->config = $container->getParameter(RapsysAirBundle::getAlias()); - //Add annotation - $draw->annotation($data['x'], $data['y'], $text); + //Set the container + $this->container = $container; - //Increase counter - $i++; - } + //Set decision + $this->decision = $decision; - //Create stroke object - $stroke = new \Imagick(); + //Set doctrine + $this->doctrine = $doctrine; - //Add new image - $stroke->newImage($width, $height, new \ImagickPixel('transparent')); + //Set facebook + $this->facebook = $facebook; - //Draw on image - $stroke->drawImage($draw); + //Set factory + $this->factory = $factory; - //Blur image - //XXX: blur the stroke canvas only - $stroke->blurImage(5,3); + //Set image + $this->image = $image; - //Set opacity to 0.5 - //XXX: see https://www.php.net/manual/en/image.evaluateimage.php - $stroke->evaluateImage(\Imagick::EVALUATE_DIVIDE, 1.5, \Imagick::CHANNEL_ALPHA); + //Set mailer + $this->mailer = $mailer; - //Compose image - $image->compositeImage($stroke, \Imagick::COMPOSITE_OVER, 0, 0); + //Set manager + $this->manager = $manager; - //Clear stroke - $stroke->clear(); + //Set map + $this->map = $map; - //Destroy stroke - unset($stroke); + //Set package + $this->package = $package; - //Clear draw - $draw->clear(); + //Set period + $this->period = new \DatePeriod( + //Start from first monday of week + new \DateTime('Monday this week'), + //Iterate on each day + new \DateInterval('P1D'), + //End with next sunday and 4 weeks + //XXX: we can't use isGranted here as AuthenticatedVoter deny access because user is likely not authenticated yet :'( + new \DateTime('Monday this week + 2 week') + ); - //Set text antialias - $draw->setTextAntialias(true); - //Draw each text - foreach($texts as $text => $data) { - //Set font - $draw->setFont($fonts[$data['font']??$defaultFont]); + //Set router + $this->router = $router; - //Set font size - $draw->setFontSize($data['size']??$defaultSize); + //Set slugger + $this->slugger = $slugger; - //Set text alignment - $draw->setTextAlignment($aligns[$data['align']??$defaultAlign]); + //Set stack + $this->stack = $stack; - //Set fill color - $draw->setFillColor(new \ImagickPixel($data['fill']??$defaultFill)); + //Set translator + $this->translator = $translator; - //Add annotation - $draw->annotation($data['x'], $data['y'], $text); + //Get main request + $this->request = $this->stack->getMainRequest(); - //With canonical text - if (!empty($data['canonical'])) { - //Prevent canonical to finish in alt - unset($texts[$text]); - } - } + //Get current locale + $this->locale = $this->request->getLocale(); - //Draw on image - $image->drawImage($draw); + //Set canonical + $canonical = null; - //Strip image exif data and properties - $image->stripImage(); + //Set alternates + $alternates = []; - //Set image format - $image->setImageFormat('jpeg'); + //Set route + //TODO: default to not found route ??? + //TODO: pour une url not found, cet attribut n'est pas défini, comment on fait ??? + //XXX: on génère une route bidon par défaut ??? + $this->route = $this->request->attributes->get('_route'); - //Save image - if (!$image->writeImage($dest)) { - //Throw error - throw new \Exception(sprintf('Unable to write image "%s"', $dest)); - } + //Set route params + $this->routeParams = $this->request->attributes->get('_route_params'); - //Get dest stat - $stat = stat($dest); + //With route and routeParams + if ($this->route !== null && $this->routeParams !== null) { + //Set canonical + $canonical = $this->router->generate($this->route, $this->routeParams, UrlGeneratorInterface::ABSOLUTE_URL); - //Return image data - return [ - 'og:image' => $package->getAbsoluteUrl('@RapsysAir/facebook/'.$stat['mtime'].$pathInfo.'.jpeg'), - 'og:image:alt' => str_replace("\n", ' ', implode(' - ', array_keys($texts))), - 'og:image:height' => $height, - 'og:image:width' => $width + //Set alternates + $alternates = [ + substr($this->locale, 0, 2) => [ + 'absolute' => $canonical + ] ]; } - //Return empty array without image - return []; + //Set the context + $this->context = [ + 'description' => null, + 'section' => null, + 'title' => null, + 'locale' => str_replace('_', '-', $this->locale), + 'contact' => [ + 'title' => $this->translator->trans($this->config['contact']['title']), + 'mail' => $this->config['contact']['mail'] + ], + 'copy' => [ + 'by' => $this->translator->trans($this->config['copy']['by']), + 'link' => $this->config['copy']['link'], + 'long' => $this->translator->trans($this->config['copy']['long']), + 'short' => $this->translator->trans($this->config['copy']['short']), + 'title' => $this->config['copy']['title'] + ], + 'site' => [ + 'donate' => $this->config['site']['donate'], + 'icon' => $this->config['site']['icon'], + 'logo' => $this->config['site']['logo'], + 'png' => $this->config['site']['png'], + 'title' => $title = $this->translator->trans($this->config['site']['title']), + 'url' => $this->router->generate($this->config['site']['url']) + ], + 'canonical' => $canonical, + 'alternates' => $alternates, + 'facebook' => [ + 'metas' => [ + 'og:type' => 'article', + 'og:site_name' => $title, + 'og:url' => $canonical, + #'fb:admins' => $this->config['facebook']['admins'], + 'fb:app_id' => $this->config['facebook']['apps'] + ], + 'texts' => [ + $this->translator->trans($this->config['site']['title']) => [ + 'font' => 'irishgrover', + 'size' => 110 + ] + ] + ], + 'forms' => [] + ]; } - /** * Renders a view * * {@inheritdoc} */ protected function render(string $view, array $parameters = [], Response $response = null): Response { - //Get request stack - $stack = $this->container->get('request_stack'); + //Create application form for role_guest + if ($this->isGranted('ROLE_GUEST')) { + //Without application form + if (empty($parameters['forms']['application'])) { + //Get favorites dances + $danceFavorites = $this->doctrine->getRepository(Dance::class)->findByUserId($this->getUser()->getId()); - //Get current request - $request = $stack->getCurrentRequest(); + //Set dance default + $danceDefault = !empty($danceFavorites)?current($danceFavorites):null; - //Get current locale - $locale = $request->getLocale(); + //Get favorites locations + $locationFavorites = $this->doctrine->getRepository(Location::class)->findByUserId($this->getUser()->getId()); - //Set locale - $parameters['locale'] = str_replace('_', '-', $locale); + //Set location default + $locationDefault = !empty($locationFavorites)?current($locationFavorites):null; - //Get router - $router = $this->container->get('router'); + //With admin + if ($this->checker->isGranted('ROLE_ADMIN')) { + //Get dances + $dances = $this->doctrine->getRepository(Dance::class)->findAll(); - //Get context path - $pathInfo = $router->getContext()->getPathInfo(); + //Get locations + $locations = $this->doctrine->getRepository(Location::class)->findAll(); + //Without admin + } else { + //Restrict to favorite dances + $dances = $danceFavorites; - //Iterate on locales excluding current one - foreach($this->config['locales'] as $current) { - //Set titles - $titles = []; + //Reset favorites + $danceFavorites = []; - //Iterate on other locales - foreach(array_diff($this->config['locales'], [$current]) as $other) { - $titles[$other] = $this->translator->trans($this->config['languages'][$current], [], null, $other); - } + //Restrict to favorite locations + $locations = $locationFavorites; - //Retrieve route matching path - $route = $router->match($pathInfo); - - //Get route name - $name = $route['_route']; - - //Unset route name - unset($route['_route']); - - //With current locale - if ($current == $locale) { - //Set locale locales context - $parameters['canonical'] = $router->generate($name, ['_locale' => $current]+$route, UrlGeneratorInterface::ABSOLUTE_URL); - } else { - //Set locale locales context - $parameters['alternates'][str_replace('_', '-', $current)] = [ - 'absolute' => $router->generate($name, ['_locale' => $current]+$route, UrlGeneratorInterface::ABSOLUTE_URL), - 'relative' => $router->generate($name, ['_locale' => $current]+$route), - 'title' => implode('/', $titles), - 'translated' => $this->translator->trans($this->config['languages'][$current], [], null, $current) - ]; - } + //Reset favorites + $locationFavorites = []; + } - //Add shorter locale - if (empty($parameters['alternates'][$shortCurrent = substr($current, 0, 2)])) { - //Set locale locales context - $parameters['alternates'][$shortCurrent] = [ - 'absolute' => $router->generate($name, ['_locale' => $current]+$route, UrlGeneratorInterface::ABSOLUTE_URL), - 'relative' => $router->generate($name, ['_locale' => $current]+$route), - 'title' => implode('/', $titles), - 'translated' => $this->translator->trans($this->config['languages'][$current], [], null, $current) - ]; - } - } + //With session application dance id + if (!empty($parameters['session']['application']['dance']['id'])) { + //Iterate on each dance + foreach($dances as $dance) { + //Found dance + if ($dance->getId() == $parameters['session']['application']['dance']['id']) { + //Set dance as default + $danceDefault = $dance; + + //Stop search + break; + } + } + } - //Create application form for role_guest - if ($this->isGranted('ROLE_GUEST')) { - //Without application form - if (empty($parameters['forms']['application'])) { - //Fetch doctrine - $doctrine = $this->getDoctrine(); + //With session location id + //XXX: set in session controller + //TODO: with new findAll that key by id, it should be as simple as isset($locations[$id]) ? + if (!empty($parameters['session']['location']['id'])) { + //Iterate on each location + foreach($locations as $location) { + //Found location + if ($location->getId() == $parameters['session']['location']['id']) { + //Set location as default + $locationDefault = $location; + + //Stop search + break; + } + } + } //Create ApplicationType form $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ @@ -420,20 +361,37 @@ abstract class AbstractController extends BaseAbstractController { 'action' => $this->generateUrl('rapsys_air_application_add'), //Set the form attribute 'attr' => [ 'class' => 'col' ], - //Set admin - 'admin' => $this->isGranted('ROLE_ADMIN'), + //Set dance choices + 'dance_choices' => $dances, + //Set dance default + 'dance_default' => $danceDefault, + //Set dance favorites + 'dance_favorites' => $danceFavorites, + //Set location choices + 'location_choices' => $locations, + //Set location default + 'location_default' => $locationDefault, + //Set location favorites + 'location_favorites' => $locationFavorites, + //With user + 'user' => $this->checker->isGranted('ROLE_ADMIN'), + //Set user choices + 'user_choices' => $this->doctrine->getRepository(User::class)->findChoicesAsArray(), //Set default user to current - 'user' => $this->getUser()->getId(), - //Set default slot to evening + 'user_default' => $this->getUser()->getId(), + //Set to session slot or evening by default //XXX: default to Evening (3) - 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3) + 'slot_default' => $this->doctrine->getRepository(Slot::class)->findOneById($parameters['session']['slot']['id']??3) ]); //Add form to context $parameters['forms']['application'] = $application->createView(); } + }/* + #XXX: removed because it fucks up the seo by displaying register and login form instead of content + #XXX: until we find a better way, removed !!! //Create login form for anonymous - } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { + elseif (!$this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { //Create LoginType form $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ //Set the action @@ -461,22 +419,21 @@ abstract class AbstractController extends BaseAbstractController { 'surname' => false, //Without password 'password' => false, + //Without slug + 'slug' => false, //Without phone 'phone' => false ]; - //Get slugger - $slugger = $this->container->get('rapsys_pack.slugger_util'); - //Create RegisterType form $register = $this->createForm('Rapsys\AirBundle\Form\RegisterType', null, $field+[ //Set the action 'action' => $this->generateUrl( 'rapsys_user_register', [ - 'mail' => $smail = $slugger->short(''), - 'field' => $sfield = $slugger->serialize($field), - 'hash' => $slugger->hash($smail.$sfield) + 'mail' => $smail = $this->slugger->short(''), + 'field' => $sfield = $this->slugger->serialize($field), + 'hash' => $this->slugger->hash($smail.$sfield) ] ), //Set the form attribute @@ -485,26 +442,84 @@ abstract class AbstractController extends BaseAbstractController { //Add form to context $parameters['forms']['register'] = $register->createView(); + }*/ + + //Without alternates + if (count($parameters['alternates']) <= 1) { + //Set routeParams + $routeParams = $this->routeParams; + + //Iterate on locales excluding current one + foreach($this->config['locales'] as $locale) { + //With current locale + if ($locale !== $this->locale) { + //Set titles + $titles = []; + + //Set route params locale + $routeParams['_locale'] = $locale; + + //Iterate on other locales + foreach(array_diff($this->config['locales'], [$locale]) as $other) { + //Set other locale title + $titles[$other] = $this->translator->trans($this->config['languages'][$locale], [], null, $other); + } + + //Set locale locales context + $parameters['alternates'][str_replace('_', '-', $locale)] = [ + 'absolute' => $this->router->generate($this->route, $routeParams, UrlGeneratorInterface::ABSOLUTE_URL), + 'relative' => $this->router->generate($this->route, $routeParams), + 'title' => implode('/', $titles), + 'translated' => $this->translator->trans($this->config['languages'][$locale], [], null, $locale) + ]; + + //Add shorter locale + if (empty($parameters['alternates'][$shortCurrent = substr($locale, 0, 2)])) { + //Set locale locales context + $parameters['alternates'][$shortCurrent] = $parameters['alternates'][str_replace('_', '-', $locale)]; + } + } + } } //With page infos and without facebook texts - if (empty($parameters['facebook']['texts']) && !empty($parameters['site']['title']) && !empty($parameters['page']['title']) && !empty($parameters['canonical'])) { - //Set facebook image - $parameters['facebook']['texts'] = [ - $parameters['site']['title'] => [ + if (count($parameters['facebook']['texts']) <= 1 && isset($parameters['title']) && isset($this->route) && isset($this->routeParams)) { + //Append facebook image texts + $parameters['facebook']['texts'] += [ + $parameters['title'] => [ 'font' => 'irishgrover', - 'size' => 110 - ], - $parameters['page']['title'] => [ 'align' => 'left' - ], - $parameters['canonical'] => [ + ]/*XXX: same problem as url, too long :'(, + $parameters['description'] => [ 'align' => 'right', 'canonical' => true, 'font' => 'labelleaurore', 'size' => 50 - ] + ]*/ ]; + + /*With short path info + We don't add this stupid url in image !!! + if (strlen($pathInfo = $this->router->generate($this->route, $this->routeParams)) <= 64) { + => [ + 'align' => 'right', + 'canonical' => true, + 'font' => 'labelleaurore', + 'size' => 50 + ] + }*/ + } + + //With empty locations link + if (empty($parameters['locations_link'])) { + //Set locations link + $parameters['locations_link'] = $this->router->generate('rapsys_air_location'); + } + + //With empty locations title + if (empty($parameters['locations_title'])) { + //Set locations title + $parameters['locations_title'] = $this->translator->trans('Locations', [], null, $this->locale); } //With canonical @@ -513,22 +528,22 @@ abstract class AbstractController extends BaseAbstractController { $parameters['facebook']['metas']['og:url'] = $parameters['canonical']; } - //With page title - if (!empty($parameters['page']['title'])) { + //With empty facebook title and title + if (empty($parameters['facebook']['metas']['og:title']) && !empty($parameters['title'])) { //Set facebook title - $parameters['facebook']['metas']['og:title'] = $parameters['page']['title']; + $parameters['facebook']['metas']['og:title'] = $parameters['title']; } - //With page description - if (!empty($parameters['page']['description'])) { + //With empty facebook description and description + if (empty($parameters['facebook']['metas']['og:description']) && !empty($parameters['description'])) { //Set facebook description - $parameters['facebook']['metas']['og:description'] = $parameters['page']['description']; + $parameters['facebook']['metas']['og:description'] = $parameters['description']; } //With locale - if (!empty($locale)) { + if (!empty($this->locale)) { //Set facebook locale - $parameters['facebook']['metas']['og:locale'] = $locale; + $parameters['facebook']['metas']['og:locale'] = $this->locale; //With alternates //XXX: locale change when fb_locale=xx_xx is provided is done in FacebookSubscriber @@ -545,37 +560,38 @@ abstract class AbstractController extends BaseAbstractController { } //Without facebook image defined and texts - if (empty($parameters['facebook']['metas']['og:image']) && !empty($parameters['facebook']['texts'])) { + if (empty($parameters['facebook']['metas']['og:image']) && !empty($this->request) && !empty($parameters['facebook']['texts']) && !empty($this->modified)) { //Get facebook image - $parameters['facebook']['metas'] += $this->getFacebookImage($pathInfo, $parameters['facebook']); + $parameters['facebook']['metas'] += $this->facebook->getImage($this->request->getPathInfo(), $parameters['facebook']['texts'], $this->modified->getTimestamp()); } //Call parent method - return $this->baseRender($view, $parameters, $response); + return parent::render($view, $parameters, $response); } /** - * TODO: define this function to limit subscribed services ??? - * XXX: see vendor/symfony/framework-bundle/Controller/AbstractController.php + * {@inheritdoc} * - public static function getSubscribedServices() { - //TODO: add asset.package ? - return [ - 'router' => '?'.RouterInterface::class, - 'request_stack' => '?'.RequestStack::class, - 'http_kernel' => '?'.HttpKernelInterface::class, - 'serializer' => '?'.SerializerInterface::class, - 'session' => '?'.SessionInterface::class, - 'security.authorization_checker' => '?'.AuthorizationCheckerInterface::class, - 'templating' => '?'.EngineInterface::class, - 'twig' => '?'.Environment::class, - 'doctrine' => '?'.ManagerRegistry::class, - 'form.factory' => '?'.FormFactoryInterface::class, - 'security.token_storage' => '?'.TokenStorageInterface::class, - 'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class, - 'parameter_bag' => '?'.ContainerBagInterface::class, - 'message_bus' => '?'.MessageBusInterface::class, - 'messenger.default_bus' => '?'.MessageBusInterface::class, - ]; - }*/ + * @see vendor/symfony/framework-bundle/Controller/AbstractController.php + */ + public static function getSubscribedServices(): array { + //Return subscribed services + return [ + 'doctrine' => ManagerRegistry::class, + 'doctrine.orm.default_entity_manager' => EntityManagerInterface::class, + 'form.factory' => FormFactoryInterface::class, + 'mailer.mailer' => MailerInterface::class, + 'rapsys_air.facebook_util' => FacebookUtil::class, + 'rapsys_pack.image_util' => ImageUtil::class, + 'rapsys_pack.map_util' => MapUtil::class, + 'rapsys_pack.path_package' => PackageInterface::class, + 'rapsys_pack.slugger_util' => SluggerUtil::class, + 'rapsys_user.access_decision_manager' => AccessDecisionManagerInterface::class, + 'request_stack' => RequestStack::class, + 'router' => RouterInterface::class, + 'security.authorization_checker' => AuthorizationCheckerInterface::class, + 'service_container' => ContainerInterface::class, + 'translator' => TranslatorInterface::class + ]; + } }