PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/helper/WidgetsHelper.php

https://github.com/kc5nra/RevTK
PHP | 425 lines | 153 code | 51 blank | 221 comment | 18 complexity | 9b39a63827c95aa4f97e77ef73c95f8f MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * Helpers to include user interface elements in the application templates.
  4. *
  5. * Uses stylesheet /css/ui/widgets.css
  6. *
  7. * @package Helpers
  8. * @author Fabrice Denis
  9. */
  10. /**
  11. * Returns HTML for a uiFilterStd widget.
  12. *
  13. * The links is an array of link definitions, in the format of the link_to() arguments:
  14. * array(
  15. * array($name, $internal_uri, $options),
  16. * array($name, $internal_uri, $options),
  17. * //...
  18. * )
  19. *
  20. * Options can be used to add attributes to the main div tag.
  21. *
  22. * Add option 'active' as an integer to specify the active item: 0=first, 1=second, etc.
  23. *
  24. * @param string $label Label, pass empty string to display switches without label
  25. * @param array $links Array of link definitions (array(name, internal_uri, options))
  26. * @param array $options Options for the main div tag (class uiFilterStd will always be addded)
  27. * Also, the 'active' option (see above)
  28. *
  29. * @return string HTML representation
  30. */
  31. function ui_filter_std($label, $links, $options = array())
  32. {
  33. // always set the widget class name in the main div tag
  34. $options['class'] = _merge_class_names(isset($options['class']) ? $options['class'] : array(), array('uiFilterStd'));
  35. if (isset($options['active']))
  36. {
  37. $active = (int)$options['active'];
  38. if ($active < count($links))
  39. {
  40. // add the active class
  41. $linkOptions = isset($links[$active][2]) ? $links[$active][2] : array();
  42. $linkOptions['class'] = (isset($linkOptions['class']) ? $linkOptions['class'] : '') . ' active';
  43. $links[$active][2] = $linkOptions;
  44. }
  45. unset($options['active']);
  46. }
  47. $view = new coreView(coreContext::getInstance());
  48. $view->getParameterHolder()->add(array('links' => $links, 'label' => $label, 'options' => $options));
  49. $view->setTemplate(dirname(__FILE__).'/templates/ui_filter_std.php');
  50. return $view->render();
  51. }
  52. /**
  53. * Set a slot and returns the HTML for a uiSelectPager.
  54. *
  55. * The slot allows to re-print the pager at the top and bottom of a table by
  56. * running the pager template only once.
  57. *
  58. * Examples:
  59. *
  60. * echo ui_select_pager($pager)
  61. * => Set and print the pager slot with the default slot name
  62. * echo ui_select_pager()
  63. * => Print the HTML for previously set slot
  64. * echo ui_select_pager($pager, 'pager2')
  65. * => Print and set a pager with a custom slot name
  66. * (allows different pagers on one template)
  67. * echo ui_select_pager(false, 'pager2')
  68. * => Print previously set pager with custom slot name
  69. *
  70. * @param mixed $pager uiSelectPager object or false
  71. * @param string $slot Slot name, leave out to use the default
  72. *
  73. * @return string HTML representation
  74. */
  75. function ui_select_pager($pager = false, $slot = 'widgets.ui.pager')
  76. {
  77. if ($pager !== false)
  78. {
  79. slot($slot);
  80. $view = new coreView(coreContext::getInstance());
  81. $view->getParameterHolder()->add(array('pager' => $pager));
  82. $view->setTemplate(dirname(__FILE__).'/templates/ui_select_pager.php');
  83. echo $view->render();
  84. end_slot();
  85. }
  86. return get_slot($slot);
  87. }
  88. /**
  89. * Return HTML for a uiSelectTable component.
  90. *
  91. * Optionally, returns the HTML for a uiSelectPager at the top and bottom of the table.
  92. *
  93. * @param uiSelectTable $table
  94. * @param uiSelectPager $pager Optional pager, to display paging links and rows-per-page
  95. *
  96. * @return string HTML representation
  97. */
  98. function ui_select_table(uiSelectTable $table, uiSelectPager $pager = null)
  99. {
  100. ob_start();
  101. if (!is_null($pager))
  102. {
  103. echo ui_select_pager($pager);
  104. }
  105. $view = new coreView(coreContext::getInstance());
  106. $view->getParameterHolder()->add(array('table' => $table));
  107. $view->setTemplate(dirname(__FILE__).'/templates/ui_select_table.php');
  108. echo $view->render();
  109. if (!is_null($pager))
  110. {
  111. echo ui_select_pager();
  112. }
  113. return ob_get_clean();
  114. }
  115. /**
  116. * uiDataFilter helper.
  117. *
  118. * Links is an associative array of link key => definition,
  119. * the parameters for each link are passed directly to the link_to() helper,
  120. * so it supports internal uris, query_string option, ids and other attributes can be set.
  121. *
  122. * To set no active link, and disable the default active link, set it to FALSE.
  123. *
  124. * Only the link name (link text) is required. 'internal_uri' defaults to '#', and
  125. * 'options' defautls to an empty array.
  126. *
  127. * The link parameter internal_uri defaults to '#' if not set.
  128. *
  129. * array(
  130. * 'link_key' => array(
  131. * 'name' => 'Link Text',
  132. * 'internal_uri' => '@view_by_votes',
  133. * 'options' => array('id' => 'view_by_botes')
  134. * ),
  135. * ....
  136. * )
  137. *
  138. * The first link is always active by default, unless an id is given.
  139. *
  140. * @param string $caption The label on the left side next to the links, eg: "View:"
  141. * @param array $links An array of link definitions
  142. * @param string $active_link The key for the active link in the $links array
  143. * @return string Html code
  144. */
  145. function ui_data_filter($caption, $links, $active_link = '')
  146. {
  147. // set first link active by default
  148. if ($active_link === '')
  149. {
  150. $keys = array_keys($links);
  151. $active_link = $keys[0];
  152. }
  153. // set defaults and active link class
  154. foreach ($links as $key => &$link)
  155. {
  156. if (!isset($link['internal_uri'])) {
  157. $link['internal_uri'] = '#';
  158. }
  159. if (!isset($link['options'])) {
  160. $link['options'] = array();
  161. }
  162. // set the active class on the active link
  163. $link['active'] = $key===$active_link;
  164. /*
  165. if ($key===$active_link)
  166. {
  167. $options = $link['options'];
  168. $options['class'] = _merge_class_names(isset($options['class']) ? $options['class'] : array(), 'active');
  169. $link['options'] = $options;
  170. }
  171. */
  172. }
  173. $view = new coreView(coreContext::getInstance());
  174. $view->getParameterHolder()->add(array('caption' => $caption, 'links' => $links, 'active_link' => $active_link));
  175. $view->setTemplate(dirname(__FILE__).'/templates/ui_data_filter.php');
  176. return $view->render();
  177. }
  178. /**
  179. * Return html structure (to echo) for tabs in the manner of "sliding doors".
  180. *
  181. * SPANs are used because they can be styled on the :hover state in IE6
  182. * ( a:hover span {...} ).
  183. *
  184. * Structure:
  185. *
  186. * <div class="ui-tabs" id="custom-id">
  187. * <ul>
  188. * <li><a href="#"><span>Link text</span></a></li>
  189. * ...
  190. * </ul>
  191. * <div class="clear"></div>
  192. * </div>
  193. *
  194. *
  195. * The $links argument is declared like this:
  196. *
  197. * array(
  198. * array($name, $internal_uri, $options),
  199. * array($name, $internal_uri, $options),
  200. * ...
  201. * )
  202. *
  203. * The tab definitions are identical to the link_to() helper:
  204. *
  205. * $name Label for the tab
  206. * $internal_uri Internal uri, or absolute url, defaults to '#' if empty (optional)
  207. * $options Html attribute options (optional)
  208. *
  209. * By default the first tab is set active (class "active" on the LI tag). Specify the
  210. * index of the tab to be active, or FALSE to not add an "active" class.
  211. *
  212. * @see http://www.alistapart.com/articles/slidingdoors/
  213. *
  214. * @param array $links An array of tab definitions (see above).
  215. * @param mixed $active Index of the active tab, defaults to the first tab.
  216. * Use FALSE to explicitly set no active tab (or use your own class).
  217. * @param array $options Options for the container DIV element. By default the class "ui-tabs"
  218. * is added. Add "uiTabs" class for defaults styles, id for the javascript component.
  219. *
  220. * @return string Html code
  221. */
  222. function ui_tabs($tabs, $active = 0, $options = array())
  223. {
  224. ob_start();
  225. // add the "ui-tabs" class name
  226. $options['class'] = _merge_class_names(isset($options['class']) ? $options['class'] : array(), array('ui-tabs'));
  227. echo tag('div', $options, true) . "\n<ul>\n";
  228. $tab_index = 0;
  229. foreach ($tabs as $tab)
  230. {
  231. $name = '<span>'.$tab[0].'</span>';
  232. $internal_uri = isset($tab[1]) ? $tab[1] : '#';
  233. $options = isset($tab[2]) ? $tab[2] : array();
  234. $class_active = (is_int($active) && $active===$tab_index) ? ' class="active"' : '';
  235. echo '<li'.$class_active.'>'.link_to($name, $internal_uri, $options).'</li>'."\n";
  236. $tab_index++;
  237. }
  238. echo "</ul>\n<div class=\"clear\"></div>\n</div>\n";
  239. return ob_get_clean();
  240. }
  241. /**
  242. * Helper to set the display property inline stlye in html templates.
  243. *
  244. * Example:
  245. * <div ... style="<3php echo ui_display($active===3) 3>">
  246. *
  247. * Echoes the display property with ending ";"
  248. */
  249. function ui_display($bDisplay)
  250. {
  251. echo $bDisplay ? 'display:block;' : 'display:none;';
  252. }
  253. /**
  254. * Start buffering contents of a rounded box.
  255. *
  256. * @param string $box_class uiBox class, style for the ui box.
  257. */
  258. function ui_box_rounded($box_class = 'uiBoxRDefault')
  259. {
  260. coreConfig::set('helpers.ui.box', $box_class);
  261. ob_start();
  262. ob_implicit_flush(0);
  263. }
  264. /**
  265. * Stops buffering the contents of a rounded box,
  266. * returns the HTML code for the box and its contents.
  267. *
  268. * @return string HTML code
  269. */
  270. function end_ui_box()
  271. {
  272. $content = ob_get_clean();
  273. $classname = coreConfig::get('helpers.ui.box', '');
  274. $view = new coreView(coreContext::getInstance());
  275. $view->getParameterHolder()->add(array('contents' => $content, 'class' => $classname));
  276. $view->setTemplate(dirname(__FILE__).'/templates/ui_box_rounded.php');
  277. return $view->render();
  278. }
  279. /**
  280. * Returns a uiIBtn element.
  281. *
  282. * The parameters are the same as for UrlHelper link_to().
  283. * The difference is an additional "type" option, and an empty uri will default to '#'.
  284. *
  285. * Example markup:
  286. *
  287. * <code>
  288. * <a href="#" class="uiIBtn uiIBtnDefault"><span><em class="icon icon-edit">Edit</em></span></a>
  289. * </code>
  290. *
  291. * Additional options:
  292. *
  293. * 'type' The type of button, defaults to "uiIBtnDefault". This sets the main class
  294. * of the uiIBtn element.
  295. * 'icon' Adds an EM element inside the SPAN, with classname "icon icon-XYZ" where XYZ
  296. * is the given icon name.
  297. *
  298. * Examples:
  299. *
  300. * echo ui_ibtn('Go');
  301. * echo ui_ibtn('Disabled', '#', array('type' => 'uiIBtnDisabled'));
  302. * echo ui_ibtn('Custom class', '#', array('class' => 'JsAction-something'));
  303. * echo ui_ibtn('Google', 'http://www.google.com' );
  304. * echo ui_ibtn('Click me!', '#', array('onclick' => 'alert("Hello world!");return false;') );
  305. *
  306. * @param string $name Button text can contain HTML (eg. <span>), will NOT be escaped
  307. * @param string $internal_uri See link_to()
  308. * @param array $options See link_to()
  309. * @return string
  310. */
  311. function ui_ibtn($name, $internal_uri = '', $options = array())
  312. {
  313. $button_type = 'uiIBtnDefault';
  314. if (isset($options['type']))
  315. {
  316. $button_type = $options['type'];
  317. unset($options['type']);
  318. }
  319. $options['class'] = _merge_class_names(isset($options['class']) ? $options['class'] : array(), array('uiIBtn', $button_type));
  320. if (isset($options['icon']))
  321. {
  322. $name = '<em class="icon icon-'.$options['icon'].'">'.$name.'</em>';
  323. unset($options['icon']);
  324. }
  325. $name = '<span>'.$name.'</span>';
  326. if ($internal_uri == '') {
  327. $internal_uri = '#';
  328. }
  329. if ($internal_uri == '#') {
  330. $options['absolute'] = true;
  331. }
  332. return link_to($name, $internal_uri, $options);
  333. }
  334. /**
  335. * Returns HTML code for a uiWindow.
  336. *
  337. * @uses jquery, jquery UI/Draggable
  338. * @uses ui_box_rounded() for the window border
  339. *
  340. * @param string $content HTML content for the uiWindow div.window-body
  341. * @param array $options Tag attributes as for tag() helper (to set id, etc)
  342. * Classes can be set, "uiWindow" class is merged
  343. *
  344. * @return string HTML code
  345. */
  346. function ui_window($content = '', $options = array())
  347. {
  348. // add uiWindow class
  349. $options['class'] = _merge_class_names(isset($options['class']) ? $options['class'] : array(), array('uiWindow'));
  350. $view = new coreView(coreContext::getInstance());
  351. $view->getParameterHolder()->add(array('content' => $content, 'options' => $options));
  352. $view->setTemplate(dirname(__FILE__).'/templates/ui_window.php');
  353. return $view->render();
  354. }
  355. /**
  356. * Merge class names given as strings or arrays (array arguments is faster).
  357. *
  358. * @param mixed $classnames Class name(s) given as a "class" attribute string, or an array of class names
  359. * @param mixed $add_classnames Class names to add, given as string or array
  360. * @return string Css "class" attribute string with all class names combined
  361. */
  362. function _merge_class_names($classnames, $add_classnames)
  363. {
  364. if (is_string($classnames)) {
  365. $classnames = preg_split('/\s+/', $classnames);
  366. }
  367. if (is_string($add_classnames)) {
  368. $add_classnames = preg_split('/\s+/', $add_classnames);
  369. }
  370. if (count($add_classnames)) {
  371. $classnames = array_merge($classnames, $add_classnames);
  372. }
  373. return implode(' ', $classnames);
  374. }