Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 139 lines (131 sloc) 4.54 kB
840e016 Initial implementation of form feature.
qiang.xue authored
1 <?php
27cc745
qiang.xue authored
2 /**
3 * CFormButtonElement class file.
4 *
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.yiiframework.com/
6247ada @samsonasik change copyright symbol
samsonasik authored
7 * @copyright 2008-2013 Yii Software LLC
27cc745
qiang.xue authored
8 * @license http://www.yiiframework.com/license/
9 */
840e016 Initial implementation of form feature.
qiang.xue authored
10
27cc745
qiang.xue authored
11 /**
12 * CFormButtonElement represents a form button element.
13 *
14 * CFormButtonElement can represent the following types of button based on {@link type} property:
15 * <ul>
0becc21 (Fixes issue 622)
qiang.xue authored
16 * <li>htmlButton: a normal button generated using {@link CHtml::htmlButton}</li>
17 * <li>htmlReset a reset button generated using {@link CHtml::htmlButton}</li>
18 * <li>htmlSubmit: a submit button generated using {@link CHtml::htmlButton}</li>
27cc745
qiang.xue authored
19 * <li>submit: a submit button generated using {@link CHtml::submitButton}</li>
20 * <li>button: a normal button generated using {@link CHtml::button}</li>
21 * <li>image: an image button generated using {@link CHtml::imageButton}</li>
22 * <li>reset: a reset button generated using {@link CHtml::resetButton}</li>
23 * <li>link: a link button generated using {@link CHtml::linkButton}</li>
24 * </ul>
25 * The {@link type} property can also be a class name or a path alias to the class. In this case,
26 * the button is generated using a widget of the specified class. Note, the widget must
27 * have a property called "name".
28 *
29 * Because CFormElement is an ancestor class of CFormButtonElement, a value assigned to a non-existing property will be
74118d6 (Fixes issue 942)
qiang.xue authored
30 * stored in {@link attributes} which will be passed as HTML attribute values to the {@link CHtml} method
27cc745
qiang.xue authored
31 * generating the button or initial values of the widget properties.
32 *
646cbb2 Documented component accessors with @property for significantly bette…
alexander.makarow authored
33 * @property string $on Scenario names separated by commas. Defaults to null.
34 *
27cc745
qiang.xue authored
35 * @author Qiang Xue <qiang.xue@gmail.com>
36 * @package system.web.form
37 * @since 1.1
38 */
840e016 Initial implementation of form feature.
qiang.xue authored
39 class CFormButtonElement extends CFormElement
40 {
27cc745
qiang.xue authored
41 /**
42 * @var array Core button types (alias=>CHtml method name)
43 */
840e016 Initial implementation of form feature.
qiang.xue authored
44 public static $coreTypes=array(
0becc21 (Fixes issue 622)
qiang.xue authored
45 'htmlButton'=>'htmlButton',
46 'htmlSubmit'=>'htmlButton',
47 'htmlReset'=>'htmlButton',
840e016 Initial implementation of form feature.
qiang.xue authored
48 'button'=>'button',
0becc21 (Fixes issue 622)
qiang.xue authored
49 'submit'=>'submitButton',
840e016 Initial implementation of form feature.
qiang.xue authored
50 'reset'=>'resetButton',
0becc21 (Fixes issue 622)
qiang.xue authored
51 'image'=>'imageButton',
840e016 Initial implementation of form feature.
qiang.xue authored
52 'link'=>'linkButton',
53 );
27cc745
qiang.xue authored
54
55 /**
56 * @var string the type of this button. This can be a class name, a path alias of a class name,
0becc21 (Fixes issue 622)
qiang.xue authored
57 * or a button type alias (submit, button, image, reset, link, htmlButton, htmlSubmit, htmlReset).
27cc745
qiang.xue authored
58 */
840e016 Initial implementation of form feature.
qiang.xue authored
59 public $type;
27cc745
qiang.xue authored
60 /**
61 * @var string name of this button
62 */
63 public $name;
64 /**
65 * @var string the label of this button. This property is ignored when a widget is used to generate the button.
66 */
840e016 Initial implementation of form feature.
qiang.xue authored
67 public $label;
68
69 private $_on;
70
27cc745
qiang.xue authored
71 /**
72 * Returns a value indicating under which scenarios this button is visible.
73 * If the value is empty, it means the button is visible under all scenarios.
74 * Otherwise, only when the model is in the scenario whose name can be found in
75 * this value, will the button be visible. See {@link CModel::scenario} for more
76 * information about model scenarios.
77 * @return string scenario names separated by commas. Defaults to null.
78 */
840e016 Initial implementation of form feature.
qiang.xue authored
79 public function getOn()
80 {
81 return $this->_on;
82 }
83
27cc745
qiang.xue authored
84 /**
72b9745 Updated PHPDoc @param parameter names
mdomba authored
85 * @param string $value scenario names separated by commas.
27cc745
qiang.xue authored
86 */
840e016 Initial implementation of form feature.
qiang.xue authored
87 public function setOn($value)
88 {
89 $this->_on=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY);
90 }
91
27cc745
qiang.xue authored
92 /**
93 * Returns this button.
94 * @return string the rendering result
95 */
840e016 Initial implementation of form feature.
qiang.xue authored
96 public function render()
97 {
74118d6 (Fixes issue 942)
qiang.xue authored
98 $attributes=$this->attributes;
840e016 Initial implementation of form feature.
qiang.xue authored
99 if(isset(self::$coreTypes[$this->type]))
100 {
101 $method=self::$coreTypes[$this->type];
102 if($method==='linkButton')
103 {
74118d6 (Fixes issue 942)
qiang.xue authored
104 if(!isset($attributes['params'][$this->name]))
105 $attributes['params'][$this->name]=1;
840e016 Initial implementation of form feature.
qiang.xue authored
106 }
4867238 @creocoder else if => elseif
creocoder authored
107 elseif($method==='htmlButton')
0becc21 (Fixes issue 622)
qiang.xue authored
108 {
f41ed5c CFormButtonElement syntax error fix
alexander.makarow authored
109 $attributes['type']=$this->type==='htmlSubmit' ? 'submit' : ($this->type==='htmlReset' ? 'reset' : 'button');
74118d6 (Fixes issue 942)
qiang.xue authored
110 $attributes['name']=$this->name;
0becc21 (Fixes issue 622)
qiang.xue authored
111 }
112 else
74118d6 (Fixes issue 942)
qiang.xue authored
113 $attributes['name']=$this->name;
840e016 Initial implementation of form feature.
qiang.xue authored
114 if($method==='imageButton')
74118d6 (Fixes issue 942)
qiang.xue authored
115 return CHtml::imageButton(isset($attributes['src']) ? $attributes['src'] : '',$attributes);
840e016 Initial implementation of form feature.
qiang.xue authored
116 else
74118d6 (Fixes issue 942)
qiang.xue authored
117 return CHtml::$method($this->label,$attributes);
840e016 Initial implementation of form feature.
qiang.xue authored
118 }
119 else
120 {
74118d6 (Fixes issue 942)
qiang.xue authored
121 $attributes['name']=$this->name;
840e016 Initial implementation of form feature.
qiang.xue authored
122 ob_start();
74118d6 (Fixes issue 942)
qiang.xue authored
123 $this->getParent()->getOwner()->widget($this->type, $attributes);
840e016 Initial implementation of form feature.
qiang.xue authored
124 return ob_get_clean();
125 }
126 }
0044126 refactoring form feature.
qiang.xue authored
127
128 /**
129 * Evaluates the visibility of this element.
130 * This method will check the {@link on} property to see if
131 * the model is in a scenario that should have this string displayed.
132 * @return boolean whether this element is visible.
133 */
134 protected function evaluateVisible()
135 {
136 return empty($this->_on) || in_array($this->getParent()->getModel()->getScenario(),$this->_on);
137 }
840e016 Initial implementation of form feature.
qiang.xue authored
138 }
Something went wrong with that request. Please try again.