HosipLan

Untitled

Jun 22nd, 2011
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This file is part of the Kdyby (http://www.kdyby.org)
  5.  *
  6.  * Copyright (c) 2008, 2011 Filip Procházka ([email protected])
  7.  *
  8.  * @license http://www.kdyby.org/license
  9.  */
  10.  
  11. namespace Kdyby\Modules\Backend;
  12.  
  13. use Kdyby;
  14. use Nette;
  15. use Nette\Latte;
  16. use Nette\Latte\MacroNode;
  17.  
  18.  
  19.  
  20. /**
  21.  * Macros for Nette\Forms.
  22.  *
  23.  * - {box $title class => 'left'} ... {/box}
  24.  * - {notif signInform}
  25.  * - {notif component}
  26.  *
  27.  * @author Filip Procházka
  28.  */
  29. class UIMacros extends Latte\Macros\MacroSet
  30. {
  31.  
  32.     /**
  33.      * @param Latte\Parser $parser
  34.      */
  35.     public static function install(Latte\Parser $parser)
  36.     {
  37.         $me = new static($parser);
  38.         $me->addMacro('box', callback($me, 'macroBoxBegin'), "?>\n\t</div>\n</div>\n<?php");
  39.         $me->addMacro('notif', callback($me, 'macroNotif'));
  40.     }
  41.  
  42.  
  43.  
  44.     /**
  45.      * @param MacroNode $node
  46.      * @param Latte\PhpWriter $writer
  47.      */
  48.     public function macroBoxBegin(MacroNode $node, $writer)
  49.     {
  50.         $title = $node->tokenizer->fetchWord();
  51.  
  52.         return $writer->write('?>
  53. <div<?php echo Nette\Utils\Html::el()->addAttributes(%node.array)->addClass("bloc")->attributes() ?>>
  54.     <div class="title"><?php echo %escape(' . $writer->formatWord($title) . '); ?></div>
  55.     <div class="content">
  56. <?php');
  57.     }
  58.  
  59.  
  60.  
  61.     /**
  62.      * @param MacroNode $node
  63.      * @param Latte\PhpWriter $writer
  64.      */
  65.     public function macroNotif(MacroNode $node, $writer)
  66.     {
  67.         $name = $node->tokenizer->fetchWord();
  68.         if ($name === FALSE) {
  69.             throw new ParseException("Missing control name in {notif}");
  70.         }
  71.  
  72.         $name = $writer->formatWord($name);
  73.  
  74.         // find control
  75.         $cmd = ($name[0] === '$' ? "if(is_object($name))\$_ctrl = $name;else" : '')
  76.             . '$_ctrl = $control->getWidget(' . $name . ');';
  77.  
  78.         // if control, list flashes
  79.         $cmd .= 'if($_ctrl instanceof Nette\Application\UI\Control && $presenter && $presenter->hasFlashSession()){'
  80.                 . '$_id = $_ctrl->getParamId("flash"); $_msgs = $presenter->getFlashSession()->$_id;}';
  81.  
  82.         // if form list errors
  83.         $cmd .= 'if($_ctrl instanceof Nette\Forms\Form){'
  84.                 . '$_msgs = $_ctrl->getErrors();}';
  85.  
  86.         // iteration begin
  87.         $cmd .= 'foreach ($_msgs as $_msg): ?>';
  88.  
  89.         // write message
  90.         $cmd .= "\n"
  91.             . '<div class="notif <?php echo %escape((is_object($_msg) && isset($_msg->type)) ? $_msg->type : "error"); ?>">'
  92.             . "\n\t" . '<?php echo %escape((is_object($_msg) && isset($_msg->message)) ? $_msg->message : $_msg); ?>'
  93.             . "\n\n</div>\n" . '<?php';
  94.  
  95.         // iteration end
  96.         $cmd .= ' endforeach;';
  97.  
  98.         return $writer->write($cmd);
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment