[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/block/ -> block.admin.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Admin page callbacks for the block module.
   6   */
   7  
   8  /**
   9   * Menu callback for admin/build/block.
  10   */
  11  function block_admin_display($theme = NULL) {
  12    global $custom_theme;
  13  
  14    // If non-default theme configuration has been selected, set the custom theme.
  15    $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
  16  
  17    // Fetch and sort blocks
  18    $blocks = _block_rehash();
  19    usort($blocks, '_block_compare');
  20  
  21    return drupal_get_form('block_admin_display_form', $blocks, $theme);
  22  }
  23  
  24  /**
  25   * Generate main blocks administration form.
  26   */
  27  function block_admin_display_form(&$form_state, $blocks, $theme = NULL) {
  28    global $theme_key, $custom_theme;
  29  
  30    // Add CSS
  31    drupal_add_css(drupal_get_path('module', 'block') .'/block.css', 'module', 'all', FALSE);
  32  
  33    // If non-default theme configuration has been selected, set the custom theme.
  34    $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
  35    init_theme();
  36  
  37    $throttle = module_exists('throttle');
  38    $block_regions = system_region_list($theme_key) + array(BLOCK_REGION_NONE => '<'. t('none') .'>');
  39  
  40    // Weights range from -delta to +delta, so delta should be at least half
  41    // of the amount of blocks present. This makes sure all blocks in the same
  42    // region get an unique weight.
  43    $weight_delta = round(count($blocks) / 2);
  44  
  45    // Build form tree
  46    $form = array(
  47      '#action' => arg(4) ? url('admin/build/block/list/'. $theme_key) : url('admin/build/block'),
  48      '#tree' => TRUE,
  49    );
  50  
  51    foreach ($blocks as $i => $block) {
  52      $key = $block['module'] .'_'. $block['delta'];
  53      $form[$key]['module'] = array(
  54        '#type' => 'value',
  55        '#value' => $block['module'],
  56      );
  57      $form[$key]['delta'] = array(
  58        '#type' => 'value',
  59        '#value' => $block['delta'],
  60      );
  61      $form[$key]['info'] = array(
  62        '#value' => check_plain($block['info'])
  63      );
  64      $form[$key]['theme'] = array(
  65        '#type' => 'hidden',
  66        '#value' => $theme_key
  67      );
  68      $form[$key]['weight'] = array(
  69        '#type' => 'weight',
  70        '#default_value' => $block['weight'],
  71        '#delta' => $weight_delta,
  72      );
  73      $form[$key]['region'] = array(
  74        '#type' => 'select',
  75        '#default_value' => $block['region'],
  76        '#options' => $block_regions,
  77      );
  78  
  79      if ($throttle) {
  80        $form[$key]['throttle'] = array('#type' => 'checkbox', '#default_value' => isset($block['throttle']) ? $block['throttle'] : FALSE);
  81      }
  82      $form[$key]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/configure/'. $block['module'] .'/'. $block['delta']));
  83      if ($block['module'] == 'block') {
  84        $form[$key]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete/'. $block['delta']));
  85      }
  86    }
  87  
  88    $form['submit'] = array(
  89      '#type' => 'submit',
  90      '#value' => t('Save blocks'),
  91    );
  92  
  93    return $form;
  94  }
  95  
  96  /**
  97   * Process main blocks administration form submission.
  98   */
  99  function block_admin_display_form_submit($form, &$form_state) {
 100    foreach ($form_state['values'] as $block) {
 101      $block['status'] = $block['region'] != BLOCK_REGION_NONE;
 102      $block['region'] = $block['status'] ? $block['region'] : '';
 103      db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s', throttle = %d WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'], $block['weight'], $block['region'], isset($block['throttle']) ? $block['throttle'] : 0, $block['module'], $block['delta'], $block['theme']);
 104    }
 105    drupal_set_message(t('The block settings have been updated.'));
 106    cache_clear_all();
 107  }
 108  
 109  /**
 110   * Helper function for sorting blocks on admin/build/block.
 111   *
 112   * Active blocks are sorted by region, then by weight.
 113   * Disabled blocks are sorted by name.
 114   */
 115  function _block_compare($a, $b) {
 116    global $theme_key;
 117    static $regions;
 118  
 119    // We need the region list to correctly order by region.
 120    if (!isset($regions)) {
 121      $regions = array_flip(array_keys(system_region_list($theme_key)));
 122      $regions[BLOCK_REGION_NONE] = count($regions);
 123    }
 124  
 125    // Separate enabled from disabled.
 126    $status = $b['status'] - $a['status'];
 127    if ($status) {
 128      return $status;
 129    }
 130    // Sort by region (in the order defined by theme .info file).
 131    if ((!empty($a['region']) && !empty($b['region'])) && ($place = ($regions[$a['region']] - $regions[$b['region']]))) {
 132      return $place;
 133    }
 134    // Sort by weight.
 135    $weight = $a['weight'] - $b['weight'];
 136    if ($weight) {
 137      return $weight;
 138    }
 139    // Sort by title.
 140    return strcmp($a['info'], $b['info']);
 141  }
 142  
 143  /**
 144   * Menu callback; displays the block configuration form.
 145   */
 146  function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
 147  
 148    $form['module'] = array('#type' => 'value', '#value' => $module);
 149    $form['delta'] = array('#type' => 'value', '#value' => $delta);
 150  
 151    $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
 152  
 153    $form['block_settings'] = array(
 154      '#type' => 'fieldset',
 155      '#title' => t('Block specific settings'),
 156      '#collapsible' => TRUE,
 157    );
 158    $form['block_settings']['title'] = array(
 159      '#type' => 'textfield',
 160      '#title' => t('Block title'),
 161      '#maxlength' => 64,
 162      '#description' => $module == 'block' ? t('The title of the block as shown to the user.') : t('Override the default title for the block. Use <em>&lt;none&gt;</em> to display no title, or leave blank to use the default block title.'),
 163      '#default_value' => $edit['title'],
 164      '#weight' => -18,
 165    );
 166  
 167  
 168    // Module-specific block configurations.
 169    if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
 170      foreach ($settings as $k => $v) {
 171        $form['block_settings'][$k] = $v;
 172      }
 173    }
 174  
 175    // Get the block subject for the page title.
 176    $info = module_invoke($module, 'block', 'list');
 177    if (isset($info[$delta])) {
 178      drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
 179    }
 180  
 181    // Standard block configurations.
 182    $form['user_vis_settings'] = array(
 183      '#type' => 'fieldset',
 184      '#title' => t('User specific visibility settings'),
 185      '#collapsible' => TRUE,
 186    );
 187    $form['user_vis_settings']['custom'] = array(
 188      '#type' => 'radios',
 189      '#title' => t('Custom visibility settings'),
 190      '#options' => array(
 191        t('Users cannot control whether or not they see this block.'),
 192        t('Show this block by default, but let individual users hide it.'),
 193        t('Hide this block by default but let individual users show it.')
 194      ),
 195      '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
 196      '#default_value' => $edit['custom'],
 197    );
 198  
 199    // Role-based visibility settings
 200    $default_role_options = array();
 201    $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
 202    while ($role = db_fetch_object($result)) {
 203      $default_role_options[] = $role->rid;
 204    }
 205    $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
 206    $role_options = array();
 207    while ($role = db_fetch_object($result)) {
 208      $role_options[$role->rid] = $role->name;
 209    }
 210    $form['role_vis_settings'] = array(
 211      '#type' => 'fieldset',
 212      '#title' => t('Role specific visibility settings'),
 213      '#collapsible' => TRUE,
 214    );
 215    $form['role_vis_settings']['roles'] = array(
 216      '#type' => 'checkboxes',
 217      '#title' => t('Show block for specific roles'),
 218      '#default_value' => $default_role_options,
 219      '#options' => $role_options,
 220      '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
 221    );
 222  
 223    $form['page_vis_settings'] = array(
 224      '#type' => 'fieldset',
 225      '#title' => t('Page specific visibility settings'),
 226      '#collapsible' => TRUE,
 227    );
 228    $access = user_access('use PHP for block visibility');
 229  
 230    if ($edit['visibility'] == 2 && !$access) {
 231      $form['page_vis_settings'] = array();
 232      $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
 233      $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
 234    }
 235    else {
 236      $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
 237      $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
 238  
 239      if ($access) {
 240        $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
 241        $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
 242      }
 243      $form['page_vis_settings']['visibility'] = array(
 244        '#type' => 'radios',
 245        '#title' => t('Show block on specific pages'),
 246        '#options' => $options,
 247        '#default_value' => $edit['visibility'],
 248      );
 249      $form['page_vis_settings']['pages'] = array(
 250        '#type' => 'textarea',
 251        '#title' => t('Pages'),
 252        '#default_value' => $edit['pages'],
 253        '#description' => $description,
 254      );
 255    }
 256  
 257    $form['submit'] = array(
 258      '#type' => 'submit',
 259      '#value' => t('Save block'),
 260    );
 261  
 262    return $form;
 263  }
 264  
 265  function block_admin_configure_validate($form, &$form_state) {
 266    if ($form_state['values']['module'] == 'block') {
 267      if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {boxes} WHERE bid != %d AND info = '%s'", $form_state['values']['delta'], $form_state['values']['info']))) {
 268        form_set_error('info', t('Please ensure that each block description is unique.'));
 269      }
 270    }
 271  }
 272  
 273  function block_admin_configure_submit($form, &$form_state) {
 274    if (!form_get_errors()) {
 275      db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, title = '%s' WHERE module = '%s' AND delta = '%s'", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $form_state['values']['delta']);
 276      db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_state['values']['module'], $form_state['values']['delta']);
 277      foreach (array_filter($form_state['values']['roles']) as $rid) {
 278        db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $form_state['values']['delta']);
 279      }
 280      module_invoke($form_state['values']['module'], 'block', 'save', $form_state['values']['delta'], $form_state['values']);
 281      drupal_set_message(t('The block configuration has been saved.'));
 282      cache_clear_all();
 283      $form_state['redirect'] = 'admin/build/block';
 284      return;
 285    }
 286  }
 287  
 288  /**
 289   * Menu callback: display the custom block addition form.
 290   */
 291  function block_add_block_form(&$form_state) {
 292    return block_admin_configure($form_state, 'block', NULL);
 293  }
 294  
 295  function block_add_block_form_validate($form, &$form_state) {
 296    if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {boxes} WHERE info = '%s'", $form_state['values']['info']))) {
 297      form_set_error('info', t('Please ensure that each block description is unique.'));
 298    }
 299  }
 300  
 301  /**
 302   * Save the new custom block.
 303   */
 304  function block_add_block_form_submit($form, &$form_state) {
 305    db_query("INSERT INTO {boxes} (body, info, format) VALUES ('%s', '%s', %d)", $form_state['values']['body'], $form_state['values']['info'], $form_state['values']['format']);
 306    $delta = db_last_insert_id('boxes', 'bid');
 307  
 308    foreach (list_themes() as $key => $theme) {
 309      if ($theme->status) {
 310        db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, '%s', %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);
 311      }
 312    }
 313  
 314    foreach (array_filter($form_state['values']['roles']) as $rid) {
 315      db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta);
 316    }
 317  
 318    drupal_set_message(t('The block has been created.'));
 319    cache_clear_all();
 320  
 321    $form_state['redirect'] = 'admin/build/block';
 322    return;
 323  }
 324  
 325  /**
 326   * Menu callback; confirm deletion of custom blocks.
 327   */
 328  function block_box_delete(&$form_state, $bid = 0) {
 329    $box = block_box_get($bid);
 330    $form['info'] = array('#type' => 'hidden', '#value' => $box['info']);
 331    $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
 332  
 333    return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $box['info'])), 'admin/build/block', '', t('Delete'), t('Cancel'));
 334  }
 335  
 336  /**
 337   * Deletion of custom blocks.
 338   */
 339  function block_box_delete_submit($form, &$form_state) {
 340    db_query('DELETE FROM {boxes} WHERE bid = %d', $form_state['values']['bid']);
 341    db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = '%s'", $form_state['values']['bid']);
 342    drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
 343    cache_clear_all();
 344    $form_state['redirect'] = 'admin/build/block';
 345    return;
 346  }
 347  
 348  /**
 349   * Process variables for block-admin-display.tpl.php.
 350   *
 351   * The $variables array contains the following arguments:
 352   * - $form
 353   *
 354   * @see block-admin-display.tpl.php
 355   * @see theme_block_admin_display()
 356   */
 357  function template_preprocess_block_admin_display_form(&$variables) {
 358    global $theme_key;
 359  
 360    $block_regions = system_region_list($theme_key);
 361    $variables['throttle'] = module_exists('throttle');
 362    $variables['block_regions'] = $block_regions + array(BLOCK_REGION_NONE => t('Disabled'));
 363  
 364    foreach ($block_regions as $key => $value) {
 365      // Highlight regions on page to provide visual reference.
 366      drupal_set_content($key, '<div class="block-region">'. $value .'</div>');
 367      // Initialize an empty array for the region.
 368      $variables['block_listing'][$key] = array();
 369    }
 370  
 371    // Initialize disabled blocks array.
 372    $variables['block_listing'][BLOCK_REGION_NONE] = array();
 373  
 374    // Set up to track previous region in loop.
 375    $last_region = '';
 376    foreach (element_children($variables['form']) as $i) {
 377      $block = &$variables['form'][$i];
 378  
 379      // Only take form elements that are blocks.
 380      if (isset($block['info'])) {
 381        // Fetch region for current block.
 382        $region = $block['region']['#default_value'];
 383  
 384        // Set special classes needed for table drag and drop.
 385        $variables['form'][$i]['region']['#attributes']['class'] = 'block-region-select block-region-'. $region;
 386        $variables['form'][$i]['weight']['#attributes']['class'] = 'block-weight block-weight-'. $region;
 387  
 388        $variables['block_listing'][$region][$i] = new stdClass();
 389        $variables['block_listing'][$region][$i]->row_class = isset($block['#attributes']['class']) ? $block['#attributes']['class'] : '';
 390        $variables['block_listing'][$region][$i]->block_modified = isset($block['#attributes']['class']) && strpos($block['#attributes']['class'], 'block-modified') !== FALSE ? TRUE : FALSE;
 391        $variables['block_listing'][$region][$i]->block_title =  drupal_render($block['info']);
 392        $variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
 393        $variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
 394        $variables['block_listing'][$region][$i]->throttle_check = $variables['throttle'] ? drupal_render($block['throttle']) : '';
 395        $variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
 396        $variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
 397        $variables['block_listing'][$region][$i]->printed = FALSE;
 398  
 399        $last_region = $region;
 400      }
 401    }
 402  
 403    $variables['form_submit'] = drupal_render($variables['form']);
 404  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7