CJuiInputWidget.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * CJuiInputWidget class file.
  4. *
  5. * @author Sebastian Thierer <sebathi@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('zii.widgets.jui.CJuiWidget');
  11. /**
  12. * CJuiInputWidget is the base class for JUI widgets that can collect user input.
  13. *
  14. * @author Sebastian Thierer <sebathi@gmail.com>
  15. * @package zii.widgets.jui
  16. * @since 1.1
  17. */
  18. abstract class CJuiInputWidget extends CJuiWidget
  19. {
  20. /**
  21. * @var CModel the data model associated with this widget.
  22. */
  23. public $model;
  24. /**
  25. * @var string the attribute associated with this widget.
  26. * The name can contain square brackets (e.g. 'name[1]') which is used to collect tabular data input.
  27. */
  28. public $attribute;
  29. /**
  30. * @var string the input name. This must be set if {@link model} is not set.
  31. */
  32. public $name;
  33. /**
  34. * @var string the input value.
  35. */
  36. public $value;
  37. /**
  38. * Resolves name and ID of the input. Source property of the name and/or source property of the attribute
  39. * could be customized by specifying first and/or second parameter accordingly.
  40. * @param string $nameProperty class property name which holds element name to be used. This parameter
  41. * is available since 1.1.14.
  42. * @param string $attributeProperty class property name which holds model attribute name to be used. This
  43. * parameter is available since 1.1.14.
  44. * @return array name and ID of the input: array('name','id').
  45. * @throws CException in case model and attribute property or name property cannot be resolved.
  46. */
  47. protected function resolveNameID($nameProperty='name',$attributeProperty='attribute')
  48. {
  49. if($this->$nameProperty!==null)
  50. $name=$this->$nameProperty;
  51. elseif(isset($this->htmlOptions[$nameProperty]))
  52. $name=$this->htmlOptions[$nameProperty];
  53. elseif($this->hasModel())
  54. $name=CHtml::activeName($this->model,$this->$attributeProperty);
  55. else
  56. throw new CException(Yii::t('zii','{class} must specify "model" and "{attribute}" or "{name}" property values.',
  57. array('{class}'=>get_class($this),'{attribute}'=>$attributeProperty,'{name}'=>$nameProperty)));
  58. if(($id=$this->getId(false))===null)
  59. {
  60. if(isset($this->htmlOptions['id']))
  61. $id=$this->htmlOptions['id'];
  62. else
  63. $id=CHtml::getIdByName($name);
  64. }
  65. return array($name,$id);
  66. }
  67. /**
  68. * @return boolean whether this widget is associated with a data model.
  69. */
  70. protected function hasModel()
  71. {
  72. return $this->model instanceof CModel && $this->attribute!==null;
  73. }
  74. }