[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/menu_block/ -> menu_block.admin.inc (source)

   1  <?php
   2  // $Id: menu_block.admin.inc,v 1.40.2.4 2010/03/24 19:44:00 johnalbin Exp $
   3  
   4  /**
   5   * @file
   6   * Provides infrequently used functions for menu_block.
   7   */
   8  
   9  /**
  10   * Menu callback: display the menu block addition form.
  11   */
  12  function menu_block_add_block_form(&$form_state) {
  13    module_load_include('inc', 'block', 'block.admin');
  14    return block_admin_configure($form_state, 'menu_block', NULL);
  15  }
  16  
  17  /**
  18   * Save the new menu block.
  19   */
  20  function menu_block_add_block_form_submit($form, &$form_state) {
  21    // Determine the delta of the new block.
  22    $block_ids = variable_get('menu_block_ids', array());
  23    $delta = empty($block_ids) ? 1 : max($block_ids) + 1;
  24  
  25    // Save the new array of blocks IDs.
  26    $block_ids[] = $delta;
  27    variable_set('menu_block_ids', $block_ids);
  28  
  29    // Save the block configuration.
  30    _menu_block_block_save($delta, $form_state['values']);
  31  
  32    // Run the normal new block submission (borrowed from block_add_block_form_submit).
  33    foreach (list_themes() as $key => $theme) {
  34      if ($theme->status) {
  35        db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, %d, %d)", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $theme->name, 0, 0, $delta, BLOCK_NO_CACHE);
  36      }
  37    }
  38  
  39    foreach (array_filter($form_state['values']['roles']) as $rid) {
  40      db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta);
  41    }
  42  
  43    drupal_set_message(t('The block has been created.'));
  44    cache_clear_all();
  45  
  46    $form_state['redirect'] = 'admin/build/block';
  47    return;
  48  }
  49  
  50  /**
  51   * Alters the block admin form to add delete links next to menu blocks.
  52   */
  53  function _menu_block_form_block_admin_display_form_alter(&$form, $form_state) {
  54    foreach (variable_get('menu_block_ids', array()) AS $delta) {
  55      $form['menu_block_' . $delta]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete-menu-block/'. $delta));
  56    }
  57  }
  58  
  59  /**
  60   * Menu callback: confirm deletion of menu blocks.
  61   */
  62  function menu_block_delete(&$form_state, $delta = 0) {
  63    $title = _menu_block_format_title(menu_block_get_config($delta));
  64    $form['block_title'] = array('#type' => 'hidden', '#value' => $title);
  65    $form['delta'] = array('#type' => 'hidden', '#value' => $delta);
  66  
  67    return confirm_form($form, t('Are you sure you want to delete the "%name" block?', array('%name' => $title)), 'admin/build/block', NULL, t('Delete'), t('Cancel'));
  68  }
  69  
  70  /**
  71   * Deletion of menu blocks.
  72   */
  73  function menu_block_delete_submit($form, &$form_state) {
  74    // Remove the menu block configuration variables.
  75    $delta = $form_state['values']['delta'];
  76    $block_ids = variable_get('menu_block_ids', array());
  77    unset($block_ids[array_search($delta, $block_ids)]);
  78    sort($block_ids);
  79    variable_set('menu_block_ids', $block_ids);
  80    variable_del("menu_block_{$delta}_title_link");
  81    variable_del("menu_block_{$delta}_admin_title");
  82    variable_del("menu_block_{$delta}_parent");
  83    variable_del("menu_block_{$delta}_level");
  84    variable_del("menu_block_{$delta}_follow");
  85    variable_del("menu_block_{$delta}_depth");
  86    variable_del("menu_block_{$delta}_expanded");
  87    variable_del("menu_block_{$delta}_sort");
  88  
  89    db_query("DELETE FROM {blocks} WHERE module = 'menu_block' AND delta = %d", $delta);
  90    db_query("DELETE FROM {blocks_roles} WHERE module = 'menu_block' AND delta = %d", $delta);
  91    drupal_set_message(t('The "%name" block has been removed.', array('%name' => $form_state['values']['block_title'])));
  92    cache_clear_all();
  93    $form_state['redirect'] = 'admin/build/block';
  94    return;
  95  }
  96  
  97  /**
  98   * Returns the 'list' $op info for hook_block().
  99   */
 100  function _menu_block_block_list() {
 101    $blocks = array();
 102    foreach (variable_get('menu_block_ids', array()) AS $delta) {
 103      $blocks[$delta]['info'] = _menu_block_format_title(menu_block_get_config($delta));
 104      // Menu blocks can't be cached because each menu item can have
 105      // a custom access callback. menu.inc manages its own caching.
 106      $blocks[$delta]['cache'] = BLOCK_NO_CACHE;
 107    }
 108    return $blocks;
 109  }
 110  
 111  /**
 112   * Return the title of the block.
 113   *
 114   * @param $config
 115   *   array The configuration of the menu block.
 116   * @return
 117   *   string The title of the block.
 118   */
 119  function _menu_block_format_title($config) {
 120    // If an administrative title is specified, use it.
 121    if (!empty($config['admin_title'])) {
 122      return check_plain($config['admin_title']);
 123    }
 124    $menus = menu_block_get_all_menus();
 125    $menus[MENU_TREE__CURRENT_PAGE_MENU] = t('Current menu');
 126    if (empty($config['menu_name']) || empty($menus[$config['menu_name']])) {
 127      $title = t('Unconfigured menu block');
 128    }
 129    else {
 130      // Show the configured levels in the block info
 131      $replacements = array(
 132        '@menu_name' => $menus[$config['menu_name']],
 133        '@level1' => $config['level'],
 134        '@level2' => $config['level'] + $config['depth'] - 1,
 135      );
 136      if ($config['parent_mlid']) {
 137        $parent_item = menu_link_load($config['parent_mlid']);
 138        $replacements['@menu_name'] = $parent_item['title'];
 139      }
 140      if ($config['follow']) {
 141        $title = t('@menu_name (active menu item)', $replacements);
 142      }
 143      elseif ($config['depth'] == 1) {
 144        $title = t('@menu_name (level @level1)', $replacements);
 145      }
 146      elseif ($config['depth']) {
 147        if ($config['expanded']) {
 148          $title = t('@menu_name (expanded levels @level1-@level2)', $replacements);
 149        }
 150        else {
 151          $title = t('@menu_name (levels @level1-@level2)', $replacements);
 152        }
 153      }
 154      else {
 155        if ($config['expanded']) {
 156          $title = t('@menu_name (expanded levels @level1+)', $replacements);
 157        }
 158        else {
 159          $title = t('@menu_name (levels @level1+)', $replacements);
 160        }
 161      }
 162    }
 163    return $title;
 164  }
 165  
 166  /**
 167   * Returns the 'configure' $op info for hook_block().
 168   */
 169  function _menu_block_block_configure($delta) {
 170    // Create a pseudo form state.
 171    $form_state = array('values' => menu_block_get_config($delta));
 172    return menu_block_configure_form($form_state);
 173  }
 174  
 175  /**
 176   * Returns the configuration form for a menu tree.
 177   *
 178   * @param $form_state
 179   *   array An associated array of configuration options should be present in the
 180   *   'values' key. If none are given, default configuration is assumed.
 181   * @return
 182   *   array The form in Form API format.
 183   */
 184  function menu_block_configure_form(&$form_state) {
 185    $config = array();
 186    // Get the config from the form state.
 187    if (!empty($form_state['values'])) {
 188      $config = $form_state['values'];
 189      if (!empty($config['parent'])) {
 190        list($config['menu_name'], $config['parent_mlid']) = explode(':', $config['parent']);
 191      }
 192    }
 193    // Merge in the default configuration.
 194    $config += menu_block_get_config();
 195  
 196    // Get the list of menus.
 197    $menus = menu_block_get_all_menus();
 198  
 199    // Build the standard and jquery versions of the parent item options.
 200    $parent_options = $parent_options_js = array();
 201    foreach ($menus AS $menu_name => $title) {
 202      if ($menu_name == MENU_TREE__CURRENT_PAGE_MENU) {
 203        $options = array($menu_name . ':0' => $title);
 204        $parent_options += $options;
 205        // For the js options, replace the root item option with better help text.
 206        $options[$menu_name . ':0'] = '<' . t('root of menu') . '>';
 207      }
 208      else {
 209        // Retrieve the entire tree of each menu.
 210        $options = menu_parent_options(array($menu_name => $title), array('mlid' => 0));
 211        $parent_options += $options;
 212        $options[$menu_name . ':0'] = '<' . t('root of @menu_name', array('@menu_name' => $title)) . '>';
 213      }
 214      // Render the options as <option> elements.
 215      $parent_options_js[$menu_name] = form_select_options(array('#value' => NULL), $options);
 216    }
 217    // Build a select element that is only needed for the jquery version.
 218    $menus_select = theme('select', array(
 219      '#title' => t('Menu'),
 220      '#value' => $config['menu_name'],
 221      '#options' => $menus,
 222      '#name' => 'parent_menu',
 223      '#id' => 'edit-parent-menu',
 224      '#attributes' => array('class' => 'menu-block-parent-menu'),
 225      '#size' => 1,
 226      '#required' => FALSE,
 227      '#multiple' => FALSE,
 228      '#parents' => array('parent_menu'),
 229    ));
 230    // Load the javascript data.
 231    $settings = array(
 232      'menu_block' => array(
 233        'menus' => $menus_select . '<label id="item-label">' . t('Item') . ':</label>',
 234        'menus_default' => $config['menu_name'],
 235        'parent_options' => $parent_options_js,
 236        'parent_default' => $config['menu_name'] . ':' . $config['parent_mlid'],
 237      ),
 238    );
 239    drupal_add_js($settings, 'setting');
 240    drupal_add_js(drupal_get_path('module', 'menu_block') . '/menu-block.js');
 241  
 242    // Build the standard form.
 243    $form['menu-block-wrapper-start'] = array('#value' => '<div id="menu-block-settings" class="menu-block-configure-form">');
 244    drupal_add_css(drupal_get_path('module', 'menu_block') . '/menu-block-admin.css');
 245    $form['title_link'] = array(
 246      '#prefix' => '<div class="menu-block-title-link">',
 247      '#type' => 'checkbox',
 248      '#title' => t('Block title as link'),
 249      '#default_value' => $config['title_link'],
 250      '#description' => t('Make the default block title a link to that menu item. An overridden block title will not be a link.'),
 251      '#suffix' => '</div>',
 252    );
 253    $form['admin_title'] = array(
 254      '#type' => 'textfield',
 255      '#default_value' => $config['admin_title'],
 256      '#title' => t('Administrative title'),
 257      '#description' => t('This title will be used administratively to identify this block. If blank, the regular title will be used.'),
 258    );
 259    $form['parent'] = array(
 260      '#type' => 'select',
 261      '#title' => t('Parent item'),
 262      '#default_value' => $config['menu_name'] . ':' . $config['parent_mlid'],
 263      '#options' => $parent_options,
 264      '#description' => t('The tree of links will only contain children of the selected parent item. Using <em>&lt;the menu selected by the page&gt;</em> can be customized on the <a href="!url">Menu block settings page</a>.', array('!url' => url('admin/settings/menu_block'))),
 265      '#attributes' => array('class' => 'menu-block-parent'),
 266      '#element_validate' => array('menu_block_configure_form_parent_validate'),
 267    );
 268    $form['level'] = array(
 269      '#type' => 'select',
 270      '#title' => t('Starting level'),
 271      '#default_value' => $config['level'],
 272      '#options' => array(
 273        '1'  => t('1st level (primary)'),
 274        '2'  => t('2nd level (secondary)'),
 275        '3'  => t('3rd level (tertiary)'),
 276        '4'  => t('4th level'),
 277        '5'  => t('5th level'),
 278        '6'  => t('6th level'),
 279        '7'  => t('7th level'),
 280        '8'  => t('8th level'),
 281        '9'  => t('9th level'),
 282      ),
 283      '#description' => t('Blocks that start with the 1st level will always be visible. Blocks that start with the 2nd level or deeper will only be visible when the trail to the active menu item is in the block’s tree.'),
 284    );
 285    // The value of "follow" in the database/config array is either FALSE or the
 286    // value of the "follow_parent" form element.
 287    if ($follow = $config['follow']) {
 288      $follow_parent = $follow;
 289      $follow = 1;
 290    }
 291    else {
 292      $follow_parent = 'active';
 293    }
 294    $form['follow'] = array(
 295      '#type' => 'checkbox',
 296      '#title' => t('Make the starting level follow the active menu item.'),
 297      '#default_value' => $follow,
 298      '#description' => t('If the active menu item is deeper than the level specified above, the starting level will follow the active menu item. Otherwise, the starting level of the tree will remain fixed.'),
 299      '#element_validate' => array('menu_block_configure_form_follow_validate'),
 300      '#attributes' => array('class' => 'menu-block-follow'),
 301    );
 302    $form['follow_parent'] = array(
 303      '#prefix' => '<div class="menu-block-follow-parent">',
 304      '#type' => 'select',
 305      '#title' => t('Starting level will be'),
 306      '#default_value' => $follow_parent,
 307      '#options' => array(
 308        'active' => t('Active menu item'),
 309        'child' => t('Children of active menu item'),
 310      ),
 311      '#description' => t('When following the active menu item, specify if the starting level should be the active menu item or its children.'),
 312      '#suffix' => '</div>',
 313    );
 314    $form['depth'] = array(
 315      '#type' => 'select',
 316      '#title' => t('Maximum depth'),
 317      '#default_value' => $config['depth'],
 318      '#options' => array(
 319        '1'  => '1',
 320        '2'  => '2',
 321        '3'  => '3',
 322        '4'  => '4',
 323        '5'  => '5',
 324        '6'  => '6',
 325        '7'  => '7',
 326        '8'  => '8',
 327        '9'  => '9',
 328        '0'  => t('Unlimited'),
 329      ),
 330      '#description' => t('From the starting level, specify the maximum depth of the menu tree.'),
 331    );
 332    $form['expanded'] = array(
 333      '#type' => 'checkbox',
 334      '#title' => t('<strong>Expand all children</strong> of this tree.'),
 335      '#default_value' => $config['expanded'],
 336    );
 337    $form['sort'] = array(
 338      '#type' => 'checkbox',
 339      '#title' => t('<strong>Sort</strong> menu tree by the active menu item’s trail.'),
 340      '#default_value' => $config['sort'],
 341      '#description' => t('Sort each item in the active trail to the top of its level. When used on a deep or wide menu tree, the active menu item’s children will be easier to see when the page is reloaded.'),
 342    );
 343    $form['menu-block-wrapper-close'] = array('#value' => '</div>');
 344  
 345    return $form;
 346  }
 347  
 348  /**
 349   * Validates the parent element of the block configuration form.
 350   */
 351  function menu_block_configure_form_parent_validate($element, &$form_state) {
 352    // The "Menu block" config has the "parent" value split into "menu_name" and
 353    // "parent_mlid" values. The value of "parent" stored in the database/config array is either FALSE
 354    list($form_state['values']['menu_name'], $form_state['values']['parent_mlid']) = explode(':', $form_state['values']['parent']);
 355  }
 356  
 357  /**
 358   * Validates the follow element of the block configuration form.
 359   */
 360  function menu_block_configure_form_follow_validate($element, &$form_state) {
 361    // The value of "follow" stored in the database/config array is either FALSE
 362    // or the value of the "follow_parent" form element.
 363    if ($form_state['values']['follow'] && !empty($form_state['values']['follow_parent'])) {
 364      $form_state['values']['follow'] = $form_state['values']['follow_parent'];
 365    }
 366  }
 367  
 368  /**
 369   * Returns the 'save' $op info for hook_block().
 370   */
 371  function _menu_block_block_save($delta, $edit) {
 372    variable_set("menu_block_{$delta}_title_link", $edit['title_link']);
 373    variable_set("menu_block_{$delta}_admin_title", $edit['admin_title']);
 374    variable_set("menu_block_{$delta}_parent", $edit['parent']);
 375    variable_set("menu_block_{$delta}_level", $edit['level']);
 376    variable_set("menu_block_{$delta}_follow", $edit['follow']);
 377    variable_set("menu_block_{$delta}_depth", $edit['depth']);
 378    variable_set("menu_block_{$delta}_expanded", $edit['expanded']);
 379    variable_set("menu_block_{$delta}_sort", $edit['sort']);
 380  }
 381  
 382  /**
 383   * Menu block admin settings form.
 384   *
 385   * @return
 386   *   The settings form used by Menu block.
 387   */
 388  function menu_block_admin_settings_form($form_state) {
 389    // Grab all the menus.
 390    $menus = menu_block_get_all_menus();
 391    unset($menus[MENU_TREE__CURRENT_PAGE_MENU]);
 392    $total_menus = count($menus);
 393  
 394    // Load stored configuration.
 395    $menu_order = variable_get('menu_block_menu_order', array('primary-links' => '', 'secondary-links' => ''));
 396  
 397    // Merge the saved configuration with any un-configured menus.
 398    $all_menus = $menu_order + $menus;
 399  
 400    $form['heading'] = array(
 401      '#value' => '<p>' . t('If a block is configured to use <em>"the menu selected by the page"</em>, the block will be generated from the first "available" menu that contains a link to the page.') . '</p>',
 402    );
 403  
 404    // Orderable list of menu selections.
 405    $form['menu_order'] = array(
 406      '#tree' => TRUE,
 407      '#theme' => 'menu_block_menu_order',
 408    );
 409  
 410    $order = 0;
 411    foreach (array_keys($all_menus) as $menu_name) {
 412      $form['menu_order'][$menu_name] = array(
 413        'title' => array(
 414          '#value' => check_plain($menus[$menu_name]),
 415        ),
 416        'available' => array(
 417          '#type' => 'checkbox',
 418          '#attributes' => array('title' => t('Select from the @menu_name menu', array('@menu_name' => $menus[$menu_name]))),
 419          '#default_value' => isset($menu_order[$menu_name]),
 420        ),
 421        'weight' => array(
 422          '#type' => 'weight',
 423          '#default_value' => $order - $total_menus,
 424          '#delta' => $total_menus,
 425          '#id' => 'edit-menu-block-menus-' . $menu_name,
 426        ),
 427      );
 428      $order++;
 429    }
 430  
 431    $form['footer_note'] = array(
 432      '#value' => '<p>' . t('The above list will <em>not</em> affect menu blocks that are configured to use a specific menu.') . '</p>',
 433    );
 434  
 435    $form['submit'] = array(
 436      '#type' => 'submit',
 437      '#value' => t('Save configuration'),
 438    );
 439  
 440    return $form;
 441  }
 442  
 443  /**
 444   * Form submission handler.
 445   */
 446  function menu_block_admin_settings_form_submit($form, &$form_state) {
 447    $menu_order = array();
 448    foreach ($form_state['values']['menu_order'] AS $menu_name => $row) {
 449      if ($row['available']) {
 450        // Add available menu and its weight to list.
 451        $menu_order[$menu_name] = (int) $row['weight'];
 452      }
 453    }
 454    // Sort the keys by the weight stored in the value.
 455    asort($menu_order);
 456    foreach ($menu_order AS $menu_name => $weight) {
 457      // Now that the array is sorted, the weight is redundant data.
 458      $menu_order[$menu_name] = '';
 459    }
 460    variable_set('menu_block_menu_order', $menu_order);
 461  }
 462  
 463  /**
 464   * Theme a drag-to-reorder table of menu selection checkboxes.
 465   */
 466  function theme_menu_block_menu_order($element) {
 467    drupal_add_tabledrag('menu-block-menus', 'order', 'sibling', 'menu-weight');
 468  
 469    $header = array(
 470      t('Menu'),
 471      t('Available'),
 472      t('Weight'),
 473    );
 474  
 475    // Generate table of draggable menu names.
 476    $rows = array();
 477    foreach (element_children($element) as $menu_name) {
 478      $element[$menu_name]['weight']['#attributes']['class'] = 'menu-weight';
 479      $rows[] = array(
 480        'data' => array(
 481          drupal_render($element[$menu_name]['title']),
 482          drupal_render($element[$menu_name]['available']),
 483          drupal_render($element[$menu_name]['weight']),
 484        ),
 485        'class' => 'draggable',
 486      );
 487    }
 488    return theme('table', $header, $rows, array('id' => 'menu-block-menus'));
 489  }


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