PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/source/libraries/projectfork/menu/context.php

https://github.com/projectfork/Projectfork
PHP | 390 lines | 269 code | 109 blank | 12 comment | 50 complexity | c6484d905bc1e708653463969525de16 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Projectfork.Library
  4. * @subpackage Menu
  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. class PFMenuContext
  12. {
  13. protected $component;
  14. protected $items;
  15. public function __construct($component = null)
  16. {
  17. $this->items = array();
  18. $this->component = (empty($component) ? JRequest::getVar('option') : $component);
  19. }
  20. protected function addItem($html)
  21. {
  22. $this->items[] = $html;
  23. }
  24. public function render($disabled_options = array())
  25. {
  26. $class = '';
  27. if (isset($disabled_options['class']) && $disabled_options['class'] != '') {
  28. $class = ' ' . $disabled_options['class'];
  29. }
  30. if (count($this->items) <= 2) {
  31. $this->items = array();
  32. $html = array();
  33. $html[] = '<div class="btn-group">';
  34. $html[] = ' <a class="btn btn-link disabled' . $class . '" href="javascript: void(0);"><span class="caret"></span></a>';
  35. $html[] = '</div>';
  36. return implode("\n", $html);
  37. }
  38. else {
  39. $html = implode("\n", $this->items);
  40. $this->items = array();
  41. return $html;
  42. }
  43. }
  44. public function start($options = array(), $return = false)
  45. {
  46. $class = '';
  47. $title = '';
  48. $pull = '';
  49. $single = false;
  50. if (isset($options['class']) && $options['class'] != '') {
  51. $class = ' ' . $options['class'];
  52. }
  53. if (isset($options['title']) && $options['title'] != '') {
  54. $title = $options['title'] . ' ';
  55. }
  56. if (isset($options['single-button']) && $options['single-button'] != '') {
  57. $single = (bool) $options['single-button'];
  58. }
  59. if (isset($options['pull']) && $options['pull'] != '') {
  60. $pull = ' pull-' . $options['pull'];
  61. }
  62. $html = array();
  63. if (!$single) {
  64. $html[] = '<div class="btn-group' . $pull . '">';
  65. $html[] = ' <a class="btn dropdown-toggle' . $class.'" data-toggle="dropdown" href="#">' . $title . '<span class="caret"></span></a>';
  66. $html[] = ' <ul class="dropdown-menu">';
  67. }
  68. else {
  69. $html[] = '<div class="btn ' . $class.'">' . $title . '</div>';
  70. }
  71. if ($return) return implode("\n", $html);
  72. $this->addItem(implode("\n", $html));
  73. }
  74. public function itemLink($icon, $title, $action, $return = false)
  75. {
  76. $html = array();
  77. $html[] = ' <li>';
  78. $html[] = ' <a href="' . $action . '"><span aria-hidden="true" class="' . $icon . '"></span> ' . JText::_($title) . '</a>';
  79. $html[] = ' </li>';
  80. if ($return) return implode("\n", $html);
  81. $this->addItem(implode("\n", $html));
  82. }
  83. public function itemCollapse($icon, $title, $action, $return = false)
  84. {
  85. $html = array();
  86. $html[] = ' <li>';
  87. $html[] = ' <a href="' . $action . '" data-toggle="collapse"><span aria-hidden="true" class="' . $icon . '"></span> ' . JText::_($title) . '</a>';
  88. $html[] = ' </li>';
  89. if ($return) return implode("\n", $html);
  90. $this->addItem(implode("\n", $html));
  91. }
  92. public function itemPlaceholder($icon, $title, $return = false)
  93. {
  94. $html = array();
  95. $html[] = ' <li>';
  96. $html[] = ' <span aria-hidden="true" class="' . $icon . '"></span> ' . JText::_($title);
  97. $html[] = ' </li>';
  98. if ($return) return implode("\n", $html);
  99. $this->addItem(implode("\n", $html));
  100. }
  101. public function itemModal($icon, $title, $action, $click = null, $size_x = '800', $size_y = '500', $return = false)
  102. {
  103. static $modal;
  104. $onclick = (empty($click) ? '' : ' onclick="' . $click . '"');
  105. // Load the modal behavior script.
  106. if (!isset($modal)) JHtml::_('behavior.modal', 'a.modal_item');
  107. $html = array();
  108. $html[] = ' <li>';
  109. $html[] = ' <a class="modal_item" href="' . $action . '" rel="{handler: \'iframe\', size: {x: ' . $size_x . ', y: ' . $size_y.'}}" ' . $onclick . '>';
  110. $html[] = ' <span aria-hidden="true" class="' . $icon.'"></span> ' . JText::_($title);
  111. $html[] = ' </a>';
  112. $html[] = ' </li>';
  113. if ($return) return implode("\n", $html);
  114. $this->addItem(implode("\n", $html));
  115. }
  116. public function itemJavaScript($icon, $title, $action, $return = false)
  117. {
  118. $html = array();
  119. $html[] = ' <li>';
  120. $html[] = ' <a onclick="' . $action . '" href="javascript:void(0);"><span aria-hidden="true" class="' . $icon . '"></span> ' . JText::_($title) . '</a>';
  121. $html[] = ' </li>';
  122. if ($return) return implode("\n", $html);
  123. $this->addItem(implode("\n", $html));
  124. }
  125. public function itemDivider($return = false)
  126. {
  127. if ($return) return ' <li class="divider"></li>';
  128. $this->addItem(' <li class="divider"></li>');
  129. }
  130. public function itemEdit($asset, $id = 0, $access = false)
  131. {
  132. if (!$access) return '';
  133. $icon = 'icon-pencil';
  134. $action = JRoute::_('index.php?option=' . $this->component . '&task=' . strval($asset) . '.edit&id=' . intval($id));
  135. $title = JText::_('COM_PROJECTFORK_ACTION_EDIT');
  136. return $this->itemLink($icon, $title, $action);
  137. }
  138. public function itemTrash($asset, $id, $access = false)
  139. {
  140. if (!$access) return '';
  141. $icon = 'icon-trash';
  142. $action = "return listItemTask('cb" . $id . "','" . $asset . ".trash');";
  143. $title = JText::_('COM_PROJECTFORK_ACTION_TRASH');
  144. return $this->itemJavaScript($icon, $title, $action);
  145. }
  146. public function itemDelete($asset, $id, $access = false)
  147. {
  148. if (!$access) return '';
  149. $icon = 'icon-remove';
  150. $action = "return listItemTask('cb" . $id . "','" . $asset . ".delete');";
  151. $title = JText::_('COM_PROJECTFORK_ACTION_DELETE');
  152. return $this->itemJavaScript($icon, $title, $action);
  153. }
  154. public function priorityList($i, $id, $asset, $selected = 0, $access = false, $css_class = 'btn-mini')
  155. {
  156. $priorities = JHtml::_('projectfork.priorityOptions');
  157. $html = array();
  158. $title = '';
  159. $class = 'very-low-priority';
  160. // Find the current priority and class
  161. foreach($priorities AS $priority)
  162. {
  163. if ($priority->value == $selected) {
  164. $title = $priority->text;
  165. switch($priority->value)
  166. {
  167. case 0:
  168. $class = 'very-low-priority';
  169. break;
  170. case 2:
  171. $class = 'btn-info low-priority';
  172. break;
  173. case 3:
  174. $class = 'btn-primary medium-priority';
  175. break;
  176. case 4:
  177. $class = 'btn-warning high-priority';
  178. break;
  179. case 5:
  180. $class = 'btn-danger very-high-priority';
  181. break;
  182. default:
  183. $class = 'very-low-priority';
  184. break;
  185. }
  186. }
  187. }
  188. $class .= ' ' . $css_class;
  189. if ($access) {
  190. $html[] = $this->start(array('title' => $title, 'class' => $class), true);
  191. foreach($priorities AS $priority)
  192. {
  193. if ($title == $priority->text) continue;
  194. $action = "document.id('priority" . $i . "').set('value', " . intval($priority->value) . "); return listItemTask('cb" . $i . "','" . $asset . ".savePriority');";
  195. $html[] = $this->itemJavaScript('icon-flag', $priority->text, $action, true);
  196. }
  197. $html[] = $this->end(true);
  198. }
  199. else {
  200. $html[] = $this->start(array('title' => $title, 'class' => $class, 'single-button' => true), true);
  201. }
  202. $html[] = '<input type="hidden" id="priority' . $i . '" name="priority[' . $id . ']" value="' . intval($selected) . '"/>';
  203. return implode("\n", $html);
  204. }
  205. public function assignedUsers($i, $id, $asset, $assigned, $access = false, $css_class = 'btn-mini')
  206. {
  207. $count = count($assigned);
  208. $class = '';
  209. $title = ($count > 0) ? $assigned[0]->name : JText::_('COM_PROJECTFORK_UNASSIGNED');
  210. $title .= ($count > 1) ? ' +'.($count - 1) : '';
  211. $class .= ' ' . $css_class;
  212. $link = PFusersHelperRoute::getUsersRoute() . '&amp;layout=modal&amp;tmpl=component&amp;field=assigned' . $i;
  213. if (!$access && $count < 2) {
  214. $html[] = $this->start(array('title' => $title, 'class' => $class, 'single-button' => true), true);
  215. $html[] = '<input type="hidden" id="assigned' . $i . '" name="assigned[' . $id . ']" value="0"/>';
  216. return implode("\n", $html);
  217. }
  218. else {
  219. // Build the script.
  220. $script = array();
  221. $script[] = ' function jSelectUser_assigned' . $i . '(id, title) {';
  222. $script[] = ' document.getElementById("assigned' . $i . '").value = id';
  223. $script[] = ' SqueezeBox.close();';
  224. $script[] = ' return listItemTask("cb' . $i . '","' . $asset . '.addUsers");';
  225. $script[] = ' }';
  226. // Add the script to the document head.
  227. JFactory::getDocument()->addScriptDeclaration(implode("\n", $script));
  228. $html[] = $this->start(array('title' => $title, 'class' => $class), true);
  229. }
  230. if ($access) {
  231. $html[] = $this->itemModal('icon-plus', 'COM_PROJECTFORK_ASSIGN_TO_USER', $link, 800, 500, true);
  232. if ($count > 1) $html[] = $this->itemDivider(true);
  233. }
  234. foreach($assigned AS $user)
  235. {
  236. $action = "$('filter_assigned').set('value', " . intval($user->user_id) . "); submitbutton();";
  237. $html[] = $this->itemJavaScript('icon-user', $user->name, $action, true);
  238. }
  239. $html[] = $this->end(true);
  240. $html[] = '<input type="hidden" id="assigned' . $i . '" name="assigned[' . $id . ']" value="0"/>';
  241. return implode("\n", $html);
  242. }
  243. public function bulkItems($actions)
  244. {
  245. $message = addslashes(JText::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST'));
  246. $html = array();
  247. if (count($actions) == 0) {
  248. $html[] = '<div class="btn-group" id="bulk-action-menu">';
  249. $html[] = ' <a class="btn btn-primary disabled" href="javascript: void(0);"><span class="caret"></span></a>';
  250. $html[] = '</div>';
  251. return implode("\n", $html);
  252. }
  253. $html[] = '<div class="btn-group" id="bulk-action-menu">';
  254. $html[] = ' <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>';
  255. $html[] = ' <ul class="dropdown-menu">';
  256. foreach($actions AS $action)
  257. {
  258. $js = "if (document.adminForm.boxchecked.value==0){alert('" . $message . "');}"
  259. . "else{Joomla.submitbutton('" . $action->value . "')}";
  260. $icon = 'icon-chevron-right';
  261. if (strpos($action->value, '.publish') !== false) $icon = 'icon-eye-open';
  262. if (strpos($action->value, '.unpublish') !== false) $icon = 'icon-eye-close';
  263. if (strpos($action->value, '.archive') !== false) $icon = 'icon-folder-open';
  264. if (strpos($action->value, '.trash') !== false) $icon = 'icon-trash';
  265. if (strpos($action->value, '.delete') !== false) $icon = 'icon-remove';
  266. if (strpos($action->value, '.checkin') !== false) $icon = 'icon-ok-sign';
  267. $html[] = $this->itemJavaScript($icon, $action->text, $js, true);
  268. }
  269. $html[] = ' </ul>';
  270. $html[] = '</div>';
  271. return implode("\n", $html);
  272. }
  273. public function end($return = false)
  274. {
  275. $html = array();
  276. $html[] = ' </ul>';
  277. $html[] = '</div>';
  278. if ($return) return implode("\n", $html);
  279. $this->addItem(implode("\n", $html));
  280. }
  281. }