block.module 13.1 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>
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  <hr />
  <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>
  <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>
  <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 ASCII or HTML boxes instead.</p>
  <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>
  <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 ASCII or HTML boxes.</p>
  <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
}

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

function block_link($type) {
Dries's avatar
Dries committed
46
  if ($type == "admin" && user_access("administer blocks")) {
Dries's avatar
 
Dries committed
47
    $links[] = la(t("blocks"), array("mod" => "block"));
Dries's avatar
 
Dries committed
48 49 50
  }

  return $links ? $links : array();
Dries's avatar
 
Dries committed
51 52
}

53 54 55 56 57 58 59 60 61 62
function block_block() {
  $result = db_query("SELECT * FROM boxes ORDER BY title");
  while ($block = db_fetch_object($result)) {
    $blocks[$block->bid]["subject"] = check_output($block->title);
    $blocks[$block->bid]["content"] = ($block->type == 2) ? eval($block->body) : $block->body;
    $blocks[$block->bid]["info"] = check_output($block->info);
  }
  return $blocks;
}

Dries's avatar
 
Dries committed
63
function block_admin_save($edit) {
64
  foreach ($edit as $key => $value) {
65
    db_query("UPDATE blocks SET region = '%s', status = '%d', custom = '%d', path = '%s', weight = '%d' WHERE name = '%s'", $value["region"], $value["status"], $value["custom"], $value["path"], $value["weight"], $key);
Dries's avatar
 
Dries committed
66 67 68 69
  }
}

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

Dries's avatar
 
Dries committed
72
  // Generate output:
73
  $output = "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n";
74
  $output .= "<tr><th>block</th><th>module</th><th>enabled</th><th>custom</th><th>weight</th><th>region</th><th>path</th><th colspan=\"2\">operations</th></tr>\n";
Dries's avatar
 
Dries committed
75

Dries's avatar
 
Dries committed
76
  while ($block = db_fetch_object($result)) {
77
    $weights = array(0 => 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Dries's avatar
 
Dries committed
78

79
    $output .= '<tr>';
Kjartan's avatar
Kjartan committed
80
    //$output .= '<td>'. la($block->name, array("mod" => "block", "op" => "view", "id" => $block->delta), "", array("title" => t("View the block details"))) .'</td>';
81
    $output .= "<td>$block->name</td>";
Kjartan's avatar
Kjartan committed
82
    $output .= '<td>'. (module_hook($block->module, "admin") ? la($block->module, array("mod" => $block->module), "", array("title" => t("Administer module"))) : $block->module) .'</td>';
83 84
    $output .= '<td align="center">'. form_checkbox(NULL, "$block->name][status", 1, $block->status) .'</td>';
    $output .= '<td align="center">'. form_checkbox(NULL, "$block->name][custom", 1, $block->custom) .'</td>';
85 86 87 88 89 90 91 92
    $output .= '<td>'. form_select(NULL, "$block->name][weight", $block->weight, $weights) .'</td>';
    $output .= '<td>'. form_select(NULL, "$block->name][region", $block->region, array("left", "right")) .'</td>';
    $output .= '<td>'. form_textfield(NULL, "$block->name][path", $block->path, 10, 255) .'</td>';
    if ($block->module == 'block') {
      $output .= '<td>'. la(t("edit"), array("mod" => "block", "op" => "edit", "id" => $block->delta)) .'</td>';
      $output .= '<td>'. la(t("delete"), array("mod" => "block", "op" => "delete", "id" => $block->delta)) .'</td>';
    }
    $output .= "</tr>\n";
Dries's avatar
 
Dries committed
93
  }
Dries's avatar
 
Dries committed
94

95
  $output .= "</table>\n";
96
  $output .= form_submit("Save blocks");
Dries's avatar
 
Dries committed
97

98
  print form($output);
Dries's avatar
 
Dries committed
99 100
}

Dries's avatar
 
Dries committed
101
function block_admin_preview() {
Dries's avatar
 
Dries committed
102 103

  $result = db_query("SELECT * FROM blocks WHERE status > 0 AND region = 0 ORDER BY weight");
Dries's avatar
 
Dries committed
104 105 106
  $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
107 108

  $result = db_query("SELECT * FROM blocks WHERE status > 0 AND region = 1 ORDER BY weight");
Dries's avatar
 
Dries committed
109 110 111
  $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
112

Dries's avatar
 
Dries committed
113 114 115 116 117 118
  $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
119 120

  $result = db_query("SELECT * FROM blocks WHERE status > 0 ORDER BY weight");
Dries's avatar
 
Dries committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  $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
138 139 140 141

  print $output;
}

Dries's avatar
 
Dries committed
142
function block_init() {
Dries's avatar
 
Dries committed
143

Dries's avatar
 
Dries committed
144 145 146 147 148 149 150 151 152 153
  $result = db_query("SELECT * FROM modules");
  while ($module = db_fetch_object($result)) {
    module_rehash($module->name);
  }

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

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
function block_box_get($bid) {
  return db_fetch_array(db_query("SELECT * FROM boxes WHERE bid = '%s'", $bid));
}

function block_box_form($edit = array()) {
  $type = array(0 => "ASCII", 1 => "HTML", 2 => "PHP");

  $form .= form_textfield("Title", "title", $edit["title"], 50, 64);
  $form .= form_textfield("Description", "info", $edit["info"], 50, 64);
  $form .= form_textarea("Body", "body", $edit["body"], 70, 10);
  $form .= form_select("Type", "type", $edit["type"], $type);

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

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

  print form($form);
}

function block_box_save($edit) {
  if ($edit["bid"]) {
    db_query("UPDATE boxes SET title = '%s', body = '%s', info = '%s', type = '%s' WHERE bid = '%s'", $edit["title"], $edit["body"], $edit["info"], $edit["type"], $edit["bid"]);
178
    return "block updated.";
179 180 181
  }
  else {
    db_query("INSERT INTO boxes (title, body, info, type) VALUES  ('%s', '%s', '%s', '%s')", $edit["title"], $edit["body"], $edit["info"], $edit["type"]);
182 183 184 185 186 187
    if (db_error()) {
      return "block added.";
    }
    else {
      return "failed to add block.";
    }
188 189 190 191 192 193
  }
}

function block_box_delete($bid) {
  if ($bid) {
    db_query("DELETE FROM boxes WHERE bid = '%s'", $bid);
194
    return "block deleted.";
195 196 197
  }
}

Dries's avatar
 
Dries committed
198
function block_admin() {
Dries's avatar
 
Dries committed
199
  global $op, $edit;
Dries's avatar
 
Dries committed
200

Dries's avatar
 
Dries committed
201
  if (user_access("administer blocks")) {
Dries's avatar
 
Dries committed
202

203 204 205 206 207 208
    $links[] = la(t("configure"), array("mod" => "block"));
    $links[] = la(t("add block"), array("mod" => "block", "op" => "add"));
    $links[] = la(t("preview"), array("mod" => "block", "op" => "preview"));
    $links[] = la(t("help"), array("mod" => "block", "op" => "help"));

    print "<small>". implode(" &middot; ", $links) ."</small><hr />";
Dries's avatar
 
Dries committed
209

Dries's avatar
 
Dries committed
210 211
    block_init();

Dries's avatar
 
Dries committed
212 213 214 215 216 217 218
    switch ($op) {
      case "help":
        block_help();
        break;
      case "preview":
        block_admin_preview();
        break;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
      case "add":
        block_box_form();
        break;
      case "edit":
        global $id;
        block_box_form(block_box_get($id));
        break;
      case "delete":
        global $id;
        print status(block_box_delete($id));
        block_init();
        block_admin_display();
        break;
      case "Save block":
        print status(block_box_save($edit));
        block_init();
        block_admin_display();
        break;
Dries's avatar
 
Dries committed
237 238 239 240 241 242 243 244 245
      case "Save blocks":
        block_admin_save($edit);
        // fall through
      default:
        block_admin_display();
    }
  }
  else {
    print message_access();
Dries's avatar
 
Dries committed
246 247
  }
}
Dries's avatar
 
Dries committed
248

249 250
function block_user($type, &$edit, &$user) {
  switch ($type) {
251 252 253 254 255 256 257 258
    case "register_form":
      $result = db_query("SELECT * FROM blocks WHERE custom = '%d' ORDER BY name", 1);

      while ($block = db_fetch_object($result)) {
        $form .= form_hidden("block][$block->name", $block->status);
      }

      return $form;
259 260 261 262
    case "edit_form":
      $result = db_query("SELECT * FROM blocks WHERE custom = '%d' ORDER BY name", 1);

      while ($block = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
263 264 265 266 267 268 269 270
        $data = module_invoke($block->module, "block");
        if ($data[$block->delta]["subject"]) {
          $form .= "<tr><td>$block->name</td><td>". form_checkbox(NULL, "block][$block->name", 1, $user->block[$block->name]) ."</td></tr>\n";
        }
      }

      if (isset($form)) {
        return form_item(t("Block configuration"), '<table border="0" cellpadding="2" cellspacing="2">'. $form .'</table>', "Enable the blocks you would like to see displayed in the side bars.");
271 272 273 274 275
      }

  }
}

Dries's avatar
 
Dries committed
276
?>