Common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 20456 2009-05-10 17:27:44Z ivantcholakov $
  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. global $charset;
  126. if (is_array($attributes)) {
  127. foreach ($attributes as $key => $value) {
  128. $strAttr .= ' ' . $key . '="' . @htmlspecialchars($value, ENT_QUOTES, $charset) . '"';
  129. }
  130. }
  131. return $strAttr;
  132. } // end func _getAttrString
  133. /**
  134. * Returns a valid atrributes array from either a string or array
  135. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  136. * @access private
  137. */
  138. function _parseAttributes($attributes)
  139. {
  140. if (is_array($attributes)) {
  141. $ret = array();
  142. foreach ($attributes as $key => $value) {
  143. if (is_int($key)) {
  144. $key = $value = strtolower($value);
  145. } else {
  146. $key = strtolower($key);
  147. }
  148. $ret[$key] = $value;
  149. }
  150. return $ret;
  151. } elseif (is_string($attributes)) {
  152. $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
  153. "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
  154. if (preg_match_all($preg, $attributes, $regs)) {
  155. for ($counter=0; $counter<count($regs[1]); $counter++) {
  156. $name = $regs[1][$counter];
  157. $check = $regs[0][$counter];
  158. $value = $regs[7][$counter];
  159. if (trim($name) == trim($check)) {
  160. $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
  161. } else {
  162. if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
  163. $value = substr($value, 1, -1);
  164. }
  165. $arrAttr[strtolower(trim($name))] = trim($value);
  166. }
  167. }
  168. return $arrAttr;
  169. }
  170. }
  171. } // end func _parseAttributes
  172. /**
  173. * Returns the array key for the given non-name-value pair attribute
  174. *
  175. * @param string $attr Attribute
  176. * @param array $attributes Array of attribute
  177. * @since 1.0
  178. * @access private
  179. * @return array key
  180. * @throws
  181. */
  182. function _getAttrKey($attr, $attributes)
  183. {
  184. if (isset($attributes[strtolower($attr)])) {
  185. return true;
  186. } else {
  187. return null;
  188. }
  189. } //end func _getAttrKey
  190. /**
  191. * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
  192. * @param array $attr1 Original attributes array
  193. * @param array $attr2 New attributes array
  194. * @access private
  195. * @return array
  196. */
  197. function _updateAttrArray(&$attr1, $attr2)
  198. {
  199. if (!is_array($attr2)) {
  200. return false;
  201. }
  202. foreach ($attr2 as $key => $value) {
  203. $attr1[$key] = $value;
  204. }
  205. } // end func _updateAtrrArray
  206. /**
  207. * Removes the given attribute from the given array
  208. *
  209. * @param string $attr Attribute name
  210. * @param array $attributes Attribute array
  211. * @since 1.4
  212. * @access public
  213. * @return void
  214. * @throws
  215. */
  216. function _removeAttr($attr, &$attributes)
  217. {
  218. $attr = strtolower($attr);
  219. if (isset($attributes[$attr])) {
  220. unset($attributes[$attr]);
  221. }
  222. } //end func _removeAttr
  223. /**
  224. * Returns the value of the given attribute
  225. *
  226. * @param string $attr Attribute name
  227. * @since 1.5
  228. * @access public
  229. * @return void
  230. * @throws
  231. */
  232. function getAttribute($attr)
  233. {
  234. $attr = strtolower($attr);
  235. if (isset($this->_attributes[$attr])) {
  236. return $this->_attributes[$attr];
  237. }
  238. return null;
  239. } //end func getAttribute
  240. /**
  241. * Sets the HTML attributes
  242. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  243. * @access public
  244. */
  245. function setAttributes($attributes)
  246. {
  247. $this->_attributes = $this->_parseAttributes($attributes);
  248. } // end func setAttributes
  249. /**
  250. * Returns the assoc array (default) or string of attributes
  251. *
  252. * @param bool Whether to return the attributes as string
  253. * @since 1.6
  254. * @access public
  255. * @return mixed attributes
  256. */
  257. function getAttributes($asString = false)
  258. {
  259. if ($asString) {
  260. return $this->_getAttrString($this->_attributes);
  261. } else {
  262. return $this->_attributes;
  263. }
  264. } //end func getAttributes
  265. /**
  266. * Updates the passed attributes without changing the other existing attributes
  267. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  268. * @access public
  269. */
  270. function updateAttributes($attributes)
  271. {
  272. $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
  273. } // end func updateAttributes
  274. /**
  275. * Removes an attribute
  276. *
  277. * @param string $attr Attribute name
  278. * @since 1.4
  279. * @access public
  280. * @return void
  281. * @throws
  282. */
  283. function removeAttribute($attr)
  284. {
  285. $this->_removeAttr($attr, $this->_attributes);
  286. } //end func removeAttribute
  287. /**
  288. * Sets the line end style to Windows, Mac, Unix or a custom string.
  289. *
  290. * @param string $style "win", "mac", "unix" or custom string.
  291. * @since 1.7
  292. * @access public
  293. * @return void
  294. */
  295. function setLineEnd($style)
  296. {
  297. switch ($style) {
  298. case 'win':
  299. $this->_lineEnd = "\15\12";
  300. break;
  301. case 'unix':
  302. $this->_lineEnd = "\12";
  303. break;
  304. case 'mac';
  305. $this->_lineEnd = "\15";
  306. break;
  307. default:
  308. $this->_lineEnd = $style;
  309. }
  310. } // end func setLineEnd
  311. /**
  312. * Sets the tab offset
  313. *
  314. * @param int $offset
  315. * @access public
  316. */
  317. function setTabOffset($offset)
  318. {
  319. $this->_tabOffset = $offset;
  320. } // end func setTabOffset
  321. /**
  322. * Returns the tabOffset
  323. *
  324. * @since 1.5
  325. * @access public
  326. * @return int
  327. */
  328. function getTabOffset()
  329. {
  330. return $this->_tabOffset;
  331. } //end func getTabOffset
  332. /**
  333. * Sets the string used to indent HTML
  334. *
  335. * @since 1.7
  336. * @param string $string String used to indent ("\11", "\t", ' ', etc.).
  337. * @access public
  338. * @return void
  339. */
  340. function setTab($string)
  341. {
  342. $this->_tab = $string;
  343. } // end func setTab
  344. /**
  345. * Sets the HTML comment to be displayed at the beginning of the HTML string
  346. *
  347. * @param string
  348. * @since 1.4
  349. * @access public
  350. * @return void
  351. */
  352. function setComment($comment)
  353. {
  354. $this->_comment = $comment;
  355. } // end func setHtmlComment
  356. /**
  357. * Returns the HTML comment
  358. *
  359. * @since 1.5
  360. * @access public
  361. * @return string
  362. */
  363. function getComment()
  364. {
  365. return $this->_comment;
  366. } //end func getComment
  367. /**
  368. * Abstract method. Must be extended to return the objects HTML
  369. *
  370. * @access public
  371. * @return string
  372. * @abstract
  373. */
  374. function toHtml()
  375. {
  376. return '';
  377. } // end func toHtml
  378. /**
  379. * Displays the HTML to the screen
  380. *
  381. * @access public
  382. */
  383. function display()
  384. {
  385. print $this->toHtml();
  386. } // end func display
  387. } // end class HTML_Common
  388. ?>