PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ePHP/libraries/Html.class.php

http://php-framework-ephp.googlecode.com/
PHP | 413 lines | 236 code | 49 blank | 128 comment | 44 complexity | 58dfb00356f45e2127971b0cc6f66ecd MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. +------------------------------------------------------------------------------
  4. * Html???
  5. * html???????????
  6. +------------------------------------------------------------------------------
  7. * @version 3.0
  8. * @author WangXian
  9. * @email admin@loopx.cn
  10. * @package libraries
  11. * @creation_date 2010-10-17
  12. * @last_modified 2010-12-25 19:02:12
  13. +------------------------------------------------------------------------------
  14. */
  15. class Html
  16. {
  17. /**
  18. * html select??????
  19. * ??str1 str1?????????selected ?????
  20. * @param string $str1
  21. * @param string $str2
  22. */
  23. public static function selected($str1,$str2)
  24. {return $str1 == $str2 ? 'selected="selected"':'';}
  25. /**
  26. * html checkbox??????
  27. * ??str1 str1?????????selected ?????
  28. * @param string $str1
  29. * @param string $str2
  30. */
  31. public static function checked($str1,$str2)
  32. {return $str1 == $str2 ? 'checked="checked"':'';}
  33. /**
  34. * ??a?????????????
  35. * @param string $uri '???/action'
  36. * @param string $text '????'
  37. * @return string
  38. */
  39. public static function a($uri, $text='')
  40. {
  41. $text = ($text == '') ? $uri : $text;
  42. return '<a href="'. U($uri) .'" title="">'. $text .'</a>';
  43. }
  44. /**
  45. * ??img?????
  46. * @param string $pic ??url
  47. * @param string $alt ??alt??
  48. * @return string
  49. */
  50. public static function img($pic, $alt='')
  51. {
  52. return '<img src="'. $pic .'" alt="'. $alt .'" />';
  53. }
  54. /**
  55. * ??n?br??
  56. * @param integer $repeat
  57. * @return string
  58. */
  59. public static function br($repeat = 1)
  60. {
  61. return str_repeat("<br />\n", $repeat);
  62. }
  63. /**
  64. * ??n?html??
  65. * @param integer $repeat
  66. * @return string
  67. */
  68. public static function nbsp($repeat = 1)
  69. {
  70. return str_repeat("&nbsp;", $repeat);
  71. }
  72. /**
  73. * ??html????link
  74. * ?????css?js?ico
  75. * @param $source ??????
  76. * @return string
  77. */
  78. public static function link_tag($source)
  79. {
  80. $ext = strrchr($source, '.');
  81. if($ext == '.css')
  82. return "<link href=\"{$source}\" rel=\"stylesheet\" type=\"text/css\" />";
  83. else if($ext == '.js' )
  84. return '<script type="text/javascript" src="'. $source .'"></script>';
  85. else if($ext == '.ico' )
  86. return '<link href="'. $source .'" rel="shortcut icon" type="image/ico" />';
  87. }
  88. /**
  89. * ??ul???html??
  90. * @param array $data ??????
  91. * @param array $attributes array('id'=>'', 'class'=>'')
  92. * @return string
  93. */
  94. public static function ul($data, $attributes=array())
  95. {
  96. if( ! is_array($data) ) return '';
  97. $attributes = $attributes + array('id'=>'', 'class'=>'');
  98. $str="<ul id=\"". $attributes['id'] ."\" class=\"". $attributes['class'] ."\">\n";
  99. foreach ($data as $k=>$v)
  100. {
  101. $str .= "\t<li>";
  102. $str .= is_array($v) ? $k.self::ul($v) : $v;
  103. $str .= "</li>\n";
  104. }
  105. $str .= "</ul>\n";
  106. return $str;
  107. }
  108. /**
  109. * ??ol???html??
  110. * @param array $data ?????????
  111. * @param array $attributes array('id'=>'', 'class'=>'')
  112. * @return string
  113. */
  114. public static function ol($data, $attributes=array())
  115. {
  116. if( ! is_array($data) ) return '';
  117. $attributes = $attributes + array('id'=>'', 'class'=>'');
  118. $str="<ol id=\"". $attributes['id'] ."\" class=\"". $attributes['class'] ."\">\n";
  119. foreach ($data as $k=>$v)
  120. {
  121. $str .= "\t<li>";
  122. $str .= is_array($v) ? $k.self::ol($v) : $v;
  123. $str .= "</li>\n";
  124. }
  125. $str .= "</ol>\n";
  126. return $str;
  127. }
  128. /**
  129. * ??html?table??
  130. * @param array $data ???????
  131. * @param array $title table??,????
  132. * @param array $attributes
  133. * @return string
  134. */
  135. public static function table($data,$title=array(),$attributes=array())
  136. {
  137. // ???????
  138. if( is_string($data) ) return $data;
  139. else if( is_array($data) && ! is_array(reset($data)) ) return implode(' ', $data);
  140. $attributes = $attributes + array('id'=>'', 'class'=>'');
  141. $str="<table id=\"". $attributes['id'] ."\" class=\"". $attributes['class'] ."\">\n";
  142. //table th
  143. if(!empty($title))
  144. {
  145. $str .= "\t<tr>\n";
  146. foreach ($title as $v) $str .= "\t\t<th>{$v}</th>\n";
  147. $str .= "\t</tr>\n";
  148. }
  149. foreach ($data as $v1)
  150. {
  151. $str .= "\t<tr>\n";
  152. foreach ($v1 as $k2=>$v2)
  153. {
  154. //???????td?id
  155. if(is_numeric($k2)) $str .= "\t\t<td>";
  156. else $str .= "\t\t<td id=\"tb_{$k2}\">";
  157. $str .= is_array($v2) ? self::table($v2) : $v2;
  158. $str .= "</td>\n";
  159. }
  160. $str .= "\t</tr>\n";
  161. }
  162. $str .= "</table>\n";
  163. return $str;
  164. }
  165. /**
  166. * ??form hidden??
  167. * -
  168. * @param mixed $name hidden???????????????
  169. * @param string $value
  170. */
  171. public static function form_hidden($name,$value='')
  172. {
  173. if(is_string($name))
  174. return '<input type="hidden" id="'. $name .'" name="'. $name .'" value="'. $value .'" />'."\n";
  175. elseif (is_array($name))
  176. {
  177. $hidden = '';
  178. foreach ($name as $k=>$v)
  179. {
  180. $hidden .= '<input type="hidden" id="'. $k .'" name="'. $k .'" value="'. $v .'" />'."\n";
  181. }
  182. return $hidden;
  183. }
  184. else return '';
  185. }
  186. /**
  187. * ???
  188. * Html::form_select(select?name?option????value?????|?????????,?????js?)
  189. * @param string $name
  190. * @param array $options
  191. * @param mixed $selected
  192. * @param string $extra
  193. * @return string
  194. */
  195. public static function form_select($name = '', $options = array(), $selected = array(), $extra = '')
  196. {
  197. if ( ! is_array($selected))
  198. {
  199. $selected = array($selected);
  200. }
  201. // If no selected state was submitted we will attempt to set it automatically
  202. if (count($selected) === 0)
  203. {
  204. // If the form name appears in the $_POST array we have a winner!
  205. if (isset($_POST[$name]))
  206. {
  207. $selected = array($_POST[$name]);
  208. }
  209. }
  210. if ($extra != '') $extra = ' '.$extra;
  211. $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
  212. $form = '<select name="'.$name.'" id="'.$name.'"'. $extra.$multiple .">\n";
  213. foreach ($options as $key => $val)
  214. {
  215. $key = (string) $key;
  216. if (is_array($val))
  217. {
  218. $form .= '<optgroup label="'.$key.'">'."\n";
  219. foreach ($val as $optgroup_key => $optgroup_val)
  220. {
  221. $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
  222. $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
  223. }
  224. $form .= '</optgroup>'."\n";
  225. }
  226. else
  227. {
  228. $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
  229. $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
  230. }
  231. }
  232. $form .= '</select>';
  233. return $form;
  234. }
  235. /**
  236. * ??checkbox
  237. * @param mixed $data
  238. * @param mixed $value
  239. * @param string $checked
  240. * @param string $extra
  241. * @return string
  242. */
  243. public static function form_checkbox($data='', $value='', $checked=FALSE, $extra='')
  244. {
  245. $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
  246. if (is_array($data) && array_key_exists('checked', $data))
  247. {
  248. $checked = $data['checked'];
  249. if ($checked == FALSE)
  250. {
  251. unset($data['checked']);
  252. }
  253. else
  254. {
  255. $data['checked'] = 'checked';
  256. }
  257. }
  258. if ($checked == TRUE)
  259. {
  260. $defaults['checked'] = 'checked';
  261. }
  262. else
  263. {
  264. unset($defaults['checked']);
  265. }
  266. return "<input ".self::_parse_form_attributes($data, $defaults).$extra." />\n";
  267. }
  268. /**
  269. * ??radio
  270. * @param mixed $data
  271. * @param mixed $value
  272. * @param string $checked
  273. * @param string $extra
  274. * @return string
  275. */
  276. public static function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
  277. {
  278. if ( ! is_array($data))
  279. {
  280. $data = array('name' => $data);
  281. }
  282. $data['type'] = 'radio';
  283. return self::form_checkbox($data, $value, $checked, $extra);
  284. }
  285. /**
  286. * Parse the form attributes
  287. * @ignore
  288. * @param mixed $attributes
  289. * @param mixed $default
  290. * @return string
  291. */
  292. private static function _parse_form_attributes($attributes, $default)
  293. {
  294. if (is_array($attributes))
  295. {
  296. foreach ($default as $key => $val)
  297. {
  298. if (isset($attributes[$key]))
  299. {
  300. $default[$key] = $attributes[$key];
  301. unset($attributes[$key]);
  302. }
  303. }
  304. if (count($attributes) > 0)
  305. {
  306. $default = array_merge($default, $attributes);
  307. }
  308. }
  309. $att = '';
  310. foreach ($default as $key => $val)
  311. {
  312. if ($key == 'value')
  313. {
  314. $val = self::form_prep($val, $default['name']);
  315. }
  316. $att .= $key . '="' . $val . '" ';
  317. }
  318. return $att;
  319. }
  320. /**
  321. * ??????
  322. * ??????????????HTML??(????)??????????
  323. * @param string $str
  324. * @param string $field_name
  325. * @return string
  326. */
  327. public static function form_prep($str = '', $field_name = '')
  328. {
  329. static $prepped_fields = array();
  330. // if the field name is an array we do this recursively
  331. if (is_array($str))
  332. {
  333. foreach ($str as $key => $val)
  334. {
  335. $str[$key] = self::form_prep($val);
  336. }
  337. return $str;
  338. }
  339. if ($str === '')
  340. {
  341. return '';
  342. }
  343. // we've already prepped a field with this name
  344. // @todo need to figure out a way to namespace this so
  345. // that we know the *exact* field and not just one with
  346. // the same name
  347. if (isset($prepped_fields[$field_name]))
  348. {
  349. return $str;
  350. }
  351. $str = htmlspecialchars($str);
  352. // In case htmlspecialchars misses these.
  353. $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
  354. if ($field_name != '')
  355. {
  356. $prepped_fields[$field_name] = $str;
  357. }
  358. return $str;
  359. }
  360. }
  361. /* End of file Html.class.php */
  362. /* Location: ./_framework/libraries/Html.class.php */