array('name' => t('userlink'), 'base' => 'userlink'));
}
/**
* Implementation of hook_perm().
*/
function userlink_perm() {
return array('create userlinks', 'view all userlinks', 'view own userlinks');
}
/**
* Implementation of hook_access().
*/
function userlink_access($op, $node) {
global $user;
if ($op == 'create') {
return user_access('create userlinks') && $user->uid;
}
if ($op == 'update' || $op == 'delete') {
if (user_access('create userlinks') && ($user->uid == $node->uid)) {
return TRUE;
}
}
}
/**
* Implementation of hook_user().
*/
function userlink_user($type, &$edit, &$user) {
if ($type == 'view' && user_access('view all userlinks', $user)) {
$items[] = array('title' => t('Links'),
'value' => l(t("View recent links."), "userlink/$user->uid", array('title' => t("Read %username's links.", array('%username' => $user->name)))),
'class' => 'userlink',
);
return array(t('History') => $items);
}
}
/**
* Implementation of hook_help().
*/
function userlink_help($section) {
switch ($section) {
case 'admin/help#userlink':
return t("
The userlink module allows registered users to maintain a list of links (or bookmarks).
A userlink is simply a URL along with a title and description.
");
case 'admin/modules#description':
return t('Enables storing and retrieving of links (or bookmarks).');
case 'node/add#userlink':
return t("A userlink is a URL along with a title and description.");
}
// Note that we can add some checks here to see if we want to put
// some help text on a dynamic page...
if ($section) {
}
}
/**
* Menu callback; displays a Drupal page containing recent blog entries.
*/
function userlink_page($a = NULL, $b = NULL, $c = NULL) {
if (is_numeric($a)) { // $a is a user ID
return userlink_page_user($a, $b, $c);
}
else {
return userlink_page_last();
}
}
function userlink_recent() {
if (arg(1)) {
if (is_numeric(arg(1))) {
return userlink_page_user(arg(1));
} else if (arg(1) == 'all') {
return userlink_page_last();
}
}
return 'no links to display';
}
/**
*
*/
function userlink_homepage() {
global $user;
if ($user->uid == 0) {
return userlink_page_last();
} else {
drupal_goto("userlink/$user->uid");
}
}
function userlink_page_termlinks($tid) {
return userlink_page_user(arg(1), 'term', $tid);
}
/**
* Displays a Drupal page containing userlink entries of a given user.
*/
function userlink_page_user($uid, $command = NULL, $tid = NULL) {
global $user;
if ($uid) {
if (is_numeric($uid)) {
$name = userlink_name_from_uid($uid);
}
$output = '';
if ($command == 'term') {
if (is_numeric($uid)) {
$result = pager_query(db_rewrite_sql("SELECT n.nid, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = %d AND n.type = 'userlink' AND n.uid = %d ORDER BY n.sticky DESC, n.created DESC"), variable_get('default_nodes_main', 10), 0, NULL, $tid, $uid);
} else {
$result = pager_query(db_rewrite_sql("SELECT n.nid, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = %d AND n.type = 'userlink' ORDER BY n.sticky DESC, n.created DESC"), variable_get('default_nodes_main', 10), 0, NULL, $tid);
}
if (db_num_rows($result)) {
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
}
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
}
else {
$output = $name ? t("%name has not created any userlinks.", array('%name' => $name)) : t("there are no userlinks in the system");
}
}
else {
if (($uid == $user->uid) && user_access('create userlinks')) {
$output .= '- ' . l(t('Add new link.'), "node/add/userlink") .'
';
}
$result = pager_query(db_rewrite_sql("SELECT n.nid, n.sticky, n.created FROM {node} n WHERE type = 'userlink' AND n.uid = %d ORDER BY n.sticky DESC, n.created DESC"), variable_get('default_nodes_main', 10), 0, NULL, $uid);
if (db_num_rows($result)) {
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
}
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
}
else {
$output = t("%name has not created any userlinks.", array('%name' => $name));
}
}
return $output;
}
else {
drupal_not_found();
}
}
/**
* Displays a Drupal page listing all users along with a link to their userlinks...
*/
function userlink_page_everyone() {
$output = "Click on a user's name to see his/her links, or " . l('click here', 'userlink/all') . " to see all links in the system.
\n";
$header = array(
array('data' => t('Username'), 'field' => 'u.name', 'sort' => 'asc'),
array('data' => t('# of links'), 'field' => 'numlinks')
);
$sql = 'SELECT u.uid AS uid, u.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid INNER JOIN {users} u ON n.uid = u.uid GROUP BY u.uid';
$sql .= tablesort_sql($header);
$sqlcount = 'SELECT COUNT(DISTINCT n.uid) FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid';
$result = pager_query($sql, 50, 0, $sqlcount);
while ($account = db_fetch_object($result)) {
$rows[] = array(l($account->name, 'userlink/' . $account->uid), $account->numlinks);
}
$output .= theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0);
return $output;
}
/**
* Displays a Drupal page containing recent userlink entries of all users.
*/
function userlink_page_last() {
global $user;
$output = '';
$result = pager_query(db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'userlink' ORDER BY n.created DESC"), variable_get('default_nodes_main', 10));
if (db_num_rows($result))
{
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
}
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
}
if (drupal_strlen($output) == 0)
{
$output = 'there are no links in the system';
}
return $output;
}
function userlink_page_links() {
$output = '';
// Add the javascript for collapsible fieldsets...
drupal_add_js('misc/collapse.js');
if (arg(1)) {
$uid = arg(1);
if (arg(1) == 'all') {
$sql = "SELECT n.nid AS nid, tn.tid AS tid, td.name AS name FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' ORDER BY name";
} else {
$sql = "SELECT n.nid AS nid, tn.tid AS tid, td.name AS name FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' AND n.uid = $uid ORDER BY name";
$name = userlink_name_from_uid($uid);
}
$result = db_query(db_rewrite_sql($sql));
$group = '';
$last_group = '';
$num_in_group = 0;
if (db_num_rows($result)) {
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
if ($row->name != $last_group) {
if ($last_group != '') {
$group .= "";
$output .= theme('fieldset', array('#title' => $last_group, '#children' => $group, '#attributes' => array('class' => 'collapsible userlink-group')));
}
$group = "\n";
$last_group = $row->name;
$num_in_group = 0;
}
$group .= '- ' . l($node->title, 'node/' . $node->nid) . "
\n";
$num_in_group++;
}
if ($num_in_group > 0) {
$group .= "
";
$output .= theme('fieldset', array('#title' => $last_group, '#children' => $group, '#attributes' => array('class' => 'collapsible userlink-group')));
}
}
}
if (drupal_strlen($output) == 0) {
$output = t('there are no links to display');
}
$output = '';
return $output;
}
function userlink_page_category() {
$header = array(
array('data' => t('Category'), 'field' => 'name'),
array('data' => t('# of links'), 'field' => 'numlinks', 'sort' => 'desc')
);
$uid = 0;
if (arg(1) && is_numeric(arg(1))) {
$uid = arg(1);
$sql = "SELECT tn.tid AS tid, td.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' AND n.uid = $uid GROUP BY tn.tid";
$sql .= tablesort_sql($header);
$sqlcount = "SELECT COUNT(DISTINCT tn.tid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' AND n.uid = $uid";
$name = userlink_name_from_uid($uid);
} else {
$sql = "SELECT tn.tid AS tid, td.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' GROUP BY tn.tid";
$sql .= tablesort_sql($header);
$sqlcount = "SELECT COUNT(DISTINCT tn.tid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink'";
}
$result = pager_query($sql, 50, 0, $sqlcount);
while ($category = db_fetch_object($result)) {
if ($uid) {
$url = 'userlink/' . $uid . '/term/' . $category->tid;
} else {
$url = 'userlink/all/term/' . $category->tid;
}
$rows[] = array(l($category->name, $url), $category->numlinks);
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0);
return $output;
}
/**
* Implementation of hook_form().
*/
function userlink_form(&$node) {
$form['url'] = array('#type' => 'textfield', '#title' => t('URL'), '#required' => TRUE, '#default_value' => $node->url ? $node->url : 'http://', '#description' => t('The URL for this link (usually begins with http://).'), '#weight' => -50);
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#description' => t('The title of this link.'), '#weight' => -30);
$form['body'] = array(
'#type' => 'textarea', '#title' => t('Description'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE, '#weight' => -20
);
return $form;
}
/**
* Implementation of hook_view().
*/
function userlink_view(&$node, $teaser = FALSE, $page = FALSE) {
if ($page) {
// Breadcrumb navigation
$breadcrumb[] = array('path' => 'userlink/all', 'title' => t('links'));
$breadcrumb[] = array('path' => 'userlink/'. $node->uid, 'title' => t("%name's links", array('%name' => $node->name)));
$breadcrumb[] = array('path' => 'node/'. $node->nid);
menu_set_location($breadcrumb);
}
$node = node_prepare($node, $teaser);
// Set the title_class, which is a FunnyMonkey added attribute of the node, so
// that the teaser title will get formatted properly (it gets referenced in
// node.tpl.php...
//
// Note that the way the title_class really gets set when $page is TRUE, is in
// userlink_load()...
$node->title_class = 'userlink-title';
// If the url is longer than X characters, just display the first X...
$url = userlink_trim_url($node->url);
// The link should always set a target window -- we are saving it
// on a site-wide basis at this point...
$atts = array('target' => variable_get('userlink_behavior', '_self'));
if ($teaser) {
$node->teaser = "" . l($url, $node->url, $atts) . "
" . $node->teaser;
}
else {
$node->body = "" . l($url, $node->url, $atts) . "
" . $node->body;
}
}
/**
* Implementation of hook_link().
*/
function userlink_link($type, $node = 0, $main = 0) {
/*
$links = array();
if ($type == 'node' && $node->type == 'blog') {
if (arg(0) != 'blog' || arg(1) != $node->uid) {
$links[] = l(t("%username's blog", array('%username' => $node->name)), "blog/$node->uid", array('title' => t("Read %username's latest blog entries.", array('%username' => $node->name))));
}
}
$links[] = l('shock the monkey tonight', 'http://www.funnymonkey.com/');
return $links;
*/
}
/**
* Implementation of hook_menu().
*/
function userlink_menu($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array('path' => 'node/add/userlink', 'title' => t('userlink entry'),
'access' => user_access('create userlinks'));
$items[] = array('path' => 'links', 'title' => t('links'),
'callback' => 'userlink_page',
'access' => user_access('access content'),
'type' => MENU_SUGGESTED_ITEM);
$items[] = array('path' => 'userlink/homepage', 'title' => t('recent links'),
'callback' => 'userlink_homepage',
'access' => user_access('view all userlinks') || user_access('view own userlinks'),
'type' => MENU_CALLBACK);
} else {
if (arg(0) == 'userlink') {
if (is_numeric(arg(1)) || (arg(1) == 'all')) {
$uid = arg(1);
$uname = is_numeric($uid) ? userlink_name_from_uid($uid) : null;
$title = $uname ? "$uname's links..." : "all links...";
$items[] = array('path' => 'userlink/'. $uid, 'title' => $title,
'callback' => 'userlink_page_links',
'access' => user_access('view all userlinks'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'userlink/' . $uid . '/list', 'title' => t('by category'),
'callback' => 'userlink_page_links',
'access' => user_access('view all userlinks'),
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -20);
$items[] = array('path' => 'userlink/'. $uid .'/categories', 'title' => t('just categories'),
'callback' => 'userlink_page_category',
'type' => MENU_LOCAL_TASK, 'weight' => -10);
$items[] = array('path' => 'userlink/' . $uid . '/everyone', 'title' => t("other users"),
'callback' => 'userlink_page_everyone',
'access' => user_access('view all userlinks'),
'type' => MENU_LOCAL_TASK, 'weight' => 20);
$items[] = array('path' => 'userlink/' . $uid . '/recent', 'title' => t('recent links'),
'callback' => 'userlink_recent',
'access' => user_access('view all userlinks'),
'type' => MENU_LOCAL_TASK, 'weight' => 10);
//
//
// If we are looking at a listing of links for a given term, add
// another tab for the listing...
//
//
if (arg(2) == 'term' && is_numeric(arg(3))) {
$term = taxonomy_get_term(arg(3));
$items[] = array('path' => 'userlink/'. $uid . '/term', 'title' => t("%who %termname links", array('%who' => $uname ? "$uname's" : 'all', '%termname' => $term->name)),
'callback' => 'userlink_page_termlinks',
'access' => user_access('view all userlinks'),
'type' => MENU_LOCAL_TASK, 'weight' => 80);
}
}
}
}
return $items;
}
/**
* Implementation of hook_block().
*
*/
function userlink_block($op = 'list', $delta = 0) {
global $user;
if ($op == 'list') {
$block[0]['info'] = t('All Links');
$block[1]['info'] = t('My Links');
$block[2]['info'] = t('Recent Links');
$block[3]['info'] = t('Popular Link Categories');
$block[4]['info'] = t("User's Link Categories");
$block[5]['info'] = t("Link Menu");
return $block;
}
else if ($op == 'view') {
if (($delta == 0) && user_access('view all userlinks')) {
$block['content'] = userlink_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, ul.url FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid WHERE n.type = 'userlink' ORDER BY n.title"), 0, 10));
$block['content'] .= ''. l(t('more'), 'links', array('title' => t('See all the links.'))) .'
';
$block['subject'] = t('All links');
return $block;
}
else if (($delta == 1) && $user->uid && user_access('view own userlinks')) {
$block['content'] = userlink_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, ul.url FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid WHERE n.type = 'userlink' AND n.uid = $user->uid ORDER BY n.title"), 0, 10));
$block['content'] .= ''. l(t('more'), 'userlink/'. $user->uid, array('title' => t('See all the links.'))) .'
';
$block['subject'] = t('My Links');
return $block;
}
else if (($delta == 2) && user_access('view all userlinks')) {
$block['content'] = userlink_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, ul.url FROM {node} n INNER JOIN {userlink} ul ON n.vid = ul.vid WHERE n.type = 'userlink' ORDER BY n.changed DESC"), 0, 10));
$block['content'] .= ''. l(t('more'), 'links', array('title' => t('See all the links.'))) .'
';
$block['subject'] = t('Recent Links');
return $block;
}
else if (($delta == 3) && user_access('view all userlinks')) {
$block['content'] = userlink_category_list(db_query_range(db_rewrite_sql("SELECT tn.tid AS tid, td.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.type = 'userlink' GROUP BY tn.tid ORDER BY numlinks DESC"), 0, 10));
$block['content'] .= ''. l(t('more'), 'userlink/all/categories', array('title' => t('See all the link categories.'))) .'
';
$block['subject'] = t('Popular Link Categories');
return $block;
}
else if (($delta == 4) && user_access('view all userlinks')) {
// This block shows "the target user's" userlinks. If we
// are on a page looking at some user's links, by category
// or otherwise, we want to show that user's categories.
//
// Also, if we are looking at a single userlink node, we
// want to show that node's owner's userlink categories.
if (arg(0) == 'userlink') {
if (is_numeric(arg(1)) || arg(1) == 'homepage') {
$uid = arg(1) == 'homepage' ? $user->uid : arg(1);
}
} else if ((arg(0) == 'node') && is_numeric(arg(1))) {
$node = node_load(arg(1));
if ($node->type == 'userlink') {
$uid = $node->uid;
}
}
if (!$uid || ($uid == 'all')) {
if ($user->uid) {
$uid = $user->uid;
}
}
if ($uid) {
$name = userlink_name_from_uid($uid);
$block['content'] = userlink_category_list(db_query_range(db_rewrite_sql("SELECT tn.tid AS tid, td.name AS name, COUNT(*) AS numlinks FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} td ON tn.tid = td.tid WHERE n.uid = $uid AND n.type = 'userlink' GROUP BY tn.tid ORDER BY numlinks DESC"), 0, 5), $uid, $name);
$block['content'] .= ''. l(t('more'), 'userlink/' . $uid, array('title' => t("See %name's link categories.", array('%name' => $name)))) .'
';
if ($uid == $user->uid) {
$block['subject'] = t("My Link Categories");
}
else {
$block['subject'] = t("$name's Link Categories");
}
return $block;
}
}
else if (($delta == 5) && user_access('access content')) {
if (user_access('create userlinks')) {
$items[] = l("Add a link", 'node/add/userlink');
}
if ($user->uid) {
$items[] = l('View my links', "userlink/$user->uid");
$items[] = l('View my categories', "userlink/$user->uid/categories");
}
if (user_access('view all userlinks')) {
$items[] = l("View everyone's links", 'userlink/all');
}
if (user_access('view all userlinks')) {
$items[] = l("View everyone's categories", 'userlink/all/categories');
}
$items[] = l('View all users', "userlink/all/everyone");
$block['content'] = theme('item_list', $items);
$block['subject'] = t('Link Menu');
return $block;
}
}
}
/**
* Implementation of hook_load().
*/
function userlink_load($node) {
$userlink = db_fetch_object(db_query("SELECT url, private FROM {userlink} WHERE vid = %d", $node->vid));
// Set the title_class so that the title can be formatted via CSS, both in node-listings
// and also when a single userlink node appears as the entire page...
//
// See node.tpl.php and page.tpl.php for where userlink-title is pushed out into the
// content...
$userlink->title_class = 'userlink-title';
return $userlink;
}
/**
* Implementation of hook_delete().
*/
function userlink_delete($node) {
db_query("DELETE FROM {userlink} WHERE nid = %d", $node->nid);
}
/**
* Implementation of hook_insert().
*/
function userlink_insert($node) {
db_query("INSERT INTO {userlink} (nid, vid, url, private) VALUES (%d, %d, '%s', %d)",
$node->nid, $node->vid, $node->url, $node->private);
}
/**
* Implementation of hook_update().
*/
function userlink_update($node) {
if ($node->revision) {
db_query("INSERT INTO {userlink} (nid, vid, url, private) VALUES (%d, %d, '%s', %d)",
$node->nid, $node->vid, $node->url, $node->private);
}
else {
db_query("UPDATE {userlink} SET url = '%s', private = %d WHERE vid = %d",
$node->url, $node->private, $node->vid);
}
}
/**
* Gather a listing of links to userlinks.
*
* @param $result
* A DB result object from a query to fetch node objects.
* @param $title
* A heading for the resulting list.
*
* @return
* An HTML list suitable as content for a block.
*/
function userlink_list($result, $title = NULL) {
// The link should always set a target window -- we are saving it
// on a site-wide basis at this point...
$atts = array('target' => variable_get('userlink_behavior', '_self'));
while ($node = db_fetch_object($result)) {
$atts['title'] = $node->url;
$items[] = l($node->title, $node->url, $atts);
}
return theme('item_list', $items, $title);
}
/**
* Gather a listing of links to userlink categories.
*
* @param $result
* A DB result object from a query to fetch node objects.
* @param $title
* A heading for the resulting list.
*
* @return
* An HTML list suitable as content for a block.
*/
function userlink_category_list($result, $uid = 0, $name = NULL) {
while ($category = db_fetch_object($result)) {
if ($uid) {
$url = 'userlink/' . $uid . '/term/' . $category->tid;
} else {
$url = 'userlink/all/term/' . $category->tid;
}
$items[] = l($category->name, $url) . " ($category->numlinks)";
}
return theme('item_list', $items);
}
/**
* Return list of vids that describe userlink entries...
*
*/
function userlink_vids() {
$a = array();
$result = db_query("SELECT vid FROM {vocabulary_node_types} WHERE type = 'userlink'");
while ($row = db_fetch_array($result)) {
$a[] = $row['vid'];
}
return $a;
}
function userlink_name_from_uid($uid) {
$result = db_query("SELECT name FROM {users} u WHERE u.uid = $uid");
return db_result($result);
}
function userlink_trim_url($url, $len = 70) {
if (drupal_strlen($url) > $len) {
$url = substr($url, 0, $len) . '...';
}
return $url;
}
function userlink_settings()
{
$form['userlink_behavior'] =
array('#type' => 'select',
'#options' => array('_self' => 'link appears in the same window',
'_blank' => 'link appears in its own target window',
'link-target' => 'link appears in a single target window'),
'#title' => t('What happens when a link is clicked'),
'#default_value' => variable_get('userlink_behavior', '_self'),
'#description' => t("The window where the linked page will appear after it is clicked on a user's page."));
return $form;
}
?>