sterntours/trunk/src/AppBundle/Twig/AppExtension.php
adametz 30c42d0508 Extension App + travelsearch
git-svn-id: http://78.47.251.156/svn/dev/sterntours-3@3481 f459cee4-fb09-11de-96c3-f9c5f16c3c76
2018-12-27 16:40:19 +00:00

149 lines
No EOL
4.6 KiB
PHP

<?php
/**
* @author Ulrich Hecht <ulrich.hecht@hecht-software.de>
* @date 02/10/2017
*/
namespace AppBundle\Twig;
use AppBundle\Service\KeywordService;
use AppBundle\Util;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Gregwar\Image\Image;
class AppExtension extends \Twig_Extension
{
private $environment;
private $keywordService;
private $requestStack;
private $template;
public function __construct(\Twig_Environment $env, KeywordService $keywordService, RequestStack $requestStack)
{
$this->environment = $env;
$this->keywordService = $keywordService;
$this->requestStack = $requestStack;
}
public function getFilters()
{
return [
'stripslashes' => new \Twig_SimpleFilter('stripslashes', [$this, 'stripslashesFilter'], [
'is_safe' => ['html']
]),
'keywords' => new \Twig_SimpleFilter('keywords', [$this, 'keywordsFilter'], [
'is_safe' => ['html']
]),
'lozad' => new \Twig_SimpleFilter('lozad', [$this, 'lozadFilter'], [
'is_safe' => ['html']
]),
];
}
public function getFunctions()
{
return [
'form_field' => new \Twig_SimpleFunction('form_field', [$this, 'formField'], [
'is_safe' => ['html']
]),
'form_field_pho' => new \Twig_SimpleFunction('form_field_pho', [$this, 'formFieldPho'], [
'is_safe' => ['html']
]),
'get_base_template' => new \Twig_SimpleFunction('get_base_template', [$this, 'getBaseTemplate']),
'get_base_url' => new \Twig_SimpleFunction('get_base_url', [$this, 'getBaseUrl']),
];
}
public function stripslashesFilter($v)
{
return stripslashes($v);
}
public function keywordsFilter($v)
{
return $this->keywordService->addKeywordLinksToHtml($v, 'keyword-link');
}
public function getBaseTemplate()
{
return ($this->requestStack->getCurrentRequest()->isXmlHttpRequest() ? 'ajax' : 'base') . '.html.twig';
}
public function getBaseUrl()
{
return Util::getBaseUrl();
}
public function formField($form, $label = null, $opt = null)
{
$this->template = $this->environment->loadTemplate('::default/form/helpers.html.twig');
return $this->template->renderBlock('form_field', [
'form' => $form,
'label' => $label,
'opt' => $opt
]);
}
/**
* Form field with placeholder only
*
* @param $form
* @param null $label
* @param null $opt
*
* @return mixed
*/
public function formFieldPho($form, $label = null, $opt = [])
{
$this->template = $this->environment->loadTemplate('::default/form/helpers.html.twig');
return $this->template->renderBlock('form_field_pho', [
'form' => $form,
'label' => $label,
'opt' => $opt
]);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'app_extension';
}
public function lozadFilter($html)
{
// return $html;
//(<img\s*?)src
$dom = new \DOMDocument;
$dom->loadHTML('<?xml encoding="utf-8" ?>'.$html);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$i = Util::getRootDir().'web/'.$image->getAttribute('src');
if(strpos($image->getAttribute('src'), 'docs') !== false){
$i = Util::getRootDir().'web/'.substr($image->getAttribute('src'), strpos($image->getAttribute('src'), 'docs'));
}
if(strpos($image->getAttribute('src'), 'uploads') !== false){
$i = Util::getRootDir().'web/'.substr($image->getAttribute('src'), strpos($image->getAttribute('src'), 'uploads'));
}
$i = Image::open($i)->cropResize(848)->guess(75);
// $i = image()..;
$image->setAttribute('data-src', "/".$i);
$image->setAttribute('src', 'images/placeholder-image.png');
$image->setAttribute('class', 'lozad');
}
$html = $dom->saveHTML();
return $html;
}
}