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

Dries's avatar
   
Dries committed
4
function block_help() {
Dries's avatar
   
Dries committed
5
6
7
8
9
10
11
12
13
14
15
16
  $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>Which sidebar each block appears in depends on which theme you're using (some are left-only, some right, some both), and also 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 on which pages you want a specific block 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!";
  $output .= "<h3>Custom Blocks</h3>";
  $output .= "<p>A custom block is a block that 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 of 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 at page view 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>";
Dries's avatar
   
Dries committed
17
18
19
20
21
22
23
24
25
26
  $output .= "<pre>
  return t(\"Welcome visitor, ... welcome message goes here ...\");
</pre>";
  $output .= "<p>If we are however dealing with a registered user, we can customize the message by using:</p>";
  $output .= "<pre>
  if (\$user->uid) {
    return t(\"Welcome \$user->name, ... welcome message goes here ...\");
  }
  else {
    return t(\"Welcome visitor, ... welcome message goes here ...\");
Dries's avatar
   
Dries committed
27
28
  }";
  $output .= "</pre></blockquote>";
Dries's avatar
   
Dries committed
29
30
  $output .= "<p>For more in-depth examples, we recommend that you check the existing boxes and use them as a starting point.</p>";
  return t($output);
31

Dries's avatar
   
Dries committed
32
33
}

34
function block_system($field){
Kjartan's avatar
Kjartan committed
35
  $system["description"] = t("Controls the boxes that are displayed around the main content.");
36
37
38
  return $system[$field];
}

Dries's avatar
   
Dries committed
39
function block_perm() {
Dries's avatar
   
Dries committed
40
41
42
43
  return array("administer blocks");
}

function block_link($type) {
Dries's avatar
Dries committed
44
  if ($type == "admin" && user_access("administer blocks")) {
Dries's avatar
   
Dries committed
45
46
47
   $help["block"] = t("Blocks are the boxes visible in the side bars on the left- and right-hand side 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 is a mask which lets you define on which pages you want the specific block to be shown. The custom checkbox lets your users hide the block from their account setting page. You can also create your own blocks, where you specify the content of the block rather than it 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("create new block"), "admin/block/add")));
    $help["create"] = 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 <a href=\"%overview\">block management</a>. The title is used when displaying the block. The description is used in the \"block\" column on the <a href=\"%overview\">block management</a> page. If you are going to place PHP code in the block, and you have <b>create PHP content</b> permission (see <a href=\"%permission\">user management >> user permissions</a>) you <B>must</b> change the type to PHP to make your code active.", array("%overview" => url("admin/block"), "%permission" => url("admin/user/permission")));
    $help["preview"] = t("This page helps show you the placement of your blocks in different themes. The numbers are the weight of each block, which is used to sort them within the sidebars.");
Dries's avatar
   
Dries committed
48

Dries's avatar
   
Dries committed
49
    menu("admin/block", "block management", "block_admin", $help["block"], 3);
Dries's avatar
   
Dries committed
50
51
    menu("admin/block/add", "create new block", "block_admin", $help["create"], 2);
    menu("admin/block/preview", "preview placement", "block_admin", $help["preview"], 3);
Dries's avatar
   
Dries committed
52
    menu("admin/block/help", "help", "block_help", NULL, 9);
Dries's avatar
   
Dries committed
53
  }
Dries's avatar
   
Dries committed
54
55
}

Dries's avatar
   
Dries committed
56
57
58
59
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
60
      $blocks[$block->bid]["info"] = $block->info;
Dries's avatar
   
Dries committed
61
62
63
64
    }
    return $blocks;
  }
  else {
Dries's avatar
   
Dries committed
65
    $block = db_fetch_object(db_query("SELECT * FROM boxes WHERE bid = %d", $delta));
Dries's avatar
   
Dries committed
66
    $data["subject"] = $block->title;
Dries's avatar
   
Dries committed
67
68
    $data["content"] = ($block->type == 1) ? eval($block->body) : $block->body;
    return $data;
69
70
71
  }
}

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

Dries's avatar
   
Dries committed
80
  return t("the block settings have been updated.");
Dries's avatar
 
Dries committed
81
82
}

Dries's avatar
   
Dries committed
83
84
/**
 * update blocks db table with blocks currently exported by modules
Dries's avatar
   
Dries committed
85
 *
Dries's avatar
   
Dries committed
86
87
88
89
90
91
 * @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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  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
117
        db_query("INSERT INTO blocks (module, delta, status, weight, region, path, custom) VALUES ('%s', '%s', %d, %d, %d, '%s', %d)",
Dries's avatar
   
Dries committed
118
                  $block["module"], $block["delta"], $block["status"], $block["weight"], $block["region"], $block["path"], $block["custom"]);
Dries's avatar
   
Dries committed
119
120

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

Dries's avatar
   
Dries committed
122
123
        // build array to sort on
        $order[$order_by[0]][] = $block[$order_by[0]];
Dries's avatar
   
Dries committed
124
125
126
127
      }
    }
  }

Dries's avatar
   
Dries committed
128
129
130
  // 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
131
132
  return $blocks;
}
Dries's avatar
   
Dries committed
133

Dries's avatar
   
Dries committed
134
function block_admin_display() {
Dries's avatar
   
Dries committed
135

Dries's avatar
   
Dries committed
136
  $blocks = _block_rehash();
Dries's avatar
   
Dries committed
137

Dries's avatar
   
Dries committed
138
  $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
139

Dries's avatar
   
Dries committed
140
  foreach ($blocks as $block) {
Dries's avatar
   
Dries committed
141
    if ($block["module"] == "block") {
Dries's avatar
   
Dries committed
142
      $edit = l(t("edit"), "admin/block/edit/". $block["delta"]);
Dries's avatar
   
Dries committed
143
      $delete = l(t("delete"), "admin/block/delete/". $block["delta"]);
144
    }
Dries's avatar
   
Dries committed
145
146
147
148
    else {
      $edit = "";
      $delete = "";
    }
Dries's avatar
   
Dries committed
149

Dries's avatar
   
Dries committed
150
    $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
151
  }
Dries's avatar
   
Dries committed
152

Dries's avatar
   
Dries committed
153
  $output = table($header, $rows);
Dries's avatar
   
Dries committed
154
  $output .= form_submit(t("Save blocks"));
Dries's avatar
 
Dries committed
155

156
  print form($output);
Dries's avatar
 
Dries committed
157
158
}

Dries's avatar
   
Dries committed
159
function block_admin_preview() {
Dries's avatar
   
Dries committed
160
161

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

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

Dries's avatar
   
Dries committed
179
  $output .= "<h3>". t("Themes with both left and right sidebars") .":</h3>\n";
Dries's avatar
   
Dries committed
180
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
   
Dries committed
181
  $output .= " <tr><td colspan=\"3\" style=\"text-align: center;\">". t("header") ."</td></tr>\n";
Dries's avatar
   
Dries committed
182
  $output .= " <tr><td>\n". ($lblocks ? $lblocks : "&nbsp;") ."</td><td style=\"width: 300px;\">&nbsp;</td><td>\n". ($rblocks ? $rblocks : "&nbsp;") ."</td></tr>\n";
Dries's avatar
   
Dries committed
183
  $output .= " <tr><td colspan=\"3\" style=\"text-align: center;\">". t("footer") ."</td></tr>\n";
Dries's avatar
   
Dries committed
184
  $output .= "</table>\n";
Dries's avatar
   
Dries committed
185
186

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

Dries's avatar
   
Dries committed
195
  $output .= "<h3>". t("Themes with right-sidebar only") .":</h3>\n";
Dries's avatar
   
Dries committed
196
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
   
Dries committed
197
  $output .= " <tr><td colspan=\"2\" style=\"text-align: center;\">". t("header") ."</td></tr>\n";
Dries's avatar
   
Dries committed
198
  $output .= " <tr><td style=\"width: 400px;\">&nbsp;</td><td>\n". ($blocks ? $blocks : "&nbsp;") ."</td></tr>\n";
Dries's avatar
   
Dries committed
199
  $output .= " <tr><td colspan=\"2\" style=\"text-align: center;\">". t("footer") ."</td></tr>\n";
Dries's avatar
   
Dries committed
200
201
  $output .= "</table>\n";

Dries's avatar
   
Dries committed
202
  $output .= "<h3>". t("Themes with left-sidebar only") .":</h3>\n";
Dries's avatar
   
Dries committed
203
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
Dries's avatar
   
Dries committed
204
  $output .= " <tr><td colspan=\"2\" style=\"text-align: center;\">". t("header") ."</td></tr>\n";
Dries's avatar
   
Dries committed
205
  $output .= " <tr><td>\n". ($blocks ? $blocks : "&nbsp;") ."</td><td style=\"width: 400px;\">&nbsp;</td></tr>\n";
Dries's avatar
   
Dries committed
206
  $output .= " <tr><td colspan=\"2\" style=\"text-align: center;\">". t("footer") ."</td></tr>\n";
Dries's avatar
   
Dries committed
207
  $output .= "</table>\n";
Dries's avatar
   
Dries committed
208
209
210
211

  print $output;
}

212
function block_box_get($bid) {
Dries's avatar
   
Dries committed
213
  return db_fetch_array(db_query("SELECT * FROM boxes WHERE bid = %d", $bid));
214
215
216
}

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

Dries's avatar
   
Dries committed
219
220
221
  $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
222
  if (user_access("create php content")) {
Dries's avatar
   
Dries committed
223
    $form .= form_select(t("Type"), "type", $edit["type"], $type);
Dries's avatar
   
Dries committed
224
  }
225
226
227
228
229

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

Dries's avatar
   
Dries committed
230
  $form .= form_submit(t("Save block"));
231
232
233
234
235

  print form($form);
}

function block_box_save($edit) {
Dries's avatar
   
Dries committed
236
237
238
239
  if (!user_access("create PHP content")) {
    $edit["type"] = 0;
  }

240
  if ($edit["bid"]) {
Dries's avatar
   
Dries committed
241
    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
242
    return t("the block has been updated.");
243
244
  }
  else {
Dries's avatar
   
Dries committed
245
    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
246
    return t("the new block has been added.");
247
248
249
250
251
  }
}

function block_box_delete($bid) {
  if ($bid) {
Dries's avatar
   
Dries committed
252
    db_query("DELETE FROM boxes WHERE bid = %d", $bid);
Dries's avatar
   
Dries committed
253
    return t("the block has been deleted.");
254
255
256
  }
}

Dries's avatar
 
Dries committed
257
function block_admin() {
Dries's avatar
   
Dries committed
258
259
  $op = $_POST["op"];
  $edit = $_POST["edit"];
Dries's avatar
   
Dries committed
260

Dries's avatar
   
Dries committed
261
  if (user_access("administer blocks")) {
Dries's avatar
   
Dries committed
262

Dries's avatar
   
Dries committed
263
264
265
266
    if (empty($op)) {
      $op = arg(2);
    }

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

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

      while ($block = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
306
        $form .= form_hidden("block][$block->module][$block->delta", $block->status);
307
308
309
      }

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

      while ($block = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
314
315
316
        $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
317
318
319
320
        }
      }

      if (isset($form)) {
Dries's avatar
   
Dries committed
321
        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."));
322
      }
Dries's avatar
   
Dries committed
323
324

      break;
325
326
327
328
329
    case "edit_validate":
      if (!$edit["block"]) {
        $edit["block"] = array();
      }
      return $edit;
330
331
332
  }
}

Dries's avatar
   
Dries committed
333
?>