block.module 15.6 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() {
 ?>
Dries's avatar
   
Dries committed
6
  <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>
7
8
9
10
11
  <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
13
  <hr /></p>
  <p>The content of the site can be almost entirely altered through <i>boxes</i>.  Simply put, boxes are small bits of text, HTML or PHP code which will get plugged into the site just like any other block.  Boxes are typically used to add custom blocks to the site.</p>
14
15
  <p>Each box consists of a title and an associated block of text, HTML or PHP code that can be as long as you wish and that will 'render' the content of the box.</p>
  <h3>PHP boxes</h3>
Dries's avatar
   
Dries committed
16
  <p>If you know how to script in PHP, PHP boxes are easy to create.  Don't worry if you're no PHP-wizard: simply use HTML boxes instead.</p>
17
  <p>You can use any piece of PHP code to make up the content of a PHP box: this implies that you can declare and use functions, consult the SQL database, access configuration settings and much more.  A PHP box's code is stored in the database and the engine will dynamically embed the PHP code just-in-time for execution.</p>
Dries's avatar
   
Dries committed
18
  <p>There are however some factors to keep in mind when using and creating PHP boxes: PHP boxes can be extremely useful and flexible, yet they can be dangerous and insecure if not properly used.  If you are not familiar with PHP, SQL or with the site engine, avoid experimenting with PHP boxes because you can - and probably will - corrupt your database or render your site unusable!  If you don't plan to do fancy stuff with boxes then you're probably better off with HTML boxes.</p>
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  <p>Remember that the code within each PHP box must be valid PHP code -- including things like correctly terminating statements with a semicolon so that the parser won't die.  It is highly recommended that you develop your boxes separately using a simple test script on top of a test database before migrating to your production environment.</p>
  <p>Note that you can use global variables such as configuration parameters within the scope of a PHP box. Also keep in mind that variables which have been given values in a PHP box will retain these values in the engine or module afterwards.</p>
  <p>You can use the <code>return</code> statement to return the actual content for your block as well.</p>
  <p><u>A basic example:</u></p>
  <p>Given the box with title "Welcome", used to create a "<i>Welcome</i>" box.  The content for this box could be created by using:</p>
  <pre>
   return "Welcome visitor, ... welcome message goes here ...";
  </pre>
  <p>If we are however dealing with a registered user, we can customize the message by using:</p>
  <pre>
   if ($user->uid) {
     return "Welcome $user->name, ... welcome message goes here ...";
   }
   else {
     return "Welcome visitor, ... welcome message goes here ...";
   }
  </pre>
  <p>For more in-depth examples, we recommend that you check the existing boxes and use them as a starting point.</p>

Dries's avatar
   
Dries committed
38
 <?php
Dries's avatar
   
Dries committed
39
40
}

41
function block_system($field){
Kjartan's avatar
Kjartan committed
42
  $system["description"] = t("Controls the boxes that are displayed around the main content.");
43
44
45
  return $system[$field];
}

Dries's avatar
   
Dries committed
46
function block_perm() {
Dries's avatar
   
Dries committed
47
48
49
50
  return array("administer blocks");
}

function block_link($type) {
Dries's avatar
Dries committed
51
  if ($type == "admin" && user_access("administer blocks")) {
Dries's avatar
   
Dries committed
52
   $help["block"] = "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 Drupal or by any of the active modules.  Adminstrators can enable or disable block, as well control the block placement by assigning them a region and/or by assigning each block (within a region) a weight to sort them vertically.  The path setting lets you define which pages you want the specific blocks to be shown.";
Dries's avatar
   
Dries committed
53

Dries's avatar
   
Dries committed
54
55
56
57
    menu("admin/block", "block management", block_admin, $help["block"], 3);
    menu("admin/block/add", "create new block", block_admin, $help["block"], 2);
    menu("admin/block/preview", "preview placement", block_admin, $help["block"], 3);
    menu("admin/block/help", "help", block_help, NULL, 9);
Dries's avatar
   
Dries committed
58
  }
Dries's avatar
   
Dries committed
59
60
}

Dries's avatar
   
Dries committed
61
62
63
64
function block_block($op = "list", $delta = 0) {
  if ($op == "list") {
    $result = db_query("SELECT bid, title, info FROM boxes ORDER BY title");
    while ($block = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
65
      $blocks[$block->bid]["info"] = $block->info;
Dries's avatar
   
Dries committed
66
67
68
69
70
    }
    return $blocks;
  }
  else {
    $block = db_fetch_object(db_query("SELECT * FROM boxes WHERE bid = '%d'", $delta));
Dries's avatar
   
Dries committed
71
    $data["subject"] = $block->title;
Dries's avatar
   
Dries committed
72
73
    $data["content"] = ($block->type == 1) ? eval($block->body) : $block->body;
    return $data;
74
75
76
  }
}

Dries's avatar
 
Dries committed
77
function block_admin_save($edit) {
Dries's avatar
   
Dries committed
78
79
  foreach ($edit as $module => $blocks) {
    foreach ($blocks as $delta => $block) {
Dries's avatar
   
Dries committed
80
81
      db_query("UPDATE blocks SET region = '%s', status = '%d', custom = '%d', path = '%s', weight = '%d' WHERE module = '%s' AND delta = '%s'",
                $block["region"], $block["status"], $block["custom"], $block["path"], $block["weight"], $module, $delta);
Dries's avatar
   
Dries committed
82
    }
Dries's avatar
 
Dries committed
83
  }
Dries's avatar
   
Dries committed
84

Dries's avatar
   
Dries committed
85
  return t("the block settings have been updated.");
Dries's avatar
 
Dries committed
86
87
}

Dries's avatar
   
Dries committed
88
89
/**
 * update blocks db table with blocks currently exported by modules
Dries's avatar
   
Dries committed
90
 *
Dries's avatar
   
Dries committed
91
92
93
94
95
96
 * @param   array   $order_by   php array_multisort() style sort ordering, eg. "weight", SORT_ASC, SORT_STRING. see {@link http://www.php.net/manual/en/function.array-multisort.php}
 * @return  array   blocks currently exported by modules, sorted by $order_by
 * @access  private
 */
function _block_rehash($order_by = array("weight")) {
  $result = db_query("SELECT * FROM blocks");
Dries's avatar
   
Dries committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  while ($old_block = db_fetch_object($result)) {
    $old_blocks[$old_block->module][$old_block->delta] = $old_block;
  }

  db_query("DELETE FROM blocks");

  foreach (module_list() as $module) {
    $module_blocks = module_invoke($module, "block", "list");
    if ($module_blocks) {
      foreach ($module_blocks as $delta => $block) {
        $block["module"] = $module;
        $block["delta"]  = $delta;
        if ($old_blocks[$module][$delta]) {
          $block["status"] = $old_blocks[$module][$delta]->status;
          $block["weight"] = $old_blocks[$module][$delta]->weight;
          $block["region"] = $old_blocks[$module][$delta]->region;
          $block["path"]   = $old_blocks[$module][$delta]->path;
          $block["custom"] = $old_blocks[$module][$delta]->custom;
        }
        else {
          $block["status"] = $block["weight"] = $block["region"] = $block["custom"] = 0;
          $block["path"]   = "";
        }

        // reinsert blocks into table
Dries's avatar
   
Dries committed
122
123
        db_query("INSERT INTO blocks (module, delta, status, weight, region, path, custom) VALUES ('%s', '%s', '%d', '%d', '%d', '%s', '%d')",
                  $block["module"], $block["delta"], $block["status"], $block["weight"], $block["region"], $block["path"], $block["custom"]);
Dries's avatar
   
Dries committed
124
125

        $blocks[] = $block;
Dries's avatar
   
Dries committed
126

Dries's avatar
   
Dries committed
127
128
        // build array to sort on
        $order[$order_by[0]][] = $block[$order_by[0]];
Dries's avatar
   
Dries committed
129
130
131
132
      }
    }
  }

Dries's avatar
   
Dries committed
133
134
135
  // sort
  array_multisort($order[$order_by[0]], $order_by[1] ? $order_by[1] : SORT_ASC, $order_by[2] ? $order_by[2] : SORT_REGULAR, $blocks);

Dries's avatar
   
Dries committed
136
137
  return $blocks;
}
Dries's avatar
   
Dries committed
138

Dries's avatar
   
Dries committed
139
function block_admin_display() {
Dries's avatar
   
Dries committed
140

Dries's avatar
   
Dries committed
141
  $blocks = _block_rehash();
Dries's avatar
   
Dries committed
142

Dries's avatar
   
Dries committed
143
144
  $header = array(t("block"), t("enabled"), t("custom"), t("weight"), t("region"), t("path"), array("data" => "operations", "colspan" => 2));

Dries's avatar
   
Dries committed
145
  foreach ($blocks as $block) {
Dries's avatar
   
Dries committed
146
    if ($block["module"] == "block") {
Dries's avatar
   
Dries committed
147
      $edit = l(t("edit"), "admin/block/edit/". $block["delta"]);
Dries's avatar
   
Dries committed
148
      $delete = l(t("delete"), "admin/block/delete/". $block["delta"]);
149
    }
Dries's avatar
   
Dries committed
150
151
152
153
    else {
      $edit = "";
      $delete = "";
    }
Dries's avatar
   
Dries committed
154
155

    $rows[] = array($block["info"], array("data" => form_checkbox(NULL, $block["module"]."][".$block["delta"]."][status", 1, $block["status"]), "align" => "center"), array("data" => form_checkbox(NULL, $block["module"]."][".$block["delta"]."][custom", 1, $block["custom"]), "align" => "center"), form_weight(NULL, $block["module"]."][".$block["delta"]."][weight", $block["weight"]), form_select(NULL, $block["module"]."][".$block["delta"]."][region", $block["region"], array("left", "right")), form_textfield(NULL, $block["module"]."][".$block["delta"]."][path", $block["path"], 10, 255), $edit, $delete);
Dries's avatar
 
Dries committed
156
  }
Dries's avatar
   
Dries committed
157

Dries's avatar
   
Dries committed
158
  $output = table($header, $rows);
159
  $output .= form_submit("Save blocks");
Dries's avatar
 
Dries committed
160

161
  print form($output);
Dries's avatar
 
Dries committed
162
163
}

Dries's avatar
   
Dries committed
164
function block_admin_preview() {
Dries's avatar
   
Dries committed
165
166

  $result = db_query("SELECT * FROM blocks WHERE status > 0 AND region = 0 ORDER BY weight");
Dries's avatar
   
Dries committed
167
  $lblocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
   
Dries committed
168
169
170
171
172
  while ($block = db_fetch_object($result)) {
    $block_data = module_invoke($block->module, "block", "list");
    $name = $block_data[$block->delta]["info"];
    $lblocks .= " <tr><td nowrap=\"nowrap\">". ($block->status == 2 ? "<b>$name</b>" : $name) ."</td><td>$block->weight</td></tr>\n";
  }
Dries's avatar
   
Dries committed
173
  $lblocks .= "</table>\n";
Dries's avatar
   
Dries committed
174
175

  $result = db_query("SELECT * FROM blocks WHERE status > 0 AND region = 1 ORDER BY weight");
Dries's avatar
   
Dries committed
176
  $rblocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
   
Dries committed
177
178
179
180
181
  while ($block = db_fetch_object($result)) {
    $block_data = module_invoke($block->module, "block", "list");
    $name = $block_data[$block->delta]["info"];
    $rblocks .= " <tr><td nowrap=\"nowrap\">". ($block->status == 2 ? "<b>$name</b>" : $name) ."</td><td>$block->weight</td></tr>\n";
  }
Dries's avatar
   
Dries committed
182
  $rblocks .= "</table>\n";
Dries's avatar
   
Dries committed
183

Dries's avatar
   
Dries committed
184
185
186
187
188
189
  $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
190
191

  $result = db_query("SELECT * FROM blocks WHERE status > 0 ORDER BY weight");
Dries's avatar
   
Dries committed
192
  $blocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
   
Dries committed
193
194
195
196
197
  while ($block = db_fetch_object($result)) {
    $block_data = module_invoke($block->module, "block", "list");
    $name = $block_data[$block->delta]["info"];
    $blocks .= " <tr><td nowrap=\"nowrap\">". ($block->status == 2 ? "<b>$name</b>" : $name) ."</td><td>$block->weight</td></tr>\n";
  }
Dries's avatar
   
Dries committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  $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
213
214
215
216

  print $output;
}

217
function block_box_get($bid) {
Dries's avatar
   
Dries committed
218
  return db_fetch_array(db_query("SELECT * FROM boxes WHERE bid = '%d'", $bid));
219
220
221
}

function block_box_form($edit = array()) {
Dries's avatar
   
Dries committed
222
  $type = array(0 => "HTML", 1 => "PHP");
223

Dries's avatar
   
Dries committed
224
  $form = form_textfield("Title", "title", $edit["title"], 50, 64);
225
226
  $form .= form_textfield("Description", "info", $edit["info"], 50, 64);
  $form .= form_textarea("Body", "body", $edit["body"], 70, 10);
Kjartan's avatar
Kjartan committed
227
  if (user_access("create php content")) {
Dries's avatar
   
Dries committed
228
229
    $form .= form_select("Type", "type", $edit["type"], $type);
  }
230
231
232
233
234
235
236
237
238
239
240

  if ($edit["bid"]) {
    $form .= form_hidden("bid", $edit["bid"]);
  }

  $form .= form_submit("Save block");

  print form($form);
}

function block_box_save($edit) {
Dries's avatar
   
Dries committed
241
242
243
244
  if (!user_access("create PHP content")) {
    $edit["type"] = 0;
  }

245
  if ($edit["bid"]) {
Dries's avatar
   
Dries committed
246
    db_query("UPDATE boxes SET title = '%s', body = '%s', info = '%s', type = '%s' WHERE bid = '%d'", $edit["title"], $edit["body"], $edit["info"], $edit["type"], $edit["bid"]);
Dries's avatar
   
Dries committed
247
    return t("the block has been updated.");
248
249
250
  }
  else {
    db_query("INSERT INTO boxes (title, body, info, type) VALUES  ('%s', '%s', '%s', '%s')", $edit["title"], $edit["body"], $edit["info"], $edit["type"]);
Dries's avatar
   
Dries committed
251
    return t("the new block has been added.");
252
253
254
255
256
  }
}

function block_box_delete($bid) {
  if ($bid) {
Dries's avatar
   
Dries committed
257
    db_query("DELETE FROM boxes WHERE bid = '%d'", $bid);
Dries's avatar
   
Dries committed
258
    return t("the block has been deleted.");
259
260
261
  }
}

Dries's avatar
 
Dries committed
262
function block_admin() {
Dries's avatar
   
Dries committed
263
  global $edit, $op;
Dries's avatar
   
Dries committed
264

Dries's avatar
   
Dries committed
265
  if (user_access("administer blocks")) {
Dries's avatar
   
Dries committed
266

Dries's avatar
   
Dries committed
267
268
269
270
    if (empty($op)) {
      $op = arg(2);
    }

Dries's avatar
   
Dries committed
271
272
273
274
    switch ($op) {
      case "preview":
        block_admin_preview();
        break;
275
276
277
278
      case "add":
        block_box_form();
        break;
      case "edit":
Dries's avatar
   
Dries committed
279
        block_box_form(block_box_get(arg(3)));
280
281
        break;
      case "delete":
Dries's avatar
   
Dries committed
282
        print status(block_box_delete(arg(3)));
Dries's avatar
   
Dries committed
283
        cache_clear_all();
284
285
286
287
        block_admin_display();
        break;
      case "Save block":
        print status(block_box_save($edit));
Dries's avatar
   
Dries committed
288
        cache_clear_all();
289
290
        block_admin_display();
        break;
Dries's avatar
   
Dries committed
291
      case "Save blocks":
Dries's avatar
   
Dries committed
292
293
        print status(block_admin_save($edit));
        cache_clear_all();
Dries's avatar
   
Dries committed
294
295
296
297
298
299
300
        // fall through
      default:
        block_admin_display();
    }
  }
  else {
    print message_access();
Dries's avatar
 
Dries committed
301
302
  }
}
Dries's avatar
   
Dries committed
303

304
305
function block_user($type, &$edit, &$user) {
  switch ($type) {
306
    case "register_form":
Dries's avatar
   
Dries committed
307
      $result = db_query("SELECT * FROM blocks WHERE custom = '%d' ORDER BY module, delta", 1);
308
309

      while ($block = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
310
        $form .= form_hidden("block][$block->module][$block->delta", $block->status);
311
312
313
      }

      return $form;
314
    case "edit_form":
Dries's avatar
   
Dries committed
315
      $result = db_query("SELECT * FROM blocks WHERE custom = '%d' ORDER BY module, delta", 1);
316
317

      while ($block = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
318
319
320
        $data = module_invoke($block->module, "block", "list");
        if ($data[$block->delta]["info"]) {
          $form .= "<tr><td>".$data[$block->delta]["info"]."</td><td>". form_checkbox(NULL, "block][$block->module][$block->delta", 1, $user->block[$block->module][$block->delta]) ."</td></tr>\n";
Kjartan's avatar
Kjartan committed
321
322
323
324
        }
      }

      if (isset($form)) {
Dries's avatar
   
Dries committed
325
        return form_item(t("Block configuration"), "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">". $form ."</table>", t("Enable the blocks you would like to see displayed in the side bars."));
326
      }
Dries's avatar
   
Dries committed
327
328

      break;
329
330
331
332
333
    case "edit_validate":
      if (!$edit["block"]) {
        $edit["block"] = array();
      }
      return $edit;
334
335
336
  }
}

Dries's avatar
   
Dries committed
337
?>