[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/modules/block/ -> block.module (source)

   1  <?php
   2  // $Id: block.module,v 1.246.2.6 2007/09/12 07:49:35 drumm Exp $
   3  
   4  /**
   5   * @file
   6   * Controls the boxes that are displayed around the main content.
   7   */
   8  
   9  /**
  10   * Denotes that a block is not enabled in any region and should not
  11   * be shown.
  12   */
  13  define('BLOCK_REGION_NONE', -1);
  14  
  15  /**
  16   * Implementation of hook_help().
  17   */
  18  function block_help($section) {
  19    switch ($section) {
  20      case 'admin/help#block':
  21        $output = '<p>'. t('Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. Blocks are usually generated automatically by modules (e.g., Recent Forum Topics), but administrators can also define custom blocks.') .'</p>';
  22        $output .= '<p>'. t('The region each block appears in depends on both which theme you are using (some themes allow greater control over block placement than others), and on the settings in the block administration section.') .'</p>';
  23        $output .= '<p>'. t('The block administration screen lets you specify the vertical placement of the blocks within a region. You do this by assigning a weight to each block. Lighter blocks (those having a smaller weight) "float up" towards the top of the region; heavier ones "sink".') .'</p>';
  24        $output .= t("<p>A block's visibility depends on:</p>
  25  <ul>
  26  <li>Its region placement. Blocks with no region assigned to them are never shown.</li>
  27  <li>Its throttle checkbox when throttle module is enabled. Throttled blocks are hidden during high server loads.</li>
  28  <li>Its page visibility settings. Blocks can be configured to be visible/hidden on certain pages.</li>
  29  <li>Its custom visibility settings. Blocks can be configured to be visible only when specific conditions are true.</li>
  30  <li>Its user visibility settings. Administrators can choose to let users decide whether to show/hide certain blocks.</li>
  31  <li>Its user-role visibility settings. Administrators can choose to let blocks be visible only for certain user roles.</li>
  32  <li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li>
  33  </ul>
  34  ");
  35        $output .= '<h3>'. t('Module blocks') .'</h3>';
  36        $output .= '<p>'. t('Some modules generate blocks that become available when the modules are enabled. These blocks can be administered via the <a href="@admin-block">blocks administration page</a>.</p>', array('@admin-block' => url('admin/build/block'))) .'</p>';
  37        $output .= '<h3>'. t('Administrator defined blocks') .'</h3>';
  38        $output .= '<p>'. t('Administrators can also define custom blocks. These blocks consist of a title, a description, and a body which can be as long as you wish. Block content can be in any of the input formats supported for other content.') .'</p>';
  39        $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@block">Block page</a>.', array('@block' => 'http://drupal.org/handbook/modules/block/')) .'</p>';
  40        return $output;
  41      case 'admin/build/block':
  42        return t('<p>Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. They are usually generated automatically by modules, but administrators can create blocks manually.</p>
  43  <p>Only enabled blocks are shown. You can position blocks by specifying which area of the page they should appear in (e.g., a sidebar).  Highlighted labels on this page show the regions into which blocks can be rendered. You can specify where within a region a block will appear by adjusting its weight.</p>
  44  <p>If you want certain blocks to disable themselves temporarily during high server loads, check the "Throttle" box. You can configure the auto-throttle on the <a href="@throttle">throttle configuration page</a> after having enabled the throttle module.</p>
  45  <p>You can configure the behaviour of each block (for example, specifying on which pages and for what users it will appear) by clicking the "configure" link for each block.</p>', array('@throttle' => url('admin/settings/throttle')));
  46      case 'admin/build/block/add':
  47        return '<p>'. t('Here you can create a new block. Once you have created this block you must make it active and give it a place on the page using <a href="@overview">blocks</a>. The description is used in the "block" column on the <a href="@overview">blocks</a> page.', array('@overview' => url('admin/build/block'))) .'</p>';
  48    }
  49  }
  50  
  51  /**
  52   * Implementation of hook_perm().
  53   */
  54  function block_perm() {
  55    return array('administer blocks', 'use PHP for block visibility');
  56  }
  57  
  58  /**
  59   * Implementation of hook_menu().
  60   */
  61  function block_menu($may_cache) {
  62    $items = array();
  63  
  64    if ($may_cache) {
  65      $items[] = array(
  66        'path' => 'admin/build/block',
  67        'title' => t('Blocks'),
  68        'access' => user_access('administer blocks'),
  69        'description' => t('Configure what block content appears in your site\'s sidebars and other regions.'),
  70        'callback' => 'drupal_get_form',
  71        'callback arguments' => array('block_admin_display'));
  72      $items[] = array('path' => 'admin/build/block/list', 'title' => t('List'),
  73        'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
  74      $items[] = array('path' => 'admin/build/block/configure', 'title' => t('Configure block'),
  75        'access' => user_access('administer blocks'),
  76        'callback' => 'drupal_get_form',
  77        'callback arguments' => array('block_admin_configure'),
  78        'type' => MENU_CALLBACK);
  79      $items[] = array('path' => 'admin/build/block/delete', 'title' => t('Delete block'),
  80        'access' => user_access('administer blocks'),
  81        'callback' => 'drupal_get_form',
  82        'callback arguments' => array('block_box_delete'),
  83        'type' => MENU_CALLBACK);
  84      $items[] = array('path' => 'admin/build/block/add', 'title' => t('Add block'),
  85        'access' => user_access('administer blocks'),
  86        'callback' => 'drupal_get_form',
  87        'callback arguments' => array('block_box_form'),
  88        'type' => MENU_LOCAL_TASK);
  89      $default = variable_get('theme_default', 'garland');
  90      foreach (list_themes() as $key => $theme) {
  91        $items[] = array(
  92          'path' => 'admin/build/block/list/'. $key,
  93          'title' => t('!key settings', array('!key' => $key)),
  94          'callback' => 'drupal_get_form',
  95          'callback arguments' => array('block_admin_display', $key),
  96          'access' => user_access('administer blocks'),
  97          'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
  98          'weight' => $key == $default ? -10 : 0,
  99        );
 100      }
 101    }
 102  
 103    return $items;
 104  }
 105  
 106  /**
 107   * Implementation of hook_block().
 108   *
 109   * Generates the administrator-defined blocks for display.
 110   */
 111  function block_block($op = 'list', $delta = 0, $edit = array()) {
 112    switch ($op) {
 113      case 'list':
 114        $blocks = array();
 115  
 116        $result = db_query('SELECT bid, info FROM {boxes} ORDER BY info');
 117        while ($block = db_fetch_object($result)) {
 118          $blocks[$block->bid]['info'] = $block->info;
 119        }
 120        return $blocks;
 121  
 122      case 'configure':
 123        $box = block_box_get($delta);
 124        if (filter_access($box['format'])) {
 125          return block_box_form($box);
 126        }
 127        break;
 128  
 129      case 'save':
 130        block_box_save($edit, $delta);
 131        break;
 132  
 133      case 'view':
 134        $block = db_fetch_object(db_query('SELECT body, format FROM {boxes} WHERE bid = %d', $delta));
 135        $data['content'] = check_markup($block->body, $block->format, FALSE);
 136        return $data;
 137    }
 138  }
 139  
 140  /**
 141   * Update the 'blocks' DB table with the blocks currently exported by modules.
 142   *
 143   * @return
 144   *   Blocks currently exported by modules.
 145   */
 146  function _block_rehash() {
 147    global $theme_key;
 148  
 149    init_theme();
 150  
 151    $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
 152    while ($old_block = db_fetch_object($result)) {
 153      $old_blocks[$old_block->module][$old_block->delta] = $old_block;
 154    }
 155  
 156    $blocks = array();
 157  
 158    foreach (module_list() as $module) {
 159      $module_blocks = module_invoke($module, 'block', 'list');
 160      if ($module_blocks) {
 161        foreach ($module_blocks as $delta => $block) {
 162          $block['module'] = $module;
 163          $block['delta']  = $delta;
 164          // If previously written to database, load values.
 165          if ($old_blocks[$module][$delta]) {
 166            $block['status'] = $old_blocks[$module][$delta]->status;
 167            $block['weight'] = $old_blocks[$module][$delta]->weight;
 168            $block['region'] = $old_blocks[$module][$delta]->region;
 169            $block['visibility'] = $old_blocks[$module][$delta]->visibility;
 170            $block['pages'] = $old_blocks[$module][$delta]->pages;
 171            $block['custom'] = $old_blocks[$module][$delta]->custom;
 172            $block['throttle'] = $old_blocks[$module][$delta]->throttle;
 173            $block['title'] = $old_blocks[$module][$delta]->title;
 174          }
 175          // Otherwise, use any set values, or else substitute defaults.
 176          else {
 177            $properties = array('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0, 'title' => '');
 178            foreach ($properties as $property => $default) {
 179              if (!isset($block[$property])) {
 180                $block[$property] = $default;
 181              }
 182            }
 183          }
 184  
 185          $blocks[] = $block;
 186        }
 187      }
 188    }
 189  
 190    db_lock_table('blocks');
 191    // Remove all blocks from table.
 192    db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);
 193  
 194    // Reinsert new set of blocks into table.
 195    foreach ($blocks as $block) {
 196      db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, title) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['title']);
 197    }
 198    db_unlock_tables();
 199  
 200    return $blocks;
 201  }
 202  
 203  /**
 204   * Generate main block administration form.
 205   */
 206  function block_admin_display($theme = NULL) {
 207    global $theme_key, $custom_theme;
 208  
 209    // Add CSS
 210    drupal_add_css(drupal_get_path('module', 'block') .'/block.css', 'module', 'all', FALSE);
 211  
 212    // If non-default theme configuration has been selected, set the custom theme.
 213    if ($theme) {
 214      $custom_theme = $theme;
 215    }
 216    else {
 217      $custom_theme = variable_get('theme_default', 'garland');
 218    }
 219    init_theme();
 220  
 221    // Fetch and sort blocks
 222    $blocks = _block_rehash();
 223    usort($blocks, '_block_compare');
 224  
 225    $throttle = module_exists('throttle');
 226    $block_regions = array(BLOCK_REGION_NONE => '<'. t('none') .'>') + system_region_list($theme_key);
 227  
 228    // Build form tree
 229    $form['#action'] = arg(3) ? url('admin/build/block/list/'. $theme_key) : url('admin/build/block');
 230    $form['#tree'] = TRUE;
 231    foreach ($blocks as $i => $block) {
 232      $form[$i]['module'] = array('#type' => 'value', '#value' => $block['module']);
 233      $form[$i]['delta'] = array('#type' => 'value', '#value' => $block['delta']);
 234      $form[$i]['info'] = array('#value' => check_plain($block['info']));
 235      $form[$i]['theme'] = array('#type' => 'hidden', '#value' => $theme_key);
 236      $form[$i]['weight'] = array('#type' => 'weight', '#default_value' => $block['weight']);
 237      $form[$i]['region'] = array('#type' => 'select',
 238        '#default_value' => $block['status'] ? (isset($block['region']) ? $block['region'] : system_default_region($theme_key)) : BLOCK_REGION_NONE,
 239        '#options' => $block_regions,
 240      );
 241  
 242      if ($throttle) {
 243        $form[$i]['throttle'] = array('#type' => 'checkbox', '#default_value' => $block['throttle']);
 244      }
 245      $form[$i]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/configure/'. $block['module'] .'/'. $block['delta']));
 246      if ($block['module'] == 'block') {
 247        $form[$i]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete/'. $block['delta']));
 248      }
 249    }
 250    $form['submit'] = array('#type' => 'submit', '#value' => t('Save blocks'));
 251  
 252    return $form;
 253  }
 254  
 255  /**
 256   * Helper function for sorting blocks on admin/build/block.
 257   *
 258   * Active blocks are sorted by region, then by weight.
 259   * Disabled blocks are sorted by name.
 260   */
 261  function _block_compare($a, $b) {
 262    $status = $b['status'] - $a['status'];
 263    // Separate enabled from disabled.
 264    if ($status) {
 265      return $status;
 266    }
 267    // Enabled blocks
 268    if ($a['status']) {
 269      $place = strcmp($a['region'], $b['region']);
 270      return $place ? $place : ($a['weight'] - $b['weight']);
 271    }
 272    // Disabled blocks
 273    else {
 274      return strcmp($a['info'], $b['info']);
 275    }
 276  }
 277  
 278  /**
 279   * Process main block administration form submission.
 280   */
 281  function block_admin_display_submit($form_id, $form_values) {
 282    foreach ($form_values as $block) {
 283      $block['status'] = $block['region'] != BLOCK_REGION_NONE;
 284      $block['region'] = $block['status'] ? $block['region'] : '';
 285      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']);
 286    }
 287    drupal_set_message(t('The block settings have been updated.'));
 288    cache_clear_all();
 289  }
 290  
 291  /**
 292   * Theme main block administration form submission.
 293   *
 294   * Note: the blocks are already sorted in the right order,
 295   * grouped by status, region and weight.
 296   */
 297  function theme_block_admin_display($form) {
 298    global $theme_key;
 299  
 300    $throttle = module_exists('throttle');
 301    $block_regions = system_region_list($theme_key);
 302  
 303    // Highlight regions on page to provide visual reference.
 304    foreach ($block_regions as $key => $value) {
 305      drupal_set_content($key, '<div class="block-region">'. $value .'</div>');
 306    }
 307  
 308    // Build rows
 309    $rows = array();
 310    $last_region = '';
 311    $last_status = 1;
 312    foreach (element_children($form) as $i) {
 313      $block = &$form[$i];
 314      // Only take form elements that are blocks.
 315      if (is_array($block['info'])) {
 316        // Fetch values
 317        $region = $block['region']['#default_value'];
 318        $status = $region != BLOCK_REGION_NONE;
 319  
 320        // Output region header
 321        if ($status && $region != $last_region) {
 322          $region_title = t('@region', array('@region' => drupal_ucfirst($block_regions[$region])));
 323          $rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
 324          $last_region = $region;
 325        }
 326        // Output disabled header
 327        elseif ($status != $last_status) {
 328          $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
 329          $last_status = $status;
 330        }
 331  
 332        // Generate block row
 333        $row = array(
 334          array('data' => drupal_render($block['info']), 'class' => 'block'),
 335          drupal_render($block['region']) . drupal_render($block['theme']),
 336          drupal_render($block['weight']),
 337        );
 338        if ($throttle) {
 339          $row[] = drupal_render($block['throttle']);
 340        }
 341        $row[] = drupal_render($block['configure']);
 342        $row[] = $block['delete'] ? drupal_render($block['delete']) : '';
 343        $rows[] = $row;
 344      }
 345    }
 346  
 347    // Finish table
 348    $header = array(t('Block'), t('Region'), t('Weight'));
 349    if ($throttle) {
 350      $header[] = t('Throttle');
 351    }
 352    $header[] = array('data' => t('Operations'), 'colspan' => 2);
 353  
 354    $output = theme('table', $header, $rows, array('id' => 'blocks'));
 355  
 356    $output .= drupal_render($form);
 357  
 358    return $output;
 359  }
 360  
 361  function block_box_get($bid) {
 362    return db_fetch_array(db_query("SELECT bx.*, bl.title FROM {boxes} bx INNER JOIN {blocks} bl ON bx.bid = bl.delta WHERE bl.module = 'block' AND bx.bid = %d", $bid));
 363  }
 364  
 365  /**
 366   * Menu callback; displays the block configuration form.
 367   */
 368  function block_admin_configure($module = NULL, $delta = 0) {
 369  
 370    $form['module'] = array('#type' => 'value', '#value' => $module);
 371    $form['delta'] = array('#type' => 'value', '#value' => $delta);
 372  
 373    $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
 374  
 375    $form['block_settings'] = array(
 376      '#type' => 'fieldset',
 377      '#title' => t('Block specific settings'),
 378      '#collapsible' => TRUE,
 379    );
 380    $form['block_settings']['title'] = array(
 381      '#type' => 'textfield',
 382      '#title' => t('Block title'),
 383      '#maxlength' => 64,
 384      '#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.'),
 385      '#default_value' => $edit['title'],
 386      '#weight' => -18,
 387    );
 388  
 389  
 390    // Module-specific block configurations.
 391    if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
 392      foreach ($settings as $k => $v) {
 393        $form['block_settings'][$k] = $v;
 394      }
 395    }
 396  
 397    // Get the block subject for the page title.
 398    $info = module_invoke($module, 'block', 'list');
 399    drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
 400  
 401    // Standard block configurations.
 402    $form['user_vis_settings'] = array(
 403      '#type' => 'fieldset',
 404      '#title' => t('User specific visibility settings'),
 405      '#collapsible' => TRUE,
 406    );
 407    $form['user_vis_settings']['custom'] = array(
 408      '#type' => 'radios',
 409      '#title' => t('Custom visibility settings'),
 410      '#options' => array(
 411        t('Users cannot control whether or not they see this block.'),
 412        t('Show this block by default, but let individual users hide it.'),
 413        t('Hide this block by default but let individual users show it.')
 414      ),
 415      '#description' =>  t('Allow individual users to customize the visibility of this block in their account settings.'),
 416      '#default_value' => $edit['custom'],
 417    );
 418  
 419    // Role-based visibility settings
 420    $default_role_options = array();
 421    $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
 422    while ($role = db_fetch_object($result)) {
 423      $default_role_options[] = $role->rid;
 424    }
 425    $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
 426    $role_options = array();
 427    while ($role = db_fetch_object($result)) {
 428      $role_options[$role->rid] = $role->name;
 429    }
 430    $form['role_vis_settings'] = array(
 431      '#type' => 'fieldset',
 432      '#title' => t('Role specific visibility settings'),
 433      '#collapsible' => TRUE,
 434    );
 435    $form['role_vis_settings']['roles'] = array(
 436      '#type' => 'checkboxes',
 437      '#title' => t('Show block for specific roles'),
 438      '#default_value' => $default_role_options,
 439      '#options' => $role_options,
 440      '#description' =>  t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
 441    );
 442  
 443    $form['page_vis_settings'] = array(
 444      '#type' => 'fieldset',
 445      '#title' => t('Page specific visibility settings'),
 446      '#collapsible' => TRUE,
 447    );
 448    $access = user_access('use PHP for block visibility');
 449  
 450    if ($edit['visibility'] == 2 && !$access) {
 451      $form['page_vis_settings'] = array();
 452      $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
 453      $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
 454    }
 455    else {
 456      $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
 457      $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>'));
 458  
 459      if ($access) {
 460        $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
 461        $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 ?>'));
 462      }
 463      $form['page_vis_settings']['visibility'] = array(
 464        '#type' => 'radios',
 465        '#title' => t('Show block on specific pages'),
 466        '#options' => $options,
 467        '#default_value' => $edit['visibility'],
 468      );
 469      $form['page_vis_settings']['pages'] = array(
 470        '#type' => 'textarea',
 471        '#title' => t('Pages'),
 472        '#default_value' => $edit['pages'],
 473        '#description' => $description,
 474      );
 475    }
 476  
 477    $form['submit'] = array(
 478      '#type' => 'submit',
 479      '#value' => t('Save block'),
 480    );
 481  
 482    return $form;
 483  }
 484  
 485  function block_admin_configure_validate($form_id, $form_values) {
 486    if ($form_values['module'] == 'block') {
 487      if (empty($form_values['info']) || db_num_rows(db_query("SELECT bid FROM {boxes} WHERE bid != %d AND info = '%s'", $form_values['delta'], $form_values['info']))) {
 488        form_set_error('info', t('Please ensure that each block description is unique.'));
 489      }
 490    }
 491  }
 492  
 493  function block_admin_configure_submit($form_id, $form_values) {
 494    if (!form_get_errors()) {
 495      db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, title = '%s' WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], trim($form_values['pages']), $form_values['custom'], $form_values['title'], $form_values['module'], $form_values['delta']);
 496      db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_values['module'], $form_values['delta']);
 497      foreach (array_filter($form_values['roles']) as $rid) {
 498        db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_values['module'], $form_values['delta']);
 499      }
 500      module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
 501      drupal_set_message(t('The block configuration has been saved.'));
 502      cache_clear_all();
 503      return 'admin/build/block';
 504    }
 505  }
 506  
 507  function block_box_form_validate($form_id, $form_values) {
 508    if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
 509      form_set_error('info', t('Please ensure that each block description is unique.'));
 510    }
 511  }
 512  
 513  function block_box_form_submit($form_id, $form_values) {
 514    if (!form_get_errors()) {
 515      if (block_box_save($form_values)) {
 516        drupal_set_message(t('The block has been created.'));
 517        return 'admin/build/block';
 518      }
 519    }
 520  }
 521  
 522  /**
 523   * Menu callback; confirm deletion of custom blocks.
 524   */
 525  function block_box_delete($bid = 0) {
 526    $box = block_box_get($bid);
 527    $form['info'] = array('#type' => 'hidden', '#value' => $box['info'] ? $box['info'] : $box['title']);
 528    $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
 529  
 530    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'));
 531  }
 532  
 533  /**
 534   * Deletion of custom blocks.
 535   */
 536  function block_box_delete_submit($form_id, $form_values) {
 537    db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
 538    db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = %d", $form_values['bid']);
 539    drupal_set_message(t('The block %name has been removed.', array('%name' => $form_values['info'])));
 540    cache_clear_all();
 541    return 'admin/build/block';
 542  };
 543  
 544  function block_box_form($edit = array()) {
 545    $form['info'] = array(
 546      '#type' => 'textfield',
 547      '#title' => t('Block description'),
 548      '#default_value' => $edit['info'],
 549      '#maxlength' => 64,
 550      '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array('@overview' => url('admin/build/block'))),
 551      '#required' => TRUE,
 552      '#weight' => -19,
 553    );
 554    $form['body_filter']['#weight'] = -17;
 555    $form['body_filter']['body'] = array(
 556      '#type' => 'textarea',
 557      '#title' => t('Block body'),
 558      '#default_value' => $edit['body'],
 559      '#rows' => 15,
 560      '#description' => t('The content of the block as shown to the user.'),
 561      '#weight' => -17,
 562    );
 563    if (!isset($edit['format'])) {
 564      $edit['format'] = FILTER_FORMAT_DEFAULT;
 565    }
 566    $form['body_filter']['format'] = filter_form($edit['format'], -16);
 567    $form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
 568  
 569    return $form;
 570  }
 571  
 572  function block_box_save($edit, $delta = NULL) {
 573    if (!filter_access($edit['format'])) {
 574      $edit['format'] = FILTER_FORMAT_DEFAULT;
 575    }
 576  
 577    if (isset($delta)) {
 578      db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta);
 579    }
 580    else {
 581      db_query("INSERT INTO {boxes} (body, info, format) VALUES  ('%s', '%s', %d)", $edit['body'], $edit['info'], $edit['format']);
 582    }
 583    return TRUE;
 584  }
 585  
 586  /**
 587   * Implementation of hook_user().
 588   *
 589   * Allow users to decide which custom blocks to display when they visit
 590   * the site.
 591   */
 592  function block_user($type, $edit, &$user, $category = NULL) {
 593    global $user;
 594    switch ($type) {
 595      case 'form':
 596        if ($category == 'account') {
 597          $rids = array_keys($user->roles);
 598          $placeholders = implode(',', array_fill(0, count($rids), '%d'));
 599          $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom != 0 AND (r.rid IN ($placeholders) OR r.rid IS NULL) ORDER BY b.weight, b.module", $rids);
 600          $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE);
 601          while ($block = db_fetch_object($result)) {
 602            $data = module_invoke($block->module, 'block', 'list');
 603            if ($data[$block->delta]['info']) {
 604              $return = TRUE;
 605              $form['block'][$block->module][$block->delta] = array('#type' => 'checkbox', '#title' => check_plain($data[$block->delta]['info']), '#default_value' => isset($user->block[$block->module][$block->delta]) ? $user->block[$block->module][$block->delta] : ($block->custom == 1));
 606            }
 607          }
 608  
 609          if ($return) {
 610            return $form;
 611          }
 612        }
 613  
 614        break;
 615      case 'validate':
 616        if (!$edit['block']) {
 617          $edit['block'] = array();
 618        }
 619        return $edit;
 620    }
 621  }
 622  
 623  /**
 624   * Return all blocks in the specified region for the current user.
 625   *
 626   * @param $region
 627   *   The name of a region.
 628   *
 629   * @return
 630   *   An array of block objects, indexed with <i>module</i>_<i>delta</i>.
 631   *   If you are displaying your blocks in one or two sidebars, you may check
 632   *   whether this array is empty to see how many columns are going to be
 633   *   displayed.
 634   *
 635   * @todo
 636   *   Add a proper primary key (bid) to the blocks table so we don't have
 637   *   to mess around with this <i>module</i>_<i>delta</i> construct.
 638   *   Currently, the blocks table has no primary key defined!
 639   */
 640  function block_list($region) {
 641    global $user, $theme_key;
 642  
 643    static $blocks = array();
 644  
 645    if (!count($blocks)) {
 646      $rids = array_keys($user->roles);
 647      $placeholders = implode(',', array_fill(0, count($rids), '%d'));
 648      $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN ($placeholders) OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", array_merge(array($theme_key), $rids));
 649      while ($block = db_fetch_object($result)) {
 650        if (!isset($blocks[$block->region])) {
 651          $blocks[$block->region] = array();
 652        }
 653        // Use the user's block visibility setting, if necessary
 654        if ($block->custom != 0) {
 655          if ($user->uid && isset($user->block[$block->module][$block->delta])) {
 656            $enabled = $user->block[$block->module][$block->delta];
 657          }
 658          else {
 659            $enabled = ($block->custom == 1);
 660          }
 661        }
 662        else {
 663          $enabled = TRUE;
 664        }
 665  
 666        // Match path if necessary
 667        if ($block->pages) {
 668          if ($block->visibility < 2) {
 669            $path = drupal_get_path_alias($_GET['q']);
 670            $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/';
 671            // Compare with the internal and path alias (if any).
 672            $page_match = preg_match($regexp, $path);
 673            if ($path != $_GET['q']) {
 674              $page_match = $page_match || preg_match($regexp, $_GET['q']);
 675            }
 676            // When $block->visibility has a value of 0, the block is displayed on
 677            // all pages except those listed in $block->pages. When set to 1, it
 678            // is displayed only on those pages listed in $block->pages.
 679            $page_match = !($block->visibility xor $page_match);
 680          }
 681          else {
 682            $page_match = drupal_eval($block->pages);
 683          }
 684        }
 685        else {
 686          $page_match = TRUE;
 687        }
 688  
 689        if ($enabled && $page_match) {
 690          // Check the current throttle status and see if block should be displayed
 691          // based on server load.
 692          if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) {
 693            $array = module_invoke($block->module, 'block', 'view', $block->delta);
 694            if (isset($array) && is_array($array)) {
 695              foreach ($array as $k => $v) {
 696                $block->$k = $v;
 697              }
 698            }
 699          }
 700          if (isset($block->content) && $block->content) {
 701            // Override default block title if a custom display title is present.
 702            if ($block->title) {
 703              // Check plain here to allow module generated titles to keep any markup.
 704              $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
 705            }
 706            $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
 707          }
 708        }
 709      }
 710    }
 711    // Create an empty array if there were no entries
 712    if (!isset($blocks[$region])) {
 713      $blocks[$region] = array();
 714    }
 715    return $blocks[$region];
 716  }


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics