* @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; //(loadHTML(''.$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; } }