Exemple #1
0
 private function copyMessagesToPayload()
 {
     $id = $this->presenter->getParameterId('flash');
     $messages = $this->presenter->getFlashSession()->{$id};
     if (!$messages or !is_array($messages)) {
         return;
     }
     $payload = $this->presenter->payload;
     $payload->messageInfo = [];
     $payload->messageError = [];
     $payload->messageWarning = [];
     $payload->messageSuccess = [];
     foreach ($messages as $flash) {
         $type = strtolower($flash->type);
         if ($type == "success") {
             $payload->messageSuccess[] = $flash->message;
         } elseif ($type == "warning") {
             $payload->messageWarning[] = $flash->message;
         } elseif ($type == "error") {
             $payload->messageError[] = $flash->message;
         } else {
             $payload->messageInfo[] = $flash->message;
         }
     }
     if (count($payload->messageInfo) == 0) {
         unset($payload->messageInfo);
     }
     if (count($payload->messageError) == 0) {
         unset($payload->messageError);
     }
     if (count($payload->messageWarning) == 0) {
         unset($payload->messageWarning);
     }
     if (count($payload->messageSuccess) == 0) {
         unset($payload->messageSuccess);
     }
 }
 /**
  * Creates template and registers helpers and latte filter
  * @param  UI\Presenter $presenter
  * @param  string		$file	Filepath to file
  * @param  string|NULL	$lang	Lang code (length=2)
  * @param  string		$class	Name of template class
  * @throws InvalidArgumentException
  * @return Nette\Templating\IFileTemplate
  */
 public function createTemplate(UI\Presenter $presenter, $file = NULL, $lang = NULL, $class = NULL)
 {
     $this->translator->setLangTo($lang);
     $latte = $this->latteFactory->create();
     $template = $class ? new $class($latte) : new Nette\Bridges\ApplicationLatte\Template($latte);
     $template->getLatte()->addFilter(NULL, 'Nette\\Templating\\Helpers::loader');
     $template->getLatte()->addFilter(NULL, '\\Helpers::loader');
     array_unshift($latte->onCompile, function ($latte) {
         $latte->getParser()->shortNoEscape = TRUE;
         $latte->getCompiler()->addMacro('cache', new CacheMacro($latte->getCompiler()));
         Nette\Bridges\ApplicationLatte\UIMacros::install($latte->getCompiler());
         FormMacros::install($latte->getCompiler());
     });
     $latte->addFilter('url', 'rawurlencode');
     // back compatiblity
     foreach (array('normalize', 'toAscii', 'webalize', 'padLeft', 'padRight', 'reverse') as $name) {
         $latte->addFilter($name, 'Nette\\Utils\\Strings::' . $name);
     }
     $template->setTranslator($this->translator);
     if (!is_null($file)) {
         $template->setFile($this->appDir . $file);
     }
     // default parameters
     $template->control = $template->_control = $presenter;
     $template->presenter = $template->_presenter = $presenter;
     if ($presenter instanceof UI\Presenter) {
         $template->user = $presenter->getUser();
         $template->netteHttpResponse = $this->httpResponse;
         $template->netteCacheStorage = $this->netteCacheStorage;
         $template->baseUri = $template->baseUrl = rtrim($this->httpRequest->getUrl()->getBaseUrl(), '/');
         $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl);
         // flash message
         if ($presenter->hasFlashSession()) {
             $id = $presenter->getParameterId('flash');
             $template->flashes = $presenter->getFlashSession()->{$id};
         }
     }
     if (!isset($template->flashes) || !is_array($template->flashes)) {
         $template->flashes = array();
     }
     return $template;
 }