Created
February 18, 2012 15:20
-
-
Save hrach/1859761 to your computer and use it in GitHub Desktop.
FormMacros old for Nette 1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Nette\Templates; | |
use Nette; | |
use Nette\String; | |
use Nette\Forms\Form; | |
/** | |
* Form macros | |
* | |
* @author Jan Marek, Jan Tvrdík | |
* @license MIT | |
*/ | |
class FormMacros extends Nette\Object | |
{ | |
/** @var Nette\Forms\Form */ | |
private static $form; | |
/** @var int */ | |
private static $containerLevel = 0; | |
public function __construct() | |
{ | |
throw new \InvalidStateException("Static class."); | |
} | |
public static function register() | |
{ | |
LatteMacros::$defaultMacros["form"] = '<?php %Nette\Templates\FormMacros::macroBegin% ?>'; | |
LatteMacros::$defaultMacros["input"] = '<?php %Nette\Templates\FormMacros::macroInput% ?>'; | |
LatteMacros::$defaultMacros["label"] = '<?php %Nette\Templates\FormMacros::macroLabel% ?>'; | |
LatteMacros::$defaultMacros["/form"] = '<?php Nette\Templates\FormMacros::end() ?>'; | |
LatteMacros::$defaultMacros["formContainer"] = '<?php %Nette\Templates\FormMacros::macroBeginContainer% ?>'; | |
LatteMacros::$defaultMacros["/formContainer"] = '<?php Nette\Templates\FormMacros::endContainer() ?>'; | |
LatteMacros::$defaultMacros["formErrors"] = '<?php foreach ($formErrors as $error): ?><div class="message-error"><?php echo Nette\Templates\TemplateHelpers::escapeHtml($error) ?></div><?php endforeach; ?>'; | |
} | |
public static function macroBegin($content) | |
{ | |
list($name, $modifiers) = self::fetchNameAndModifiers($content); | |
return "\$formErrors = Nette\Templates\FormMacros::begin($name, \$control, $modifiers)->getErrors()"; | |
} | |
public static function macroBeginContainer($content) | |
{ | |
$name = LatteFilter::formatString(LatteFilter::fetchToken($content)); | |
return 'Nette\Templates\FormMacros::beginContainer(' . $name . ')'; | |
} | |
public static function beginContainer($name) | |
{ | |
$container = self::$form[$name]; | |
if (!$container instanceof Nette\Forms\Container) { | |
throw new \InvalidArgumentException(); | |
} | |
self::$form = $container; | |
self::$containerLevel++; | |
} | |
public static function endContainer() | |
{ | |
if (self::$containerLevel < 1) { | |
throw new \InvalidStateException('Trying to close container which is not open.'); | |
} | |
self::$form = self::$form->getParent(); | |
self::$containerLevel--; | |
} | |
public static function begin($form, $control, $modifiers = array()) | |
{ | |
if ($form instanceof Form) { | |
self::$form = $form; | |
} else { | |
if (substr($form, -4) !== 'Form') $form .= 'Form'; | |
self::$form = $control[$form]; | |
} | |
if (isset($modifiers["class"])) { | |
self::$form->getElementPrototype()->class[] = $modifiers["class"]; | |
} | |
self::$form->render("begin"); | |
return self::$form; | |
} | |
public static function end() | |
{ | |
if (self::$containerLevel > 0) { | |
throw new \InvalidStateException('There are some unclosed containers.'); | |
} | |
self::$form->render("end"); | |
} | |
public static function macroInput($content) | |
{ | |
list($name, $modifiers) = self::fetchNameAndModifiers($content); | |
return "Nette\Templates\FormMacros::input($name, $modifiers)"; | |
} | |
public static function input($name, $modifiers = array()) | |
{ | |
$input = self::$form[$name]->getControl(); | |
if (isset($modifiers["size"])) { | |
$input->size($modifiers["size"]); | |
} | |
if (isset($modifiers["rows"])) { | |
$input->rows($modifiers["rows"]); | |
} | |
if (isset($modifiers["cols"])) { | |
$input->cols($modifiers["cols"]); | |
} | |
if (isset($modifiers["class"])) { | |
$input->class[] = $modifiers["class"]; | |
} | |
if (isset($modifiers["style"])) { | |
$input->style($modifiers["style"]); | |
} | |
if (isset($modifiers['placeholder'])) { | |
$input->placeholder($modifiers['placeholder']); | |
} | |
if (isset($modifiers['maxlength'])) { | |
$input->maxlength($modifiers['maxlength']); | |
} | |
if (isset($modifiers['tabindex'])) { | |
$input->tabindex($modifiers['tabindex']); | |
} | |
if (isset($modifiers['caption'])) { | |
$input->value($modifiers['caption']); | |
} | |
if (isset($modifiers['readonly'])) { | |
$input->readonly(TRUE); | |
} | |
echo $input; | |
} | |
public static function macroLabel($content) | |
{ | |
list($name, $modifiers) = self::fetchNameAndModifiers($content); | |
return "Nette\Templates\FormMacros::label($name, $modifiers)"; | |
} | |
public static function label($name, $modifiers = array()) | |
{ | |
$label = self::$form[$name]->getLabel(); | |
if (isset($modifiers["text"])) { | |
$label->setText($modifiers["text"]); | |
} | |
if (isset($modifiers["class"])) { | |
$label->class[] = $modifiers["class"]; | |
} | |
if (isset($modifiers["style"])) { | |
$label->style($modifiers["style"]); | |
} | |
// podpora pro volitelnost | |
if (!self::$form[$name]->getOption('required')) { | |
$label->class[] = 'optional'; | |
if (isset($modifiers['optional']) && $modifiers['optional']) { | |
$label->setHtml(htmlspecialchars($label->getText(), ENT_QUOTES) . '<small>nepovinné</small>'); | |
} | |
} | |
echo $label; | |
} | |
// helper | |
private static function fetchNameAndModifiers($code) | |
{ | |
$name = LatteFilter::fetchToken($code); | |
$modifiers = LatteFilter::formatArray($code); | |
$name = LatteFilter::formatString($name); | |
$modifiers = $modifiers ?: 'array()'; | |
return array($name, $modifiers); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment