/source/libraries/projectfork/toolbar/toolbar.php

https://github.com/projectfork/Projectfork · PHP · 294 lines · 217 code · 69 blank · 8 comment · 43 complexity · cd96fd9862cacfa1ee105f01ca79aa52 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Projectfork.Library
  4. * @subpackage Toolbar
  5. *
  6. * @author Tobias Kuhn (eaxs)
  7. * @copyright Copyright (C) 2006-2012 Tobias Kuhn. All rights reserved.
  8. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL, see LICENSE.txt
  9. */
  10. defined('_JEXEC') or die();
  11. abstract class PFToolbar
  12. {
  13. protected static $html = array();
  14. protected static $group_open = false;
  15. public static function render()
  16. {
  17. return implode("\n", self::$html);
  18. }
  19. public static function clear()
  20. {
  21. self::$html = array();
  22. self::$group_open = false;
  23. }
  24. public static function group()
  25. {
  26. if (self::$group_open) {
  27. self::$html[] = '</div>';
  28. self::$group_open = false;
  29. }
  30. else {
  31. self::$html[] = '<div class="btn-group">';
  32. self::$group_open = true;
  33. }
  34. }
  35. public static function button($text, $task = '', $list = false, $options = array())
  36. {
  37. self::$html[] = self::renderButton($text, $task, $list, $options);
  38. }
  39. public static function filterButton($isset = false, $target = '#filters')
  40. {
  41. $class = ($isset ? ' active' : '');
  42. $html = array();
  43. $html[] = '<div class="btn-group pull-right">';
  44. $html[] = '<a data-toggle="collapse" data-target="' . $target . '" class="btn' . $class . '">';
  45. $html[] = '<span aria-hidden="true" class="icon-filter"></span> ' . JText::_('JSEARCH_FILTER');
  46. $html[] = '</a>';
  47. $html[] = '</div>';
  48. self::$html[] = implode("", $html);
  49. }
  50. public static function listButton($items, $options = array())
  51. {
  52. $list = array();
  53. $html = array();
  54. $class = (isset($options['class']) ? $options['class'] : '');
  55. $icon = (isset($options['icon']) ? $options['icon'] : 'icon-pencil');
  56. foreach ($items AS $item)
  57. {
  58. if (isset($item['options'])) {
  59. if (array_key_exists('access', $item['options'])) {
  60. if ($item['options']['access'] == false) {
  61. continue;
  62. }
  63. }
  64. }
  65. $list[] = $item;
  66. }
  67. $count = count($list);
  68. if ($count == 0) {
  69. return;
  70. }
  71. $html[] = '<div class="btn-group">';
  72. $html[] = '<a class="btn ' . $class . ' dropdown-toggle disabled" data-toggle="dropdown" id="btn-bulk">';
  73. $html[] = ' <i class="' . $icon . '"></i> ';
  74. $html[] = '</a>';
  75. $html[] = ' <ul class="dropdown-menu">';
  76. foreach($list AS $i => $item)
  77. {
  78. $text = $item['text'];
  79. $task = (isset($item['task']) ? $item['task'] : '');
  80. $lst = (isset($item['list']) ? $item['list'] : true);
  81. $opts = (isset($item['options']) ? $item['options'] : array());
  82. $html[] = self::renderListItem($text, $task, $lst, $opts);
  83. }
  84. $html[] = ' </ul>';
  85. $html[] = '</div>';
  86. self::$html[] = implode("", $html);
  87. }
  88. public static function dropdownButton($items, $options = array())
  89. {
  90. $list = array();
  91. $html = array();
  92. $class = (isset($options['class']) ? $options['class'] : 'btn-info');
  93. $icon = (isset($options['icon']) ? $options['icon'] : 'icon-plus icon-white');
  94. foreach ($items AS $item)
  95. {
  96. if (isset($item['options'])) {
  97. if (array_key_exists('access', $item['options'])) {
  98. if ($item['options']['access'] == false) {
  99. continue;
  100. }
  101. }
  102. }
  103. $list[] = $item;
  104. }
  105. $count = count($list);
  106. if ($count == 0) {
  107. return;
  108. }
  109. if ($count == 1) {
  110. $text = $list[0]['text'];
  111. $task = (isset($list[0]['task']) ? $list[0]['task'] : '');
  112. $lst = (isset($list[0]['list']) ? $list[0]['list'] : false);
  113. $opts = (isset($list[0]['options']) ? $list[0]['options'] : array());
  114. if (!isset($opts['class']) && isset($options['class'])) {
  115. $opts['class'] = $options['class'];
  116. }
  117. if (!isset($opts['icon']) && isset($options['icon'])) {
  118. $opts['icon'] = $options['icon'];
  119. }
  120. self::button($text, $task, $lst, $opts);
  121. }
  122. else {
  123. $reverse = array_reverse($list);
  124. $first = array_pop($reverse);
  125. $text = $first['text'];
  126. $task = (isset($first['task']) ? $first['task'] : '');
  127. $lst = (isset($first['list']) ? $first['list'] : false);
  128. $opts = (isset($first['options']) ? $first['options'] : array());
  129. if (!isset($opts['class']) && isset($options['class'])) {
  130. $opts['class'] = $options['class'];
  131. }
  132. if (!isset($opts['icon']) && isset($options['icon'])) {
  133. $opts['icon'] = $options['icon'];
  134. }
  135. if (!isset($opts['id']) && isset($options['id'])) {
  136. $opts['id'] = $options['id'];
  137. }
  138. $html[] = '<div class="btn-group">';
  139. $html[] = self::renderButton($text, $task, $lst, $opts);
  140. $html[] = '<a class="btn ' . $class . ' dropdown-toggle" data-toggle="dropdown">';
  141. $html[] = ' <span class="caret"></span>';
  142. $html[] = '</a>';
  143. $html[] = ' <ul class="dropdown-menu">';
  144. foreach($list AS $i => $item)
  145. {
  146. if ($i == 0) continue;
  147. $text = $item['text'];
  148. $task = (isset($item['task']) ? $item['task'] : '');
  149. $lst = (isset($item['list']) ? $item['list'] : false);
  150. $opts = (isset($item['options']) ? $item['options'] : array());
  151. $html[] = self::renderListItem($text, $task, $lst, $opts);
  152. }
  153. $html[] = ' </ul>';
  154. $html[] = '</div>';
  155. self::$html[] = implode("", $html);
  156. }
  157. }
  158. protected static function renderButton($text, $task = '', $list = false, $options = array())
  159. {
  160. $html = array();
  161. $class = (isset($options['class']) ? $options['class'] : 'btn-info');
  162. $href = (isset($options['href']) ? $options['href'] : 'javascript:void(0);');
  163. $icon = (isset($options['icon']) ? $options['icon'] : 'icon-plus icon-white');
  164. $id = (isset($options['id']) ? ' id="' . $options['id'] . '"' : '');
  165. if (array_key_exists('access', $options)) {
  166. if ($options['access'] !== true) {
  167. return '';
  168. }
  169. }
  170. $html[] = '<a class="btn ' . $class . '" href="' . $href . '"';
  171. if ($task) {
  172. $html[] = 'onclick="';
  173. if ($list) {
  174. $message = addslashes(JText::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST'));
  175. $html[] = "if (document.adminForm.boxchecked.value==0){alert('$message');}else{Joomla.submitbutton('$task')}";
  176. }
  177. else {
  178. $html[] = "Joomla.submitbutton('$task');";
  179. }
  180. $html[] = '" ';
  181. }
  182. $html[] = $id;
  183. $html[] = '>';
  184. $html[] = '<i class="' . $icon . '"></i> ';
  185. $html[] = addslashes(JText::_($text));
  186. $html[] = '</a>';
  187. return implode("", $html);
  188. }
  189. protected static function renderListItem($text, $task = '', $list = false, $options = array())
  190. {
  191. $html = array();
  192. $href = (isset($options['href']) ? $options['href'] : 'javascript:void(0);');
  193. $icon = (isset($options['icon']) ? $options['icon'] : '');
  194. if (isset($options['access'])) {
  195. if ($options['access'] == false) {
  196. return '';
  197. }
  198. }
  199. if ($text == 'divider') {
  200. $html[] = '<li class="divider"></li>';
  201. return implode("", $html);
  202. }
  203. $html[] = '<li>';
  204. $html[] = '<a href="' . $href . '"';
  205. if ($task) {
  206. $html[] = 'onclick="';
  207. if ($list) {
  208. $message = addslashes(JText::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST'));
  209. $html[] = "if (document.adminForm.boxchecked.value==0){alert('$message');}else{Joomla.submitbutton('$task')}";
  210. }
  211. else {
  212. $html[] = "Joomla.submitbutton('$task');";
  213. }
  214. $html[] = '" ';
  215. }
  216. $html[] = '>';
  217. if ($icon) {
  218. $html[] = '<i class="' . $icon . '"></i> ';
  219. }
  220. $html[] = addslashes(JText::_($text));
  221. $html[] = '</a>';
  222. $html[] = '</li>';
  223. return implode("", $html);
  224. }
  225. }