| 1 |
<?php
|
| 2 |
// $Id: microsummary.module,v 1.6 2008/04/30 00:36:39 greggles Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* microsummary.module - basically provides module integration to Drupal
|
| 7 |
* and inserts the microsummary link into your site's pages.
|
| 8 |
*/
|
| 9 |
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_help().
|
| 13 |
*/
|
| 14 |
function microsummary_help($path, $arg) {
|
| 15 |
switch ($path) {
|
| 16 |
case 'admin/help#microsummary':
|
| 17 |
return t('Creates a simple microsummary for your site showing one of a couple things.
|
| 18 |
The default is to provide three different microsummaries.
|
| 19 |
The module can also be configured to show other microsummaries based upon
|
| 20 |
nodes that the administrator has created.');
|
| 21 |
case 'admin/modules#description':
|
| 22 |
return t('Provides microsummaries for your site.');
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implementation of hook_menu().
|
| 28 |
*/
|
| 29 |
function microsummary_menu() {
|
| 30 |
$items = array();
|
| 31 |
|
| 32 |
$items['microsummary_posts'] = array(
|
| 33 |
'title' => 'Microsummary posts page',
|
| 34 |
'page callback' => 'microsummary_page',
|
| 35 |
'page arguments' => array('posts'),
|
| 36 |
'type' => MENU_CALLBACK,
|
| 37 |
'access' => 'access content',
|
| 38 |
);
|
| 39 |
$items['microsummary_users'] = array(
|
| 40 |
'title' => 'Microsummary users page',
|
| 41 |
'page callback' => 'microsummary_page',
|
| 42 |
'page arguments' => array('users'),
|
| 43 |
'type' => MENU_CALLBACK,
|
| 44 |
'access' => 'access content',
|
| 45 |
);
|
| 46 |
$items['microsummary_latest_blog'] = array(
|
| 47 |
'title' => 'Microsummary latest blog page',
|
| 48 |
'page callback' => 'microsummary_page',
|
| 49 |
'page arguments' => array('blog'),
|
| 50 |
'type' => MENU_CALLBACK,
|
| 51 |
'access' => 'access content',
|
| 52 |
);
|
| 53 |
$items['admin/settings/microsummary'] = array(
|
| 54 |
'title' => 'Microsummary Settings',
|
| 55 |
'description' => 'Configure microsummaries.',
|
| 56 |
'page callback' => 'drupal_get_form',
|
| 57 |
'page arguments' => array('microsummary_admin_settings'),
|
| 58 |
'access' => 'administer site configuration',
|
| 59 |
'type' => MENU_NORMAL_ITEM,
|
| 60 |
);
|
| 61 |
|
| 62 |
// Show the custom microsummary nodes if configured.
|
| 63 |
$nids = variable_get('microsummary_nids', FALSE);
|
| 64 |
if ($nids) {
|
| 65 |
foreach (explode(',', $nids) as $nid) {
|
| 66 |
$node = node_load($nid);
|
| 67 |
if ($node->nid) {
|
| 68 |
$items['microsummary/'. $node->nid] = array(
|
| 69 |
'title' => 'Custom Microsummary',
|
| 70 |
'description' => 'Microsummary Pages.',
|
| 71 |
'page callback' => 'microsummary_page',
|
| 72 |
'page arguments' => array('node'),
|
| 73 |
'access' => 'access content',
|
| 74 |
'type' => MENU_CALLBACK,
|
| 75 |
);
|
| 76 |
}
|
| 77 |
}
|
| 78 |
}
|
| 79 |
|
| 80 |
if (variable_get('microsummary_default_inclusion', TRUE)) {
|
| 81 |
drupal_set_html_head(microsummary_get_links());
|
| 82 |
}
|
| 83 |
|
| 84 |
return $items;
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Implementation of hook_settings().
|
| 89 |
*/
|
| 90 |
function microsummary_admin_settings() {
|
| 91 |
if (user_access('administer site configuration')) {
|
| 92 |
$form["general"]["microsummary_show_defaults"] = array('#type' => 'checkbox',
|
| 93 |
'#title' => t('Show default microsummaries'),
|
| 94 |
'#default_value' => variable_get('microsummary_show_defaults', TRUE),
|
| 95 |
'#description' => t('Should this website show the default microsumaries.'),
|
| 96 |
);
|
| 97 |
|
| 98 |
$form["general"]["microsummary_default_inclusion"] = array('#type' => 'checkbox',
|
| 99 |
'#title' => t('Default linking method'),
|
| 100 |
'#default_value' => variable_get('microsummary_default_inclusion', TRUE),
|
| 101 |
'#description' => t("Using microsummary's default inclusion method is easier but may be a performance problem on busy websites. If you use the block method then you should shut this off to remove duplicate microsummaries."),
|
| 102 |
);
|
| 103 |
|
| 104 |
|
| 105 |
$form["general"]["microsummary_nids"] = array('#type' => 'textarea',
|
| 106 |
'#title' => t('Microsummary nodes'), '#default_value' => variable_get('microsummary_nids', ''),
|
| 107 |
'#description' => t('List of node IDs that have php bodies designed specifically to provide a microsummary. Separate each node id with commas like "2,5,7" (but without the quotes).')
|
| 108 |
);
|
| 109 |
|
| 110 |
$form["general"]["microsummary_prefix"] = array('#type' => 'textfield',
|
| 111 |
'#title' => t('Microsummary prefix'), '#size' => 20, '#maxlength' => 30,
|
| 112 |
'#default_value' => variable_get('microsummary_prefix', ''),
|
| 113 |
'#description' => t('An optional prefix for the microsummary. This value will be placed at the beginning of the microsummary bookmark. Firefox shows the user as few as the first 24 characters so it is recommended to keep this value short.'),
|
| 114 |
);
|
| 115 |
|
| 116 |
$form["general"]["microsummary_suffix"] = array('#type' => 'textfield',
|
| 117 |
'#title' => t('Microsummary suffix'), '#size' => 20, '#maxlength' => 30,
|
| 118 |
'#default_value' => variable_get('microsummary_suffix', ''),
|
| 119 |
'#description' => t('An optional suffix for the microsummary. This value will be placed at the end of the microsummary bookmark. Firefox shows the user as few as the first 24 characters so this value may not be seen.'),
|
| 120 |
);
|
| 121 |
|
| 122 |
}
|
| 123 |
return system_settings_form($form);
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
* callback for microsummary pages
|
| 128 |
*
|
| 129 |
*/
|
| 130 |
function microsummary_page($type) {
|
| 131 |
|
| 132 |
global $user;
|
| 133 |
switch ($type) {
|
| 134 |
case 'posts':
|
| 135 |
$node_count = 0;
|
| 136 |
|
| 137 |
//get some information about content
|
| 138 |
if (!$user->uid) {
|
| 139 |
//return count new/updated nodes in last week
|
| 140 |
$new = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT n.nid) FROM {node} n WHERE n.created > %d"), time() - 30 * 24 * 60 * 60));
|
| 141 |
$updated = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT n.nid) FROM {node} n WHERE n.changed > %d"), time() - 30 * 24 * 60 * 60));
|
| 142 |
}
|
| 143 |
else {
|
| 144 |
//return count new/updated nodes for this user
|
| 145 |
$new = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT n.nid) FROM {node} n LEFT OUTER JOIN {history} h ON n.nid = h.nid AND h.uid = %d WHERE h.nid IS NULL"), $user->uid));
|
| 146 |
$updated = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT n.nid) FROM {node} n INNER JOIN {history} h ON n.nid = h.nid WHERE h.uid = %d AND h.timestamp < n.changed"), $user->uid));
|
| 147 |
//TODO: return count new comments for this user
|
| 148 |
}
|
| 149 |
$output = t('posts %new new %updated updated',
|
| 150 |
array('%new' => $new, '%updated' => $updated));
|
| 151 |
|
| 152 |
break;
|
| 153 |
case 'users':
|
| 154 |
//get some status on users
|
| 155 |
$registered = db_result(db_query(db_rewrite_sql("SELECT COUNT(uid) FROM {users} WHERE status = 1;")));
|
| 156 |
$active = db_result(db_query(db_rewrite_sql("SELECT COUNT(uid) FROM {users} WHERE login > 0")));
|
| 157 |
|
| 158 |
$output = t('users: %registered registered %active active',
|
| 159 |
array('%new' => $new, '%updated' => $updated , '%registered' => $registered , '%active' => $active));
|
| 160 |
break;
|
| 161 |
|
| 162 |
case 'blog':
|
| 163 |
$rec = db_fetch_object(db_query("SELECT title, created FROM {node} WHERE status = 1 ORDER BY created DESC LIMIT 1"));
|
| 164 |
$posted = format_date($rec->created, 'custom', variable_get('date_format_short', 'm/d/Y - H:i'));
|
| 165 |
$output = t('%title posted: %posted', array('%title' => $rec->title, '%posted' => $posted));
|
| 166 |
break;
|
| 167 |
|
| 168 |
case 'node':
|
| 169 |
$node = node_load(arg(1));
|
| 170 |
$output = str_replace(array("\r\n", "\n", "\r"), "", check_markup($node->body, $node->format, FALSE));
|
| 171 |
break;
|
| 172 |
default:
|
| 173 |
$output = t('Whoops - Configure your microsummaries!');
|
| 174 |
}
|
| 175 |
print variable_get('microsummary_prefix', '') . $output . variable_get('microsummary_suffix', '');
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
* Implementation for hook_block
|
| 180 |
*/
|
| 181 |
function microsummary_block($op = 'list', $delta = 0) {
|
| 182 |
global $user;
|
| 183 |
if ($op == 'list') {
|
| 184 |
$block[0]['info'] = t('Microsummary block');
|
| 185 |
return $block;
|
| 186 |
}
|
| 187 |
else if ($op == 'view') {
|
| 188 |
switch ($delta) {
|
| 189 |
// Shows microsummary block
|
| 190 |
case 0 :
|
| 191 |
$block['content'] = microsummary_get_links();
|
| 192 |
$block['subject'] = '';
|
| 193 |
|
| 194 |
return $block;
|
| 195 |
|
| 196 |
break;
|
| 197 |
}
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
/**
|
| 202 |
* function used to insert the microsummary links into the page
|
| 203 |
*
|
| 204 |
* @param return
|
| 205 |
* HTML formatted content suitable to be inserted in a block or theme
|
| 206 |
*/
|
| 207 |
function microsummary_get_links() {
|
| 208 |
$output = '';
|
| 209 |
$nids = variable_get('microsummary_nids', FALSE);
|
| 210 |
|
| 211 |
//Only parse this data if we need it.
|
| 212 |
if (variable_get('microsummary_show_defaults', TRUE) || $nids) {
|
| 213 |
$protocol = 'http://';
|
| 214 |
|
| 215 |
// Are we on a secure page?
|
| 216 |
if ($_SERVER['HTTPS']) {
|
| 217 |
$protocol = 'https://';
|
| 218 |
}
|
| 219 |
|
| 220 |
$protocol = 'http://'; //should microsummaries ever be on https? TODO: enable that.
|
| 221 |
$host = getenv("SERVER_NAME");
|
| 222 |
if (variable_get('clean_url', 0)==0) {
|
| 223 |
$url_prefix = $protocol . $host . base_path() .'?q=';
|
| 224 |
}
|
| 225 |
else {
|
| 226 |
$url_prefix = $protocol . $host . base_path();
|
| 227 |
}
|
| 228 |
}
|
| 229 |
|
| 230 |
// Only show the default microsummaries if they are enabled.
|
| 231 |
if (variable_get('microsummary_show_defaults', TRUE)) {
|
| 232 |
$output .= '<link rel="microsummary" href="'. url($url_prefix .'microsummary_posts') .'" />';
|
| 233 |
$output .= '<link rel="microsummary" href="'. url($url_prefix .'microsummary_users') .'" />';
|
| 234 |
$output .= '<link rel="microsummary" href="'. url($url_prefix .'microsummary_latest_blog') .'" />';
|
| 235 |
}
|
| 236 |
|
| 237 |
// If there are nodes configured for microsummary, advertise those.
|
| 238 |
if ($nids) {
|
| 239 |
foreach (explode(',', $nids) as $nid) {
|
| 240 |
$output .= '<link rel="microsummary" href="'. url($url_prefix .'microsummary/'. $nid) .'" />';
|
| 241 |
}
|
| 242 |
}
|
| 243 |
return $output;
|
| 244 |
}
|