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

4
function block_help($section = "admin/system/block/help") {
5 6 7
  $output = "";

  switch ($section) {
8
    case 'admin/system/block/help':
9 10 11 12
      $output .= "<p>Blocks are the boxes visible in the sidebar(s) of your web site. These are usually generated automatically by modules (e.g. recent forum topics), but you can also create your own blocks using either static HTML or dynamic PHP content.</p>";
      $output .= "<p>The sidebar each block appears in depends on both which theme you're using (some are left-only, some right, some both), and on the settings in block management.</p><p>Whether a block is visible in the first place depends on three things:</p><ul><li>It must have its \"enabled\" box checked in block management.</li><li>If it has its \"custom\" box checked in block management, the user must have chosen to display it in their user preferences.</li><li>If the \"path\" field in block management is set, the visitor must be on a page that matches the path specification (more on this later).</li></ul>";
      $output .= "<p>The block management screen also lets you specify the vertical sort-order of the blocks within a sidebar. You do this by assigning a <b>weight</b> to each block. Lighter blocks (smaller weight) \"float up\" towards the top of the sidebar. Heavier ones \"sink down\" towards the bottom of it. Once you've positioned things just so, you can preview what the layout will look like in different types of themes by clicking the preview placement link in the menu to the left.</p>";
      $output .= "<p>The path setting lets you define the pages on which a specific block is visable. If you leave the path blank it will appear on all pages. The path uses a regular expression syntax so remember to escape special characters!</p>";
13
      $output .= "<p>In case you do not know what a regular expression is, you should read about them in the PHP manual. The chapter to look at is the one on %pcre.</p>";
14
      $output .= "<p>However, for basic tasks it is sufficient to look at the following examples:</p>";
15
      $output .= "<p>If the block should only show up on blog pages, use &lt;/blog&gt;.  To display on all node views use &lt;/node/view&gt;.  The angular brackets are used as delimiters of the regular expression.  To show up on either forum or book pages use &lt;(/forum|/book)&gt;.  The round brackets form a group of expressions, divided by the | character. It matches if any of the expressions in it match.  A more complicated example is &lt;/node/add/(story|blog|image)&gt;. Blocks which have their paths set to this expression will show up on story, block, or image composition pages.  If you want to show a block an all pages, but not the search page, use &lt;(^/$|[^(search)$/]+)&gt;</p>";
16 17 18 19 20 21 22 23 24
      $output .= "<h3>Custom Blocks</h3>";
      $output .= "<p>A custom block contains admin-supplied HTML, text or PHP content (as opposed to being generated automatically by a module). Each custom block consists of a title, a description, and a body containing text, HTML, or PHP code which can be as long as you wish. The Drupal engine will 'render' the content of the custom block.</p>";
      $output .= "<h4>PHP in custom blocks</h4>";
      $output .= "<p>If you know how to script in PHP, Drupal gives you the power to embed any script you like inside a block. It will be executed when the page is viewed and dynamically embedded into the page. This gives you amazing flexibility and power, but of course with that comes danger and insecurity if you don't write good code. If you are not familiar with PHP, SQL or with the site engine, avoid experimenting with PHP custom blocks because you can corrupt your database or render your site insecure or even unusable! If you don't plan to do fancy stuff with custom blocks then you're probably better off with straight HTML.</p>";
      $output .= "<p>Remember that the code within each PHP custom block 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 cusom blocks separately using a simple test script on top of a test database before migrating to your production environment.</p>";
      $output .= "<p>Notes:</p><ul><li>You can use global variables, such as configuration parameters, within the scope of a PHP box but remember that variables which have been given values in a PHP box will retain these values in the engine or module afterwards.</li><li>register_globals is now set to <b>off</b> by default. If you need form information you need to get it from the \"superglobals\" \$_POST, \$_GET, etc.</li><li>You can use the <code>return</code> statement to return the actual content for your block as well.</li></ul>";
      $output .= "<p>A basic example:</p>";
      $output .= "<blockquote><p>You want to have a box with the title \"Welcome\" that you use to greet your visitors. The content for this box could be created by going:</p>";
      $output .= "<pre>
25 26
  return t(\"Welcome visitor, ... welcome message goes here ...\");
</pre>";
27 28
      $output .= "<p>If we are however dealing with a registered user, we can customize the message by using:</p>";
      $output .= "<pre>
29 30 31 32 33
  if (\$user->uid) {
    return t(\"Welcome \$user->name, ... welcome message goes here ...\");
  }
  else {
    return t(\"Welcome visitor, ... welcome message goes here ...\");
34
  }";
35 36
      $output .= "</pre></blockquote>";
      $output .= "<p>For more in-depth examples, we recommend that you check the existing boxes and use them as a starting point.</p>";
37
      $output = t($output, array("%pcre" => "<a href=\"http://php.net/pcre/\">". t("Perl-Compatible Regular Expressions (PCRE)") ."</a>"));
38 39
      break;
    case 'admin/system/modules':
40
      $output = t("Controls the boxes that are displayed around the main content.");
41
      break;
42
    case 'admin/system/block':
43
      $output = t("Blocks are the boxes in the left- and right- side bars of the web site, depending on the choosen theme.  They are created by <b>active</b> Drupal modules.  In order to view a block it must be enabled. You can assign the block's placement by giving it a region and a weight. The region specifies which side of the page the block is on, and the weight sorts blocks within a region. Lighter (smaller weight value) blocks \"float up\" towards the top of the page. The path setting lets you define which pages you want a block to be shown on. The custom checkbox lets your users hide the block using their account setting. You can also create your own blocks, where you specify the content of the block rather than its being generated by a module (you can even use PHP in these). You can create one of these by clicking the %createblock link in the menu to the left. Edit and delete links will become active below for these customized blocks.", array("%createblock" => l(t("new block"), "admin/system/block/add")));
44
      break;
45
    case 'admin/system/block/add':
46
      $output = t("Here you can create a custom content block. Once you have created this block you must make it active, and give it a place on the page using %overview. The title is used when displaying the block. The description is used in the \"block\" column on the %overview page. If you are going to place PHP code in the block, and you have <b>create php content</b> permission (see the %permission page) you <b>must</b> change the type to PHP to make your code active.", array("%overview" => l(t("blocks"), "admin/system/block"), "%permission" => l(t("permissions"), "admin/user/permission")));
47
      break;
48
    case 'admin/system/block/preview':
49
      $output = t("This page show you the placement of your blocks in different themes types. The numbers are the weight of each block, which is used to sort them within the sidebars.");
50 51
      break;
  }
52

53
  return $output;
54 55
}

56
function block_system($field){
57 58
  $output = "";

Dries's avatar
Dries committed
59
  if ($field == "description") { $output = block_help("admin/system/modules"); }
60 61

  return $output;
62 63
}

64
function block_perm() {
Dries's avatar
Dries committed
65 66 67 68
  return array("administer blocks");
}

function block_link($type) {
69
  if ($type == "system" && user_access("administer blocks")) {
Dries's avatar
Dries committed
70

71 72 73 74
    menu("admin/system/block", t("blocks"), "block_admin", block_help("admin/system/block"), 3);
    menu("admin/system/block/add", t("new block"), "block_admin", block_help("admin/system/block/add"), 2);
    menu("admin/system/block/preview", t("preview placement"), "block_admin", block_help("admin/system/block/preview"), 3);
    menu("admin/system/block/help", t("help"), "block_help", NULL, 9);
75
  }
76 77
}

Dries's avatar
Dries committed
78 79
function block_block($op = "list", $delta = 0) {
  if ($op == "list") {
80
    $result = db_query("SELECT bid, title, info FROM {boxes} ORDER BY title");
Dries's avatar
Dries committed
81
    while ($block = db_fetch_object($result)) {
82
      $blocks[$block->bid]["info"] = $block->info;
Dries's avatar
Dries committed
83 84 85 86
    }
    return $blocks;
  }
  else {
87
    $block = db_fetch_object(db_query("SELECT * FROM {boxes} WHERE bid = %d", $delta));
88
    $data["subject"] = $block->title;
Dries's avatar
Dries committed
89 90
    $data["content"] = ($block->type == 1) ? eval($block->body) : $block->body;
    return $data;
91 92 93
  }
}

Dries's avatar
Dries committed
94
function block_admin_save($edit) {
Dries's avatar
Dries committed
95 96
  foreach ($edit as $module => $blocks) {
    foreach ($blocks as $delta => $block) {
97
      db_query("UPDATE {blocks} SET region = %d, status = %d, custom = %d, path = '%s', weight = %d WHERE module = '%s' AND delta = '%s'",
98
                $block["region"], $block["status"], $block["custom"], $block["path"], $block["weight"], $module, $delta);
Dries's avatar
Dries committed
99
    }
Dries's avatar
Dries committed
100
  }
Dries's avatar
Dries committed
101

Dries's avatar
Dries committed
102
  return t("the block settings have been updated.");
Dries's avatar
Dries committed
103 104
}

105 106
/**
 * update blocks db table with blocks currently exported by modules
107
 *
108 109 110 111 112
 * @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")) {
113
  $result = db_query("SELECT * FROM {blocks} ");
Dries's avatar
Dries committed
114 115 116 117
  while ($old_block = db_fetch_object($result)) {
    $old_blocks[$old_block->module][$old_block->delta] = $old_block;
  }

118
  db_query("DELETE FROM {blocks} ");
Dries's avatar
Dries committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

  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
139
        db_query("INSERT INTO {blocks} (module, delta, status, weight, region, path, custom) VALUES ('%s', '%s', %d, %d, %d, '%s', %d)",
140
                  $block["module"], $block["delta"], $block["status"], $block["weight"], $block["region"], $block["path"], $block["custom"]);
Dries's avatar
Dries committed
141 142

        $blocks[] = $block;
143

144 145
        // build array to sort on
        $order[$order_by[0]][] = $block[$order_by[0]];
Dries's avatar
Dries committed
146 147 148 149
      }
    }
  }

150 151 152
  // 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
153 154
  return $blocks;
}
Dries's avatar
Dries committed
155

Dries's avatar
Dries committed
156
function block_admin_display() {
Dries's avatar
Dries committed
157

158
  $blocks = _block_rehash();
Dries's avatar
Dries committed
159

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

Dries's avatar
Dries committed
162
  foreach ($blocks as $block) {
Dries's avatar
Dries committed
163
    if ($block["module"] == "block") {
164 165
      $edit = l(t("edit"), "admin/system/block/edit/". $block["delta"]);
      $delete = l(t("delete"), "admin/system/block/delete/". $block["delta"]);
166
    }
167 168 169 170
    else {
      $edit = "";
      $delete = "";
    }
Dries's avatar
Dries committed
171

172
    $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(t("left"), t("right"))), form_textfield(NULL, $block["module"]."][".$block["delta"]."][path", $block["path"], 10, 255), $edit, $delete);
Dries's avatar
Dries committed
173
  }
Dries's avatar
Dries committed
174

Dries's avatar
Dries committed
175
  $output = table($header, $rows);
176
  $output .= form_submit(t("Save blocks"));
Dries's avatar
Dries committed
177

178
  return form($output);
Dries's avatar
Dries committed
179 180
}

Dries's avatar
Dries committed
181
function block_admin_preview() {
182

183
  $result = db_query("SELECT * FROM {blocks} WHERE status > 0 AND region = 0 ORDER BY weight");
184
  $lblocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
Dries committed
185 186 187
  while ($block = db_fetch_object($result)) {
    $block_data = module_invoke($block->module, "block", "list");
    $name = $block_data[$block->delta]["info"];
188
    $lblocks .= " <tr><td>". ($block->status == 2 ? "<b>$name</b>" : $name) ."</td><td>$block->weight</td></tr>\n";
Dries's avatar
Dries committed
189
  }
190
  $lblocks .= "</table>\n";
191

192
  $result = db_query("SELECT * FROM {blocks} WHERE status > 0 AND region = 1 ORDER BY weight");
193
  $rblocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
Dries committed
194 195 196
  while ($block = db_fetch_object($result)) {
    $block_data = module_invoke($block->module, "block", "list");
    $name = $block_data[$block->delta]["info"];
197
    $rblocks .= " <tr><td>". ($block->status == 2 ? "<b>$name</b>" : $name) ."</td><td>$block->weight</td></tr>\n";
Dries's avatar
Dries committed
198
  }
199
  $rblocks .= "</table>\n";
Dries's avatar
Dries committed
200

201
  $output .= "<h3>". t("Themes with both left and right sidebars") .":</h3>\n";
202
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
203
  $output .= " <tr><td colspan=\"3\" style=\"text-align: center;\">". t("header") ."</td></tr>\n";
204
  $output .= " <tr><td>\n". ($lblocks ? $lblocks : "&nbsp;") ."</td><td style=\"width: 300px;\">&nbsp;</td><td>\n". ($rblocks ? $rblocks : "&nbsp;") ."</td></tr>\n";
205
  $output .= " <tr><td colspan=\"3\" style=\"text-align: center;\">". t("footer") ."</td></tr>\n";
206
  $output .= "</table>\n";
207

208
  $result = db_query("SELECT * FROM {blocks} WHERE status > 0 ORDER BY weight");
209
  $blocks .= "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
Dries committed
210 211 212
  while ($block = db_fetch_object($result)) {
    $block_data = module_invoke($block->module, "block", "list");
    $name = $block_data[$block->delta]["info"];
213
    $blocks .= " <tr><td>". ($block->status == 2 ? "<b>$name</b>" : $name) ."</td><td>$block->weight</td></tr>\n";
Dries's avatar
Dries committed
214
  }
215 216
  $blocks .= "</table>\n";

217
  $output .= "<h3>". t("Themes with right-sidebar only") .":</h3>\n";
218
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
219
  $output .= " <tr><td colspan=\"2\" style=\"text-align: center;\">". t("header") ."</td></tr>\n";
220
  $output .= " <tr><td style=\"width: 400px;\">&nbsp;</td><td>\n". ($blocks ? $blocks : "&nbsp;") ."</td></tr>\n";
221
  $output .= " <tr><td colspan=\"2\" style=\"text-align: center;\">". t("footer") ."</td></tr>\n";
222 223
  $output .= "</table>\n";

224
  $output .= "<h3>". t("Themes with left-sidebar only") .":</h3>\n";
225
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
226
  $output .= " <tr><td colspan=\"2\" style=\"text-align: center;\">". t("header") ."</td></tr>\n";
227
  $output .= " <tr><td>\n". ($blocks ? $blocks : "&nbsp;") ."</td><td style=\"width: 400px;\">&nbsp;</td></tr>\n";
228
  $output .= " <tr><td colspan=\"2\" style=\"text-align: center;\">". t("footer") ."</td></tr>\n";
229
  $output .= "</table>\n";
230

231
  return $output;
232 233
}

234
function block_box_get($bid) {
235
  return db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid));
236 237 238
}

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

241 242 243
  $form = form_textfield(t("Title"), "title", $edit["title"], 50, 64);
  $form .= form_textfield(t("Description"), "info", $edit["info"], 50, 64);
  $form .= form_textarea(t("Body"), "body", $edit["body"], 70, 10);
Kjartan's avatar
Kjartan committed
244
  if (user_access("create php content")) {
245
    $form .= form_select(t("Type"), "type", $edit["type"], $type);
Dries's avatar
Dries committed
246
  }
247 248 249 250 251

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

252
  $form .= form_submit(t("Save block"));
253

254
  return form($form);
255 256 257
}

function block_box_save($edit) {
258
  if (!user_access("create php content")) {
Dries's avatar
Dries committed
259 260 261
    $edit["type"] = 0;
  }

262
  if ($edit["bid"]) {
263
    db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', type = %d WHERE bid = %d", $edit["title"], $edit["body"], $edit["info"], $edit["type"], $edit["bid"]);
Dries's avatar
Dries committed
264
    return t("the block has been updated.");
265 266
  }
  else {
267
    db_query("INSERT INTO {boxes} (title, body, info, type) VALUES  ('%s', '%s', '%s', %d)", $edit["title"], $edit["body"], $edit["info"], $edit["type"]);
Dries's avatar
Dries committed
268
    return t("the new block has been added.");
269 270 271 272 273
  }
}

function block_box_delete($bid) {
  if ($bid) {
274
    db_query("DELETE FROM {boxes} WHERE bid = %d", $bid);
Dries's avatar
Dries committed
275
    return t("the block has been deleted.");
276 277 278
  }
}

Dries's avatar
Dries committed
279
function block_admin() {
280 281
  $op = $_POST["op"];
  $edit = $_POST["edit"];
282

Dries's avatar
Dries committed
283
  if (user_access("administer blocks")) {
284

Dries's avatar
Dries committed
285
    if (empty($op)) {
286
      $op = arg(3);
Dries's avatar
Dries committed
287 288
    }

289 290
    switch ($op) {
      case "preview":
291
        $output = block_admin_preview();
292
        break;
293
      case "add":
294
        $output = block_box_form();
295 296
        break;
      case "edit":
297
        $output = block_box_form(block_box_get(arg(4)));
298 299
        break;
      case "delete":
300
        $output = status(block_box_delete(arg(4)));
Dries's avatar
Dries committed
301
        cache_clear_all();
302
        $output .= block_admin_display();
303
        break;
304
      case t("Save block"):
305
        $output = status(block_box_save($edit));
Dries's avatar
Dries committed
306
        cache_clear_all();
307
        $output .= block_admin_display();
308
        break;
309
      case t("Save blocks"):
310
        $output = status(block_admin_save($edit));
Dries's avatar
Dries committed
311
        cache_clear_all();
312 313
        // fall through
      default:
314
        $output .= block_admin_display();
315
    }
316 317

    return $output;
318 319
  }
  else {
320
    return message_access();
Dries's avatar
Dries committed
321 322
  }
}
323

324 325
function block_user($type, &$edit, &$user) {
  switch ($type) {
326
    case "register_form":
327
      $result = db_query("SELECT * FROM {blocks} WHERE custom = %d ORDER BY module, delta", 1);
328 329

      while ($block = db_fetch_object($result)) {
Dries's avatar
Dries committed
330
        $form .= form_hidden("block][$block->module][$block->delta", $block->status);
331 332 333
      }

      return $form;
334
    case "edit_form":
335
      $result = db_query("SELECT * FROM {blocks} WHERE custom = %d ORDER BY module, delta", 1);
336 337

      while ($block = db_fetch_object($result)) {
Dries's avatar
Dries committed
338 339 340
        $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
341 342 343 344
        }
      }

      if (isset($form)) {
Dries's avatar
Dries committed
345
        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."));
346
      }
347 348

      break;
349 350 351 352 353
    case "edit_validate":
      if (!$edit["block"]) {
        $edit["block"] = array();
      }
      return $edit;
354 355 356
  }
}

357
?>