block.module 7.24 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5
function block_help() {
 ?>
6 7 8 9 10 11
  <p>Blocks are the boxes visible in the side bars on the left- and right-hand side of the website.  They are either exported by the engine or by any of the active modules.  To really get your teeth into a drupal website, you are going to have to deal with blocks and administering blocks in a fairly sophisticated fashion.  This means you will need to understand how the block placement strategy works.</p>
  <p>The placement of blocks is delegated to the administrator. In most cases (i.e., the "custom" blocks), the user has complete control -- using preferences -- over whether or not they are enabled.</p>
  <p>An administrator can lay out and arrange the available blocks to fit in two regions: "left" and "right".  Regions simply contain blocks.  In addition, an administrator can assign each block (within a region) a weight to sort them vertically.  The heavier blocks will sink and the lighter blocks will be positioned nearer the top.</p>
  <p>As mentioned, blocks may be arranged to fit in two regions: left and right.  For theme builders, each region is identified by a corresponding constant: "left" and "right".</p>
  <p>The path setting lets you define which pages you want the specific blocks to be shown. If you leave the path blank it will show on all pages. The path uses a regular expression syntax so remember to escape special characters!<br />Examples:
  <ul><li>Only show on node pages: ^/node\.php</li><li>Only show on the user page: ^/module\.php\?mod=user</li><li>Show in main page and blog page: ^/(index\.php|module\.php\?mod=blog)</li></ul>
Dries's avatar
 
Dries committed
12
 <?php
Dries's avatar
 
Dries committed
13 14
}

Dries's avatar
 
Dries committed
15
function block_perm() {
Dries's avatar
 
Dries committed
16 17 18 19
  return array("administer blocks");
}

function block_link($type) {
Dries's avatar
Dries committed
20
  if ($type == "admin" && user_access("administer blocks")) {
Dries's avatar
 
Dries committed
21
    $links[] = la(t("blocks"), array("mod" => "block"));
Dries's avatar
 
Dries committed
22 23 24
  }

  return $links ? $links : array();
Dries's avatar
 
Dries committed
25 26
}

Dries's avatar
 
Dries committed
27 28
function block_admin_save($edit) {
  foreach ($edit as $key=>$value) {
Dries's avatar
 
Dries committed
29
    db_query("UPDATE blocks SET region = '%s', status = '%s', path = '%s', weight = '%s' WHERE name = '%s'", $value["region"], $value["status"], $value["path"], $value["weight"], $key);
Dries's avatar
 
Dries committed
30 31 32 33
  }
}

function block_admin_display() {
Dries's avatar
 
Dries committed
34
  $result = db_query("SELECT * FROM blocks ORDER BY module");
Dries's avatar
 
Dries committed
35

Dries's avatar
 
Dries committed
36
  // Generate output:
Kjartan's avatar
Kjartan committed
37
  $output .= "<form action=\"". drupal_url(array("mod" => "block"), "admin") ." method=\"post\">\n";
38 39
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
  $output .= " <tr><th>block</th><th>module</th><th>status</th><th>weight</th><th>region</th><th>path</th></tr>\n";
Dries's avatar
 
Dries committed
40

Dries's avatar
 
Dries committed
41
  while ($block = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
42
    $module = module_hook($block->module, "admin") ? la($block->module, array("mod" => $block->module)) : $block->module;
Dries's avatar
 
Dries committed
43

44 45 46 47 48
    $status = "<select name=\"edit[$block->name][status]\">\n";
    $status .= " <option value=\"2\"". (($block->status == 2) ? " selected" : "") .">enabled: always</option>\n";
    $status .= " <option value=\"1\"". (($block->status == 1) ? " selected" : "") .">enabled: custom</option>\n";
    $status .= " <option value=\"0\"". (($block->status == 0) ? " selected" : "") .">disabled</option>\n";
    $status .= "</select>\n";
Dries's avatar
 
Dries committed
49

50
    $weight = "<select name=\"edit[$block->name][weight]\">\n";
Dries's avatar
 
Dries committed
51
    for ($count = 0; $count < 10; $count++) {
52
      $weight .= "<option value=\"$count\"". (($block->weight == $count) ? " selected" : "") .">$count</option>\n";
Dries's avatar
 
Dries committed
53
    }
54
    $weight .= "</select>\n";
Dries's avatar
 
Dries committed
55

56 57 58 59
    $region = "<select name=\"edit[$block->name][region]\">\n";
    $region .= " <option value=\"0\"". (($block->region == 0) ? " selected" : "") .">left</option>\n";
    $region .= " <option value=\"1\"". (($block->region == 1) ? " selected" : "") .">right</option>\n";
    $region .= "</select>\n";
Dries's avatar
 
Dries committed
60

61
    $path = "<input name=\"edit[$block->name][path]\" value=\"$block->path\">\n";
Dries's avatar
 
Dries committed
62

63
    $output .= " <tr><td>". $block->name ."</td><td align=\"center\">$module</td><td>$status</td><td>$weight</td><td>$region</td><td>$path</td></tr>\n";
Dries's avatar
 
Dries committed
64
  }
Dries's avatar
 
Dries committed
65

66 67 68
  $output .= "</table>\n";
  $output .= "<input name=\"op\" type=\"submit\" value=\"Save blocks\">\n";
  $output .= "</form>\n";
Dries's avatar
 
Dries committed
69 70 71 72

  print $output;
}

Dries's avatar
 
Dries committed
73
function block_admin_preview() {
Dries's avatar
 
Dries committed
74 75

  $result = db_query("SELECT * FROM blocks WHERE status > 0 AND region = 0 ORDER BY weight");
Dries's avatar
 
Dries committed
76 77 78
  $lblocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
  while ($block = db_fetch_object($result)) $lblocks .= " <tr><td nowrap>". ($block->status == 2 ? "<b>$block->name</b>" : $block->name) ."</td><td>$block->weight</td></tr>\n";
  $lblocks .= "</table>\n";
Dries's avatar
 
Dries committed
79 80

  $result = db_query("SELECT * FROM blocks WHERE status > 0 AND region = 1 ORDER BY weight");
Dries's avatar
 
Dries committed
81 82 83
  $rblocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
  while ($block = db_fetch_object($result)) $rblocks .= " <tr><td nowrap>". ($block->status == 2 ? "<b>$block->name</b>" : $block->name) ."</td><td>$block->weight</td></tr>\n";
  $rblocks .= "</table>\n";
Dries's avatar
 
Dries committed
84

Dries's avatar
 
Dries committed
85 86 87 88 89 90
  $output .= "<h3>layout scheme #1:</h3>\n";
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
  $output .= " <tr><td align=\"center\" colspan=\"3\">header</td></tr>\n";
  $output .= " <tr><td>\n". ($lblocks ? $lblocks : "&nbsp;") ."</td><td width=\"300\">&nbsp;</td><td>\n". ($rblocks ? $rblocks : "&nbsp;") ."</td></tr>\n";
  $output .= " <tr><td align=\"center\" colspan=\"3\">footer</td></tr>\n";
  $output .= "</table>\n";
Dries's avatar
 
Dries committed
91 92

  $result = db_query("SELECT * FROM blocks WHERE status > 0 ORDER BY weight");
Dries's avatar
 
Dries committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  $blocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
  while ($block = db_fetch_object($result)) $blocks .= " <tr><td nowrap>". ($block->status == 2 ? "<b>$block->name</b>" : $block->name) ."</td><td>$block->weight</td></tr>\n";
  $blocks .= "</table>\n";

  $output .= "<h3>layout scheme #2:</h3>\n";
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
  $output .= " <tr><td align=\"center\" colspan=\"2\">header</td></tr>\n";
  $output .= " <tr><td width=\"400\">&nbsp;</td><td>\n". ($blocks ? $blocks : "&nbsp;") ."</td></tr>\n";
  $output .= " <tr><td align=\"center\" colspan=\"2\">footer</td></tr>\n";
  $output .= "</table>\n";

  $output .= "<h3>layout scheme #3:</h3>\n";
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
  $output .= " <tr><td align=\"center\" colspan=\"2\">header</td></tr>\n";
  $output .= " <tr><td>\n". ($blocks ? $blocks : "&nbsp;") ."</td><td width=\"400\">&nbsp;</td></tr>\n";
  $output .= " <tr><td align=\"center\" colspan=\"2\">footer</td></tr>\n";
  $output .= "</table>\n";
Dries's avatar
 
Dries committed
110 111 112 113

  print $output;
}

Dries's avatar
 
Dries committed
114
function block_init() {
Dries's avatar
 
Dries committed
115

Dries's avatar
 
Dries committed
116 117 118 119 120 121 122 123 124 125
  $result = db_query("SELECT * FROM modules");
  while ($module = db_fetch_object($result)) {
    module_rehash($module->name);
  }

  foreach (module_list() as $name) {
    module_rehash($name);
  }
}

Dries's avatar
 
Dries committed
126
function block_admin() {
Dries's avatar
 
Dries committed
127
  global $op, $edit;
Dries's avatar
 
Dries committed
128

Dries's avatar
 
Dries committed
129
  if (user_access("administer blocks")) {
Dries's avatar
 
Dries committed
130

Kjartan's avatar
Kjartan committed
131
    print "<small>". la(t("configure"), array("mod" => "block")) ." | ". la(t("preview"), array("mod" => "block", "op" => "preview")) ." | ". la(t("help"), array("mod" => "block", "op" => "help")) ."</small><hr>\n";
Dries's avatar
 
Dries committed
132

Dries's avatar
 
Dries committed
133 134
    block_init();

Dries's avatar
 
Dries committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    switch ($op) {
      case "help":
        block_help();
        break;
      case "preview":
        block_admin_preview();
        break;
      case "Save blocks":
        block_admin_save($edit);
        // fall through
      default:
        block_admin_display();
    }
  }
  else {
    print message_access();
Dries's avatar
 
Dries committed
151 152
  }
}
Dries's avatar
 
Dries committed
153

Dries's avatar
 
Dries committed
154
?>