<?php
namespace Kiwi\PageBundle\Service;
use Symfony\Component\HttpFoundation\Session\Session;
use Twig_Environment;
use Symfony\Component\HttpFoundation\Response;
class PageFrontendHandle {
protected $page_handle;
protected $placeholders_handle;
protected $page_modules_handle;
protected $languages;
protected $kiweePageFrontendHandle;
var $controller = false;
var $request = false;
var $response = false;
var $page = false;
var $lang = false;
var $config = array();
public function __construct(\Kiwi\PageBundle\Service\PageHandle $page_handle,
\Kiwi\LayoutTemplatesBundle\Service\LayoutTemplatesGroupsTemplatesPlaceholdersHandle $placeholders_handle,
\Kiwi\PageBundle\Service\PageModulesFactory $page_modules_handle,
\Kiwi\LanguagesBundle\Service\LanguagesHandle $languages,
\Kiwee\PageBundle\Service\PageFrontendHandle $kiweePageFrontendHandle) {
$this->page_handle = $page_handle;
$this->placeholders_handle = $placeholders_handle;
$this->page_modules_handle = $page_modules_handle;
$this->languages = $languages;
$this->kiweePageFrontendHandle = $kiweePageFrontendHandle;
}
public function setController($controller) {
$this->controller = $controller;
}
public function setPage($lang, $page_uid) {
$this->page = $this->page_handle->getPageBySeoName($page_uid);
$this->lang = $lang;
}
public function setPageByReference($lang, \Kiwi\PageBundle\Entity\PageTree $page) {
$this->page = $page;
$this->lang = $lang;
}
private function setDefaultPage() {
}
public function setRequest(\Symfony\Component\HttpFoundation\Request $request) {
$this->request = $request;
}
public function setResponse(\Symfony\Component\HttpFoundation\Response $response) {
$this->response = $response;
}
public function setConfig(Array $config) {
$this->config = $config;
}
public function getPageRedirectUrl() {
$ret = false;
if($this->page->getRedirect()) {
$page_seo_name = $this->languages->getLangEntry($this->page->getRedirect()->getPageName(),$this->lang);
$ret = $this->getDevPrefix().'/'.$this->lang.'/'.$page_seo_name;
}
return $ret;
}
public function getRedirectSuffix() {
$ret = null;
if($this->page->getRedirect()) {
$ret = $this->page->getRedirectSuffix();
}
return $ret;
}
private function getDevPrefix() {
$dev_prefix = '';
if($this->controller->get( 'kernel' )->getEnvironment() == 'dev')
$dev_prefix = '/app_dev.php';
return $dev_prefix;
}
private function getPageData() {
$ret = array();
$this->config['response'] = $this->response;
$this->config['lang'] = $this->lang;
$this->config['page_id'] = $this->page->getId();
$this->config['cookie_agree'] = $this->controller->get('cookie_agree_handle')->getHandle($this->lang);
//append info of eshop loged user
$this->config['eshop_user'] = $this->controller->get('eshop_users_handle')->getCurrentUser();
$this->config['eshop_basket'] = $this->controller->get('eshop_basket_handle')->getBasketItems(0,$this->lang);
$this->config['last_products'] = $this->controller->get('eshop_products_handle')->getCookieProduct($this->request, $this->lang);
$ret = array_merge($ret,$this->config);
$page_placeholders = $this->placeholders_handle->getPlaceholders($this->page->getLayout()->getId());
if($page_placeholders) {
foreach($page_placeholders as $placeholder) {
$modules = $this->page_modules_handle->getPlaceholderModules($placeholder,true);
$placeholder_html = '';
if($modules) {
foreach($modules as $module) {
$append_module = true;
if($module->getLanguage()) {
$append_module = false;
if($this->config['lang'] == $module->getLanguage()->GetCode())
$append_module = true;
}
if($append_module) {
$module_html = $this->page_modules_handle->getKiwiModule(md5($module->getNamespace()))->getKiwiFrontendModuleBox($module,$this->config);
if($this->getIsKewee()) {
$placeholder_html .= $this->kiweePageFrontendHandle->getModuleHtml(
$module->getNamespace(),
$module,
$this->config,
$module_html
);
} else {
$placeholder_html .= $module_html;
}
}
}
}
if($this->getIsKewee()) {
$ret['d'][$placeholder->getId()] = $this->kiweePageFrontendHandle->getPlaceholderHtml($placeholder_html);
} else {
$ret['d'][$placeholder->getId()] = $placeholder_html;
}
}
}
return $ret;
}
public function getPageHtml() {
if($this->page) {
$response = new Response();
$this->setResponse($response);
$this->page_modules_handle->setPage($this->page);
$this->page_modules_handle->setRequest($this->request);
$this->page_modules_handle->setResponse($response);
$twig = clone $this->controller->get('twig');
$page_html = $twig->render($this->controller->get('BL')->cacheStringTemplate(
'{% autoescape false %}'.$this->page->getLayout()->getTpl().'{% endautoescape %}'),
$this->getPageData()
);
$response->setContent($page_html);
$response->setStatusCode(Response::HTTP_OK);
$response->headers->set('Content-Type', 'text/html');
$this->setPageMeta();
return $response;
} else {
$this->setPageMeta();
$this->setDefaultPage();
if($this->page) {
} else {
trigger_error('Požadovaná stránka nebyla nalezena', E_USER_ERROR);
}
}
}
public function getIsKewee() {
return false;
}
public function setPageMeta($title = null, $image = null, $description = null) {
$session = new Session();
$session->set('page_meta_title',$title);
$session->set('page_meta_image',$image);
$session->set('page_meta_description',$description);
}
}