[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/features/includes/ -> features.menu.inc (source)

   1  <?php
   2  // $Id: features.menu.inc,v 1.1.2.13 2010/08/12 19:30:35 yhahn Exp $
   3  
   4  /**
   5   * Implementation of hook_features_api().
   6   */
   7  function menu_features_api() {
   8    return array(
   9      'menu_custom' => array(
  10        'name' => t('Menus'),
  11        'default_hook' => 'menu_default_menu_custom',
  12        'feature_source' => TRUE,
  13        'default_file' => FEATURES_DEFAULTS_INCLUDED,
  14      ),
  15      'menu_links' => array(
  16        'name' => t('Menu links'),
  17        'default_hook' => 'menu_default_menu_links',
  18        'feature_source' => TRUE,
  19        'default_file' => FEATURES_DEFAULTS_INCLUDED,
  20      ),
  21      // DEPRECATED
  22      'menu' => array(
  23        'name' => t('Menu items'),
  24        'default_hook' => 'menu_default_items',
  25        'default_file' => FEATURES_DEFAULTS_INCLUDED,
  26        'feature_source' => FALSE,
  27      ),
  28    );
  29  }
  30  
  31  /**
  32   * Implementation of hook_features_export().
  33   * DEPRECATED: This implementation simply migrates deprecated `menu` items
  34   * to the `menu_links` type.
  35   */
  36  function menu_features_export($data, &$export, $module_name = '') {
  37    $pipe = array();
  38    foreach ($data as $path) {
  39      $pipe['menu_links'][] = "features:{$path}";
  40    }
  41    return $pipe;
  42  }
  43  
  44  /**
  45   * Implementation of hook_features_export_options().
  46   */
  47  function menu_custom_features_export_options() {
  48    $options = array();
  49    $result = db_query("SELECT * FROM {menu_custom}");
  50    while ($row = db_fetch_array($result)) {
  51      $options[$row['menu_name']] = $row['title'];
  52    }
  53    return $options;
  54  }
  55  
  56  /**
  57   * Implementation of hook_features_export().
  58   */
  59  function menu_custom_features_export($data, &$export, $module_name = '') {
  60    // Default hooks are provided by the feature module so we need to add
  61    // it as a dependency.
  62    $export['dependencies']['features'] = 'features';
  63    $export['dependencies']['menu'] = 'menu';
  64  
  65    // Collect a menu to module map
  66    $pipe = array();
  67    $map = features_get_default_map('menu_custom', 'menu_name');
  68    foreach ($data as $menu_name) {
  69      // If this menu is provided by a different module, add it as a dependency.
  70      if (isset($map[$menu_name]) && $map[$menu_name] != $module_name) {
  71        $export['dependencies'][$map[$menu_name]] = $map[$menu_name];
  72      }
  73      else {
  74        $export['features']['menu_custom'][$menu_name] = $menu_name;
  75      }
  76    }
  77    return $pipe;
  78  }
  79  
  80  /**
  81   * Implementation of hook_features_export_render()
  82   */
  83  function menu_custom_features_export_render($module, $data) {
  84    $code = array();
  85    $code[] = '  $menus = array();';
  86    $code[] = '';
  87  
  88    $translatables = array();
  89    foreach ($data as $menu_name) {
  90      $result = db_query("SELECT menu_name, title, description FROM {menu_custom} WHERE menu_name = '%s'", $menu_name);
  91      while ($row = db_fetch_array($result)) {
  92        $export = features_var_export($row, '  ');
  93        $code[] = "  // Exported menu: {$menu_name}";
  94        $code[] = "  \$menus['{$menu_name}'] = {$export};";
  95        $translatables[] = $row['title'];
  96        $translatables[] = $row['description'];
  97      }
  98    }
  99    if (!empty($translatables)) {
 100      $code[] = features_translatables_export($translatables, '  ');
 101    }
 102  
 103    $code[] = '';
 104    $code[] = '  return $menus;';
 105    $code = implode("\n", $code);
 106    return array('menu_default_menu_custom' => $code);
 107  }
 108  
 109  /**
 110   * Implementation of hook_features_export_revert().
 111   */
 112  function menu_custom_features_revert($module) {
 113    menu_custom_features_rebuild($module);
 114  }
 115  
 116  /**
 117   * Implementation of hook_features_export_rebuild().
 118   */
 119  function menu_custom_features_rebuild($module) {
 120    if ($defaults = features_get_default('menu_custom', $module)) {
 121      foreach ($defaults as $menu) {
 122        $existing = db_result(db_query("SELECT menu_name FROM {menu_custom} WHERE menu_name = '%s'", $menu['menu_name']));
 123        drupal_write_record('menu_custom', $menu, $existing ? array('menu_name') : array());
 124      }
 125    }
 126  }
 127  
 128  /**
 129   * Implementation of hook_features_export_options().
 130   */
 131  function menu_links_features_export_options() {
 132    $menu_links = menu_parent_options(array_reverse(menu_get_menus()), NULL);
 133    $options = array();
 134    foreach ($menu_links as $key => $name) {
 135      list($menu_name, $mlid) = explode(':', $key, 2);
 136      if ($mlid != 0) {
 137        $link = menu_link_load($mlid);
 138        $identifier = menu_links_features_identifier($link);
 139        $options[$identifier] = "{$menu_name}: {$name}";
 140      }
 141    }
 142    return $options;
 143  }
 144  
 145  /**
 146   * Callback for generating the menu link exportable identifier.
 147   */
 148  function menu_links_features_identifier($link) {
 149    return isset($link['menu_name'], $link['link_path']) ? "{$link['menu_name']}:{$link['link_path']}" : FALSE;
 150  }
 151  
 152  /**
 153   * Implementation of hook_features_export().
 154   */
 155  function menu_links_features_export($data, &$export, $module_name = '') {
 156    // Default hooks are provided by the feature module so we need to add
 157    // it as a dependency.
 158    $export['dependencies']['features'] = 'features';
 159    $export['dependencies']['menu'] = 'menu';
 160  
 161    // Collect a link to module map
 162    $pipe = array();
 163    $map = features_get_default_map('menu_links', 'menu_links_features_identifier');
 164    foreach ($data as $identifier) {
 165      if ($link = features_menu_link_load($identifier)) {
 166        // If this link is provided by a different module, add it as a dependency.
 167        if (isset($map[$identifier]) && $map[$identifier] != $module_name) {
 168          $export['dependencies'][$map[$identifier]] = $map[$identifier];
 169        }
 170        else {
 171          $export['features']['menu_links'][$identifier] = $identifier;
 172        }
 173        // For now, exclude a variety of common menus from automatic export.
 174        // They may still be explicitly included in a Feature if the builder
 175        // chooses to do so.
 176        if (!in_array($link['menu_name'], array('features', 'primary-links', 'secondary-links', 'navigation', 'admin', 'devel'))) {
 177          $pipe['menu_custom'][] = $link['menu_name'];
 178        }
 179      }
 180    }
 181    return $pipe;
 182  }
 183  
 184  /**
 185   * Implementation of hook_features_export_render()
 186   */
 187  function menu_links_features_export_render($module, $data) {
 188    $code = array();
 189    $code[] = '  $menu_links = array();';
 190    $code[] = '';
 191  
 192    $translatables = array();
 193    foreach ($data as $identifier) {
 194      if ($link = features_menu_link_load($identifier)) {
 195        // Replace plid with a parent path.
 196        if (!empty($link['plid']) && $parent = menu_link_load($link['plid'])) {
 197          $link['parent_path'] = $parent['link_path'];
 198        }
 199        unset($link['plid']);
 200        unset($link['mlid']);
 201  
 202        $code[] = "  // Exported menu link: {$identifier}";
 203        $code[] = "  \$menu_links['{$identifier}'] = ". features_var_export($link, '  ') .";";
 204        $translatables[] = $link['link_title'];
 205      }
 206    }
 207    if (!empty($translatables)) {
 208      $code[] = features_translatables_export($translatables, '  ');
 209    }
 210  
 211    $code[] = '';
 212    $code[] = '  return $menu_links;';
 213    $code = implode("\n", $code);
 214    return array('menu_default_menu_links' => $code);
 215  }
 216  
 217  /**
 218   * Implementation of hook_features_export_revert().
 219   */
 220  function menu_links_features_revert($module) {
 221    menu_links_features_rebuild($module);
 222  }
 223  
 224  /**
 225   * Implementation of hook_features_export_rebuild().
 226   */
 227  function menu_links_features_rebuild($module) {
 228    if ($menu_links = features_get_default('menu_links', $module)) {
 229      menu_links_features_rebuild_ordered($menu_links);
 230    }
 231  }
 232  
 233  /**
 234   * Generate a depth tree of all menu links.
 235   */
 236  function menu_links_features_rebuild_ordered($menu_links, $reset = FALSE) {
 237    static $ordered;
 238    static $all_links;
 239    if (!isset($ordered) || $reset) {
 240      $ordered = array();
 241      $unordered = features_get_default('menu_links');
 242  
 243      // Order all links by depth.
 244      if ($unordered) {
 245        do {
 246          $current = count($unordered);
 247          foreach ($unordered as $key => $link) {
 248            $identifier = menu_links_features_identifier($link);
 249            $parent = isset($link['parent_path']) ? "{$link['menu_name']}:{$link['parent_path']}" : '';
 250            if (empty($parent)) {
 251              $ordered[$identifier] = 0;
 252              $all_links[$identifier] = $link;
 253              unset($unordered[$key]);
 254            }
 255            elseif (isset($ordered[$parent])) {
 256              $ordered[$identifier] = $ordered[$parent] + 1;
 257              $all_links[$identifier] = $link;
 258              unset($unordered[$key]);
 259            }
 260          }
 261        } while (count($unordered) < $current);
 262      }
 263      asort($ordered);
 264    }
 265  
 266    // Ensure any default menu items that do not exist are created.
 267    foreach (array_keys($ordered) as $identifier) {
 268      $link = $all_links[$identifier];
 269      $existing = features_menu_link_load($identifier);
 270      if (!$existing || in_array($link, $menu_links)) {
 271        // Retrieve the mlid if this is an existing item.
 272        if ($existing) {
 273          $link['mlid'] = $existing['mlid'];
 274        }
 275        // Retrieve the plid for a parent link.
 276        if (!empty($link['parent_path']) && $parent = features_menu_link_load("{$link['menu_name']}:{$link['parent_path']}")) {
 277          $link['plid'] = $parent['mlid'];
 278        }
 279        else {
 280          $link['plid'] = 0;
 281        }
 282        menu_link_save($link);
 283      }
 284    }
 285  }
 286  
 287  /**
 288   * Load a menu link by its menu_name:link_path identifier.
 289   */
 290  function features_menu_link_load($identifier) {
 291    list($menu_name, $link_path) = explode(':', $identifier, 2);
 292    $result = db_query("SELECT menu_name, mlid, plid, link_path, router_path, link_title, options, module, hidden, external, has_children, expanded, weight FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menu_name, $link_path);
 293    while ($link = db_fetch_array($result)) {
 294      $link['options'] = unserialize($link['options']);
 295      return $link;
 296    }
 297    return FALSE;
 298  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7