Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This file is part of the Kdyby (http://www.kdyby.org)
- *
- * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
- *
- * @license http://www.kdyby.org/license
- */
- namespace Kdyby\Modules\Backend;
- use Kdyby;
- use Nette;
- use Nette\Latte;
- use Nette\Latte\MacroNode;
- /**
- * Macros for Nette\Forms.
- *
- * - {box $title class => 'left'} ... {/box}
- * - {notif signInform}
- * - {notif component}
- *
- * @author Filip Procházka
- */
- class UIMacros extends Latte\Macros\MacroSet
- {
- /**
- * @param Latte\Parser $parser
- */
- public static function install(Latte\Parser $parser)
- {
- $me = new static($parser);
- $me->addMacro('box', callback($me, 'macroBoxBegin'), "?>\n\t</div>\n</div>\n<?php");
- $me->addMacro('notif', callback($me, 'macroNotif'));
- }
- /**
- * @param MacroNode $node
- * @param Latte\PhpWriter $writer
- */
- public function macroBoxBegin(MacroNode $node, $writer)
- {
- $title = $node->tokenizer->fetchWord();
- return $writer->write('?>
- <div<?php echo Nette\Utils\Html::el()->addAttributes(%node.array)->addClass("bloc")->attributes() ?>>
- <div class="title"><?php echo %escape(' . $writer->formatWord($title) . '); ?></div>
- <div class="content">
- <?php');
- }
- /**
- * @param MacroNode $node
- * @param Latte\PhpWriter $writer
- */
- public function macroNotif(MacroNode $node, $writer)
- {
- $name = $node->tokenizer->fetchWord();
- if ($name === FALSE) {
- throw new ParseException("Missing control name in {notif}");
- }
- $name = $writer->formatWord($name);
- // find control
- $cmd = ($name[0] === '$' ? "if(is_object($name))\$_ctrl = $name;else" : '')
- . '$_ctrl = $control->getWidget(' . $name . ');';
- // if control, list flashes
- $cmd .= 'if($_ctrl instanceof Nette\Application\UI\Control && $presenter && $presenter->hasFlashSession()){'
- . '$_id = $_ctrl->getParamId("flash"); $_msgs = $presenter->getFlashSession()->$_id;}';
- // if form list errors
- $cmd .= 'if($_ctrl instanceof Nette\Forms\Form){'
- . '$_msgs = $_ctrl->getErrors();}';
- // iteration begin
- $cmd .= 'foreach ($_msgs as $_msg): ?>';
- // write message
- $cmd .= "\n"
- . '<div class="notif <?php echo %escape((is_object($_msg) && isset($_msg->type)) ? $_msg->type : "error"); ?>">'
- . "\n\t" . '<?php echo %escape((is_object($_msg) && isset($_msg->message)) ? $_msg->message : $_msg); ?>'
- . "\n\n</div>\n" . '<?php';
- // iteration end
- $cmd .= ' endforeach;';
- return $writer->write($cmd);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment