PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/pearlib/HTML/Common.php

https://code.google.com/p/s3db/
PHP | 419 lines | 306 code | 15 blank | 98 comment | 6 complexity | 26e35251570e31cf30cfee842bdcb2b5 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Adam Daniel <adaniel1@eesus.jnj.com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Common.php,v 1.8 2003/06/19 18:52:50 mansion Exp $
  20. /**
  21. * Base class for all HTML classes
  22. *
  23. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  24. * @version 1.7
  25. * @since PHP 4.0.3pl1
  26. * @abstract
  27. */
  28. class HTML_Common {
  29. /**
  30. * Associative array of table attributes
  31. * @var array
  32. * @access private
  33. */
  34. var $_attributes = array();
  35. /**
  36. * Tab offset of the table
  37. * @var int
  38. * @access private
  39. */
  40. var $_tabOffset = 0;
  41. /**
  42. * Tab string
  43. * @var string
  44. * @since 1.7
  45. * @access private
  46. */
  47. var $_tab = "\11";
  48. /**
  49. * Contains the line end string
  50. * @var string
  51. * @since 1.7
  52. * @access private
  53. */
  54. var $_lineEnd = "\12";
  55. /**
  56. * HTML comment on the object
  57. * @var string
  58. * @since 1.5
  59. * @access private
  60. */
  61. var $_comment = '';
  62. /**
  63. * Class constructor
  64. * @param mixed $attributes Associative array of table tag attributes
  65. * or HTML attributes name="value" pairs
  66. * @param int $tabOffset Indent offset in tabs
  67. * @access public
  68. */
  69. function HTML_Common($attributes = null, $tabOffset = 0)
  70. {
  71. $this->setAttributes($attributes);
  72. $this->setTabOffset($tabOffset);
  73. } // end constructor
  74. /**
  75. * Returns the current API version
  76. * @access public
  77. * @returns double
  78. */
  79. function apiVersion()
  80. {
  81. return 1.7;
  82. } // end func apiVersion
  83. /**
  84. * Returns the lineEnd
  85. *
  86. * @since 1.7
  87. * @access private
  88. * @return string
  89. * @throws
  90. */
  91. function _getLineEnd()
  92. {
  93. return $this->_lineEnd;
  94. } // end func getLineEnd
  95. /**
  96. * Returns a string containing the unit for indenting HTML
  97. *
  98. * @since 1.7
  99. * @access private
  100. * @return string
  101. */
  102. function _getTab()
  103. {
  104. return $this->_tab;
  105. } // end func _getTab
  106. /**
  107. * Returns a string containing the offset for the whole HTML code
  108. *
  109. * @return string
  110. * @access private
  111. */
  112. function _getTabs()
  113. {
  114. return str_repeat($this->_getTab(), $this->_tabOffset);
  115. } // end func _getTabs
  116. /**
  117. * Returns an HTML formatted attribute string
  118. * @param array $attributes
  119. * @return string
  120. * @access private
  121. */
  122. function _getAttrString($attributes)
  123. {
  124. $strAttr = '';
  125. if (is_array($attributes)) {
  126. foreach ($attributes as $key => $value) {
  127. $strAttr .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
  128. }
  129. }
  130. return $strAttr;
  131. } // end func _getAttrString
  132. /**
  133. * Returns a valid atrributes array from either a string or array
  134. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  135. * @access private
  136. */
  137. function _parseAttributes($attributes)
  138. {
  139. if (is_array($attributes)) {
  140. $ret = array();
  141. foreach ($attributes as $key => $value) {
  142. if (is_int($key)) {
  143. $key = $value = strtolower($value);
  144. } else {
  145. $key = strtolower($key);
  146. }
  147. $ret[$key] = $value;
  148. }
  149. return $ret;
  150. } elseif (is_string($attributes)) {
  151. $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
  152. "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
  153. if (preg_match_all($preg, $attributes, $regs)) {
  154. for ($counter=0; $counter<count($regs[1]); $counter++) {
  155. $name = $regs[1][$counter];
  156. $check = $regs[0][$counter];
  157. $value = $regs[7][$counter];
  158. if (trim($name) == trim($check)) {
  159. $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
  160. } else {
  161. if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
  162. $value = substr($value, 1, -1);
  163. }
  164. $arrAttr[strtolower(trim($name))] = trim($value);
  165. }
  166. }
  167. return $arrAttr;
  168. }
  169. }
  170. } // end func _parseAttributes
  171. /**
  172. * Returns the array key for the given non-name-value pair attribute
  173. *
  174. * @param string $attr Attribute
  175. * @param array $attributes Array of attribute
  176. * @since 1.0
  177. * @access private
  178. * @return array key
  179. * @throws
  180. */
  181. function _getAttrKey($attr, $attributes)
  182. {
  183. if (isset($attributes[strtolower($attr)])) {
  184. return true;
  185. } else {
  186. return null;
  187. }
  188. } //end func _getAttrKey
  189. /**
  190. * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
  191. * @param array $attr1 Original attributes array
  192. * @param array $attr2 New attributes array
  193. * @access private
  194. * @return array
  195. */
  196. function _updateAttrArray(&$attr1, $attr2)
  197. {
  198. if (!is_array($attr2)) {
  199. return false;
  200. }
  201. foreach ($attr2 as $key => $value) {
  202. $attr1[$key] = $value;
  203. }
  204. } // end func _updateAtrrArray
  205. /**
  206. * Removes the given attribute from the given array
  207. *
  208. * @param string $attr Attribute name
  209. * @param array $attributes Attribute array
  210. * @since 1.4
  211. * @access public
  212. * @return void
  213. * @throws
  214. */
  215. function _removeAttr($attr, &$attributes)
  216. {
  217. $attr = strtolower($attr);
  218. if (isset($attributes[$attr])) {
  219. unset($attributes[$attr]);
  220. }
  221. } //end func _removeAttr
  222. /**
  223. * Returns the value of the given attribute
  224. *
  225. * @param string $attr Attribute name
  226. * @since 1.5
  227. * @access public
  228. * @return void
  229. * @throws
  230. */
  231. function getAttribute($attr)
  232. {
  233. $attr = strtolower($attr);
  234. if (isset($this->_attributes[$attr])) {
  235. return $this->_attributes[$attr];
  236. }
  237. return null;
  238. } //end func getAttribute
  239. /**
  240. * Sets the HTML attributes
  241. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  242. * @access public
  243. */
  244. function setAttributes($attributes)
  245. {
  246. $this->_attributes = $this->_parseAttributes($attributes);
  247. } // end func setAttributes
  248. /**
  249. * Returns the assoc array (default) or string of attributes
  250. *
  251. * @param bool Whether to return the attributes as string
  252. * @since 1.6
  253. * @access public
  254. * @return mixed attributes
  255. */
  256. function getAttributes($asString = false)
  257. {
  258. if ($asString) {
  259. return $this->_getAttrString($this->_attributes);
  260. } else {
  261. return $this->_attributes;
  262. }
  263. } //end func getAttributes
  264. /**
  265. * Updates the passed attributes without changing the other existing attributes
  266. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  267. * @access public
  268. */
  269. function updateAttributes($attributes)
  270. {
  271. $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
  272. } // end func updateAttributes
  273. /**
  274. * Removes an attribute
  275. *
  276. * @param string $attr Attribute name
  277. * @since 1.4
  278. * @access public
  279. * @return void
  280. * @throws
  281. */
  282. function removeAttribute($attr)
  283. {
  284. $this->_removeAttr($attr, $this->_attributes);
  285. } //end func removeAttribute
  286. /**
  287. * Sets the line end style to Windows, Mac, Unix or a custom string.
  288. *
  289. * @param string $style "win", "mac", "unix" or custom string.
  290. * @since 1.7
  291. * @access public
  292. * @return void
  293. */
  294. function setLineEnd($style)
  295. {
  296. switch ($style) {
  297. case 'win':
  298. $this->_lineEnd = "\15\12";
  299. break;
  300. case 'unix':
  301. $this->_lineEnd = "\12";
  302. break;
  303. case 'mac';
  304. $this->_lineEnd = "\15";
  305. break;
  306. default:
  307. $this->_lineEnd = $style;
  308. }
  309. } // end func setLineEnd
  310. /**
  311. * Sets the tab offset
  312. *
  313. * @param int $offset
  314. * @access public
  315. */
  316. function setTabOffset($offset)
  317. {
  318. $this->_tabOffset = $offset;
  319. } // end func setTabOffset
  320. /**
  321. * Returns the tabOffset
  322. *
  323. * @since 1.5
  324. * @access public
  325. * @return int
  326. */
  327. function getTabOffset()
  328. {
  329. return $this->_tabOffset;
  330. } //end func getTabOffset
  331. /**
  332. * Sets the string used to indent HTML
  333. *
  334. * @since 1.7
  335. * @param string $string String used to indent ("\11", "\t", ' ', etc.).
  336. * @access public
  337. * @return void
  338. */
  339. function setTab($string)
  340. {
  341. $this->_tab = $string;
  342. } // end func setTab
  343. /**
  344. * Sets the HTML comment to be displayed at the beginning of the HTML string
  345. *
  346. * @param string
  347. * @since 1.4
  348. * @access public
  349. * @return void
  350. */
  351. function setComment($comment)
  352. {
  353. $this->_comment = $comment;
  354. } // end func setHtmlComment
  355. /**
  356. * Returns the HTML comment
  357. *
  358. * @since 1.5
  359. * @access public
  360. * @return string
  361. */
  362. function getComment()
  363. {
  364. return $this->_comment;
  365. } //end func getComment
  366. /**
  367. * Abstract method. Must be extended to return the objects HTML
  368. *
  369. * @access public
  370. * @return string
  371. * @abstract
  372. */
  373. function toHtml()
  374. {
  375. return '';
  376. } // end func toHtml
  377. /**
  378. * Displays the HTML to the screen
  379. *
  380. * @access public
  381. */
  382. function display()
  383. {
  384. print $this->toHtml();
  385. } // end func display
  386. } // end class HTML_Common
  387. ?>