You are here

megamenu.utilities.inc in Megamenu 7

Same filename and directory in other branches
  1. 6.2 megamenu.utilities.inc
  2. 6 megamenu.utilities.inc

Helper/utility functions for Megamenu.

File

megamenu.utilities.inc
View source
<?php

/**
 * @file
 * Helper/utility functions for Megamenu.
 */

/**
 * function menu_list()
 *
 * Helper function lists menu machine names except 'navigation'.
 * @todo I think this will have to be changed to some stored value, but maybe
 *   not? could still be 1 function?
 */
function _megamenu_menulist() {
  $menu_list = menu_get_menus();
  unset($menu_list['navigation']);
  return array_keys($menu_list);
}
function _megamenu_enabled_menus() {
  $menus = db_select('megamenu', 'm')
    ->condition('m.enabled', 1)
    ->fields('m', array(
    'menu_name',
  ))
    ->execute()
    ->fetchCol();
  if (!empty($menus)) {
    return $menus;
  }
  return FALSE;
}

/**
 * Retrieve a menu and pre-style before theming.
 *
 * This function currently removes all "hidden" items, which could be
 * handled in the single iteration of the theme function.  But, I forsee
 * a need for other prep work that might be simmplfied with this second function
 *
 * Will be used in the future to add/alter attributes prior to theming
 *
 * @param $menuname
 *    The name of the menu to extract
 *
 * @return
 *    The pre-styled menu tree
 */
function _megamenu_get_menu_tree($menuname) {
  $menutree = menu_tree_all_data($menuname);

  // Contains active trail.
  $menutree_page = menu_tree_page_data($menuname);
  foreach ($menutree as $tier_1_key => $tier_1_item) {
    if ($menutree_page && $menutree_page[$tier_1_key]['link']['in_active_trail']) {
      $menutree[$tier_1_key]['link']['in_active_trail'] = TRUE;
    }
    if ($tier_1_item['link']['hidden'] == 1 || empty($tier_1_item['link']['link_title'])) {
      unset($menutree[$tier_1_key]);
    }
    elseif ($tier_1_item['below']) {
      foreach ($tier_1_item['below'] as $tier_2_key => $tier_2_item) {
        if ($menutree[$tier_1_key]['below'][$tier_2_key]['link']['in_active_trail']) {
          $menutree[$tier_1_key]['below'][$tier_2_key]['link']['in_active_trail'] = TRUE;
        }
        if ($tier_2_item['link']['hidden'] == 1 || empty($tier_2_item['link']['link_title'])) {
          unset($menutree[$tier_1_key]['below'][$tier_2_key]);
        }
        else {
          if ($tier_2_item['below']) {
            foreach ($tier_2_item['below'] as $tier_3_key => $tier_3_item) {
              if ($menutree[$tier_1_key]['below'][$tier_2_key]['below'][$tier_3_key]['link']['in_active_trail']) {
                $menutree[$tier_1_key]['below'][$tier_2_key]['below'][$tier_3_key]['link']['in_active_trail'] = TRUE;
              }
              if ($tier_3_item['link']['hidden'] == 1 || empty($tier_3_item['link']['link_title'])) {
                unset($menutree[$tier_1_key]['below'][$tier_2_key]['below'][$tier_3_key]);
              }
              elseif ($tier_3_item['below']) {
                unset($menutree[$tier_1_key]['below'][$tier_2_key]['below'][$tier_3_key]['below']);
              }
            }

            // end level 3 loop
          }
        }
      }

      // end level 2 loop
    }
  }

  // end level 1 loop
  return $menutree;
}

/**
 * Determine if a particular mega menu item is active.
 *
 * @todo make it better
 *
 * @param <string> $branch (of mega menu)
 */
function _megamenu_active_classes($branch) {
  if (!is_array($branch)) {
    return '';
  }
  $active = '';
  if ($branch['link']['in_active_trail']) {
    if (!isset($branch['link']['below']) || $branch['link']['below'] == FALSE) {
      $active = ' active';
    }
    $active .= ' active-trail';
  }
  return $active;
}

/**
 * Remove the list wrapping div - we don't need it.
 *
 * @todo Use regex replace instead?
 *
 * @param <strong> $list_markup
 */
function _megamenu_strip_list_wrapper($output = null) {

  // Remove opening wrapper div class.
  $output = str_replace('<div class="item-list">', '', $output);

  // Remove wrapper end div closer.
  $output = substr($output, 0, strlen($output) - strlen('</div>'));
  return $output;
}

/**
 * Determine the zebra, half, and order attributes of an item.
 *
 * This is a helper function to generate count based classes
 * based on an items position in the sequence and the total count of
 * items. These classes consist of an item's zebra (even/odd), half
 * (half1/half2), and order (first/last).
 *
 * @param int $position
 *    An item's numerical position within a sequence.
 * @param int $total_count
 *    The total count of items.
 *
 * @return string
 *    zebra (event/odd), halves (half1/half2), and order (first/last). clases are
 *    returned as a space delimited list.
 */
function _megamenu_count_attributes($position, $total_count) {
  $zebra = $position % 2 ? ' even' : ' odd';
  $halves = $position < $total_count / 2 ? ' half-1' : ' half-2';
  if ($position == 0) {
    $order = ' leaf-' . $position . ' first';
  }
  elseif ($position == $total_count - 1) {
    $order = ' leaf-' . $position . ' last';
  }
  else {
    $order = ' leaf-' . $position;
  }
  return $position . $zebra . $halves . $order;
}
function _megamenu_is_enabled($menu_name) {
  $result = db_query('SELECT enabled FROM {megamenu} WHERE menu_name = :menu_name', array(
    ':menu_name' => $menu_name,
  ))
    ->fetchField();
  return $result;
}
function _megamenu_get_menu_orientation_by_name($menu_name) {
  $result = db_query('SELECT menu_orientation FROM {megamenu} WHERE menu_name = :menu_name', array(
    ':menu_name' => $menu_name,
  ))
    ->fetchField();
  return $result;
}
function _megamenu_get_skin_by_name($menu_name) {
  $result = db_query('SELECT skin FROM {megamenu} WHERE menu_name = :menu_name', array(
    ':menu_name' => $menu_name,
  ))
    ->fetchField();
  return $result;
}
function _megamenu_get_slot_orientation_by_name($menu_name) {
  $result = db_query('SELECT slot_orientation FROM {megamenu} WHERE menu_name = :menu_name', array(
    ':menu_name' => $menu_name,
  ))
    ->fetchField();
  return $result;
}

/**
 * Check if an entry exists for the given menu name. If not, then insert a row.
 *
 * @param string $menu_name
 *   Verified menus name.
 */
function _megamenu_verify_menu_entry($menu_name) {
  $result = db_query('SELECT menu_name FROM {megamenu} WHERE menu_name = :menu_name', array(
    ':menu_name' => $menu_name,
  ))
    ->fetchField();
  if (!$result) {
    db_insert('megamenu')
      ->fields(array(
      'menu_name' => $menu_name,
      'enabled' => 0,
    ))
      ->execute();
  }
}

/**
 * Determine if a particular mega menu is using a default supplied skin.
 *
 * @todo Rewrite this function! This is just a quick and dirty solution.
 *
 * @param string $menu_name
 *   Verified menus name.
 *
 * @return bool
 *   Check if skin is default skin.
 */
function _megamenu_is_skin_default($menu_name) {
  $result = db_query('SELECT skin FROM {megamenu} WHERE menu_name = :menu_name', array(
    ':menu_name' => $menu_name,
  ))
    ->fetchField();
  return $result === 'minimal' || $result === 'friendly';
}

/**
 * Return a translated menu item if one exists.
 */
function _megamenu_get_translated_menu_title($current_menu, $mlid) {
  $item = menu_link_load($mlid);
  if (module_exists('i18n_menu')) {
    $item['title'] = _i18n_menu_link_title($item);
  }
  return $item['title'];
}

Functions

Namesort descending Description
_megamenu_active_classes Determine if a particular mega menu item is active.
_megamenu_count_attributes Determine the zebra, half, and order attributes of an item.
_megamenu_enabled_menus
_megamenu_get_menu_orientation_by_name
_megamenu_get_menu_tree Retrieve a menu and pre-style before theming.
_megamenu_get_skin_by_name
_megamenu_get_slot_orientation_by_name
_megamenu_get_translated_menu_title Return a translated menu item if one exists.
_megamenu_is_enabled
_megamenu_is_skin_default Determine if a particular mega menu is using a default supplied skin.
_megamenu_menulist function menu_list()
_megamenu_strip_list_wrapper Remove the list wrapping div - we don't need it.
_megamenu_verify_menu_entry Check if an entry exists for the given menu name. If not, then insert a row.