PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/View/Helper/BootstrapHelper.php

http://github.com/loadsys/twitter-bootstrap-helper
PHP | 249 lines | 147 code | 13 blank | 89 comment | 37 complexity | 1e2450bc64e639434dc3576928c2e902 MD5 | raw file
  1. <?php
  2. class BootstrapHelper extends AppHelper {
  3. public $helpers = array("Html", "Session");
  4. /**
  5. * Displays an h1 tag wrapped in a div with the page-header class
  6. *
  7. * @param string $title
  8. * @return string
  9. */
  10. public function pageHeader($title){
  11. return $this->Html->tag(
  12. "div",
  13. "<h1>$title</h1>",
  14. array("class" => "page-header")
  15. );
  16. }
  17. /**
  18. * Creates a Bootstrap label with $messsage and optionally the $type. Any
  19. * options that could get passed to HtmlHelper::tag can be passed in the
  20. * third param.
  21. *
  22. * @param string $message
  23. * @param string $type
  24. * @param array $options
  25. * @access public
  26. * @return string
  27. */
  28. public function label($message = "", $style = "", $options = array()) {
  29. $class = "label";
  30. $valid = array("success", "important", "warning", "info", "inverse");
  31. if (!empty($style) && in_array($style, $valid)) {
  32. $class .= " label-{$style}";
  33. }
  34. if (isset($options["class"]) && !empty($options["class"])) {
  35. $options["class"] = $class . " " . $options["class"];
  36. } else {
  37. $options["class"] = $class;
  38. }
  39. return $this->Html->tag("span", $message, $options);
  40. }
  41. /**
  42. * Creates a Bootstrap badge with $num and optional $style. Any options
  43. * that could get passed to the HtmlHelper::tag can be passed in the 3rd
  44. * param
  45. *
  46. * @param integer $num
  47. * @param string $style
  48. * @param array $options
  49. * @return string
  50. */
  51. public function badge($num = 0, $style = "", $options = array()) {
  52. $class = "badge";
  53. $valid = array("success", "warning", "important", "info", "inverse");
  54. if (!empty($style) && in_array($style, $valid)) {
  55. $class .= " badge-{$style}";
  56. }
  57. if (isset($options["class"]) && !empty($options["class"])) {
  58. $options["class"] = $class . " " . $options["class"];
  59. } else {
  60. $options["class"] = $class;
  61. }
  62. return $this->Html->tag("span", $num, $options);
  63. }
  64. /**
  65. * progress
  66. *
  67. * @param string $style
  68. * @param array $options
  69. * @return string
  70. */
  71. public function progress($options = array()) {
  72. $class = "progress";
  73. $width = 0;
  74. $valid = array("info", "success", "warning", "danger");
  75. if (isset($options["style"]) && in_array($options["style"], $valid)) {
  76. $class .= " progress-{$options["style"]}";
  77. }
  78. if (isset($options["striped"]) && $options["striped"]) {
  79. $class .= " progress-striped";
  80. }
  81. if (isset($options["active"]) && $options["active"]) {
  82. $class .= " active";
  83. }
  84. if (
  85. isset($options["width"]) &&
  86. !empty($options["width"]) &&
  87. is_int($options["width"])
  88. ) {
  89. $width = $options["width"];
  90. }
  91. $bar = $this->Html->tag(
  92. "div",
  93. "",
  94. array("class" => "bar", "style" => "width: {$width}%;")
  95. );
  96. return $this->Html->tag("div", $bar, array("class" => $class));
  97. }
  98. /**
  99. * Takes the name of an icon and returns the i tag with the appropriately
  100. * named class. The second param will switch between black and white
  101. * icon sets.
  102. *
  103. * @param mixed $name
  104. * @access public
  105. * @return void
  106. */
  107. public function icon($name, $color = "black") {
  108. $class = "icon-{$name}";
  109. if ($color === "white") {
  110. $class = "{$class} icon-white";
  111. }
  112. return $this->Html->tag("i", false, array("class" => $class));
  113. }
  114. /**
  115. * Renders alert markup and takes a style and closable option
  116. *
  117. * @param mixed $content
  118. * @param array $options
  119. * @access public
  120. * @return void
  121. */
  122. public function alert($content, $options = array()) {
  123. $close = "";
  124. if (isset($options['closable']) && $options['closable']) {
  125. $close = '<a class="close" data-dismiss="alert">&times;</a>';
  126. }
  127. $style = isset($options["style"]) ? $options["style"] : "warning";
  128. $types = array("info", "success", "error", "warning");
  129. if ($style === "flash") {
  130. $style = "warning";
  131. }
  132. if (strtolower($style) === "auth") {
  133. $style = "error";
  134. }
  135. if (!in_array($style, array_merge($types, array("auth", "flash")))) {
  136. $class = "alert alert-warning {$style}";
  137. } else {
  138. $class = "alert alert-{$style}";
  139. }
  140. return $this->Html->tag(
  141. 'div',
  142. "{$close}{$content}",
  143. array("class" => $class)
  144. );
  145. }
  146. /**
  147. * Captures the Session flash if it is set and renders it in the proper
  148. * markup for the twitter bootstrap styles. The default key of "flash",
  149. * gets translated to the warning styles. Other valid $keys are "info",
  150. * "success", "error". The $key "auth" with use the error styles because
  151. * that is when the auth form fails.
  152. *
  153. * @param string $key
  154. * @param $options
  155. * @access public
  156. * @return string
  157. */
  158. public function flash($key = "flash", $options = array()) {
  159. $content = $this->_flash_content($key);
  160. if (empty($content)) { return ''; }
  161. $close = false;
  162. if (isset($options['closable']) && $options['closable']) {
  163. $close = true;
  164. }
  165. return $this->alert($content, array("style" => $key, "closable" => $close));
  166. }
  167. /**
  168. * By default it checks $this->flash() for 5 different keys of valid
  169. * flash types and returns the string.
  170. *
  171. * @param array $options
  172. * @access public
  173. * @return string
  174. */
  175. public function flashes($options = array()) {
  176. if (!isset($options["keys"]) || !$options["keys"]) {
  177. $options["keys"] = array("info", "success", "error", "warning", "flash");
  178. }
  179. if (isset($options["auth"]) && $options["auth"]) {
  180. $options["keys"][] = "auth";
  181. unset($options["auth"]);
  182. }
  183. $keys = $options["keys"];
  184. unset($options["keys"]);
  185. $out = '';
  186. foreach($keys as $key) {
  187. $out .= $this->flash($key, $options);
  188. }
  189. return $out;
  190. }
  191. /**
  192. * Returns the content from SessionHelper::flash() for the passed in
  193. * $key.
  194. *
  195. * @param string $key
  196. * @access public
  197. * @return void
  198. */
  199. public function _flash_content($key = "flash") {
  200. return $this->Session->flash($key, array("element" => null));
  201. }
  202. /**
  203. * Displays the alert-message.block-messgae div's from the twitter
  204. * bootstrap.
  205. *
  206. * @param string $message
  207. * @param array $links
  208. * @param array $options
  209. * @access public
  210. * @return string
  211. */
  212. public function block($message = null, $options = array()) {
  213. $style = "";
  214. $valid = array("success", "info", "error");
  215. if (isset($options["style"]) && in_array($options["style"], $valid)) {
  216. $style = " alert-{$options["style"]}";
  217. }
  218. $class = "alert alert-block{$style}";
  219. $close = $heading = "";
  220. if (isset($options["closable"]) && $options["closable"]) {
  221. $close = '<a class="close" data-dismiss="alert">&times;</a>';
  222. }
  223. if (isset($options["heading"]) && !empty($options["heading"])) {
  224. $heading = $this->Html->tag(
  225. "h4",
  226. $options["heading"],
  227. array("class" => "alert-heading")
  228. );
  229. }
  230. return $this->Html->tag(
  231. "div",
  232. $close.$heading.$message,
  233. array("class" => $class)
  234. );
  235. }
  236. }