PageRenderTime 66ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/framework/web/helpers/CHtml.php

https://bitbucket.org/negge/tlklan2
PHP | 2221 lines | 1107 code | 109 blank | 1005 comment | 138 complexity | 6b2e4e429e880392769792b7690aba10 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause, GPL-3.0

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

Large files files are truncated, but you can click here to view the full file