| 1 |
<?php
|
| 2 |
// $Id: custom_links.module,v 1.5 2008/01/27 09:16:01 eaton Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu().
|
| 6 |
*/
|
| 7 |
function custom_links_menu() {
|
| 8 |
$items = array();
|
| 9 |
|
| 10 |
$items['admin/build/custom_links'] = array(
|
| 11 |
'title' => 'Custom links',
|
| 12 |
'description' => 'Add custom links to specific content types.',
|
| 13 |
'page callback' => 'custom_links_page',
|
| 14 |
'access callback' => 'user_access',
|
| 15 |
'access arguments' => array('administer custom links'),
|
| 16 |
'file' => 'custom_links.admin.inc',
|
| 17 |
);
|
| 18 |
|
| 19 |
$items['admin/build/custom_links/add'] = array(
|
| 20 |
'title' => 'Add custom breadcrumb',
|
| 21 |
'type' => MENU_CALLBACK,
|
| 22 |
'page callback' => 'drupal_get_form',
|
| 23 |
'page arguments' => array('custom_links_form'),
|
| 24 |
'access callback' => 'user_access',
|
| 25 |
'access arguments' => array('administer custom links'),
|
| 26 |
'file' => 'custom_links.admin.inc',
|
| 27 |
);
|
| 28 |
|
| 29 |
$items['admin/build/custom_links/edit'] = array(
|
| 30 |
'title' => 'Edit custom breadcrumb',
|
| 31 |
'type' => MENU_CALLBACK,
|
| 32 |
'page callback' => 'drupal_get_form',
|
| 33 |
'page arguments' => array('custom_links_form'),
|
| 34 |
'access callback' => 'user_access',
|
| 35 |
'access arguments' => array('administer custom links'),
|
| 36 |
'file' => 'custom_links.admin.inc',
|
| 37 |
);
|
| 38 |
|
| 39 |
return $items;
|
| 40 |
}
|
| 41 |
|
| 42 |
function custom_links_theme() {
|
| 43 |
$items = array();
|
| 44 |
$items['custom_links_block'] = array(
|
| 45 |
'arguments' => array('links' => array()),
|
| 46 |
);
|
| 47 |
return $items;
|
| 48 |
}
|
| 49 |
|
| 50 |
function custom_links_link($type, $node = NULL, $teaser = FALSE) {
|
| 51 |
if ($type == 'node') {
|
| 52 |
return _custom_links_build_links($node, $teaser);
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
function custom_links_perm() {
|
| 57 |
return array('administer custom links');
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Implementation of hook_block().
|
| 62 |
*
|
| 63 |
* Generates a block with links for the current node.
|
| 64 |
*/
|
| 65 |
function custom_links_block($op = 'list', $delta = 0) {
|
| 66 |
if ($op == 'list') {
|
| 67 |
$blocks[0]['info'] = t('Custom node links');
|
| 68 |
return $blocks;
|
| 69 |
}
|
| 70 |
else if ($op == 'view' && arg(0) == 'node' && is_numeric(arg(1))) {
|
| 71 |
$node = node_load(arg(1));
|
| 72 |
$links = _custom_links_build_links($node, FALSE, TRUE);
|
| 73 |
if (count($links)) {
|
| 74 |
$block['subject'] = t('Links');
|
| 75 |
$block['content'] = theme('custom_links_block', $links);
|
| 76 |
return $block;
|
| 77 |
}
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
| 81 |
function _custom_links_build_links($node, $teaser = FALSE, $block = FALSE) {
|
| 82 |
$links = array();
|
| 83 |
$custom_links = _custom_links_load_all_links();
|
| 84 |
|
| 85 |
foreach ($custom_links as $link) {
|
| 86 |
if (($block && $link->display != 3) || (!$block && $link->display == 3)) {
|
| 87 |
continue;
|
| 88 |
}
|
| 89 |
if (($link->display == 0 && $teaser) || ($link->display == 1 && !$teaser)) {
|
| 90 |
continue;
|
| 91 |
}
|
| 92 |
if ($link->node_type && $node->type != $link->node_type) {
|
| 93 |
continue;
|
| 94 |
}
|
| 95 |
if ($link->viewer_perm && !user_access($link->viewer_perm)) {
|
| 96 |
continue;
|
| 97 |
}
|
| 98 |
if ($link->author_perm) {
|
| 99 |
$author = user_load(array('uid' => $node->uid));
|
| 100 |
if (!user_access($link->author_perm, $author)) {
|
| 101 |
continue;
|
| 102 |
}
|
| 103 |
}
|
| 104 |
|
| 105 |
$links[$link->link_key]['title'] = $link->title;
|
| 106 |
if (!empty($link->path)) {
|
| 107 |
$links[$link->link_key]['href'] = $link->path;
|
| 108 |
}
|
| 109 |
if (!empty($link->query)) {
|
| 110 |
$links[$link->link_key]['query'] = $link->query;
|
| 111 |
}
|
| 112 |
if (!empty($link->fragment)) {
|
| 113 |
$links[$link->link_key]['fragment'] = $link->fragment;
|
| 114 |
}
|
| 115 |
|
| 116 |
if (module_exists('token')) {
|
| 117 |
$links[$link->link_key] = token_replace($links[$link->link_key], 'node', $node);
|
| 118 |
}
|
| 119 |
|
| 120 |
$links[$link->link_key]['html'] = $link->check_html;
|
| 121 |
}
|
| 122 |
return $links;
|
| 123 |
}
|
| 124 |
|
| 125 |
function _custom_links_load_link($lid) {
|
| 126 |
$sql = 'SELECT * FROM {custom_link} WHERE lid = %d';
|
| 127 |
$result = db_query($sql, $lid);
|
| 128 |
$link = db_fetch_object($result);
|
| 129 |
return $link;
|
| 130 |
}
|
| 131 |
|
| 132 |
function _custom_links_load_all_links($refresh = FALSE) {
|
| 133 |
static $links;
|
| 134 |
if ($refresh || !isset($links)) {
|
| 135 |
$sql = 'SELECT * FROM {custom_link}';
|
| 136 |
$result = db_query($sql);
|
| 137 |
|
| 138 |
$links = array();
|
| 139 |
while ($link = db_fetch_object($result)) {
|
| 140 |
$links[] = $link;
|
| 141 |
}
|
| 142 |
}
|
| 143 |
return $links;
|
| 144 |
}
|
| 145 |
|
| 146 |
function _custom_links_save_link($link = NULL) {
|
| 147 |
if (isset($link->lid)) {
|
| 148 |
drupal_write_record('custom_link', $link, 'lid');
|
| 149 |
}
|
| 150 |
else {
|
| 151 |
drupal_write_record('custom_link', $link);
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
function _custom_links_delete_link($lid) {
|
| 156 |
$sql = 'DELETE FROM {custom_link} WHERE lid = %d';
|
| 157 |
db_query($sql, $lid);
|
| 158 |
}
|
| 159 |
|
| 160 |
function theme_custom_links_block($links) {
|
| 161 |
return theme('links', $links);
|
| 162 |
}
|