1: <?php
2: /**
3: * CInputWidget class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CInputWidget is the base class for widgets that collect user inputs.
13: *
14: * CInputWidget declares properties common among input widgets. An input widget
15: * can be associated with a data model and an attribute, or a name and a value.
16: * If the former, the name and the value will be generated automatically.
17: * Child classes may use {@link resolveNameID} and {@link hasModel}.
18: *
19: * @author Qiang Xue <qiang.xue@gmail.com>
20: * @package system.web.widgets
21: * @since 1.0
22: */
23: abstract class CInputWidget extends CWidget
24: {
25: /**
26: * @var CModel the data model associated with this widget.
27: */
28: public $model;
29: /**
30: * @var string the attribute associated with this widget.
31: * The name can contain square brackets (e.g. 'name[1]') which is used to collect tabular data input.
32: */
33: public $attribute;
34: /**
35: * @var string the input name. This must be set if {@link model} is not set.
36: */
37: public $name;
38: /**
39: * @var string the input value
40: */
41: public $value;
42: /**
43: * @var array additional HTML options to be rendered in the input tag
44: */
45: public $htmlOptions=array();
46:
47:
48: /**
49: * @return array the name and the ID of the input.
50: * @throws CException in case input name and ID cannot be resolved.
51: */
52: protected function resolveNameID()
53: {
54: if($this->name!==null)
55: $name=$this->name;
56: elseif(isset($this->htmlOptions['name']))
57: $name=$this->htmlOptions['name'];
58: elseif($this->hasModel())
59: $name=CHtml::activeName($this->model,$this->attribute);
60: else
61: throw new CException(Yii::t('yii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>get_class($this))));
62:
63: if(($id=$this->getId(false))===null)
64: {
65: if(isset($this->htmlOptions['id']))
66: $id=$this->htmlOptions['id'];
67: else
68: $id=CHtml::getIdByName($name);
69: }
70:
71: return array($name,$id);
72: }
73:
74: /**
75: * @return boolean whether this widget is associated with a data model.
76: */
77: protected function hasModel()
78: {
79: return $this->model instanceof CModel && $this->attribute!==null;
80: }
81: }