| 1 |
<?php
|
| 2 |
// $Id: menu_block.admin.inc,v 1.28 2009/04/09 06:00:05 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 |
include_once './' . drupal_get_path('module', 'block') . '/block.admin.inc';
|
| 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($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}_parent");
|
| 81 |
variable_del("menu_block_{$delta}_level");
|
| 82 |
variable_del("menu_block_{$delta}_follow");
|
| 83 |
variable_del("menu_block_{$delta}_depth");
|
| 84 |
variable_del("menu_block_{$delta}_expanded");
|
| 85 |
variable_del("menu_block_{$delta}_sort");
|
| 86 |
|
| 87 |
db_query("DELETE FROM {blocks} WHERE module = 'menu_block' AND delta = %d", $delta);
|
| 88 |
db_query("DELETE FROM {blocks_roles} WHERE module = 'menu_block' AND delta = %d", $delta);
|
| 89 |
drupal_set_message(t('The "%name" block has been removed.', array('%name' => $form_state['values']['block_title'])));
|
| 90 |
cache_clear_all();
|
| 91 |
$form_state['redirect'] = 'admin/build/block';
|
| 92 |
return;
|
| 93 |
}
|
| 94 |
|
| 95 |
/**
|
| 96 |
* Returns the 'list' $op info for hook_block().
|
| 97 |
*/
|
| 98 |
function _menu_block_block_list() {
|
| 99 |
$blocks = array();
|
| 100 |
foreach (variable_get('menu_block_ids', array()) AS $delta) {
|
| 101 |
$blocks[$delta]['info'] = _menu_block_format_title($delta);
|
| 102 |
// Menu blocks can't be cached because each menu item can have
|
| 103 |
// a custom access callback. menu.inc manages its own caching.
|
| 104 |
$blocks[$delta]['cache'] = BLOCK_NO_CACHE;
|
| 105 |
}
|
| 106 |
return $blocks;
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Return the title of the block.
|
| 111 |
*
|
| 112 |
* @param $delta
|
| 113 |
* int The delta of the menu block
|
| 114 |
* @return
|
| 115 |
* string The title of the block
|
| 116 |
*/
|
| 117 |
function _menu_block_format_title($delta) {
|
| 118 |
list($menu_name, $parent_mlid) = split(':', variable_get("menu_block_{$delta}_parent", ':0'));
|
| 119 |
$menus = menu_block_get_all_menus();
|
| 120 |
if (empty($menu_name) || empty($menus[$menu_name])) {
|
| 121 |
$title = t('Unconfigured menu block');
|
| 122 |
}
|
| 123 |
else {
|
| 124 |
$level = variable_get("menu_block_{$delta}_level", 1);
|
| 125 |
$follow = variable_get("menu_block_{$delta}_follow", 0);
|
| 126 |
$depth = variable_get("menu_block_{$delta}_depth", 0);
|
| 127 |
$expanded = variable_get("menu_block_{$delta}_expanded", 0);
|
| 128 |
// Show the configured levels in the block info
|
| 129 |
$replacements = array('@menu_name' => $menus[$menu_name], '@level1' => $level, '@level2' => $level + $depth - 1);
|
| 130 |
if ($parent_mlid) {
|
| 131 |
$parent_item = menu_link_load($parent_mlid);
|
| 132 |
$replacements['@menu_name'] = $parent_item['title'];
|
| 133 |
}
|
| 134 |
if ($follow) {
|
| 135 |
$title = t('@menu_name (active menu item)', $replacements);
|
| 136 |
}
|
| 137 |
elseif ($depth == 1) {
|
| 138 |
$title = t('@menu_name (level @level1)', $replacements);
|
| 139 |
}
|
| 140 |
elseif ($depth) {
|
| 141 |
if ($expanded) {
|
| 142 |
$title = t('@menu_name (expanded levels @level1-@level2)', $replacements);
|
| 143 |
}
|
| 144 |
else {
|
| 145 |
$title = t('@menu_name (levels @level1-@level2)', $replacements);
|
| 146 |
}
|
| 147 |
}
|
| 148 |
else {
|
| 149 |
if ($expanded) {
|
| 150 |
$title = t('@menu_name (expanded levels @level1+)', $replacements);
|
| 151 |
}
|
| 152 |
else {
|
| 153 |
$title = t('@menu_name (levels @level1+)', $replacements);
|
| 154 |
}
|
| 155 |
}
|
| 156 |
}
|
| 157 |
return $title;
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Returns the 'configure' $op info for hook_block().
|
| 162 |
*/
|
| 163 |
function _menu_block_block_configure($delta) {
|
| 164 |
// Get the list of menus.
|
| 165 |
$menus = menu_block_get_all_menus();
|
| 166 |
// Get the parent item defaults.
|
| 167 |
$parent_default = variable_get("menu_block_{$delta}_parent", 'primary-links:0');
|
| 168 |
list($menus_default, ) = split(':', $parent_default);
|
| 169 |
|
| 170 |
// Build the standard and jquery versions of the parent item options.
|
| 171 |
$parent_options = $parent_options_js = array();
|
| 172 |
foreach ($menus AS $menu_name => $title) {
|
| 173 |
// Retrieve the entire tree of each menu.
|
| 174 |
$options = menu_parent_options(array($menu_name => $title), array('mlid' => 0));
|
| 175 |
$parent_options += $options;
|
| 176 |
// Render the options as <option> elements.
|
| 177 |
$options[$menu_name . ':0'] = '<' . t('root of @menu_name', array('@menu_name' => $title)) . '>';
|
| 178 |
$parent_options_js[$menu_name] = form_select_options(array('#value' => NULL), $options);
|
| 179 |
}
|
| 180 |
// Build a select element that is only needed for the jquery version.
|
| 181 |
$menus_select = theme('select', array(
|
| 182 |
'#title' => t('Menu'),
|
| 183 |
'#value' => $menus_default,
|
| 184 |
'#options' => $menus,
|
| 185 |
'#name' => 'parent_menu',
|
| 186 |
'#id' => 'edit-parent-menu',
|
| 187 |
'#size' => 1,
|
| 188 |
'#required' => FALSE,
|
| 189 |
'#multiple' => FALSE,
|
| 190 |
'#parents' => array('parent_menu'),
|
| 191 |
));
|
| 192 |
// Load the javascript data.
|
| 193 |
$settings = array(
|
| 194 |
'menu_block' => array(
|
| 195 |
'menus' => $menus_select . '<label id="item-label">' . t('Item') . ':</label>',
|
| 196 |
'menus_default' => $menus_default,
|
| 197 |
'parent_options' => $parent_options_js,
|
| 198 |
'parent_default' => $parent_default,
|
| 199 |
),
|
| 200 |
);
|
| 201 |
drupal_add_js($settings, 'setting');
|
| 202 |
drupal_add_js(drupal_get_path('module', 'menu_block') . '/menu-block.js');
|
| 203 |
|
| 204 |
// Build the standard form.
|
| 205 |
$form['wrapper-start'] = array('#value' => '<div id="menu-block-settings">');
|
| 206 |
drupal_add_css(drupal_get_path('module', 'menu_block') . '/menu-block-admin.css');
|
| 207 |
$form['title_link'] = array(
|
| 208 |
'#type' => 'checkbox',
|
| 209 |
'#title' => t('Block title as link'),
|
| 210 |
'#default_value' => variable_get("menu_block_{$delta}_title_link", 0),
|
| 211 |
'#description' => t('Make the default block title a link to that menu item. An overridden block title will not be a link.'),
|
| 212 |
);
|
| 213 |
$form['parent'] = array(
|
| 214 |
'#type' => 'select',
|
| 215 |
'#title' => t('Parent item'),
|
| 216 |
'#default_value' => $parent_default,
|
| 217 |
'#options' => $parent_options,
|
| 218 |
'#description' => t('The tree of links will only contain children of the selected parent item.'),
|
| 219 |
);
|
| 220 |
$form['level'] = array(
|
| 221 |
'#type' => 'select',
|
| 222 |
'#title' => t('Starting level'),
|
| 223 |
'#default_value' => variable_get("menu_block_{$delta}_level", 1),
|
| 224 |
'#options' => array(
|
| 225 |
'1' => t('1st level (primary)'),
|
| 226 |
'2' => t('2nd level (secondary)'),
|
| 227 |
'3' => t('3rd level (tertiary)'),
|
| 228 |
'4' => t('4th level'),
|
| 229 |
'5' => t('5th level'),
|
| 230 |
'6' => t('6th level'),
|
| 231 |
'7' => t('7th level'),
|
| 232 |
'8' => t('8th level'),
|
| 233 |
'9' => t('9th level'),
|
| 234 |
),
|
| 235 |
'#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.'),
|
| 236 |
);
|
| 237 |
if ($follow = variable_get("menu_block_{$delta}_follow", 0)) {
|
| 238 |
$follow_parent = $follow;
|
| 239 |
$follow = 1;
|
| 240 |
}
|
| 241 |
else {
|
| 242 |
$follow_parent = 'active';
|
| 243 |
}
|
| 244 |
$form['follow'] = array(
|
| 245 |
'#type' => 'checkbox',
|
| 246 |
'#title' => t('Make the starting level follow the active menu item.'),
|
| 247 |
'#default_value' => $follow,
|
| 248 |
'#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.'),
|
| 249 |
);
|
| 250 |
$form['follow_parent'] = array(
|
| 251 |
'#type' => 'select',
|
| 252 |
'#title' => t('Starting level will be'),
|
| 253 |
'#default_value' => $follow_parent,
|
| 254 |
'#options' => array(
|
| 255 |
'active' => t('Active menu item'),
|
| 256 |
'child' => t('Children of active menu item'),
|
| 257 |
),
|
| 258 |
'#description' => t('When following the active menu item, specify if the starting level should be the active menu item or its children.'),
|
| 259 |
);
|
| 260 |
$form['depth'] = array(
|
| 261 |
'#type' => 'select',
|
| 262 |
'#title' => t('Maximum depth'),
|
| 263 |
'#default_value' => variable_get("menu_block_{$delta}_depth", 0),
|
| 264 |
'#options' => array(
|
| 265 |
'1' => '1',
|
| 266 |
'2' => '2',
|
| 267 |
'3' => '3',
|
| 268 |
'4' => '4',
|
| 269 |
'5' => '5',
|
| 270 |
'6' => '6',
|
| 271 |
'7' => '7',
|
| 272 |
'8' => '8',
|
| 273 |
'9' => '9',
|
| 274 |
'0' => t('Unlimited'),
|
| 275 |
),
|
| 276 |
'#description' => t('From the starting level, specify the maximum depth of the menu tree.'),
|
| 277 |
);
|
| 278 |
$form['expanded'] = array(
|
| 279 |
'#type' => 'checkbox',
|
| 280 |
'#prefix' => '<div id="expanded-wrapper"><strong>' . t('Expand children') . ':</strong>',
|
| 281 |
'#suffix' => '</div>',
|
| 282 |
'#title' => t('Expand all the sub-menus of this tree.'),
|
| 283 |
'#default_value' => variable_get("menu_block_{$delta}_expanded", 0),
|
| 284 |
);
|
| 285 |
$form['sort'] = array(
|
| 286 |
'#type' => 'checkbox',
|
| 287 |
'#prefix' => '<div id="sort-wrapper"><strong>' . t('Sort') . ':</strong>',
|
| 288 |
'#suffix' => '</div>',
|
| 289 |
'#title' => t('Sort menu tree by the active menu item’s trail.'),
|
| 290 |
'#default_value' => variable_get("menu_block_{$delta}_sort", 0),
|
| 291 |
'#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.'),
|
| 292 |
);
|
| 293 |
$form['wrapper-close'] = array('#value' => '</div>');
|
| 294 |
return $form;
|
| 295 |
}
|
| 296 |
|
| 297 |
/**
|
| 298 |
* Returns the 'save' $op info for hook_block().
|
| 299 |
*/
|
| 300 |
function _menu_block_block_save($delta, $edit) {
|
| 301 |
if ($edit['follow'] && !empty($edit['follow_parent'])) {
|
| 302 |
$edit['follow'] = $edit['follow_parent'];
|
| 303 |
}
|
| 304 |
variable_set("menu_block_{$delta}_title_link", $edit['title_link']);
|
| 305 |
variable_set("menu_block_{$delta}_parent", $edit['parent']);
|
| 306 |
variable_set("menu_block_{$delta}_level", $edit['level']);
|
| 307 |
variable_set("menu_block_{$delta}_follow", $edit['follow']);
|
| 308 |
variable_set("menu_block_{$delta}_depth", $edit['depth']);
|
| 309 |
variable_set("menu_block_{$delta}_expanded", $edit['expanded']);
|
| 310 |
variable_set("menu_block_{$delta}_sort", $edit['sort']);
|
| 311 |
}
|