/**
* Generate frontend from url key
*
* @return ViewModel
*/
public function indexAction()
{
$coreConfig = $this->getServiceLocator()->get('CoreConfig');
$viewModel = new ViewModel();
$this->events()->trigger('Front', 'preDispatch', $this, array('viewModel' => $viewModel));
if ($coreConfig->getValue('site_is_offline') == 1) {
$isAdmin = $this->getServiceLocator()->get('Auth')->hasIdentity();
if (!$isAdmin) {
$document = Document\Model::fromId($coreConfig->getValue('site_offline_document'));
if (empty($document)) {
$viewModel->setTemplate('gc-frontend/site-is-offline');
$viewModel->setTerminal(true);
return $viewModel;
}
}
}
try {
$document = $this->getServiceLocator()->get('CurrentDocument');
} catch (Exception $e) {
//Don't care, page is just not found
}
$variables = array();
if (empty($document)) {
// 404
$this->getResponse()->setStatusCode(404);
$layout = Layout\Model::fromId($coreConfig->getValue('site_404_layout'));
if (empty($layout)) {
$viewModel->setTerminal(true);
}
} else {
//Load properties from document id
$properties = new Property\Collection();
$properties->load(null, null, $document->getId());
foreach ($properties->getProperties() as $property) {
$value = $property->getValue();
if ($this->isSerialized($value)) {
$value = unserialize($value);
}
$viewModel->setVariable($property->getIdentifier(), $value);
$this->layout()->setVariable($property->getIdentifier(), $value);
$variables[$property->getIdentifier()] = $value;
}
//Set view from database
$view = $document->getView();
$layout = $document->getLayout();
}
if (!empty($layout)) {
$this->layout()->setTemplate('layout/' . $layout->getIdentifier());
}
if (!empty($view)) {
$viewModel->setTemplate('view/' . $view->getIdentifier());
}
if ($this->getRequest()->isXmlHttpRequest()) {
if ($this->params()->fromQuery('terminate_layout') or $this->params()->fromPost('terminate_layout')) {
$viewModel->setTerminal(true);
}
}
$this->events()->trigger('Front', 'postDispatch', $this, array('viewModel' => $viewModel));
return $viewModel;
}