TaniPHP
[ class tree: TaniPHP ] [ index: TaniPHP ] [ all elements ]

Source for file html.php

Documentation is available at html.php

  1. <?php
  2. /**
  3. * This file is part of the TaniPHP Framework.
  4. * Copyright (C) 2005 by Dumitru Postoronca
  5. *
  6. * @package TaniPHP Helpers
  7. * @copyright Copyright &copy; 2005, Dumitru Postoronca
  8. * @license http://www.opensource.org/licenses/lgpl-license.php
  9. */
  10.  
  11. /**
  12. * HTML helper class
  13. *
  14. * @static
  15. * @package TaniPHP Helpers
  16. * @author Dumitru Postoronca
  17. */
  18. class HTML
  19. {
  20. /**
  21. * Will cycle thorough given array of values. Useful for nice table design.
  22. *
  23. * The following example will cycle the table row background making bluesh(#CCF) stripes
  24. * <code>
  25. * <?php foreach($i as $j) { ?>
  26. * <tr style="background-color: <?php echo HTML::cycle(array('#FFF', '#CCF'));?>">
  27. * <td><!-- show data --></td>
  28. * </tr>
  29. * <?php } ?>
  30. * </code>
  31. *
  32. * @static
  33. */
  34. function cycle($array)
  35. {
  36. static $i = 0;
  37. if($i >= count($array)) $i = 0;
  38. return $array[$i++];
  39. }
  40. /**
  41. * Will make a string more human-readable by replacing "_" with " ", and making words uppercase
  42. *
  43. * @static
  44. * @param string $word Word to "humanize"
  45. */
  46. function humanize($word)
  47. {
  48. $word = str_replace("_", " ", $word);
  49. return ucwords($word);
  50. }
  51. /**
  52. * Generates an URL according to config file
  53. *
  54. * @static
  55. * @param string $controller Controller to link to. Will use default controller if not set.
  56. * @param string $action Action to link to. Will use default action if not set.
  57. * @param array $params Associative array of parameters to pass by URL.
  58. */
  59. function getURL($controller = "", $action = "", $params=array())
  60. {
  61. if(empty($controller)) {
  62. $controller = Framework::getConfiguration('common', 'defaultController');
  63. }
  64. if(empty($action)) {
  65. $action = Framework::getConfiguration('common', 'defaultAction');
  66. }
  67. // generate url to controller/action pair using config file
  68. $url_pattern = Framework::getConfiguration("common", "url");
  69. $trans = array('{C}' => $controller, '{A}' => $action);
  70. $url = strtr($url_pattern, $trans);
  71.  
  72. // append other parameters to url
  73. $url_param_pattern = Framework::getConfiguration('common', 'url_params');
  74. foreach($params as $param => $value) {
  75. $trans = array('{P}' => $param, '{V}' => urlencode($value));
  76.  
  77. $url .= strtr($url_param_pattern, $trans);
  78. }
  79. return $url;
  80. }
  81. /**
  82. * Outputs an generated URL
  83. *
  84. * @static
  85. * @see getURL
  86. */
  87. function echoURL($controller = "", $action = "", $params = array())
  88. {
  89. echo HTML::getURL($controller, $action, $params);
  90. }
  91.  
  92. /**
  93. * Loads a shared component into the view
  94. *
  95. * @static
  96. * @param string $file Component to load
  97. */
  98. function load($file)
  99. {
  100. if(file_exists(ROOT . 'views/shared/' . $file)) {
  101. include(ROOT . 'views/shared/' . $file);
  102. } else {
  103. Framework::croak("HTML::load - component file <i>$file</i> not found.");
  104. }
  105. }
  106. /**
  107. * Prints a edit/new form for the model instance passed as param
  108. *
  109. * @static
  110. * @see HTML::getForm()
  111. * @param object $model A model instance to generate form for
  112. */
  113. /*function printForm($model)
  114. {
  115. echo "<table>";
  116. $form = HTML::getForm($model);
  117. foreach ($form as $t) {
  118. echo '<tr class="' . HTML::cycle(array('color1', 'color2')) . '">' . "\n";
  119. echo "<td>${t['label']}</td>\n";
  120. echo "<td>\n";
  121. if(!isset($t['labels'])) {
  122. echo $t['tag'];
  123. } else {
  124. echo "<table>\n";
  125. $c = count($t['labels']);
  126. for($i = 0; $i < $c; $i++) {
  127. echo "<tr>\n";
  128. echo "<td>{$t['labels'][$i]}</td>\n";
  129. echo "<td>{$t['tags'][$i]}</td>\n";
  130. echo "</tr>\n";
  131. }
  132. echo "</table>\n";
  133. }
  134. echo "</td></tr>\n";
  135. }
  136. echo "</table>\n";
  137. }*/
  138. }
  139. ?>

Documentation generated on Fri, 16 Dec 2005 13:16:42 +0200 by phpDocumentor 1.3.0RC4