PageRenderTime 25ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/themes/mothership/mothership/functions/menu.php

https://bitbucket.org/lucascsilva/slpg
PHP | 340 lines | 241 code | 38 blank | 61 comment | 44 complexity | e06312343b87fba4974287a95e16db6c MD5 | raw file
  1. <?php
  2. //TODO remove classes from the <a>
  3. //kill of the <ul class="menu" around the menues
  4. //we already have the menu-block-wrapper that adds a <nav tag
  5. function mothership_menu_tree($variables) {
  6. if(theme_get_setting('mothership_classes_menu_wrapper')){
  7. return '<ul>' . $variables['tree'] . '</ul>';
  8. }else{
  9. return '<ul class="menu">' . $variables['tree'] . '</ul>';
  10. }
  11. }
  12. /*
  13. walk through each menu link and kill the classes we dont want
  14. */
  15. function mothership_menu_link(array $variables) {
  16. //clean up the classes
  17. // $remove = array('first','last','leaf','collapsed','expanded','expandable');
  18. $remove = array();
  19. if(theme_get_setting('mothership_classes_menu_items_firstlast')){
  20. $remove[] .= "first";
  21. $remove[] .= "last";
  22. }
  23. if(theme_get_setting('mothership_classes_menu_leaf')){
  24. $remove[] .= "leaf";
  25. }
  26. if(theme_get_setting('mothership_classes_menu_collapsed')){
  27. $remove[] .= "collapsed";
  28. $remove[] .= "expanded";
  29. $remove[] .= "expandable";
  30. }
  31. if(theme_get_setting('mothership_classes_menu_items_active')){
  32. $remove[] .= "active-trail";
  33. $remove[] .= "active";
  34. }
  35. if($remove){
  36. $variables['element']['#attributes']['class'] = array_diff($variables['element']['#attributes']['class'],$remove);
  37. }
  38. //Remove thee menu-mlid-[NUMBER]
  39. if(theme_get_setting('mothership_classes_menu_items_mlid')){
  40. $variables['element']['#attributes']['class'] = preg_grep('/^menu-mlid-/', $variables['element']['#attributes']['class'], PREG_GREP_INVERT);
  41. }
  42. //if we wanna remove the class for realz so nozing passes
  43. if(theme_get_setting('mothership_classes_menu_items')){
  44. unset($variables['element']['#attributes']['class']);
  45. }
  46. //test if we even have to have class defined
  47. if($variables['element']['#attributes']['class']){
  48. // dpr($variables['element']['#attributes']['class']);
  49. }else{
  50. unset($variables['element']['#attributes']['class']);
  51. }
  52. $element = $variables['element'];
  53. $sub_menu = '';
  54. if ($element['#below']) {
  55. $sub_menu = drupal_render($element['#below']);
  56. }
  57. // dpr($variables['element']['#attributes']);
  58. $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  59. return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
  60. }
  61. // TODO: book nav.
  62. //http://api.drupal.org/api/drupal/modules--book--book.module/function/template_preprocess_book_navigation/7
  63. /*
  64. @hook_pager
  65. we rewrites this so we can get shorter class names
  66. Remove all the pager- prefixes classes we dont need this the pager have the pager class on the ul
  67. pager-first & pager-last removed we use the css :first-child instead
  68. we add a daddy item (whos your daddy) so the wrapper item_list gets an idea who called it
  69. */
  70. function mothership_pager($variables) {
  71. $tags = $variables['tags'];
  72. $element = $variables['element'];
  73. $parameters = $variables['parameters'];
  74. $quantity = $variables['quantity'];
  75. global $pager_page_array, $pager_total;
  76. // Calculate various markers within this pager piece:
  77. // Middle is used to "center" pages around the current page.
  78. $pager_middle = ceil($quantity / 2);
  79. // current is the page we are currently paged to
  80. $pager_current = $pager_page_array[$element] + 1;
  81. // first is the first page listed by this pager piece (re quantity)
  82. $pager_first = $pager_current - $pager_middle + 1;
  83. // last is the last page listed by this pager piece (re quantity)
  84. $pager_last = $pager_current + $quantity - $pager_middle;
  85. // max is the maximum page number
  86. $pager_max = $pager_total[$element];
  87. // End of marker calculations.
  88. // Prepare for generation loop.
  89. $i = $pager_first;
  90. if ($pager_last > $pager_max) {
  91. // Adjust "center" if at end of query.
  92. $i = $i + ($pager_max - $pager_last);
  93. $pager_last = $pager_max;
  94. }
  95. if ($i <= 0) {
  96. // Adjust "center" if at start of query.
  97. $pager_last = $pager_last + (1 - $i);
  98. $i = 1;
  99. }
  100. // End of generation loop preparation.
  101. $li_first = theme('pager_first', array('text' => (isset($tags[0]) ? $tags[0] : t('« first')), 'element' => $element, 'parameters' => $parameters));
  102. $li_previous = theme('pager_previous', array('text' => (isset($tags[1]) ? $tags[1] : t('‹ previous')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  103. $li_next = theme('pager_next', array('text' => (isset($tags[3]) ? $tags[3] : t('next ›')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
  104. $li_last = theme('pager_last', array('text' => (isset($tags[4]) ? $tags[4] : t('last »')), 'element' => $element, 'parameters' => $parameters));
  105. if ($pager_total[$element] > 1) {
  106. if ($li_first) {
  107. $items[] = array(
  108. // 'class' => array('first'),
  109. 'data' => $li_first,
  110. );
  111. }
  112. if ($li_previous) {
  113. $items[] = array(
  114. 'class' => array('previous'),
  115. 'data' => $li_previous,
  116. );
  117. }
  118. // When there is more than one page, create the pager list.
  119. if ($i != $pager_max) {
  120. if ($i > 1) {
  121. $items[] = array(
  122. 'class' => array('ellipsis'),
  123. 'data' => '…',
  124. );
  125. }
  126. // Now generate the actual pager piece.
  127. for (; $i <= $pager_last && $i <= $pager_max; $i++) {
  128. if ($i < $pager_current) {
  129. $items[] = array(
  130. // 'class' => array('pager-item'),
  131. 'data' => theme('pager_previous', array('text' => $i, 'element' => $element, 'interval' => ($pager_current - $i), 'parameters' => $parameters)),
  132. );
  133. }
  134. if ($i == $pager_current) {
  135. $items[] = array(
  136. 'class' => array('current'),
  137. 'data' => $i,
  138. );
  139. }
  140. if ($i > $pager_current) {
  141. $items[] = array(
  142. // 'class' => array('pager-item'),
  143. 'data' => theme('pager_next', array('text' => $i, 'element' => $element, 'interval' => ($i - $pager_current), 'parameters' => $parameters)),
  144. );
  145. }
  146. }
  147. if ($i < $pager_max) {
  148. $items[] = array(
  149. 'class' => array('ellipsis'),
  150. 'data' => '…',
  151. );
  152. }
  153. }
  154. // End generation.
  155. if ($li_next) {
  156. $items[] = array(
  157. 'class' => array('next'),
  158. 'data' => $li_next,
  159. );
  160. }
  161. if ($li_last) {
  162. $items[] = array(
  163. // 'class' => array('last'),
  164. 'data' => $li_last,
  165. );
  166. }
  167. //we wrap this in *gasp* so
  168. return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array(
  169. 'items' => $items,
  170. 'attributes' => array('class' => array('pager') ),
  171. 'daddy' => 'pager'
  172. ));
  173. }
  174. }
  175. /*
  176. views pagers
  177. theme_views_mini_pager
  178. original: /views/theme/theme.inc
  179. */
  180. function mothership_views_mini_pager($vars) {
  181. global $pager_page_array, $pager_total;
  182. $tags = $vars['tags'];
  183. $element = $vars['element'];
  184. $parameters = $vars['parameters'];
  185. $quantity = $vars['quantity'];
  186. // Calculate various markers within this pager piece:
  187. // Middle is used to "center" pages around the current page.
  188. $pager_middle = ceil($quantity / 2);
  189. // current is the page we are currently paged to
  190. $pager_current = $pager_page_array[$element] + 1;
  191. // max is the maximum page number
  192. $pager_max = $pager_total[$element];
  193. // End of marker calculations.
  194. $li_previous = theme('pager_previous',
  195. array(
  196. 'text' => (isset($tags[1]) ? $tags[1] : t('‹‹')),
  197. 'element' => $element,
  198. 'interval' => 1,
  199. 'parameters' => $parameters,
  200. )
  201. );
  202. if (empty($li_previous)) {
  203. $li_previous = "&nbsp;";
  204. }
  205. $li_next = theme('pager_next',
  206. array(
  207. 'text' => (isset($tags[3]) ? $tags[3] : t('››')),
  208. 'element' => $element,
  209. 'interval' => 1,
  210. 'parameters' => $parameters,
  211. )
  212. );
  213. if (empty($li_next)) {
  214. $li_next = "&nbsp;";
  215. }
  216. if ($pager_total[$element] > 1) {
  217. $items[] = array(
  218. 'class' => array('previous'),
  219. 'data' => $li_previous,
  220. );
  221. $items[] = array(
  222. 'class' => array('current'),
  223. 'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
  224. );
  225. $items[] = array(
  226. 'class' => array('next'),
  227. 'data' => $li_next,
  228. );
  229. return theme('item_list',
  230. array(
  231. 'items' => $items,
  232. 'title' => NULL,
  233. 'type' => 'ul',
  234. 'attributes' => array('class' => array('pager')),
  235. 'daddy' => 'pager'
  236. )
  237. );
  238. }
  239. }
  240. /*
  241. the non saying item-list class haw now added an -daddy element
  242. so if the theme that calls the itemlist adds an 'daddy' => '-pager' to the theme call
  243. the item list haves an idea of what it is
  244. */
  245. function mothership_item_list($variables) {
  246. $items = $variables['items'];
  247. $title = $variables['title'];
  248. $type = $variables['type'];
  249. $attributes = $variables['attributes'];
  250. //get the daddy if its set and add it is item-list-$daddy
  251. if(isset($variables['daddy'])){
  252. $wrapperclass = "item-list-" . $variables['daddy'];
  253. }else{
  254. $wrapperclass = "item-list";
  255. }
  256. $output = '<div class="'. $wrapperclass .'">';
  257. if (isset($title)) {
  258. $output .= '<h3>' . $title . '</h3>';
  259. }
  260. if (!empty($items)) {
  261. $output .= "<$type" . drupal_attributes($attributes) . '>';
  262. $num_items = count($items);
  263. foreach ($items as $i => $item) {
  264. $attributes = array();
  265. $children = array();
  266. $data = '';
  267. if (is_array($item)) {
  268. foreach ($item as $key => $value) {
  269. if ($key == 'data') {
  270. $data = $value;
  271. }
  272. elseif ($key == 'children') {
  273. $children = $value;
  274. }
  275. else {
  276. $attributes[$key] = $value;
  277. }
  278. }
  279. }
  280. else {
  281. $data = $item;
  282. }
  283. if (count($children) > 0) {
  284. // Render nested list.
  285. $data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes));
  286. }
  287. if ($i == 0) {
  288. //TODO remove first
  289. $attributes['class'][] = 'first';
  290. }
  291. if ($i == $num_items - 1) {
  292. //TODO remove last
  293. $attributes['class'][] = 'last';
  294. }
  295. $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n";
  296. }
  297. $output .= "</$type>";
  298. }
  299. $output .= '</div>';
  300. return $output;
  301. }