phpDocumentor page
[ class tree: page ] [ index: page ] [ all elements ]

Source for file Text.php

Documentation is available at Text.php

  1. <?
  2. /**
  3. * @package page
  4. * @filesource
  5. * @see HTML_PAGE_UTIL_PATH.'/Text.php'
  6. * @copyright (c) http://Finn-Rasmussen.com
  7. * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
  8. * @author http://Finn-Rasmussen.com
  9. * @version 1.9
  10. * @since 21-oct-2005
  11. */
  12.  
  13. /**
  14. * The required files
  15. */
  16. require_once(HTML_PATH.'/Html.php');
  17. if (defined('HTML_LOG_UTIL_PATH')) {
  18. require_once(HTML_LOG_UTIL_PATH.'/Log.php');
  19. }
  20.  
  21. /**
  22. * Generates a plain Text object, surronded by a html element
  23. * <code>
  24. * Usage:
  25. * $text = new Text($text,$element,$class,$name);
  26. * print $text->getHtml();
  27. * Or
  28. * Text::display($text,$element,$class,$name);
  29. * </code>
  30. * @package page
  31. */
  32.  
  33. class Text extends Html {
  34. var $text = ''; // The text to show
  35. var $element = ''; // The html element
  36. var $class = ''; // The class name
  37. var $name = ''; // The name/id for a link identifier
  38.  
  39.  
  40. /**
  41. * Constructor
  42. * @param String $text The text to show
  43. * @param String $element The html element, if any
  44. * @param String $class The css class name
  45. * @param String $name The name/id attribute for a link identifier
  46. */
  47. function Text($text='',$element='',$class='',$name='') {
  48. $this->Html();
  49. $this->text = $text;
  50. $this->element = $element;
  51. $this->class = $class;
  52. $this->name = $name;
  53. if ($this->element!='') {
  54. switch ($this->element) {
  55. case 'pre':
  56. case 'div':
  57. case 'p':
  58. case 'hr':
  59. case 'br':
  60. case 'ol':
  61. case 'ul':
  62. case 'li':
  63. case 'b':
  64. case 'h1':
  65. case 'h2':
  66. case 'h3':
  67. case 'h4':
  68. case 'h5':
  69. case 'h6':
  70. // Ok
  71. break;
  72. default:
  73. $msg = $this->getClassName().' unsupported html element, found='.$this->element;
  74. if (defined('HTML_LOG_UTIL_PATH')) {
  75. Log::fatal(__FILE__,__LINE__,$msg);
  76. } else {
  77. // TODO what ?
  78. die($this->getMsg(LOG_LEVEL_FATAL, $msg));
  79. }
  80. }
  81. }
  82. }
  83.  
  84. /**
  85. * Returns the start html for the text, optional surronded by the html element
  86. * @return String the complete html
  87. */
  88. function getStart() {
  89. $html = '';
  90. if ($this->element!='') {
  91. if ($this->element!='br' && $this->element!='hr') {
  92. $html .= $this->html;
  93. $html .= '<'.$this->element;
  94. $html .= $this->getAttribute('class');
  95. $html .= ">";
  96. switch ($this->element) {
  97. case 'h1':
  98. case 'h2':
  99. case 'h3':
  100. case 'h4':
  101. case 'h5':
  102. case 'h6':
  103. case 'p':
  104. case 'li':
  105. break;
  106. default:
  107. $html .= "\r\n";
  108. break;
  109. }
  110. } else {
  111. // <br />, <hr /> See getEnd()
  112. }
  113. }
  114. return $html;
  115. }
  116.  
  117. /**
  118. * Returns the end html for the text, optional surronded by the html element
  119. * @return String the complete html
  120. */
  121. function getEnd() {
  122. $html = '';
  123. if ($this->element!='') {
  124. if ($this->element!='br' && $this->element!='hr') {
  125. $html .= '</'.$this->element.">\r\n";
  126. } else {
  127. $html .= '<'.$this->element." />\r\n"; // <br />
  128. }
  129. }
  130. return $html;
  131. }
  132.  
  133. /**
  134. * Returns the html for the text, optional surronded by the html element
  135. * @return String the complete html
  136. */
  137. function getHtml() {
  138. $html = '';
  139. if ($this->text=='' && $this->element=='' && $this->getSizeof()==0) {
  140. $msg = $this->getClassName().'->getHtml(), no text or elements found';
  141. if (defined('HTML_LOG_UTIL_PATH')) {
  142. Log::warn(__FILE__,__LINE__,$msg);
  143. $html .= TEXT_NO_DATA.'<!-- '.$this->getMsg(LOG_LEVEL_ERROR, $msg)." -->\r\n";
  144. } else {
  145. $html .= TEXT_NO_DATA.'<!-- '.$msg." -->\r\n";
  146. }
  147. }
  148. $elem = '';
  149. if ($this->getSizeof ()>0) {
  150. $elem .= $this->getElements();
  151. }
  152. if ($this->text!='' || $elem !='' && $this->getSizeof ()>0) {
  153. $html .= $this->getStart();
  154. }
  155. if ($this->text!='') {
  156. if ($this->name!='') {
  157. switch ($this->element) {
  158. case 'h1':
  159. case 'h2':
  160. case 'h3':
  161. case 'h4':
  162. case 'h5':
  163. case 'h6':
  164. // i.e. <h1><a name=""name>text</a></h1>
  165. $link = new Link($this->text,'','','','','',$this->name);
  166. $html .= $link->getHtml();
  167. break;
  168. default:
  169. // Ignore, Use standard
  170. $html .= htmlspecialchars(stripslashes($this->text)); // Translate html special chars
  171. break;
  172. }
  173. } else {
  174. $html .= htmlspecialchars(stripslashes($this->text)); // Translate html special chars
  175. }
  176. }
  177. if ($elem !='') {
  178. $html .= $elem;
  179. }
  180. if ($this->text!='' || $elem !='' || $this->element!='') { // Removed && $this->getSizeof ()>0
  181. $html .= $this->getEnd();
  182. }
  183. return $html;
  184. }
  185.  
  186. /**
  187. * Display start
  188. * <code>
  189. * Usage:
  190. * Text::start($element,$class);
  191. * </code>
  192. * @static
  193. * @param String $element The html element, if any
  194. * @param String $class The css class name
  195. */
  196. function start($element='',$class='') {
  197. $html = new Text('',$element,$class);
  198. $html->addHtml($html->getStart());
  199. }
  200.  
  201. /**
  202. * Display end
  203. * <code>
  204. * Usage:
  205. * Text::end($element);
  206. * </code>
  207. * @static
  208. * @param String $element The html element, if any
  209. */
  210. function end($element='') {
  211. $html = new Text('',$element);
  212. $html->addHtml($html->getEnd());
  213. }
  214.  
  215. /**
  216. * Display html
  217. * <code>
  218. * Usage:
  219. * Text::display($text,$element,$class,$name);
  220. * </code>
  221. * @static
  222. * @param String $text The text to show
  223. * @param String $element The html element, if any
  224. * @param String $class The css class name
  225. * @param String $name The name/id attribute for a link identifier
  226. */
  227. function display($text='',$element='',$class='',$name='') {
  228. $html = new Text($text,$element,$class,$name);
  229. $html->addHtml();
  230. }
  231. }
  232. ?>

Documentation generated on Thu, 22 Dec 2005 17:20:16 +0100 by phpDocumentor 1.3.0RC3