block.module 7.27 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
22
23
24
    $links[] = "<a href=\"admin.php?mod=block\">blocks</a>";
  }

  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) {
29
    db_query("UPDATE blocks SET region = '". check_input($value[region]) ."', status = '". check_input($value[status]) ."', path = '". check_input($value[path]) ."', weight = '". check_input($value[weight]) ."' WHERE name = '". check_input($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:
37
38
39
  $output .= "<form action=\"admin.php?mod=block\" method=\"post\">\n";
  $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") ? "<A HREF=\"admin.php?mod=$block->module\">$block->module</A>" : $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
60
61
    $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";
    
    $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
76
77
78
79
80
81
82
83

  $result = db_query("SELECT * FROM blocks WHERE status > 0 AND region = 0 ORDER BY weight");
  $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";

  $result = db_query("SELECT * FROM blocks WHERE status > 0 AND region = 1 ORDER BY weight");
  $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
  $output .= "<H3>layout scheme #1:</H3>\n";
Dries's avatar
   
Dries committed
86
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
   
Dries committed
87
  $output .= " <TR><TD ALIGN=\"center\" COLSPAN=\"3\">header</TD></TR>\n";
Dries's avatar
   
Dries committed
88
  $output .= " <TR><TD>\n". ($lblocks ? $lblocks : "&nbsp;") ."</TD><TD WIDTH=\"300\">&nbsp;</TD><TD>\n". ($rblocks ? $rblocks : "&nbsp;") ."</TD></TR>\n";
Dries's avatar
   
Dries committed
89
  $output .= " <TR><TD ALIGN=\"center\" COLSPAN=\"3\">footer</TD></TR>\n";
Dries's avatar
   
Dries committed
90
91
92
93
94
95
96
  $output .= "</TABLE>\n";

  $result = db_query("SELECT * FROM blocks WHERE status > 0 ORDER BY weight");
  $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";

Dries's avatar
   
Dries committed
97
  $output .= "<H3>layout scheme #2:</H3>\n";
Dries's avatar
   
Dries committed
98
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
   
Dries committed
99
  $output .= " <TR><TD ALIGN=\"center\" COLSPAN=\"2\">header</TD></TR>\n";
Dries's avatar
   
Dries committed
100
  $output .= " <TR><TD WIDTH=\"400\">&nbsp;</TD><TD>\n". ($blocks ? $blocks : "&nbsp;") ."</TD></TR>\n";
Dries's avatar
   
Dries committed
101
  $output .= " <TR><TD ALIGN=\"center\" COLSPAN=\"2\">footer</TD></TR>\n";
Dries's avatar
   
Dries committed
102
103
  $output .= "</TABLE>\n";

Dries's avatar
   
Dries committed
104
  $output .= "<H3>layout scheme #3:</H3>\n";
Dries's avatar
   
Dries committed
105
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
   
Dries committed
106
  $output .= " <TR><TD ALIGN=\"center\" COLSPAN=\"2\">header</TD></TR>\n";
Dries's avatar
   
Dries committed
107
  $output .= " <TR><TD>\n". ($blocks ? $blocks : "&nbsp;") ."</TD><TD WIDTH=\"400\">&nbsp;</TD></TR>\n";
Dries's avatar
   
Dries committed
108
  $output .= " <TR><TD ALIGN=\"center\" COLSPAN=\"2\">footer</TD></TR>\n";
Dries's avatar
   
Dries committed
109
110
111
112
113
  $output .= "</TABLE>\n";

  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

Dries's avatar
   
Dries committed
131
132
    print "<SMALL><A HREF=\"admin.php?mod=block\">configure</A> | <A HREF=\"admin.php?mod=block&op=preview\">preview</A> | <A HREF=\"admin.php?mod=block&op=help\">help</A></SMALL><HR>\n";

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
  }
}
153

Dries's avatar
 
Dries committed
154
?>