Permalink
Browse files

No commit message

  • Loading branch information...
1 parent 840e016 commit 27cc745f2ccf245e3095b0594ffcd781bd1818bf qiang.xue committed Jun 26, 2009
@@ -1,62 +0,0 @@
-<?php
-
-abstract class CBaseFormElement extends CComponent
-{
- public $attributes=array();
- private $_visible;
-
- abstract function render();
-
- public function __toString()
- {
- return $this->render();
- }
-
- public function __get($name)
- {
- $getter='get'.$name;
- if(method_exists($this,$getter))
- return $this->$getter();
- else if(isset($this->attributes[$name]))
- return $this->attributes[$name];
- else
- throw new CException('');
- }
-
- public function __set($name,$value)
- {
- $setter='set'.$name;
- if(method_exists($this,$setter))
- $this->$setter($value);
- else
- $this->attributes[$name]=$value;
- }
-
- public function configure($config)
- {
- if(is_string($config))
- $config=require($config);
- if(is_array($config))
- {
- foreach($config as $name=>$value)
- $this->$name=$value;
- }
- }
-
- public function getVisible()
- {
- if($this->_visible===null)
- $this->_visible=$this->evaluateVisible();
- return $this->_visible;
- }
-
- public function setVisible($value)
- {
- $this->_visible=$value;
- }
-
- protected function evaluateVisible()
- {
- return true;
- }
-}
@@ -1,6 +1,24 @@
<?php
+/**
+ * CForm class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2009 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
-class CForm extends CBaseFormElement implements ArrayAccess
+/**
+ * CForm is the base class providing the common features needed by data model objects.
+ *
+ * CForm defines the basic framework for data models that need to be validated.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id$
+ * @package system.web.form
+ * @since 1.1
+ */
+class CForm extends CFormElement implements ArrayAccess
{
public $legend;
public $description; // TODO!!!
@@ -12,15 +30,14 @@ class CForm extends CBaseFormElement implements ArrayAccess
public $buttonElementClass='CFormButtonElement';
public $formElementClass='CForm';
- private $_parent;
private $_model;
private $_elements;
private $_buttons;
public function __construct($parent,$model=null)
{
- $this->_parent=$parent;
+ $this->parent=$parent;
$this->_model=$model;
}
@@ -42,7 +59,7 @@ protected function getUniqueId()
public function getSubmitMethod()
{
- return strcasecmp($this->getRootForm()->method,'get') ? 'post' : 'get';
+ return strcasecmp($this->getRoot()->method,'get') ? 'post' : 'get';
}
public function loadData()
@@ -81,33 +98,28 @@ protected function evaluateVisible()
return false;
}
- public function getParentForm()
- {
- return $this->_parent instanceof self ? $this->_parent : null;
- }
-
- public function getRootForm()
+ public function getRoot()
{
$root=$this;
- while($root->_parent instanceof self)
- $root=$root->_parent;
+ while($root->parent instanceof self)
+ $root=$root->parent;
return $root;
}
public function getOwner()
{
- $owner=$this->_parent;
+ $owner=$this->parent;
while($owner instanceof self)
- $owner=$owner->_parent;
+ $owner=$owner->parent;
return $owner;
}
public function getModel()
{
- if($this->_model!==null)
- return $this->_model;
- else if($this->_parent instanceof self)
- return $this->_parent->getModel();
+ $form=$this;
+ while($form->_model===null && $form->parent instanceof self)
+ $form=$form->parent;
+ return $form->_model;
}
public function setModel($model)
@@ -118,7 +130,7 @@ public function setModel($model)
public function getElements()
{
if($this->_elements===null)
- $this->_elements=new CFormInputCollection($this);
+ $this->_elements=new CFormElementCollection($this,false);
return $this->_elements;
}
@@ -132,7 +144,7 @@ public function setElements($elements)
public function getButtons()
{
if($this->_buttons===null)
- $this->_buttons=new CFormButtonCollection($this);
+ $this->_buttons=new CFormElementCollection($this,true);
return $this->_buttons;
}
@@ -146,10 +158,10 @@ public function setButtons($buttons)
public function render()
{
$output='';
- if(!$this->_parent instanceof self)
+ if(!$this->parent instanceof self)
$output.=$this->begin();
$output.=$this->body();
- if(!$this->_parent instanceof self)
+ if(!$this->parent instanceof self)
$output.=$this->end();
return $output;
}
@@ -225,8 +237,7 @@ public function offsetExists($offset)
*/
public function offsetGet($offset)
{
- if($this->getElements()->contains($offset))
- return $this->getElements()->itemAt($offset);
+ return $this->getElements()->itemAt($offset);
}
/**
@@ -1,27 +0,0 @@
-<?php
-
-class CFormButtonCollection extends CMap
-{
- private $_form;
-
- public function __construct($form)
- {
- parent::__construct();
- $this->_form=$form;
- }
-
- public function add($key,$value)
- {
- if(is_array($value))
- {
- $class=$this->_form->buttonElementClass;
- $value['name']=$key;
- $button=new $class($this->_form,$value);
- parent::add($key, $button);
- }
- else if($value instanceof CFormButtonElement)
- parent::add($key,$value);
- else
- throw new CException('');
- }
-}
@@ -1,35 +1,102 @@
<?php
+/**
+ * CFormButtonElement class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2009 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+/**
+ * CFormButtonElement represents a form button element.
+ *
+ * CFormButtonElement can represent the following types of button based on {@link type} property:
+ * <ul>
+ * <li>submit: a submit button generated using {@link CHtml::submitButton}</li>
+ * <li>button: a normal button generated using {@link CHtml::button}</li>
+ * <li>image: an image button generated using {@link CHtml::imageButton}</li>
+ * <li>reset: a reset button generated using {@link CHtml::resetButton}</li>
+ * <li>link: a link button generated using {@link CHtml::linkButton}</li>
+ * </ul>
+ * The {@link type} property can also be a class name or a path alias to the class. In this case,
+ * the button is generated using a widget of the specified class. Note, the widget must
+ * have a property called "name".
+ *
+ * Because CFormElement is an ancestor class of CFormButtonElement, a value assigned to a non-existing property will be
+ * stored in {@link attributes} which will be passed as HTML attribute values to the {@link CHtml} method
+ * generating the button or initial values of the widget properties.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id$
+ * @package system.web.form
+ * @since 1.1
+ */
class CFormButtonElement extends CFormElement
{
+ /**
+ * @var array Core button types (alias=>CHtml method name)
+ */
public static $coreTypes=array(
'submit'=>'submitButton',
'button'=>'button',
'image'=>'imageButton',
'reset'=>'resetButton',
'link'=>'linkButton',
);
- public $name;
+
+ /**
+ * @var string the type of this button. This can be a class name, a path alias of a class name,
+ * or a button type alias (submit, button, image, reset, or link).
+ */
public $type;
+ /**
+ * @var string name of this button
+ */
+ public $name;
+ /**
+ * @var string the label of this button. This property is ignored when a widget is used to generate the button.
+ */
public $label;
private $_on;
+ /**
+ * Evaluates the visibility of this element.
+ * This method will check the {@link on} property to see if
+ * the model is in a scenario that should have this string displayed.
+ * @return boolean whether this element is visible.
+ */
protected function evaluateVisible()
{
- return empty($this->on) || in_array($this->getForm()->getModel()->getScenario(),$this->on);
+ return empty($this->_on) || in_array($this->getParent()->getModel()->getScenario(),$this->_on);
}
+ /**
+ * Returns a value indicating under which scenarios this button is visible.
+ * If the value is empty, it means the button is visible under all scenarios.
+ * Otherwise, only when the model is in the scenario whose name can be found in
+ * this value, will the button be visible. See {@link CModel::scenario} for more
+ * information about model scenarios.
+ * @return string scenario names separated by commas. Defaults to null.
+ */
public function getOn()
{
return $this->_on;
}
+ /**
+ * @param string scenario names separated by commas.
+ */
public function setOn($value)
{
$this->_on=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY);
}
+ /**
+ * Returns this button.
+ * @return string the rendering result
+ */
public function render()
{
$attributes=$this->attributes;
@@ -52,7 +119,7 @@ public function render()
{
$attributes['name']=$this->name;
ob_start();
- $this->getForm()->getOwner()->widget($this->type, $attributes);
+ $this->getParent()->getOwner()->widget($this->type, $attributes);
return ob_get_clean();
}
}
Oops, something went wrong.

0 comments on commit 27cc745

Please sign in to comment.