1+ <?php
2+
3+ namespace WebChemistry \Forms \Controls \Wizard ;
4+
5+ use Latte \CompileException ;
6+ use Latte \Engine ;
7+ use Latte \Macros \MacroSet ;
8+ use Latte \MacroNode ;
9+ use Latte \PhpWriter ;
10+ use Nette \Bridges \ApplicationLatte \UIMacros ;
11+ use Nette \Utils \Strings ;
12+
13+ class Macros extends MacroSet {
14+
15+ public static function install (Engine $ latte ) {
16+ $ me = new self ($ latte ->getCompiler ());
17+
18+ $ me ->addMacro ('wizard ' , array ($ me , 'wizardStart ' ), array ($ me , 'wizardEnd ' ));
19+ $ me ->addMacro ('step ' , array ($ me , 'stepStart ' ), '} ' );
20+ }
21+
22+ public function wizardStart (MacroNode $ node , PhpWriter $ writer ) {
23+ $ words = $ node ->tokenizer ->fetchWords ();
24+ if (!$ words ) {
25+ throw new CompileException ('Missing control name in {wizard} ' );
26+ }
27+ $ name = $ writer ->formatWord ($ words [0 ]);
28+ $ method = isset ($ words [1 ]) ? ucfirst ($ words [1 ]) : '' ;
29+ $ method = Strings::match ($ method , '#^\w*\z# ' ) ? "render $ method " : "{ \"render $ method \"} " ;
30+ $ param = $ writer ->formatArray ();
31+ if (!Strings::contains ($ node ->args , '=> ' )) {
32+ $ param = substr ($ param , $ param [0 ] === '[ ' ? 1 : 6 , -1 ); // removes array() or []
33+ }
34+
35+ return ($ name [0 ] === '$ ' ? "if (is_object( $ name)) \$_l->tmp = $ name; else " : '' )
36+ . '$_l->tmp = $_control->getComponent( ' . $ name . '); '
37+ . 'if (!$_l->tmp instanceof WebChemistry\Forms\Controls\IWizard) throw new \Exception( \'Wizard must be instance of WebChemistry\Forms\Controls\IWizard \'); '
38+ . '$wizard = new WebChemistry\Forms\Controls\Wizard\Facade($_l->tmp); ' ;
39+ }
40+
41+ public function stepStart (MacroNode $ node , PhpWriter $ writer ) {
42+ $ word = $ node ->tokenizer ->fetchWord ();
43+ if (!is_numeric ($ word ) && !in_array ($ word , array ('success ' , '"success" ' , "'success' " ))) {
44+ throw new CompileException ('First parameter in {step} must be a numeric. ' );
45+ }
46+
47+ if ($ word === 'success ' ) {
48+ return 'if ($wizard->isSuccess()) { ' ;
49+ }
50+
51+ return 'if ($wizard->getCurrentStep() === ' . $ word . ' && !$wizard->isSuccess()) { $wizardForm = $form = $wizard->getCurrentComponent(); ' ;
52+ }
53+
54+ public function wizardEnd () {}
55+ }
0 commit comments