PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/framework/web/helpers/CHtml.php

https://github.com/joshuaswarren/weatherhub
PHP | 1257 lines | 541 code | 71 blank | 645 comment | 85 complexity | 48d36d1c7270dcab8ede5a9341135e32 MD5 | raw file
  1. <?php
  2. /**
  3. * CHtml class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CHtml is a static class that provides a collection of helper methods for creating HTML views.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @version $Id: CHtml.php 3306 2011-06-23 15:26:33Z qiang.xue $
  15. * @package system.web.helpers
  16. * @since 1.0
  17. */
  18. class CHtml
  19. {
  20. const ID_PREFIX='yt';
  21. /**
  22. * @var string the CSS class for displaying error summaries (see {@link errorSummary}).
  23. */
  24. public static $errorSummaryCss='errorSummary';
  25. /**
  26. * @var string the CSS class for displaying error messages (see {@link error}).
  27. */
  28. public static $errorMessageCss='errorMessage';
  29. /**
  30. * @var string the CSS class for highlighting error inputs. Form inputs will be appended
  31. * with this CSS class if they have input errors.
  32. */
  33. public static $errorCss='error';
  34. /**
  35. * @var string the CSS class for required labels. Defaults to 'required'.
  36. * @see label
  37. */
  38. public static $requiredCss='required';
  39. /**
  40. * @var string the HTML code to be prepended to the required label.
  41. * @see label
  42. */
  43. public static $beforeRequiredLabel='';
  44. /**
  45. * @var string the HTML code to be appended to the required label.
  46. * @see label
  47. */
  48. public static $afterRequiredLabel=' <span class="required">*</span>';
  49. /**
  50. * @var integer the counter for generating automatic input field names.
  51. * @since 1.0.4
  52. */
  53. public static $count=0;
  54. /**
  55. * Encodes special characters into HTML entities.
  56. * The {@link CApplication::charset application charset} will be used for encoding.
  57. * @param string $text data to be encoded
  58. * @return string the encoded data
  59. * @see http://www.php.net/manual/en/function.htmlspecialchars.php
  60. */
  61. public static function encode($text)
  62. {
  63. return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
  64. }
  65. /**
  66. * Decodes special HTML entities back to the corresponding characters.
  67. * This is the opposite of {@link encode()}.
  68. * @param string $text data to be decoded
  69. * @return string the decoded data
  70. * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
  71. * @since 1.1.8
  72. */
  73. public static function decode($text)
  74. {
  75. return htmlspecialchars_decode($text,ENT_QUOTES);
  76. }
  77. /**
  78. * Encodes special characters in an array of strings into HTML entities.
  79. * Both the array keys and values will be encoded if needed.
  80. * If a value is an array, this method will also encode it recursively.
  81. * The {@link CApplication::charset application charset} will be used for encoding.
  82. * @param array $data data to be encoded
  83. * @return array the encoded data
  84. * @see http://www.php.net/manual/en/function.htmlspecialchars.php
  85. * @since 1.0.4
  86. */
  87. public static function encodeArray($data)
  88. {
  89. $d=array();
  90. foreach($data as $key=>$value)
  91. {
  92. if(is_string($key))
  93. $key=htmlspecialchars($key,ENT_QUOTES,Yii::app()->charset);
  94. if(is_string($value))
  95. $value=htmlspecialchars($value,ENT_QUOTES,Yii::app()->charset);
  96. else if(is_array($value))
  97. $value=self::encodeArray($value);
  98. $d[$key]=$value;
  99. }
  100. return $d;
  101. }
  102. /**
  103. * Generates an HTML element.
  104. * @param string $tag the tag name
  105. * @param array $htmlOptions the element attributes. The values will be HTML-encoded using {@link encode()}.
  106. * Since version 1.0.5, if an 'encode' attribute is given and its value is false,
  107. * the rest of the attribute values will NOT be HTML-encoded.
  108. * Since version 1.1.5, attributes whose value is null will not be rendered.
  109. * @param mixed $content the content to be enclosed between open and close element tags. It will not be HTML-encoded.
  110. * If false, it means there is no body content.
  111. * @param boolean $closeTag whether to generate the close tag.
  112. * @return string the generated HTML element tag
  113. */
  114. public static function tag($tag,$htmlOptions=array(),$content=false,$closeTag=true)
  115. {
  116. $html='<' . $tag . self::renderAttributes($htmlOptions);
  117. if($content===false)
  118. return $closeTag ? $html.' />' : $html.'>';
  119. else
  120. return $closeTag ? $html.'>'.$content.'</'.$tag.'>' : $html.'>'.$content;
  121. }
  122. /**
  123. * Generates an open HTML element.
  124. * @param string $tag the tag name
  125. * @param array $htmlOptions the element attributes. The values will be HTML-encoded using {@link encode()}.
  126. * Since version 1.0.5, if an 'encode' attribute is given and its value is false,
  127. * the rest of the attribute values will NOT be HTML-encoded.
  128. * Since version 1.1.5, attributes whose value is null will not be rendered.
  129. * @return string the generated HTML element tag
  130. */
  131. public static function openTag($tag,$htmlOptions=array())
  132. {
  133. return '<' . $tag . self::renderAttributes($htmlOptions) . '>';
  134. }
  135. /**
  136. * Generates a close HTML element.
  137. * @param string $tag the tag name
  138. * @return string the generated HTML element tag
  139. */
  140. public static function closeTag($tag)
  141. {
  142. return '</'.$tag.'>';
  143. }
  144. /**
  145. * Encloses the given string within a CDATA tag.
  146. * @param string $text the string to be enclosed
  147. * @return string the CDATA tag with the enclosed content.
  148. */
  149. public static function cdata($text)
  150. {
  151. return '<![CDATA[' . $text . ']]>';
  152. }
  153. /**
  154. * Generates a meta tag that can be inserted in the head section of HTML page.
  155. * @param string $content content attribute of the meta tag
  156. * @param string $name name attribute of the meta tag. If null, the attribute will not be generated
  157. * @param string $httpEquiv http-equiv attribute of the meta tag. If null, the attribute will not be generated
  158. * @param array $options other options in name-value pairs (e.g. 'scheme', 'lang')
  159. * @return string the generated meta tag
  160. * @since 1.0.1
  161. */
  162. public static function metaTag($content,$name=null,$httpEquiv=null,$options=array())
  163. {
  164. if($name!==null)
  165. $options['name']=$name;
  166. if($httpEquiv!==null)
  167. $options['http-equiv']=$httpEquiv;
  168. $options['content']=$content;
  169. return self::tag('meta',$options);
  170. }
  171. /**
  172. * Generates a link tag that can be inserted in the head section of HTML page.
  173. * Do not confuse this method with {@link link()}. The latter generates a hyperlink.
  174. * @param string $relation rel attribute of the link tag. If null, the attribute will not be generated.
  175. * @param string $type type attribute of the link tag. If null, the attribute will not be generated.
  176. * @param string $href href attribute of the link tag. If null, the attribute will not be generated.
  177. * @param string $media media attribute of the link tag. If null, the attribute will not be generated.
  178. * @param array $options other options in name-value pairs
  179. * @return string the generated link tag
  180. * @since 1.0.1
  181. */
  182. public static function linkTag($relation=null,$type=null,$href=null,$media=null,$options=array())
  183. {
  184. if($relation!==null)
  185. $options['rel']=$relation;
  186. if($type!==null)
  187. $options['type']=$type;
  188. if($href!==null)
  189. $options['href']=$href;
  190. if($media!==null)
  191. $options['media']=$media;
  192. return self::tag('link',$options);
  193. }
  194. /**
  195. * Encloses the given CSS content with a CSS tag.
  196. * @param string $text the CSS content
  197. * @param string $media the media that this CSS should apply to.
  198. * @return string the CSS properly enclosed
  199. */
  200. public static function css($text,$media='')
  201. {
  202. if($media!=='')
  203. $media=' media="'.$media.'"';
  204. return "<style type=\"text/css\"{$media}>\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</style>";
  205. }
  206. /**
  207. * Registers a 'refresh' meta tag.
  208. * This method can be invoked anywhere in a view. It will register a 'refresh'
  209. * meta tag with {@link CClientScript} so that the page can be refreshed in
  210. * the specified seconds.
  211. * @param integer $seconds the number of seconds to wait before refreshing the page
  212. * @param string $url the URL to which the page should be redirected to. If empty, it means the current page.
  213. * @since 1.1.1
  214. */
  215. public static function refresh($seconds, $url='')
  216. {
  217. $content="$seconds";
  218. if($url!=='')
  219. $content.=';'.self::normalizeUrl($url);
  220. Yii::app()->clientScript->registerMetaTag($content,null,'refresh');
  221. }
  222. /**
  223. * Links to the specified CSS file.
  224. * @param string $url the CSS URL
  225. * @param string $media the media that this CSS should apply to.
  226. * @return string the CSS link.
  227. */
  228. public static function cssFile($url,$media='')
  229. {
  230. if($media!=='')
  231. $media=' media="'.$media.'"';
  232. return '<link rel="stylesheet" type="text/css" href="'.self::encode($url).'"'.$media.' />';
  233. }
  234. /**
  235. * Encloses the given JavaScript within a script tag.
  236. * @param string $text the JavaScript to be enclosed
  237. * @return string the enclosed JavaScript
  238. */
  239. public static function script($text)
  240. {
  241. return "<script type=\"text/javascript\">\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</script>";
  242. }
  243. /**
  244. * Includes a JavaScript file.
  245. * @param string $url URL for the JavaScript file
  246. * @return string the JavaScript file tag
  247. */
  248. public static function scriptFile($url)
  249. {
  250. return '<script type="text/javascript" src="'.self::encode($url).'"></script>';
  251. }
  252. /**
  253. * Generates an opening form tag.
  254. * This is a shortcut to {@link beginForm}.
  255. * @param mixed $action the form action URL (see {@link normalizeUrl} for details about this parameter.)
  256. * @param string $method form method (e.g. post, get)
  257. * @param array $htmlOptions additional HTML attributes (see {@link tag}).
  258. * @return string the generated form tag.
  259. */
  260. public static function form($action='',$method='post',$htmlOptions=array())
  261. {
  262. return self::beginForm($action,$method,$htmlOptions);
  263. }
  264. /**
  265. * Generates an opening form tag.
  266. * Note, only the open tag is generated. A close tag should be placed manually
  267. * at the end of the form.
  268. * @param mixed $action the form action URL (see {@link normalizeUrl} for details about this parameter.)
  269. * @param string $method form method (e.g. post, get)
  270. * @param array $htmlOptions additional HTML attributes (see {@link tag}).
  271. * @return string the generated form tag.
  272. * @since 1.0.4
  273. * @see endForm
  274. */
  275. public static function beginForm($action='',$method='post',$htmlOptions=array())
  276. {
  277. $htmlOptions['action']=$url=self::normalizeUrl($action);
  278. $htmlOptions['method']=$method;
  279. $form=self::tag('form',$htmlOptions,false,false);
  280. $hiddens=array();
  281. if(!strcasecmp($method,'get') && ($pos=strpos($url,'?'))!==false)
  282. {
  283. foreach(explode('&',substr($url,$pos+1)) as $pair)
  284. {
  285. if(($pos=strpos($pair,'='))!==false)
  286. $hiddens[]=self::hiddenField(urldecode(substr($pair,0,$pos)),urldecode(substr($pair,$pos+1)),array('id'=>false));
  287. }
  288. }
  289. $request=Yii::app()->request;
  290. if($request->enableCsrfValidation && !strcasecmp($method,'post'))
  291. $hiddens[]=self::hiddenField($request->csrfTokenName,$request->getCsrfToken(),array('id'=>false));
  292. if($hiddens!==array())
  293. $form.="\n".self::tag('div',array('style'=>'display:none'),implode("\n",$hiddens));
  294. return $form;
  295. }
  296. /**
  297. * Generates a closing form tag.
  298. * @return string the generated tag
  299. * @since 1.0.4
  300. * @see beginForm
  301. */
  302. public static function endForm()
  303. {
  304. return '</form>';
  305. }
  306. /**
  307. * Generates a stateful form tag.
  308. * A stateful form tag is similar to {@link form} except that it renders an additional
  309. * hidden field for storing persistent page states. You should use this method to generate
  310. * a form tag if you want to access persistent page states when the form is submitted.
  311. * @param mixed $action the form action URL (see {@link normalizeUrl} for details about this parameter.)
  312. * @param string $method form method (e.g. post, get)
  313. * @param array $htmlOptions additional HTML attributes (see {@link tag}).
  314. * @return string the generated form tag.
  315. */
  316. public static function statefulForm($action='',$method='post',$htmlOptions=array())
  317. {
  318. return self::form($action,$method,$htmlOptions)."\n".
  319. self::tag('div',array('style'=>'display:none'),self::pageStateField(''));
  320. }
  321. /**
  322. * Generates a hidden field for storing persistent page states.
  323. * This method is internally used by {@link statefulForm}.
  324. * @param string $value the persistent page states in serialized format
  325. * @return string the generated hidden field
  326. */
  327. public static function pageStateField($value)
  328. {
  329. return '<input type="hidden" name="'.CController::STATE_INPUT_NAME.'" value="'.$value.'" />';
  330. }
  331. /**
  332. * Generates a hyperlink tag.
  333. * @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag.
  334. * @param mixed $url a URL or an action route that can be used to create a URL.
  335. * See {@link normalizeUrl} for more details about how to specify this parameter.
  336. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  337. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  338. * @return string the generated hyperlink
  339. * @see normalizeUrl
  340. * @see clientChange
  341. */
  342. public static function link($text,$url='#',$htmlOptions=array())
  343. {
  344. if($url!=='')
  345. $htmlOptions['href']=self::normalizeUrl($url);
  346. self::clientChange('click',$htmlOptions);
  347. return self::tag('a',$htmlOptions,$text);
  348. }
  349. /**
  350. * Generates a mailto link.
  351. * @param string $text link body. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag.
  352. * @param string $email email address. If this is empty, the first parameter (link body) will be treated as the email address.
  353. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  354. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  355. * @return string the generated mailto link
  356. * @see clientChange
  357. * @since 1.0.1
  358. */
  359. public static function mailto($text,$email='',$htmlOptions=array())
  360. {
  361. if($email==='')
  362. $email=$text;
  363. return self::link($text,'mailto:'.$email,$htmlOptions);
  364. }
  365. /**
  366. * Generates an image tag.
  367. * @param string $src the image URL
  368. * @param string $alt the alternative text display
  369. * @param array $htmlOptions additional HTML attributes (see {@link tag}).
  370. * @return string the generated image tag
  371. */
  372. public static function image($src,$alt='',$htmlOptions=array())
  373. {
  374. $htmlOptions['src']=$src;
  375. $htmlOptions['alt']=$alt;
  376. return self::tag('img',$htmlOptions);
  377. }
  378. /**
  379. * Generates a button.
  380. * @param string $label the button label
  381. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  382. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  383. * @return string the generated button tag
  384. * @see clientChange
  385. */
  386. public static function button($label='button',$htmlOptions=array())
  387. {
  388. if(!isset($htmlOptions['name']))
  389. {
  390. if(!array_key_exists('name',$htmlOptions))
  391. $htmlOptions['name']=self::ID_PREFIX.self::$count++;
  392. }
  393. if(!isset($htmlOptions['type']))
  394. $htmlOptions['type']='button';
  395. if(!isset($htmlOptions['value']))
  396. $htmlOptions['value']=$label;
  397. self::clientChange('click',$htmlOptions);
  398. return self::tag('input',$htmlOptions);
  399. }
  400. /**
  401. * Generates a button using HTML button tag.
  402. * This method is similar to {@link button} except that it generates a 'button'
  403. * tag instead of 'input' tag.
  404. * @param string $label the button label. Note that this value will be directly inserted in the button element
  405. * without being HTML-encoded.
  406. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  407. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  408. * @return string the generated button tag
  409. * @see clientChange
  410. * @since 1.0.8
  411. */
  412. public static function htmlButton($label='button',$htmlOptions=array())
  413. {
  414. if(!isset($htmlOptions['name']))
  415. $htmlOptions['name']=self::ID_PREFIX.self::$count++;
  416. if(!isset($htmlOptions['type']))
  417. $htmlOptions['type']='button';
  418. self::clientChange('click',$htmlOptions);
  419. return self::tag('button',$htmlOptions,$label);
  420. }
  421. /**
  422. * Generates a submit button.
  423. * @param string $label the button label
  424. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  425. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  426. * @return string the generated button tag
  427. * @see clientChange
  428. */
  429. public static function submitButton($label='submit',$htmlOptions=array())
  430. {
  431. $htmlOptions['type']='submit';
  432. return self::button($label,$htmlOptions);
  433. }
  434. /**
  435. * Generates a reset button.
  436. * @param string $label the button label
  437. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  438. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  439. * @return string the generated button tag
  440. * @see clientChange
  441. */
  442. public static function resetButton($label='reset',$htmlOptions=array())
  443. {
  444. $htmlOptions['type']='reset';
  445. return self::button($label,$htmlOptions);
  446. }
  447. /**
  448. * Generates an image submit button.
  449. * @param string $src the image URL
  450. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  451. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  452. * @return string the generated button tag
  453. * @see clientChange
  454. */
  455. public static function imageButton($src,$htmlOptions=array())
  456. {
  457. $htmlOptions['src']=$src;
  458. $htmlOptions['type']='image';
  459. return self::button('submit',$htmlOptions);
  460. }
  461. /**
  462. * Generates a link submit button.
  463. * @param string $label the button label
  464. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  465. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  466. * @return string the generated button tag
  467. * @see clientChange
  468. */
  469. public static function linkButton($label='submit',$htmlOptions=array())
  470. {
  471. if(!isset($htmlOptions['submit']))
  472. $htmlOptions['submit']=isset($htmlOptions['href']) ? $htmlOptions['href'] : '';
  473. return self::link($label,'#',$htmlOptions);
  474. }
  475. /**
  476. * Generates a label tag.
  477. * @param string $label label text. Note, you should HTML-encode the text if needed.
  478. * @param string $for the ID of the HTML element that this label is associated with.
  479. * If this is false, the 'for' attribute for the label tag will not be rendered (since version 1.0.11).
  480. * @param array $htmlOptions additional HTML attributes.
  481. * Starting from version 1.0.2, the following HTML option is recognized:
  482. * <ul>
  483. * <li>required: if this is set and is true, the label will be styled
  484. * with CSS class 'required' (customizable with CHtml::$requiredCss),
  485. * and be decorated with {@link CHtml::beforeRequiredLabel} and
  486. * {@link CHtml::afterRequiredLabel}.</li>
  487. * </ul>
  488. * @return string the generated label tag
  489. */
  490. public static function label($label,$for,$htmlOptions=array())
  491. {
  492. if($for===false)
  493. unset($htmlOptions['for']);
  494. else
  495. $htmlOptions['for']=$for;
  496. if(isset($htmlOptions['required']))
  497. {
  498. if($htmlOptions['required'])
  499. {
  500. if(isset($htmlOptions['class']))
  501. $htmlOptions['class'].=' '.self::$requiredCss;
  502. else
  503. $htmlOptions['class']=self::$requiredCss;
  504. $label=self::$beforeRequiredLabel.$label.self::$afterRequiredLabel;
  505. }
  506. unset($htmlOptions['required']);
  507. }
  508. return self::tag('label',$htmlOptions,$label);
  509. }
  510. /**
  511. * Generates a text field input.
  512. * @param string $name the input name
  513. * @param string $value the input value
  514. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  515. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  516. * @return string the generated input field
  517. * @see clientChange
  518. * @see inputField
  519. */
  520. public static function textField($name,$value='',$htmlOptions=array())
  521. {
  522. self::clientChange('change',$htmlOptions);
  523. return self::inputField('text',$name,$value,$htmlOptions);
  524. }
  525. /**
  526. * Generates a hidden input.
  527. * @param string $name the input name
  528. * @param string $value the input value
  529. * @param array $htmlOptions additional HTML attributes (see {@link tag}).
  530. * @return string the generated input field
  531. * @see inputField
  532. */
  533. public static function hiddenField($name,$value='',$htmlOptions=array())
  534. {
  535. return self::inputField('hidden',$name,$value,$htmlOptions);
  536. }
  537. /**
  538. * Generates a password field input.
  539. * @param string $name the input name
  540. * @param string $value the input value
  541. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  542. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  543. * @return string the generated input field
  544. * @see clientChange
  545. * @see inputField
  546. */
  547. public static function passwordField($name,$value='',$htmlOptions=array())
  548. {
  549. self::clientChange('change',$htmlOptions);
  550. return self::inputField('password',$name,$value,$htmlOptions);
  551. }
  552. /**
  553. * Generates a file input.
  554. * Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'.
  555. * After the form is submitted, the uploaded file information can be obtained via $_FILES[$name] (see
  556. * PHP documentation).
  557. * @param string $name the input name
  558. * @param string $value the input value
  559. * @param array $htmlOptions additional HTML attributes (see {@link tag}).
  560. * @return string the generated input field
  561. * @see inputField
  562. */
  563. public static function fileField($name,$value='',$htmlOptions=array())
  564. {
  565. return self::inputField('file',$name,$value,$htmlOptions);
  566. }
  567. /**
  568. * Generates a text area input.
  569. * @param string $name the input name
  570. * @param string $value the input value
  571. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  572. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  573. * @return string the generated text area
  574. * @see clientChange
  575. * @see inputField
  576. */
  577. public static function textArea($name,$value='',$htmlOptions=array())
  578. {
  579. $htmlOptions['name']=$name;
  580. if(!isset($htmlOptions['id']))
  581. $htmlOptions['id']=self::getIdByName($name);
  582. else if($htmlOptions['id']===false)
  583. unset($htmlOptions['id']);
  584. self::clientChange('change',$htmlOptions);
  585. return self::tag('textarea',$htmlOptions,isset($htmlOptions['encode']) && !$htmlOptions['encode'] ? $value : self::encode($value));
  586. }
  587. /**
  588. * Generates a radio button.
  589. * @param string $name the input name
  590. * @param boolean $checked whether the radio button is checked
  591. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  592. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  593. * Since version 1.1.2, a special option named 'uncheckValue' is available that can be used to specify
  594. * the value returned when the radio button is not checked. When set, a hidden field is rendered so that
  595. * when the radio button is not checked, we can still obtain the posted uncheck value.
  596. * If 'uncheckValue' is not set or set to NULL, the hidden field will not be rendered.
  597. * @return string the generated radio button
  598. * @see clientChange
  599. * @see inputField
  600. */
  601. public static function radioButton($name,$checked=false,$htmlOptions=array())
  602. {
  603. if($checked)
  604. $htmlOptions['checked']='checked';
  605. else
  606. unset($htmlOptions['checked']);
  607. $value=isset($htmlOptions['value']) ? $htmlOptions['value'] : 1;
  608. self::clientChange('click',$htmlOptions);
  609. if(array_key_exists('uncheckValue',$htmlOptions))
  610. {
  611. $uncheck=$htmlOptions['uncheckValue'];
  612. unset($htmlOptions['uncheckValue']);
  613. }
  614. else
  615. $uncheck=null;
  616. if($uncheck!==null)
  617. {
  618. // add a hidden field so that if the radio button is not selected, it still submits a value
  619. if(isset($htmlOptions['id']) && $htmlOptions['id']!==false)
  620. $uncheckOptions=array('id'=>self::ID_PREFIX.$htmlOptions['id']);
  621. else
  622. $uncheckOptions=array('id'=>false);
  623. $hidden=self::hiddenField($name,$uncheck,$uncheckOptions);
  624. }
  625. else
  626. $hidden='';
  627. // add a hidden field so that if the radio button is not selected, it still submits a value
  628. return $hidden . self::inputField('radio',$name,$value,$htmlOptions);
  629. }
  630. /**
  631. * Generates a check box.
  632. * @param string $name the input name
  633. * @param boolean $checked whether the check box is checked
  634. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  635. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  636. * Since version 1.1.2, a special option named 'uncheckValue' is available that can be used to specify
  637. * the value returned when the checkbox is not checked. When set, a hidden field is rendered so that
  638. * when the checkbox is not checked, we can still obtain the posted uncheck value.
  639. * If 'uncheckValue' is not set or set to NULL, the hidden field will not be rendered.
  640. * @return string the generated check box
  641. * @see clientChange
  642. * @see inputField
  643. */
  644. public static function checkBox($name,$checked=false,$htmlOptions=array())
  645. {
  646. if($checked)
  647. $htmlOptions['checked']='checked';
  648. else
  649. unset($htmlOptions['checked']);
  650. $value=isset($htmlOptions['value']) ? $htmlOptions['value'] : 1;
  651. self::clientChange('click',$htmlOptions);
  652. if(array_key_exists('uncheckValue',$htmlOptions))
  653. {
  654. $uncheck=$htmlOptions['uncheckValue'];
  655. unset($htmlOptions['uncheckValue']);
  656. }
  657. else
  658. $uncheck=null;
  659. if($uncheck!==null)
  660. {
  661. // add a hidden field so that if the radio button is not selected, it still submits a value
  662. if(isset($htmlOptions['id']) && $htmlOptions['id']!==false)
  663. $uncheckOptions=array('id'=>self::ID_PREFIX.$htmlOptions['id']);
  664. else
  665. $uncheckOptions=array('id'=>false);
  666. $hidden=self::hiddenField($name,$uncheck,$uncheckOptions);
  667. }
  668. else
  669. $hidden='';
  670. // add a hidden field so that if the checkbox is not selected, it still submits a value
  671. return $hidden . self::inputField('checkbox',$name,$value,$htmlOptions);
  672. }
  673. /**
  674. * Generates a drop down list.
  675. * @param string $name the input name
  676. * @param string $select the selected value
  677. * @param array $data data for generating the list options (value=>display).
  678. * You may use {@link listData} to generate this data.
  679. * Please refer to {@link listOptions} on how this data is used to generate the list options.
  680. * Note, the values and labels will be automatically HTML-encoded by this method.
  681. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  682. * attributes are recognized. See {@link clientChange} and {@link tag} for more details.
  683. * In addition, the following options are also supported specifically for dropdown list:
  684. * <ul>
  685. * <li>encode: boolean, specifies whether to encode the values. Defaults to true. This option has been available since version 1.0.5.</li>
  686. * <li>prompt: string, specifies the prompt text shown as the first list option. Its value is empty. Note, the prompt text will NOT be HTML-encoded.</li>
  687. * <li>empty: string, specifies the text corresponding to empty selection. Its value is empty.
  688. * Starting from version 1.0.10, the 'empty' option can also be an array of value-label pairs.
  689. * Each pair will be used to render a list option at the beginning. Note, the text label will NOT be HTML-encoded.</li>
  690. * <li>options: array, specifies additional attributes for each OPTION tag.
  691. * The array keys must be the option values, and the array values are the extra
  692. * OPTION tag attributes in the name-value pairs. For example,
  693. * <pre>
  694. * array(
  695. * 'value1'=>array('disabled'=>true, 'label'=>'value 1'),
  696. * 'value2'=>array('label'=>'value 2'),
  697. * );
  698. * </pre>
  699. * This option has been available since version 1.0.3.
  700. * </li>
  701. * </ul>
  702. * @return string the generated drop down list
  703. * @see clientChange
  704. * @see inputField
  705. * @see listData
  706. */
  707. public static function dropDownList($name,$select,$data,$htmlOptions=array())
  708. {
  709. $htmlOptions['name']=$name;
  710. if(!isset($htmlOptions['id']))
  711. $htmlOptions['id']=self::getIdByName($name);
  712. else if($htmlOptions['id']===false)
  713. unset($htmlOptions['id']);
  714. self::clientChange('change',$htmlOptions);
  715. $options="\n".self::listOptions($select,$data,$htmlOptions);
  716. return self::tag('select',$htmlOptions,$options);
  717. }
  718. /**
  719. * Generates a list box.
  720. * @param string $name the input name
  721. * @param mixed $select the selected value(s). This can be either a string for single selection or an array for multiple selections.
  722. * @param array $data data for generating the list options (value=>display)
  723. * You may use {@link listData} to generate this data.
  724. * Please refer to {@link listOptions} on how this data is used to generate the list options.
  725. * Note, the values and labels will be automatically HTML-encoded by this method.
  726. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  727. * attributes are also recognized. See {@link clientChange} and {@link tag} for more details.
  728. * In addition, the following options are also supported specifically for list box:
  729. * <ul>
  730. * <li>encode: boolean, specifies whether to encode the values. Defaults to true. This option has been available since version 1.0.5.</li>
  731. * <li>prompt: string, specifies the prompt text shown as the first list option. Its value is empty. Note, the prompt text will NOT be HTML-encoded.</li>
  732. * <li>empty: string, specifies the text corresponding to empty selection. Its value is empty.
  733. * Starting from version 1.0.10, the 'empty' option can also be an array of value-label pairs.
  734. * Each pair will be used to render a list option at the beginning. Note, the text label will NOT be HTML-encoded.</li>
  735. * <li>options: array, specifies additional attributes for each OPTION tag.
  736. * The array keys must be the option values, and the array values are the extra
  737. * OPTION tag attributes in the name-value pairs. For example,
  738. * <pre>
  739. * array(
  740. * 'value1'=>array('disabled'=>true, 'label'=>'value 1'),
  741. * 'value2'=>array('label'=>'value 2'),
  742. * );
  743. * </pre>
  744. * This option has been available since version 1.0.3.
  745. * </li>
  746. * </ul>
  747. * @return string the generated list box
  748. * @see clientChange
  749. * @see inputField
  750. * @see listData
  751. */
  752. public static function listBox($name,$select,$data,$htmlOptions=array())
  753. {
  754. if(!isset($htmlOptions['size']))
  755. $htmlOptions['size']=4;
  756. if(isset($htmlOptions['multiple']))
  757. {
  758. if(substr($name,-2)!=='[]')
  759. $name.='[]';
  760. }
  761. return self::dropDownList($name,$select,$data,$htmlOptions);
  762. }
  763. /**
  764. * Generates a check box list.
  765. * A check box list allows multiple selection, like {@link listBox}.
  766. * As a result, the corresponding POST value is an array.
  767. * @param string $name name of the check box list. You can use this name to retrieve
  768. * the selected value(s) once the form is submitted.
  769. * @param mixed $select selection of the check boxes. This can be either a string
  770. * for single selection or an array for multiple selections.
  771. * @param array $data value-label pairs used to generate the check box list.
  772. * Note, the values will be automatically HTML-encoded, while the labels will not.
  773. * @param array $htmlOptions addtional HTML options. The options will be applied to
  774. * each checkbox input. The following special options are recognized:
  775. * <ul>
  776. * <li>template: string, specifies how each checkbox is rendered. Defaults
  777. * to "{input} {label}", where "{input}" will be replaced by the generated
  778. * check box input tag while "{label}" be replaced by the corresponding check box label.</li>
  779. * <li>separator: string, specifies the string that separates the generated check boxes.</li>
  780. * <li>checkAll: string, specifies the label for the "check all" checkbox.
  781. * If this option is specified, a 'check all' checkbox will be displayed. Clicking on
  782. * this checkbox will cause all checkboxes checked or unchecked. This option has been
  783. * available since version 1.0.4.</li>
  784. * <li>checkAllLast: boolean, specifies whether the 'check all' checkbox should be
  785. * displayed at the end of the checkbox list. If this option is not set (default)
  786. * or is false, the 'check all' checkbox will be displayed at the beginning of
  787. * the checkbox list. This option has been available since version 1.0.4.</li>
  788. * <li>labelOptions: array, specifies the additional HTML attributes to be rendered
  789. * for every label tag in the list. This option has been available since version 1.0.10.</li>
  790. * </ul>
  791. * @return string the generated check box list
  792. */
  793. public static function checkBoxList($name,$select,$data,$htmlOptions=array())
  794. {
  795. $template=isset($htmlOptions['template'])?$htmlOptions['template']:'{input} {label}';
  796. $separator=isset($htmlOptions['separator'])?$htmlOptions['separator']:"<br/>\n";
  797. unset($htmlOptions['template'],$htmlOptions['separator']);
  798. if(substr($name,-2)!=='[]')
  799. $name.='[]';
  800. if(isset($htmlOptions['checkAll']))
  801. {
  802. $checkAllLabel=$htmlOptions['checkAll'];
  803. $checkAllLast=isset($htmlOptions['checkAllLast']) && $htmlOptions['checkAllLast'];
  804. }
  805. unset($htmlOptions['checkAll'],$htmlOptions['checkAllLast']);
  806. $labelOptions=isset($htmlOptions['labelOptions'])?$htmlOptions['labelOptions']:array();
  807. unset($htmlOptions['labelOptions']);
  808. $items=array();
  809. $baseID=self::getIdByName($name);
  810. $id=0;
  811. $checkAll=true;
  812. foreach($data as $value=>$label)
  813. {
  814. $checked=!is_array($select) && !strcmp($value,$select) || is_array($select) && in_array($value,$select);
  815. $checkAll=$checkAll && $checked;
  816. $htmlOptions['value']=$value;
  817. $htmlOptions['id']=$baseID.'_'.$id++;
  818. $option=self::checkBox($name,$checked,$htmlOptions);
  819. $label=self::label($label,$htmlOptions['id'],$labelOptions);
  820. $items[]=strtr($template,array('{input}'=>$option,'{label}'=>$label));
  821. }
  822. if(isset($checkAllLabel))
  823. {
  824. $htmlOptions['value']=1;
  825. $htmlOptions['id']=$id=$baseID.'_all';
  826. $option=self::checkBox($id,$checkAll,$htmlOptions);
  827. $label=self::label($checkAllLabel,$id,$labelOptions);
  828. $item=strtr($template,array('{input}'=>$option,'{label}'=>$label));
  829. if($checkAllLast)
  830. $items[]=$item;
  831. else
  832. array_unshift($items,$item);
  833. $name=strtr($name,array('['=>'\\[',']'=>'\\]'));
  834. $js=<<<EOD
  835. jQuery('#$id').click(function() {
  836. jQuery("input[name='$name']").attr('checked', this.checked);
  837. });
  838. jQuery("input[name='$name']").click(function() {
  839. jQuery('#$id').attr('checked', !jQuery("input[name='$name']:not(:checked)").length);
  840. });
  841. jQuery('#$id').attr('checked', !jQuery("input[name='$name']:not(:checked)").length);
  842. EOD;
  843. $cs=Yii::app()->getClientScript();
  844. $cs->registerCoreScript('jquery');
  845. $cs->registerScript($id,$js);
  846. }
  847. return implode($separator,$items);
  848. }
  849. /**
  850. * Generates a radio button list.
  851. * A radio button list is like a {@link checkBoxList check box list}, except that
  852. * it only allows single selection.
  853. * @param string $name name of the radio button list. You can use this name to retrieve
  854. * the selected value(s) once the form is submitted.
  855. * @param mixed $select selection of the radio buttons. This can be either a string
  856. * for single selection or an array for multiple selections.
  857. * @param array $data value-label pairs used to generate the radio button list.
  858. * Note, the values will be automatically HTML-encoded, while the labels will not.
  859. * @param array $htmlOptions addtional HTML options. The options will be applied to
  860. * each radio button input. The following special options are recognized:
  861. * <ul>
  862. * <li>template: string, specifies how each radio button is rendered. Defaults
  863. * to "{input} {label}", where "{input}" will be replaced by the generated
  864. * radio button input tag while "{label}" will be replaced by the corresponding radio button label.</li>
  865. * <li>separator: string, specifies the string that separates the generated radio buttons.</li>
  866. * <li>labelOptions: array, specifies the additional HTML attributes to be rendered
  867. * for every label tag in the list. This option has been available since version 1.0.10.</li>
  868. * </ul>
  869. * @return string the generated radio button list
  870. */
  871. public static function radioButtonList($name,$select,$data,$htmlOptions=array())
  872. {
  873. $template=isset($htmlOptions['template'])?$htmlOptions['template']:'{input} {label}';
  874. $separator=isset($htmlOptions['separator'])?$htmlOptions['separator']:"<br/>\n";
  875. unset($htmlOptions['template'],$htmlOptions['separator']);
  876. $labelOptions=isset($htmlOptions['labelOptions'])?$htmlOptions['labelOptions']:array();
  877. unset($htmlOptions['labelOptions']);
  878. $items=array();
  879. $baseID=self::getIdByName($name);
  880. $id=0;
  881. foreach($data as $value=>$label)
  882. {
  883. $checked=!strcmp($value,$select);
  884. $htmlOptions['value']=$value;
  885. $htmlOptions['id']=$baseID.'_'.$id++;
  886. $option=self::radioButton($name,$checked,$htmlOptions);
  887. $label=self::label($label,$htmlOptions['id'],$labelOptions);
  888. $items[]=strtr($template,array('{input}'=>$option,'{label}'=>$label));
  889. }
  890. return implode($separator,$items);
  891. }
  892. /**
  893. * Generates a link that can initiate AJAX requests.
  894. * @param string $text the link body (it will NOT be HTML-encoded.)
  895. * @param mixed $url the URL for the AJAX request. If empty, it is assumed to be the current URL. See {@link normalizeUrl} for more details.
  896. * @param array $ajaxOptions AJAX options (see {@link ajax})
  897. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  898. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  899. * @return string the generated link
  900. * @see normalizeUrl
  901. * @see ajax
  902. */
  903. public static function ajaxLink($text,$url,$ajaxOptions=array(),$htmlOptions=array())
  904. {
  905. if(!isset($htmlOptions['href']))
  906. $htmlOptions['href']='#';
  907. $ajaxOptions['url']=$url;
  908. $htmlOptions['ajax']=$ajaxOptions;
  909. self::clientChange('click',$htmlOptions);
  910. return self::tag('a',$htmlOptions,$text);
  911. }
  912. /**
  913. * Generates a push button that can initiate AJAX requests.
  914. * @param string $label the button label
  915. * @param mixed $url the URL for the AJAX request. If empty, it is assumed to be the current URL. See {@link normalizeUrl} for more details.
  916. * @param array $ajaxOptions AJAX options (see {@link ajax})
  917. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  918. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  919. * @return string the generated button
  920. */
  921. public static function ajaxButton($label,$url,$ajaxOptions=array(),$htmlOptions=array())
  922. {
  923. $ajaxOptions['url']=$url;
  924. $htmlOptions['ajax']=$ajaxOptions;
  925. return self::button($label,$htmlOptions);
  926. }
  927. /**
  928. * Generates a push button that can submit the current form in POST method.
  929. * @param string $label the button label
  930. * @param mixed $url the URL for the AJAX request. If empty, it is assumed to be the current URL. See {@link normalizeUrl} for more details.
  931. * @param array $ajaxOptions AJAX options (see {@link ajax})
  932. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  933. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  934. * @return string the generated button
  935. */
  936. public static function ajaxSubmitButton($label,$url,$ajaxOptions=array(),$htmlOptions=array())
  937. {
  938. $ajaxOptions['type']='POST';
  939. $htmlOptions['type']='submit';
  940. return self::ajaxButton($label,$url,$ajaxOptions,$htmlOptions);
  941. }
  942. /**
  943. * Generates the JavaScript that initiates an AJAX request.
  944. * @param array $options AJAX options. The valid options are specified in the jQuery ajax documentation.
  945. * The following special options are added for convenience:
  946. * <ul>
  947. * <li>update: string, specifies the selector whose HTML content should be replaced
  948. * by the AJAX request result.</li>
  949. * <li>replace: string, specifies the selector whose target should be replaced
  950. * by the AJAX request result.</li>
  951. * </ul>
  952. * Note, if you specify the 'success' option, the above options will be ignored.
  953. * @return string the generated JavaScript
  954. * @see http://docs.jquery.com/Ajax/jQuery.ajax#options
  955. */
  956. public static function ajax($options)
  957. {
  958. Yii::app()->getClientScript()->registerCoreScript('jquery');
  959. if(!isset($options['url']))
  960. $options['url']='js:location.href';
  961. else
  962. $options['url']=self::normalizeUrl($options['url']);
  963. if(!isset($options['cache']))
  964. $options['cache']=false;
  965. if(!isset($options['data']) && isset($options['type']))
  966. $options['data']='js:jQuery(this).parents("form").serialize()';
  967. foreach(array('beforeSend','complete','error','success') as $name)
  968. {
  969. if(isset($options[$name]) && strpos($options[$name],'js:')!==0)
  970. $options[$name]='js:'.$options[$name];
  971. }
  972. if(isset($options['update']))
  973. {
  974. if(!isset($options['success']))
  975. $options['success']='js:function(html){jQuery("'.$options['update'].'").html(html)}';
  976. unset($options['update']);
  977. }
  978. if(isset($options['replace']))
  979. {
  980. if(!isset($options['success']))
  981. $options['success']='js:function(html){jQuery("'.$options['replace'].'").replaceWith(html)}';
  982. unset($options['replace']);
  983. }
  984. return 'jQuery.ajax('.CJavaScript::encode($options).');';
  985. }
  986. /**
  987. * Generates the URL for the published assets.
  988. * @param string $path the path of the asset to be published
  989. * @param boolean $hashByName whether the published directory should be named as the hashed basename.
  990. * If false, the name will be the hashed dirname of the path being published.
  991. * Defaults to false. Set true if the path being published is shared among
  992. * different extensions.
  993. * @return string the asset URL
  994. */
  995. public static function asset($path,$hashByName=false)
  996. {
  997. return Yii::app()->getAssetManager()->publish($path,$hashByName);
  998. }
  999. /**
  1000. * Normalizes the input parameter to be a valid URL.
  1001. *
  1002. * If the input parameter is an empty string, the currently requested URL will be returned.
  1003. *
  1004. * If the input parameter is a non-empty string, it is treated as a valid URL and will
  1005. * be returned without any change.
  1006. *
  1007. * If the input parameter is an array, it is treated as a controller route and a list of
  1008. * GET parameters, and the {@link CController::createUrl} method will be invoked to
  1009. * create a URL. In this case, the first array element refers to the controller route,
  1010. * and the rest key-value pairs refer to the additional GET parameters for the URL.
  1011. * For example, <code>array('post/list', 'page'=>3)</code> may be used to generate the URL
  1012. * <code>/index.php?r=post/list&page=3</code>.
  1013. *
  1014. * @param mixed $url the parameter to be used to generate a valid URL
  1015. * @return string the normalized URL
  1016. */
  1017. public static function normalizeUrl($url)
  1018. {
  1019. if(is_array($url))
  1020. {
  1021. if(isset($url[0]))
  1022. {
  1023. if(($c=Yii::app()->getController())!==null)
  1024. $url=$c->createUrl($url[0],array_splice($url,1));
  1025. else
  1026. $url=Yii::app()->createUrl($url[0],array_splice($url,1));
  1027. }
  1028. else
  1029. $url='';
  1030. }
  1031. return $url==='' ? Yii::app()->getRequest()->getUrl() : $url;
  1032. }
  1033. /**
  1034. * Generates an input HTML tag.
  1035. * This method generates an input HTML tag based on the given input name and value.
  1036. * @param string $type the input type (e.g. 'text', 'radio')
  1037. * @param string $name the input name
  1038. * @param string $value the input value
  1039. * @param array $htmlOptions additional HTML attributes for the HTML tag (see {@link tag}).
  1040. * @return string the generated input tag
  1041. */
  1042. protected static function inputField($type,$name,$value,$htmlOptions)
  1043. {
  1044. $htmlOptions['type']=$type;
  1045. $htmlOptions['value']=$value;
  1046. $htmlOptions['name']=$name;
  1047. if(!isset($htmlOptions['id']))
  1048. $htmlOptions['id']=self::getIdByName($name);
  1049. else if($htmlOptions['id']===false)
  1050. unset($htmlOptions['id']);
  1051. return self::tag('input',$htmlOptions);
  1052. }
  1053. /**
  1054. * Generates a label tag for a model attribute.
  1055. * The label text is the attribute label and the label is associated with
  1056. * the input for the attribute (see {@link CModel::getAttributeLabel}.
  1057. * If the attribute has input error, the label's CSS class will be appended with {@link errorCss}.
  1058. * @param CModel $model the data model
  1059. * @param string $attribute the attribute
  1060. * @param array $htmlOptions additional HTML attributes. The following special options are recognized:
  1061. * <ul>
  1062. * <li>required: if this is set and is true, the label will be styled
  1063. * with CSS class 'required' (customizable with CHtml::$requiredCss),
  1064. * and be decorated with {@link CHtml::beforeRequiredLabel} and
  1065. * {@link CHtml::afterRequiredLabel}. This option has been available since version 1.0.2.</li>
  1066. * <li>label: this specifies the label to be displayed. If this is not set,
  1067. * {@link CModel::getAttributeLabel} will be called to get the label for display.
  1068. * If the label is specified as false, no label will be rendered.
  1069. * This option has been available since version 1.0.4.</li>
  1070. * </ul>
  1071. * @return string the generated label tag
  1072. */
  1073. public static function activeLabel($model,$attribute,$htmlOptions=array())
  1074. {
  1075. if(isset($htmlOptions['for']))
  1076. {
  1077. $for=$htmlOptions['for'];
  1078. unset($htmlOptions['for']);
  1079. }
  1080. else
  1081. $for=self::getIdByName(self::resolveName($model,$attribute));
  1082. if(isset($htmlOptions['label']))
  1083. {
  1084. if(($label=$htmlOptions['label'])===false)
  1085. return '';
  1086. unset($htmlOptions['label']);
  1087. }
  1088. else
  1089. $label=$model->getAttributeLabel($attribute);
  1090. if($model->hasErrors($attribute))
  1091. self::addErrorCss($htmlOptions);
  1092. return self::label($label,$for,$htmlOptions);
  1093. }
  1094. /**
  1095. * Generates a label tag for a model attribute.
  1096. * This is an enhanced version of {@link activeLabel}. It will render additional
  1097. * CSS class and mark when the attribute is required.
  1098. * In particular, it calls {@link CModel::isAttributeRequired} to determine
  1099. * if the attribute is required.
  1100. * If so, it will add a CSS class {@link CHtml::requiredCss} to the label,
  1101. * and decorate the label with {@link CHtml::beforeRequiredLabel} and
  1102. * {@link CHtml::afterRequiredLabel}.
  1103. * @param CModel $model the data model
  1104. * @param string $attribute the attribute
  1105. * @param array $htmlOptions additional HTML attributes.
  1106. * @return string the generated label tag
  1107. * @since 1.0.2
  1108. */
  1109. public static function activeLabelEx($model,$attribute,$htmlOptions=array())
  1110. {
  1111. $realAttribute=$attribute;
  1112. self::resolveName($model,$attribute); // strip off square brackets if any
  1113. $htmlOptions['required']=$model->isAttributeRequired($attribute);
  1114. return self::activeLabel($model,$realAttribute,$htmlOptions);
  1115. }
  1116. /**
  1117. * Generates a text field input for a model attribute.
  1118. * If the attribute has input error, the input field's CSS class will
  1119. * be appended with {@link errorCss}.
  1120. * @param CModel $model the data model
  1121. * @param string $attribute the attribute
  1122. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  1123. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  1124. * @return string the generated input field
  1125. * @see clientChange
  1126. * @see activeInputField
  1127. */
  1128. public static function activeTextField($model,$attribute,$htmlOptions=array())
  1129. {
  1130. self::resolveNameID($model,$attribute,$htmlOptions);
  1131. self::clientChange('change',$htmlOptions);
  1132. return self::activeInputField('text',$model,$attribute,$htmlOptions);
  1133. }
  1134. /**
  1135. * Generates a hidden input for a model attribute.
  1136. * @param CModel $model the data model
  1137. * @param string $attribute the attribute
  1138. * @param array $htmlOptions additional HTML attributes.
  1139. * @return string the generated input field
  1140. * @see activeInputField
  1141. */
  1142. public static function activeHiddenField($model,$attribute,$htmlOptions=array())
  1143. {
  1144. self::resolveNameID($model,$attribute,$htmlOptions);
  1145. return self::activeInputField('hidden',$model,$attribute,$htmlOptions);
  1146. }
  1147. /**
  1148. * Generates a password field input for a model attribute.
  1149. * If the attribute has input error, the input field's CSS class will
  1150. * be appended with {@link errorCss}.
  1151. * @param CModel $model the data model
  1152. * @param string $attribute the attribute
  1153. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  1154. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  1155. * @return string the generated input field
  1156. * @see clientChange
  1157. * @see activeInputField
  1158. */
  1159. public static function activePasswordField($model,$attribute,$htmlOptions=array())
  1160. {
  1161. self::resolveNameID($model,$attribute,$htmlOptions);
  1162. self::clientChange('change',$htmlOptions);
  1163. return self::activeInputField('password',$model,$attribute,$htmlOptions);
  1164. }
  1165. /**
  1166. * Generates a text area input for a model attribute.
  1167. * If the attribute has input error, the input field's CSS class will
  1168. * be appended with {@link errorCss}.
  1169. * @param CModel $model the data model
  1170. * @param string $attribute the attribute
  1171. * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special
  1172. * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.)
  1173. * @return string the generated text area
  1174. * @see clientChange
  1175. */
  1176. public static function activeTextArea($model,$attribute,$htmlOptions=array())
  1177. {
  1178. self::resolveNameID($model,$attribute,$htmlOptions);
  1179. self::clientChange('change',$htmlOptions);
  1180. if($model->hasErrors($attribute))
  1181. self::addErrorCss($htmlOptions);
  1182. $text=self::resolveValue($model,$attribute);
  1183. return self::tag('textarea',$htmlOptions,isset($htmlOptions['encode']) && !$htmlOptions['encode'] ? $text : self::encode($text));
  1184. }
  1185. /**
  1186. *