/core/lib/ui.inc.php

https://github.com/sareche/thebuggenie · PHP · 227 lines · 115 code · 13 blank · 99 comment · 16 complexity · 207cfd746c74dc0078a0981b12678a55 MD5 · raw file

  1. <?php
  2. /**
  3. * UI functions
  4. *
  5. * @author Daniel Andre Eikeland <zegenie@zegeniestudios.net>
  6. * @version 2.0
  7. * @license http://www.opensource.org/licenses/mozilla1.1.php Mozilla Public License 1.1 (MPL 1.1)
  8. * @package thebuggenie
  9. */
  10. /**
  11. * Returns an <img> tag with a specified image
  12. *
  13. * @param string $image image source
  14. * @param array $params[optional] html parameters
  15. * @param boolean $notheme[optional] whether this is a themed image or a top level path
  16. * @param string $module whether this is a module image or in the core image set
  17. * @param boolean $relative whether the path is relative or absolute
  18. *
  19. * @return string
  20. */
  21. function image_tag($image, $params = array(), $notheme = false, $module = 'core', $relative = true)
  22. {
  23. if ($notheme)
  24. {
  25. $params['src'] = $image;
  26. }
  27. else
  28. {
  29. if ($module != 'core' && !file_exists(THEBUGGENIE_PATH . 'iconsets/' . TBGSettings::getIconsetName() . "/{$module}/" . $image))
  30. {
  31. $params['src'] = TBGContext::getTBGPath() . "iconsets/" . TBGSettings::getIconsetName() . "/modules/{$module}/" . $image;
  32. }
  33. elseif ($module != 'core')
  34. {
  35. $params['src'] = TBGContext::getTBGPath() . 'iconsets/' . TBGSettings::getIconsetName() . "/{$module}/" . $image;
  36. }
  37. else
  38. {
  39. $params['src'] = TBGContext::getTBGPath() . 'iconsets/' . TBGSettings::getIconsetName() . '/' . $image;
  40. }
  41. }
  42. if (!$relative)
  43. {
  44. if ($notheme)
  45. {
  46. $params['src'] = TBGContext::getTBGPath() . $params['src'];
  47. }
  48. $params['src'] = TBGContext::getUrlHost() . $params['src'];
  49. }
  50. if (!isset($params['alt']))
  51. {
  52. $params['alt'] = $image;
  53. }
  54. return "<img " . parseHTMLoptions($params) . '>';
  55. }
  56. /**
  57. * Returns the URL to a specified image
  58. *
  59. * @param string $image image source
  60. * @param bool $notheme[optional] whether this is a themed image or a top level path
  61. *
  62. * @return string
  63. */
  64. function image_url($image, $notheme = false, $module = 'core', $relative = true)
  65. {
  66. if ($notheme)
  67. {
  68. $params['src'] = $image;
  69. }
  70. else
  71. {
  72. if ($module != 'core' && !file_exists(THEBUGGENIE_PATH . 'themes/' . TBGSettings::getThemeName() . "/{$module}/" . $image))
  73. {
  74. $params['src'] = TBGContext::getTBGPath() . "themes/modules/{$module}/" . TBGSettings::getThemeName() . '/' . $image;
  75. }
  76. elseif ($module != 'core')
  77. {
  78. $params['src'] = TBGContext::getTBGPath() . 'themes/' . TBGSettings::getThemeName() . "/{$module}/" . $image;
  79. }
  80. else
  81. {
  82. $params['src'] = TBGContext::getTBGPath() . 'themes/' . TBGSettings::getThemeName() . '/' . $image;
  83. }
  84. }
  85. if (!$relative)
  86. {
  87. $params['src'] = TBGContext::getUrlHost() . $params['src'];
  88. }
  89. return $params['src'];
  90. }
  91. /**
  92. * Returns an <a> tag linking to a specified url
  93. *
  94. * @param string $url link target
  95. * @param string $link_text the text displayed in the tag
  96. * @param array $params[optional] html parameters
  97. *
  98. * @return string
  99. */
  100. function link_tag($url, $link_text = null, $params = array())
  101. {
  102. $params['href'] = $url;
  103. if ($link_text === null) $link_text = $url;
  104. return "<a " . parseHTMLoptions($params) . ">{$link_text}</a>";
  105. }
  106. /**
  107. * Returns a csrf_token hidden input tag to use in forms
  108. *
  109. * @return string
  110. */
  111. function csrf_tag()
  112. {
  113. return '<input type="hidden" name="csrf_token" value="' . TBGContext::generateCSRFtoken() . '">';
  114. }
  115. /**
  116. * Return a javascript link tag
  117. *
  118. * @see link_tag()
  119. *
  120. * @param string $link_text the text displayed in the tag
  121. * @param array $params[optional] html parameters
  122. *
  123. * @return string
  124. */
  125. function javascript_link_tag($link_text, $params = array())
  126. {
  127. return link_tag('javascript:void(0);', $link_text, $params);
  128. }
  129. /**
  130. * Returns an <input type="image"> tag
  131. *
  132. * @param string $image image source
  133. * @param array $params[optional] html parameters
  134. * @param bool $notheme[optional] whether this is a themed image or a top level path
  135. *
  136. * @return string
  137. */
  138. function image_submit_tag($image, $params = array(), $notheme = false)
  139. {
  140. $params['src'] = (!$notheme) ? TBGContext::getTBGPath() . 'themes/' . TBGSettings::getThemeName() . '/' . $image : $image;
  141. return '<input type="image" ' . parseHTMLoptions($params) . ' />';
  142. }
  143. /**
  144. * Includes a template with specified parameters
  145. *
  146. * @param string $template name of template to load, or module/template to load
  147. * @param array $params key => value pairs of parameters for the template
  148. */
  149. function include_template($template, $params = array())
  150. {
  151. return TBGActionComponent::includeTemplate($template, $params);
  152. }
  153. /**
  154. * Return a rendered template with specified parameters
  155. *
  156. * @param string $template name of template to load, or module/template to load
  157. * @param array $params key => value pairs of parameters for the template
  158. */
  159. function get_template_html($template, $params = array())
  160. {
  161. return TBGAction::returnTemplateHTML($template, $params);
  162. }
  163. /**
  164. * Includes a component with specified parameters
  165. *
  166. * @param string $component name of component to load, or module/component to load
  167. * @param array $params key => value pairs of parameters for the template
  168. */
  169. function include_component($component, $params = array())
  170. {
  171. return TBGActionComponent::includeComponent($component, $params);
  172. }
  173. /**
  174. * Return a rendered component with specified parameters
  175. *
  176. * @param string $component name of component to load, or module/component to load
  177. * @param array $params key => value pairs of parameters for the template
  178. */
  179. function get_component_html($component, $params = array())
  180. {
  181. return TBGAction::returnComponentHTML($component, $params);
  182. }
  183. /**
  184. * Generate a url based on a route
  185. *
  186. * @param string $name The route key
  187. * @param array $params key => value pairs of route parameters
  188. *
  189. * @return string
  190. */
  191. function make_url($name, $params = array(), $relative = true)
  192. {
  193. return TBGContext::getRouting()->generate($name, $params, $relative);
  194. }
  195. /**
  196. * Returns a string with html options based on an array
  197. *
  198. * @param array $options an array of options
  199. *
  200. * @return string
  201. */
  202. function parseHTMLoptions($options)
  203. {
  204. $option_strings = array();
  205. if (!is_array($options))
  206. {
  207. throw new Exception('Invalid HTML options. Must be an array with key => value pairs corresponding to html attributes');
  208. }
  209. foreach ($options as $key => $val)
  210. {
  211. $option_strings[$key] = "{$key}=\"{$val}\"";
  212. }
  213. return implode(' ', array_values($option_strings));
  214. }