2011-02-18 19:17:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form\Renderer;
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
use Symfony\Component\Form\FormInterface;
|
2011-03-24 09:55:12 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Theme\FormThemeInterface;
|
2011-03-24 13:27:21 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Theme\FormThemeFactoryInterface;
|
2011-02-18 19:17:44 +00:00
|
|
|
|
2011-03-26 11:25:47 +00:00
|
|
|
class ThemeRenderer implements FormRendererInterface, \ArrayAccess, \IteratorAggregate
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-03-21 19:56:33 +00:00
|
|
|
private $form;
|
2011-02-18 19:17:44 +00:00
|
|
|
|
2011-03-24 13:27:21 +00:00
|
|
|
private $block;
|
|
|
|
|
|
|
|
private $themeFactory;
|
2011-02-18 19:17:44 +00:00
|
|
|
|
2011-03-24 09:55:12 +00:00
|
|
|
private $theme;
|
2011-02-18 19:17:44 +00:00
|
|
|
|
2011-03-24 21:20:54 +00:00
|
|
|
private $vars;
|
2011-02-18 19:17:44 +00:00
|
|
|
|
2011-03-19 13:31:25 +00:00
|
|
|
/**
|
2011-03-21 19:56:33 +00:00
|
|
|
* Is the form attached to this renderer rendered?
|
2011-03-19 13:31:25 +00:00
|
|
|
*
|
|
|
|
* Rendering happens when either the widget or the row method was called.
|
|
|
|
* Row implicitly includes widget, however certain rendering mechanisms
|
|
|
|
* have to skip widget rendering when a row is rendered.
|
|
|
|
*
|
2011-03-24 13:27:21 +00:00
|
|
|
* @var Boolean
|
2011-03-19 13:31:25 +00:00
|
|
|
*/
|
2011-02-28 16:01:11 +00:00
|
|
|
private $rendered = false;
|
|
|
|
|
2011-03-24 21:20:54 +00:00
|
|
|
private $children = array();
|
|
|
|
|
|
|
|
public function __construct(FormInterface $form, FormThemeFactoryInterface $themeFactory, $template = null, ThemeRenderer $parent = null)
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-03-24 21:20:54 +00:00
|
|
|
$this->form = $form;
|
2011-03-24 13:27:21 +00:00
|
|
|
$this->themeFactory = $themeFactory;
|
2011-03-24 21:20:54 +00:00
|
|
|
$this->parent = $parent;
|
|
|
|
$this->vars = new RendererVarBag();
|
2011-03-24 13:27:21 +00:00
|
|
|
|
2011-03-24 21:20:54 +00:00
|
|
|
if (null !== $template) {
|
|
|
|
$this->setTemplate($template);
|
2011-03-02 15:00:31 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 15:37:14 +00:00
|
|
|
$types = (array)$form->getTypes();
|
|
|
|
|
|
|
|
foreach ($types as $type) {
|
2011-03-24 21:20:54 +00:00
|
|
|
$type->buildRenderer($this, $form);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 21:20:54 +00:00
|
|
|
foreach ($form as $key => $child) {
|
|
|
|
$this->children[$key] = new self($child, $themeFactory, null, $parent);
|
|
|
|
}
|
2011-03-25 15:37:14 +00:00
|
|
|
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$type->buildRendererBottomUp($this, $form);
|
|
|
|
}
|
2011-03-18 11:50:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 13:27:21 +00:00
|
|
|
public function setTemplate($template)
|
|
|
|
{
|
|
|
|
$this->setTheme($this->themeFactory->create($template));
|
|
|
|
}
|
|
|
|
|
2011-03-24 09:55:12 +00:00
|
|
|
public function setTheme(FormThemeInterface $theme)
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-03-24 09:55:12 +00:00
|
|
|
$this->theme = $theme;
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 09:55:12 +00:00
|
|
|
public function getTheme()
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-03-24 09:55:12 +00:00
|
|
|
return $this->theme;
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 13:27:21 +00:00
|
|
|
public function setBlock($block)
|
|
|
|
{
|
|
|
|
$this->block = $block;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBlock()
|
|
|
|
{
|
|
|
|
return $this->block;
|
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function setVar($name, $value)
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-03-24 21:20:54 +00:00
|
|
|
$this->vars[$name] = $value;
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 21:08:08 +00:00
|
|
|
public function setAttribute($name, $value)
|
|
|
|
{
|
|
|
|
// handling through $this->changes not necessary
|
|
|
|
$this->vars['attr'][$name] = $value;
|
|
|
|
}
|
|
|
|
|
2011-02-25 00:11:53 +00:00
|
|
|
public function hasVar($name)
|
|
|
|
{
|
|
|
|
return array_key_exists($name, $this->vars);
|
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getVar($name)
|
2011-02-19 12:46:32 +00:00
|
|
|
{
|
|
|
|
// TODO exception handling
|
2011-03-24 21:08:08 +00:00
|
|
|
if (isset($this->vars[$name])) {
|
|
|
|
return $this->vars[$name];
|
|
|
|
}
|
|
|
|
return null;
|
2011-02-19 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2011-02-28 15:50:23 +00:00
|
|
|
public function getVars()
|
|
|
|
{
|
2011-03-24 22:54:56 +00:00
|
|
|
$this->initialize();
|
|
|
|
|
2011-02-28 15:50:23 +00:00
|
|
|
return $this->vars;
|
|
|
|
}
|
|
|
|
|
2011-02-28 16:01:11 +00:00
|
|
|
public function isRendered()
|
|
|
|
{
|
|
|
|
return $this->rendered;
|
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getWidget(array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-02-28 16:01:11 +00:00
|
|
|
$this->rendered = true;
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->render('widget', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getErrors(array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->render('errors', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getRow(array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-03-19 13:31:25 +00:00
|
|
|
$this->rendered = true;
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->render('row', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-28 15:50:23 +00:00
|
|
|
public function getRest(array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-02-28 15:50:23 +00:00
|
|
|
return $this->render('rest', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-21 19:56:33 +00:00
|
|
|
* Renders the label of the given form
|
2011-02-18 19:17:44 +00:00
|
|
|
*
|
2011-03-21 19:56:33 +00:00
|
|
|
* @param FormInterface $form The form to render the label for
|
2011-03-24 13:27:21 +00:00
|
|
|
* @param array $params Additional variables passed to the block
|
2011-02-18 19:17:44 +00:00
|
|
|
*/
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getLabel($label = null, array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
|
|
|
if (null !== $label) {
|
2011-02-21 17:16:40 +00:00
|
|
|
$vars['label'] = $label;
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->render('label', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 13:40:34 +00:00
|
|
|
public function getEnctype()
|
|
|
|
{
|
2011-03-18 16:03:12 +00:00
|
|
|
return $this->render('enctype', $this->vars);
|
2011-03-17 13:40:34 +00:00
|
|
|
}
|
|
|
|
|
2011-03-24 13:27:21 +00:00
|
|
|
protected function render($part, array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-03-24 13:27:21 +00:00
|
|
|
return $this->theme->render($this->block, $part, array_replace(
|
2011-02-21 17:16:40 +00:00
|
|
|
$this->vars,
|
|
|
|
$vars
|
2011-02-18 19:17:44 +00:00
|
|
|
));
|
|
|
|
}
|
2011-03-24 22:54:56 +00:00
|
|
|
|
2011-03-24 21:20:54 +00:00
|
|
|
public function getParent()
|
|
|
|
{
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getChildren()
|
|
|
|
{
|
|
|
|
return $this->children;
|
|
|
|
}
|
|
|
|
|
2011-03-02 21:56:20 +00:00
|
|
|
public function offsetGet($name)
|
|
|
|
{
|
2011-03-26 08:42:45 +00:00
|
|
|
$this->initialize();
|
|
|
|
return $this->vars['fields'][$name];
|
2011-03-02 21:56:20 +00:00
|
|
|
}
|
2011-03-24 22:54:56 +00:00
|
|
|
|
2011-03-02 21:56:20 +00:00
|
|
|
public function offsetExists($name)
|
|
|
|
{
|
2011-03-26 08:42:45 +00:00
|
|
|
$this->initialize();
|
|
|
|
return isset($this->vars['fields'][$name]);
|
2011-03-02 21:56:20 +00:00
|
|
|
}
|
2011-03-24 22:54:56 +00:00
|
|
|
|
2011-03-02 21:56:20 +00:00
|
|
|
public function offsetSet($name, $value)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('Not supported');
|
|
|
|
}
|
2011-03-24 22:54:56 +00:00
|
|
|
|
2011-03-02 21:56:20 +00:00
|
|
|
public function offsetUnset($name)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('Not supported');
|
|
|
|
}
|
2011-03-26 11:25:47 +00:00
|
|
|
|
|
|
|
public function getIterator()
|
|
|
|
{
|
2011-03-26 11:28:46 +00:00
|
|
|
$this->initialize();
|
|
|
|
|
2011-03-26 11:25:47 +00:00
|
|
|
if (isset($this->vars['fields'])) {
|
|
|
|
$this->rendered = true;
|
|
|
|
return new \ArrayIterator($this->vars['fields']);
|
|
|
|
}
|
|
|
|
return new \ArrayIterator(array());
|
|
|
|
}
|
2011-03-24 21:08:08 +00:00
|
|
|
}
|