Index: inc/admin/class.dc.form.php
===================================================================
--- inc/admin/class.dc.form.php	(revision 1056)
+++ inc/admin/class.dc.form.php	(revision 1058)
@@ -1,56 +1,76 @@
 <?php
-
-
-class dcFormNode extends Twig_Node {
-    public function __construct($name, Twig_NodeInterface $body, $lineno, $tag = null)
-    {
-        parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag);
-    }
-
-    /**
-     * Compiles the node to PHP.
-     *
-     * @param Twig_Compiler A Twig_Compiler instance
-     */
-    public function compile(Twig_Compiler $compiler)
-    {
-        $compiler
-            ->addDebugInfo($this)
-            ->write("\$context['dc_form']->beginForm('".
-            	 $this->getAttribute('name')."');\n")
-            ->subcompile($this->getNode('body'))
-            ->write("\$context['dc_form']->renderHiddenWidgets();\n")
-            ->write("\$context['dc_form']->endForm();\n")
-        ;
-    }
-
-}
-
-class dcFormTokenParser extends Twig_TokenParser {
-
-	public function parse(Twig_Token $token) {
-        $lineno = $token->getLine();
-        $stream = $this->parser->getStream();
-        $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
-        $stream->expect(Twig_Token::BLOCK_END_TYPE);
-        $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
-        $stream->expect(Twig_Token::BLOCK_END_TYPE);
-        return new dcFormNode ($name, $body, $token->getLine(), $this->getTag());
-	}
-
+# -- BEGIN LICENSE BLOCK ---------------------------------------
+#
+# This file is part of Dotclear 2.
+#
+# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
+# Licensed under the GPL version 2.0 license.
+# See LICENSE file or
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+#
+# -- END LICENSE BLOCK -----------------------------------------
+if (!defined('DC_RC_PATH')) { return; }
+
+/**
+ * Template form node
+ */
+class dcFormNode extends Twig_Node
+{
+	public function __construct($name,Twig_NodeInterface $body,$lineno,$tag=null)
+	{
+		parent::__construct(array('body' => $body),array('name' => $name),$lineno,$tag);
+	}
+	
+	/**
+	* Compiles the node to PHP.
+	*
+	* @param Twig_Compiler A Twig_Compiler instance
+	*/
+	public function compile(Twig_Compiler $compiler)
+	{
+		$compiler
+			->addDebugInfo($this)
+			->write("\$context['dc_form']->beginForm('".
+				$this->getAttribute('name')."');\n")
+			->subcompile($this->getNode('body'))
+			->write("\$context['dc_form']->renderHiddenWidgets();\n")
+			->write("\$context['dc_form']->endForm();\n")
+		;
+	}
+}
+
+/**
+ * Template form token parser
+ */
+class dcFormTokenParser extends Twig_TokenParser
+{
+	public function parse(Twig_Token $token)
+	{
+		$lineno = $token->getLine();
+		$stream = $this->parser->getStream();
+		$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
+		$stream->expect(Twig_Token::BLOCK_END_TYPE);
+		$body = $this->parser->subparse(array($this,'decideBlockEnd'),true);
+		$stream->expect(Twig_Token::BLOCK_END_TYPE);
+		
+		return new dcFormNode($name,$body,$token->getLine(),$this->getTag());
+	}
+	
 	public function decideBlockEnd(Twig_Token $token)
-    {
-        return $token->test('endform');
-    }
-
-
-    public function getTag()
-    {
-        return 'form';
-    }
-
-}
-
-class dcFormExtension extends Twig_Extension {
+	{
+		return $token->test('endform');
+	}
+	
+	public function getTag()
+	{
+		return 'form';
+	}
+}
+
+/**
+ * Template form extension
+ */
+class dcFormExtension extends Twig_Extension
+{
 	protected $template;
 	protected $tpl;
@@ -60,6 +80,7 @@
 	protected $forms;
 	protected $currentForm;
-
-	public function __construct($core){
+	
+	public function __construct($core)
+	{
 		$this->core = $core;
 		$this->tpl = 'form_layout.html.twig';
@@ -67,48 +88,70 @@
 		$this->currentForm = null;
 	}
-
-    public function initRuntime(Twig_Environment $environment)
-    {
-    	$this->twig = $environment;
+	
+	public function initRuntime(Twig_Environment $environment)
+	{
+		$this->twig = $environment;
 		$this->template = $this->twig->loadTemplate($this->tpl);
-    	$this->blocks = $this->template->getBlocks();
-    }
-
-    public function getGlobals() {
-    	return array('dc_form' => $this);
-    }
-
-	public function getFunctions() {
+		$this->blocks = $this->template->getBlocks();
+	}
+	
+	public function getGlobals()
+	{
+		return array('dc_form' => $this);
+	}
+	
+	public function getFunctions()
+	{
 		return array(
-			'form_field' => new Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))),
-            '_form_is_choice_group'    => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))),
-            '_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html')))
-
+			'form_field' => new Twig_Function_Method(
+				$this,
+				'renderWidget',
+				array('is_safe' => array('html'))
+			),
+			'_form_is_choice_group' => new Twig_Function_Method(
+				$this,
+				'isChoiceGroup',
+				array('is_safe' => array('html'))
+			),
+			'_form_is_choice_selected' => new Twig_Function_Method(
+				$this,
+				'isChoiceSelected',
+				array('is_safe' => array('html'))
+			)
 		);
 	}
-
-	public function isChoiceGroup($choice) {
+	
+	public function isChoiceGroup($choice)
+	{
 		return is_array($choice);
 	}
-	public function isChoiceSelected($choice,$value) {
+	
+	public function isChoiceSelected($choice,$value)
+	{
 		return $choice == $value;
-
-	}
-
-	public function getTokenParsers() {
+	}
+	
+	public function getTokenParsers()
+	{
 		return array(new dcFormTokenParser());
 	}
-
-	public function renderWidget($name,$attributes=array()) {
+	
+	public function renderWidget($name,$attributes=array())
+	{
 		$field = $this->currentForm->$name;
 		if ($field) {
 			echo $this->template->renderBlock(
 				$field->getWidgetBlock(),
-				array_merge($field->getAttributes(),array('attr'=>$attributes)),
-				$this->blocks);
-		}
-	}
-
-	public function renderHiddenWidgets() {
+				array_merge(
+					$field->getAttributes(),
+					array('attr' => $attributes)
+				),
+				$this->blocks
+			);
+		}
+	}
+
+	public function renderHiddenWidgets()
+	{
 		foreach ($this->currentForm->getHiddenFields() as $h) {
 			$this->renderWidget($h->getName());
@@ -116,32 +159,46 @@
 	}
 
-	public function getName() {
+	public function getName()
+	{
 		return 'dc_form';
 	}
 
-	public function addForm(dcForm $form) {
-		$this->forms[$form->getName()]=$form;
-	}
-
-	public function beginForm($name) {
+	public function addForm(dcForm $form)
+	{
+		$this->forms[$form->getName()] = $form;
+	}
+
+	public function beginForm($name)
+	{
 		if (isset($this->forms[$name])) {
 			$this->currentForm = $this->forms[$name];
 			$this->currentForm->begin();
-		} else {
-			throw new Twig_Error_Runtime("Form '".$name."' does not exist");
-		}
-	}
-	public function endForm() {
+		}
+		else {
+			throw new Twig_Error_Runtime(sprintf(
+				'Form "%s" does not exist',
+				$name
+			));
+		}
+	}
+	
+	public function endForm()
+	{
 		$this->currentForm->end();
 		$this->currentForm = null;
 	}
-
-}
-
+}
+
+/**
+ * Template form exception
+ */
 class InvalidFieldException extends Exception {
 }
 
-
-class dcForm {
+/**
+ * Template form
+ */
+class dcForm
+{
 	protected $name;
 	protected $core;
@@ -153,9 +210,14 @@
 	protected $errors;
 	
-	private function addNonce() {
-		$this->addField(new dcFieldHidden(array('xd_check'), $this->core->getNonce()));
-	}
-	
-	public function __construct($core,$name,$action, $method='POST') {
+	private function addNonce()
+	{
+		$this->addField(
+			new dcFieldHidden(array('xd_check'),
+			$this->core->getNonce())
+		);
+	}
+	
+	public function __construct($core,$name,$action,$method='POST')
+	{
 		$this->core = $core;
 		$this->name = $name;
@@ -171,60 +233,69 @@
 		}
 	}
-
-	public function getName() {
+	
+	public function getName()
+	{
 		return $this->name;
 	}
-
-	public function getErrors() {
+	
+	public function getErrors()
+	{
 		return $this->errors;
 	}
 	
-	public function addField(dcField $f) {
+	public function addField(dcField $f)
+	{
+		if ($f instanceof dcFieldAction) {
+			$this->submitfields[$f->getName()] = $f;
+		}
+		if ($f instanceof dcFieldHidden) {
+			$this->hiddenfields[$f->getName()] = $f;
+		}
+		$this->fields[$f->getName()] = $f;
 		
-		if ($f instanceof dcFieldAction) {
-			$this->submitfields[$f->getName()]=$f;
-		}
-		if ($f instanceof dcFieldHidden) {
-			$this->hiddenfields[$f->getName()]=$f;
-		}
-
-		$this->fields[$f->getName()]=$f;
 		return $this;
 	}
-
-	public function begin() {
-		echo '<form method="'.$this->method.'" action="'.$this->action.'">';
-	}
-
-	public function end() {
+	
+	public function begin()
+	{
+		echo sprintf(
+			'<form method="%s" action="%s">',
+			$this->method,
+			$this->action
+		);
+	}
+	
+	public function end()
+	{
 		echo '</form>';
 	}
-
-
-    public function __isset($name) {
+	
+	public function __isset($name)
+	{
 		return isset($this->fields[$name]);
-    }
-
-    public function __get($name) {
-		if (isset($this->fields[$name])) {
-			return $this->fields[$name];
-		} else {
-			return null;
-		}
-    }
-
-    public function __set($name,$value) {
+	}
+	
+	public function __get($name)
+	{
+		return isset($this->fields[$name]) ? 
+			$this->fields[$name] : null;
+	}
+	
+	public function __set($name,$value)
+	{
 		if (isset($this->fields[$name])) {
 			$this->fields[$name]->setAttribute('value',$value);
 		}
-    }
-
-	public function isSubmitted() {
-		$from = ($this->method == 'POST')?$_POST:$_GET;
+	}
+	
+	public function isSubmitted()
+	{
+		$from = $this->method == 'POST' ? $_POST : $_GET;
 		echo "form fields :\n";
 	}
 	
-	public function setup() {
-		$from = ($this->method=='POST')?$_POST:$_GET;
+	public function setup()
+	{
+		$from = $this->method == 'POST' ? $_POST : $_GET;
 		foreach ($this->fields as $f) {
 			$f->setup($from);
@@ -232,5 +303,5 @@
 		foreach ($this->submitfields as $f) {
 			if ($f->isDefined()) {
-				$ret = call_user_func ($f->getAction(),$this);
+				$ret = call_user_func($f->getAction(),$this);
 				return;
 			}
@@ -238,21 +309,27 @@
 	}
 	
-	public function check() {
+	public function check()
+	{
 		foreach ($this->fields as $f) {
 			try {
 				$f->check();
-			} catch (InvalidFieldException $e) {
-				$this->errors[]=$e->getMessage();
 			}
-		}
-	}
-	
-	public function getHiddenFields() {
+			catch (InvalidFieldException $e) {
+				$this->errors[] = $e->getMessage();
+			}
+		}
+	}
+	
+	public function getHiddenFields()
+	{
 		return $this->hiddenfields;
 	}
-	
-}
-
-abstract class dcField {
+}
+
+/**
+ * Template form field
+ */
+abstract class dcField
+{
 	protected $attributes;
 	protected $name;
@@ -261,14 +338,17 @@
 	protected $defined;
 	
-	protected function getNID($nid) {
+	protected function getNID($nid)
+	{
 		if (is_array($nid)) {
 			$this->name = $nid[0];
 			$this->id = !empty($nid[1]) ? $nid[1] : null;
-		} else {
+		}
+		else {
 			$this->id = $this->name = $nid;
 		}
 	}
-
-	public function __construct($name, $value, $attributes = array()) {
+	
+	public function __construct($name,$value,$attributes=array())
+	{
 		$this->getNID($name);
 		$this->attributes = $attributes;
@@ -278,30 +358,40 @@
 		$this->attributes['value'] = $this->value;
 		$this->defined = false;
-
-	}
-
+	}
+	
+	public function __toString()
+	{
+		return $this->value;
+	}
+	
 	abstract public function getWidgetBlock();
-
-	public function setAttribute ($name,$value) {
+	
+	public function setAttribute($name,$value)
+	{
 		$this->attributes[$name] = $value;
 	}
-
-	public function getAttributes() {
+	
+	public function getAttributes()
+	{
 		return $this->attributes;
-
-	}
-
-	public function getName(){
+	}
+	
+	public function getName()
+	{
 		return $this->name;
 	}
-
-	public function check() {
+	
+	public function check()
+	{
 		if (!$this->defined && $this->attributes['defined']) {
-			throw new InvalidFieldException(sprintf('Field "%s" is mandatory'),$this->attributes['label']);
-		}
-
-	}
-	
-	public function setup($from) {
+			throw new InvalidFieldException(sprintf(
+				'Field "%s" is mandatory',
+				$this->attributes['label'])
+			);
+		}
+	}
+	
+	public function setup($from)
+	{
 		if (isset($from[$this->id])) {
 			$this->value = $from[$this->id];
@@ -310,78 +400,117 @@
 	}
 	
-	public function isDefined() {
+	public function isDefined()
+	{
 		return $this->defined;
 	}
-	
-}
-
-
-class dcFieldText extends dcField {
-
-	public function getWidgetBlock() {
-		return "field_text";
-	}
-
-}
-
-class dcFieldTextArea extends dcField {
-
-	public function getWidgetBlock() {
+}
+
+/**
+ * Template form field of type "password"
+ */
+class dcFieldPassword extends dcField
+{
+	public function getWidgetBlock()
+	{
+		return "field_password";
+	}
+}
+
+/**
+ * Template form field of type "text"
+ */
+class dcFieldText extends dcField
+{
+	public function getWidgetBlock()
+	{
+	return "field_text";
+	}
+}
+
+/**
+ * Template form field of type "textarea"
+ */
+class dcFieldTextArea extends dcField
+{
+	public function getWidgetBlock()
+	{
 		return "field_textarea";
 	}
-
-}
-
-class dcFieldHidden extends dcField {
-
-	public function getWidgetBlock() {
+}
+
+/**
+ * Template form field of type "hidden"
+ */
+class dcFieldHidden extends dcField
+{
+	public function getWidgetBlock()
+	{
 		return "field_hidden";
 	}
-
-}
-
-class dcFieldCheckbox extends dcField {
-
-	public function getWidgetBlock() {
+}
+
+/**
+ * Template form field of type "checkbox"
+ */
+class dcFieldCheckbox extends dcField
+{
+	public function getWidgetBlock()
+	{
 		return "field_checkbox";
 	}
-
-}
-
-abstract class dcFieldAction extends dcField {
+}
+
+/**
+ * Template form action
+ */
+abstract class dcFieldAction extends dcField
+{
 	protected $action;
-	public function __construct($name, $value, $attributes = array()) {
-		parent::__construct($name, $value, $attributes);
+	
+	public function __construct($name,$value,$attributes=array())
+	{
+		parent::__construct($name,$value,$attributes);
+		
 		if (isset($attributes['action'])) {
 			$this->action = $attributes['action'];
 		}
 	}
-	public function getAction() {
+	
+	public function getAction()
+	{
 		return $this->action;
 	}
 }
 
-class dcFieldSubmit extends dcFieldAction {
-
-	public function getWidgetBlock() {
+/**
+ * Template form field of type "submit"
+ */
+class dcFieldSubmit extends dcFieldAction
+{
+	public function getWidgetBlock()
+	{
 		return "field_submit";
 	}
-
-}
-
-class dcFieldCombo extends dcField {
+}
+
+/**
+ * Template form field of type "combo"
+ */
+class dcFieldCombo extends dcField
+{
 	protected $options;
-
-	public function __construct($name, $value, $options, $attributes = array()) {
+	
+	public function __construct($name,$value,$options,$attributes=array())
+	{
 		$this->options = $options;
 		parent::__construct($name,$value,$attributes);
 		$this->attributes['options']=$options;
 	}
-
-	public function getWidgetBlock() {
+	
+	public function getWidgetBlock()
+	{
 		return "field_combo";
 	}
 
 }
-
 ?>
