Guest User

Untitled

a guest
Jul 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Nette\Templates;
  4.  
  5. use Nette\Forms\Form;
  6. use Nette\String;
  7. use InvalidStateException;
  8.  
  9.  
  10. /**
  11. * Form macros
  12. *
  13. * @author Jan Marek
  14. * @license MIT
  15. */
  16. class FormMacros extends \Nette\Object
  17. {
  18.  
  19. private static $form;
  20.  
  21.  
  22.  
  23. public function __construct()
  24. {
  25. throw new InvalidStateException("Static class.");
  26. }
  27.  
  28.  
  29.  
  30. public static function register()
  31. {
  32. LatteMacros::$defaultMacros["form"] = '<?php %Nette\Templates\FormMacros::macroBegin% ?>';
  33. LatteMacros::$defaultMacros["input"] = '<?php %Nette\Templates\FormMacros::macroInput% ?>';
  34. LatteMacros::$defaultMacros["label"] = '<?php %Nette\Templates\FormMacros::macroLabel% ?>';
  35. LatteMacros::$defaultMacros["errors"] = '<?php %Nette\Templates\FormMacros::macroErrors% ?>';
  36. LatteMacros::$defaultMacros["/form"] = '<?php Nette\Templates\FormMacros::end() ?>';
  37. }
  38.  
  39.  
  40.  
  41. public static function macroBegin($content)
  42. {
  43. list($name, $modifiers) = self::fetchNameAndModifiers($content);
  44. return "\$formErrors = Nette\Templates\FormMacros::begin($name, \$control, $modifiers)->getErrors()";
  45. }
  46.  
  47.  
  48.  
  49. public static function begin($form, $control, $modifiers = array())
  50. {
  51. if ($form instanceof Form) {
  52. self::$form = $form;
  53. } else {
  54. self::$form = $control[$form];
  55. }
  56.  
  57. if (isset($modifiers["class"])) {
  58. self::$form->getElementPrototype()->class[] = $modifiers["class"];
  59. }
  60.  
  61. self::$form->render("begin");
  62.  
  63. return self::$form;
  64. }
  65.  
  66.  
  67.  
  68. public static function end()
  69. {
  70. self::$form->render("end");
  71. }
  72.  
  73.  
  74.  
  75. public static function macroInput($content)
  76. {
  77. list($name, $modifiers) = self::fetchNameAndModifiers($content);
  78. return "Nette\Templates\FormMacros::input($name, $modifiers)";
  79. }
  80.  
  81.  
  82.  
  83. public static function input($name, $modifiers = array())
  84. {
  85. $input = self::$form[$name]->getControl();
  86.  
  87. if (isset($modifiers["size"])) {
  88. $input->size($modifiers["size"]);
  89. }
  90.  
  91. if (isset($modifiers["rows"])) {
  92. $input->rows($modifiers["rows"]);
  93. }
  94.  
  95. if (isset($modifiers["cols"])) {
  96. $input->cols($modifiers["cols"]);
  97. }
  98.  
  99. if (isset($modifiers["class"])) {
  100. $input->class[] = $modifiers["class"];
  101. }
  102.  
  103. if (isset($modifiers["style"])) {
  104. $input->style($modifiers["style"]);
  105. }
  106.  
  107. echo $input;
  108. }
  109.  
  110.  
  111.  
  112. public static function macroLabel($content)
  113. {
  114. list($name, $modifiers) = self::fetchNameAndModifiers($content);
  115. return "Nette\Templates\FormMacros::label($name, $modifiers)";
  116. }
  117.  
  118.  
  119.  
  120. public static function label($name, $modifiers = array())
  121. {
  122. $label = self::$form[$name]->getLabel();
  123.  
  124. if (isset($modifiers["text"])) {
  125. $label->setText($modifiers["text"]);
  126. }
  127.  
  128. if (isset($modifiers["class"])) {
  129. $label->class[] = $modifiers["class"];
  130. }
  131.  
  132. if (isset($modifiers["style"])) {
  133. $label->style($modifiers["style"]);
  134. }
  135.  
  136. echo $label;
  137. }
  138.  
  139.  
  140.  
  141. public static function macroErrors($content) {
  142. return "Nette\Templates\FormMacros::errors()";
  143. }
  144.  
  145.  
  146.  
  147. public static function errors() {
  148. if (self::$form->getErrors()) {
  149. echo "<ul class=\"errors\">\n";
  150. foreach (self::$form->getErrors() as $error) {
  151. echo "\t<li class=\"error\">". $error ."</li>\n";
  152. }
  153. echo "</ul>\n";
  154. }
  155. }
  156.  
  157.  
  158.  
  159. private static function fetchNameAndModifiers($code)
  160. {
  161. $name = LatteFilter::fetchToken($code);
  162. $latte = new LatteMacros();
  163. $modifiers = $latte->formatArray($code);
  164.  
  165. $name = String::startsWith($name, '$') ? $name : "'$name'";
  166. $modifiers = $modifiers ?: "array()";
  167.  
  168. return array($name, $modifiers);
  169. }
  170. }
Add Comment
Please, Sign In to add comment