337 lines
18 KiB
Plaintext
337 lines
18 KiB
Plaintext
<?php
|
|
// $Id$
|
|
|
|
function block_help() {
|
|
$output .= "<p>Blocks are the boxes visible in the side bars on the left- and/or right-hand side of the web site, depending on the choosen theme. They are either exported by the Drupal engine or by any of the active modules. To really get your teeth into a Drupal web site, 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>";
|
|
$output .= "<p>The placement of blocks is delegated to the administrator. In most cases (i.e., the ". l("\"custom\" blocks","admin/block/add") ."), the user has complete control -- using preferences -- over whether or not they are enabled.</p>";
|
|
$output .= "<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\" towards the bottom of the column while the lighter blocks will \"float\" towards the top.</p>";
|
|
$output .= "<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\". If there is only one region all the blocks are sorted by weight.</p>";
|
|
$output .= "<p>The path setting lets you define which pages you want the 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!<br />Examples:<ul><li>Only the show block on node pages: ^/node\\.php</li><li>Only show the block on the user page: ^/module\\.php\\?mod=user</li><li>Show the block in main and blog pages: ^/(index\\.php|module\\.php\\?mod=blog)</li></ul><hr /></p>";
|
|
$output .= "<p>The content of the site can be almost entirely altered through ". l("<i>custom blocks</i>", "admin/block/add") .". Simply put, custom blocks are small bits of text, HTML or PHP code which will get plugged into the site just like any other block.</p>";
|
|
$output .= "<p>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 .= "<h3>PHP in custom blocks</h3><p>If you know how to script in PHP, PHP custom blocks are easy to create. But don't worry if you're no PHP-wizard: simply use HTML instead.</p>";
|
|
$output .= "<p>You can use any piece of PHP code to make up the content of a PHP custom block: this implies that you can declare and use functions, consult the SQL database, access configuration settings and much more. A PHP custom blocks' code is stored in the database and the engine will dynamically embed the PHP code just-in-time for execution.</p>";
|
|
$output .= "<p>There are however some factors to keep in mind when using and creating PHP custom blocks: PHP custom blocks 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 custom blocks because you can - and probably will - corrupt your database or render your site unusable! If you don't plan to do fancy stuff with custom blocks then you're probably better off with 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>Note:<br /><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>. If you need form information you need to get it from the \"superglobals\" \$_POST, \$_GET, etc.</li></ul></p>";
|
|
$output .= "<p>You can use the <code>return</code> statement to return the actual content for your block as well.</p>";
|
|
$output .= "<p><u>A basic example:</u></p>";
|
|
$output .= "<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>";
|
|
$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 ...\");
|
|
}
|
|
</pre>";
|
|
$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);
|
|
|
|
}
|
|
|
|
function block_system($field){
|
|
$system["description"] = t("Controls the boxes that are displayed around the main content.");
|
|
return $system[$field];
|
|
}
|
|
|
|
function block_perm() {
|
|
return array("administer blocks");
|
|
}
|
|
|
|
function block_link($type) {
|
|
if ($type == "admin" && user_access("administer blocks")) {
|
|
$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, then you can assign the block's placement by giving it a region and/or a weight within that region. This sorts them vertically, the smaller the weight, the lighter the block and it will \"float\" 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 tells Drupal to use a custom designed block, see both <a href=\"%help\">help</a> and <a href=\"%block\">create new block</a> for more information on custom blocks. If you have a custom block then the \"edit\" and \"delete\" operations will be displayed to edit/delete your custom block.", array("%help" => url("admin/block/help"), "%block" => url("admin/block/add")));
|
|
$help["create"] = t("Below create a block to be used in the side bars. Once you have created this block you must make it active, and give it a place on the page by 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 shows you the placement of your blocks. Each block is represented by its block name, and it's weight. <b>Layout scheme #1</b> is a layout with both left and right columns. <b>Layout scheme #2</b> has only a right column. And <b>layout scheme #3</b> only a left column.");
|
|
|
|
menu("admin/block", "block management", "block_admin", $help["block"], 3);
|
|
menu("admin/block/add", "create new block", "block_admin", $help["create"], 2);
|
|
menu("admin/block/preview", "preview placement", "block_admin", $help["preview"], 3);
|
|
menu("admin/block/help", "help", "block_help", NULL, 9);
|
|
}
|
|
}
|
|
|
|
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)) {
|
|
$blocks[$block->bid]["info"] = $block->info;
|
|
}
|
|
return $blocks;
|
|
}
|
|
else {
|
|
$block = db_fetch_object(db_query("SELECT * FROM boxes WHERE bid = %d", $delta));
|
|
$data["subject"] = $block->title;
|
|
$data["content"] = ($block->type == 1) ? eval($block->body) : $block->body;
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
function block_admin_save($edit) {
|
|
foreach ($edit as $module => $blocks) {
|
|
foreach ($blocks as $delta => $block) {
|
|
db_query("UPDATE blocks SET region = %d, 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);
|
|
}
|
|
}
|
|
|
|
return t("the block settings have been updated.");
|
|
}
|
|
|
|
/**
|
|
* update blocks db table with blocks currently exported by modules
|
|
*
|
|
* @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");
|
|
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
|
|
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"]);
|
|
|
|
$blocks[] = $block;
|
|
|
|
// build array to sort on
|
|
$order[$order_by[0]][] = $block[$order_by[0]];
|
|
}
|
|
}
|
|
}
|
|
|
|
// sort
|
|
array_multisort($order[$order_by[0]], $order_by[1] ? $order_by[1] : SORT_ASC, $order_by[2] ? $order_by[2] : SORT_REGULAR, $blocks);
|
|
|
|
return $blocks;
|
|
}
|
|
|
|
function block_admin_display() {
|
|
|
|
$blocks = _block_rehash();
|
|
|
|
$header = array(t("block"), t("enabled"), t("custom"), t("weight"), t("region"), t("path"), array("data" => "operations", "colspan" => 2));
|
|
|
|
foreach ($blocks as $block) {
|
|
if ($block["module"] == "block") {
|
|
$edit = l(t("edit"), "admin/block/edit/". $block["delta"]);
|
|
$delete = l(t("delete"), "admin/block/delete/". $block["delta"]);
|
|
}
|
|
else {
|
|
$edit = "";
|
|
$delete = "";
|
|
}
|
|
|
|
$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);
|
|
}
|
|
|
|
$output = table($header, $rows);
|
|
$output .= form_submit("Save blocks");
|
|
|
|
print form($output);
|
|
}
|
|
|
|
function block_admin_preview() {
|
|
|
|
$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)) {
|
|
$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";
|
|
}
|
|
$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)) {
|
|
$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";
|
|
}
|
|
$rblocks .= "</table>\n";
|
|
|
|
$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 : " ") ."</td><td width=\"300\"> </td><td>\n". ($rblocks ? $rblocks : " ") ."</td></tr>\n";
|
|
$output .= " <tr><td align=\"center\" colspan=\"3\">footer</td></tr>\n";
|
|
$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)) {
|
|
$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";
|
|
}
|
|
$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\"> </td><td>\n". ($blocks ? $blocks : " ") ."</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 : " ") ."</td><td width=\"400\"> </td></tr>\n";
|
|
$output .= " <tr><td align=\"center\" colspan=\"2\">footer</td></tr>\n";
|
|
$output .= "</table>\n";
|
|
|
|
print $output;
|
|
}
|
|
|
|
function block_box_get($bid) {
|
|
return db_fetch_array(db_query("SELECT * FROM boxes WHERE bid = %d", $bid));
|
|
}
|
|
|
|
function block_box_form($edit = array()) {
|
|
$type = array(0 => "HTML", 1 => "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);
|
|
if (user_access("create php content")) {
|
|
$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 (!user_access("create PHP content")) {
|
|
$edit["type"] = 0;
|
|
}
|
|
|
|
if ($edit["bid"]) {
|
|
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"]);
|
|
return t("the block has been updated.");
|
|
}
|
|
else {
|
|
db_query("INSERT INTO boxes (title, body, info, type) VALUES ('%s', '%s', '%s', %d)", $edit["title"], $edit["body"], $edit["info"], $edit["type"]);
|
|
return t("the new block has been added.");
|
|
}
|
|
}
|
|
|
|
function block_box_delete($bid) {
|
|
if ($bid) {
|
|
db_query("DELETE FROM boxes WHERE bid = %d", $bid);
|
|
return t("the block has been deleted.");
|
|
}
|
|
}
|
|
|
|
function block_admin() {
|
|
$op = $_POST["op"];
|
|
$edit = $_POST["edit"];
|
|
|
|
if (user_access("administer blocks")) {
|
|
|
|
if (empty($op)) {
|
|
$op = arg(2);
|
|
}
|
|
|
|
switch ($op) {
|
|
case "preview":
|
|
block_admin_preview();
|
|
break;
|
|
case "add":
|
|
block_box_form();
|
|
break;
|
|
case "edit":
|
|
block_box_form(block_box_get(arg(3)));
|
|
break;
|
|
case "delete":
|
|
print status(block_box_delete(arg(3)));
|
|
cache_clear_all();
|
|
block_admin_display();
|
|
break;
|
|
case "Save block":
|
|
print status(block_box_save($edit));
|
|
cache_clear_all();
|
|
block_admin_display();
|
|
break;
|
|
case "Save blocks":
|
|
print status(block_admin_save($edit));
|
|
cache_clear_all();
|
|
// fall through
|
|
default:
|
|
block_admin_display();
|
|
}
|
|
}
|
|
else {
|
|
print message_access();
|
|
}
|
|
}
|
|
|
|
function block_user($type, &$edit, &$user) {
|
|
switch ($type) {
|
|
case "register_form":
|
|
$result = db_query("SELECT * FROM blocks WHERE custom = %d ORDER BY module, delta", 1);
|
|
|
|
while ($block = db_fetch_object($result)) {
|
|
$form .= form_hidden("block][$block->module][$block->delta", $block->status);
|
|
}
|
|
|
|
return $form;
|
|
case "edit_form":
|
|
$result = db_query("SELECT * FROM blocks WHERE custom = %d ORDER BY module, delta", 1);
|
|
|
|
while ($block = db_fetch_object($result)) {
|
|
$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";
|
|
}
|
|
}
|
|
|
|
if (isset($form)) {
|
|
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."));
|
|
}
|
|
|
|
break;
|
|
case "edit_validate":
|
|
if (!$edit["block"]) {
|
|
$edit["block"] = array();
|
|
}
|
|
return $edit;
|
|
}
|
|
}
|
|
|
|
?>
|