/** * Preview a css */ public function previewAction() { /* @var $form \Zork\Form\Form */ /* @var $model \Grid\Customize\Model\Sheet\Model */ $params = $this->params(); $request = $this->getRequest(); $locator = $this->getServiceLocator(); $id = $params->fromRoute('id'); $rootId = is_numeric($id) ? (int) $id : null; $model = $locator->get('Grid\\Customize\\Model\\Sheet\\Model'); $form = $locator->get('Form')->get('Grid\\Customize\\Css'); $sheet = $model->createEmpty($rootId); $form->setHydrator($model->getMapper())->bind($sheet); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $this->messenger()->add('customize.preview.success', 'customize', Message::LEVEL_INFO); $id = $rootId === null ? 'global' : $rootId; $prefix = 'public'; $file = sprintf(static::PREVIEW_FILE, $id) . '.'; do { $suffix = new DateTime(); } while (file_exists($prefix . $file . $suffix->toHash() . static::PREVIEW_EXTENSION)); $url = $file . $suffix->toHash() . static::PREVIEW_EXTENSION; $path = $prefix . $url; $sheet->render($path); $locator->get('Grid\\Customize\\Service\\CssPreview')->setPreviewById($rootId, $url); if (null === $rootId) { return $this->redirect()->toUrl('/'); } else { return $this->redirect()->toRoute('Grid\\Paragraph\\Render\\Paragraph', array('locale' => (string) $this->locale(), 'paragraphId' => $rootId)); } } else { $this->messenger()->add('customize.preview.failed', 'customize', Message::LEVEL_ERROR); return $this->redirect()->toRoute('Grid\\Customize\\CssAdmin\\List', array('locale' => (string) $this->locale())); } } }