'admin/settings/bookmarks',
'title' => t('Bookmark settings'),
'description' => t('Settings per content type.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'bookmarks_settings',
'type' => MENU_NORMAL_ITEM,
);
$access = user_access('access bookmarks');
// Main menu item
$items[] = array(
'path' => "bookmarks/$user->uid",
'title' => t('my bookmarks'),
'callback' => 'bookmarks_page',
'access' => $access);
// Top level tabs
$items[] = array(
'path' => "bookmarks/$user->uid/overview",
'title' => t('Bookmarks'),
'access' => $access,
'weight' => -10,
'type' => MENU_DEFAULT_LOCAL_TASK);
$items[] = array(
'path' => "bookmarks/$user->uid/add",
'title' => t('add bookmark'),
'callback' => 'bookmarks_page',
'access' => $access,
'weight' => 10,
'type' => MENU_LOCAL_TASK);
}
return $items;
}
/**
* Implementation of hook_perm().
*/
function bookmarks_perm() {
return array('access bookmarks');
}
/********************************************************************
* Theme Hooks
********************************************************************/
/**
@addtogroup themeable
Bookmarks module specific theme functions.
@{
**/
/**
Returns an user's bookmarks block.
@return array the paramter to pass to the block function.
**/
function theme_bookmarks_block() {
global $user;
// Do not let anonymous users have bookmarks, even if the admin decides this
if ($user->uid != 0 && user_access('access bookmarks')) {
$result = db_query('SELECT url, title FROM {bookmarks} WHERE uid = %d ORDER BY title', $user->uid);
$bookmarks = array();
while ($data = db_fetch_object($result)) {
$bookmarks[] = '
'. theme('bookmarks_delete', $data->url) .'
'. _bookmarks_get_link($data->url, $data->title);
}
// Print bookmarks list as an item list
$output = (count($bookmarks) ? theme('item_list', $bookmarks) : t('You have no bookmarks.'));
$links['bookmarks_quick_link'] = array(
'title' => t("quick link"),
'href' => "bookmarks/$user->uid/add/quick",
'attributes' => array('title' => t('Bookmark the current page.')),
'query' => 'title='. urlencode(drupal_get_title()),
);
$links['bookmarks_manage'] = array (
'title' => t("manage"),
'href' => "bookmarks/$user->uid"
);
$output .= ''. theme("links", $links) .'
';
return array(
'subject' => t('%username\'s bookmarks', array('%username' => $user->name)),
'content' => $output
);
}
// Not a logged in user or has no rights
else {
return FALSE;
}
}
/**
* Implementation of hook_settings()
* DG NEW fumction for admin settings
*/
function bookmarks_settings() {
$form = array();
$form['nodetypes'] = array(
'#type' => 'fieldset',
'#title' => t('Enable bookmarks links for these content types'),
);
foreach(node_get_types() as $type => $name) {
$form['nodetypes'][BOOKMARK_NODE_TYPE . $type] = array(
'#type' => 'checkbox',
'#title' => $name->type,
'#return_value' => 1,
'#default_value' => variable_get(BOOKMARK_NODE_TYPE . $type, 0),
);
}
return system_settings_form($form);
}
/**
* Implementation of hook_link().
*/
function bookmarks_link($type, $node = NULL, $teaser = FALSE) {
global $user;
$userid = $user->uid;
//First we have a look at our database to find out what we have
$result = db_query('SELECT url, title FROM {bookmarks} WHERE uid = %d ORDER BY title', $user->uid );
while ($data = db_fetch_object($result)) {
//$data = db_fetch_object($result);
$bookmarktitle = $data->title;
$bookmarkurl = $data->url;
}
$links = array();
// Create link for nodes not bookmarked yet
if ($type == 'node' && !$teaser && (variable_get(BOOKMARK_NODE_TYPE . $node->type, 0)) && $node->title != $bookmarktitle ) {
$links['bookmarks_quick_link_add'] = array(
'title' => t('bookmark'),
'href' => 'bookmarks/'.$userid. '/add/quick',
'attributes' => array('class' => 'iconBookmark', 'title' => t('Click to bookmark this content'), 'rel' => 'nofollow',),
'query' => 'title='. urlencode(drupal_get_title())
);
}
// Create link for bookmarked nodes
if ($type == 'node' && !$teaser && (variable_get(BOOKMARK_NODE_TYPE . $node->type, 0)) && $node->title == $bookmarktitle ) {
$links['bookmarks_quick_link_delete'] = array(
'title' => t('unbookmark'),
'href' => 'bookmarks/'.$userid. '/delete',
'attributes' => array('class' => 'iconUnbookmark', 'title' => t('Click to unbookmark this content'), 'rel' => 'nofollow',),
'query' => 'url=' . ($bookmarkurl)
);
}
return $links ;
}
/**
Returns a bookmarks delete icon.
@param bookmark_url The URL of the page to remove
@return string the delete icon to be emitted
**/
function theme_bookmarks_delete($url) {
global $user;
$query = 'url=' . urlencode($url). '&block=1';
return l(theme('image', drupal_get_path("module","bookmarks") .'/trash.gif', t('delete')), "bookmarks/$user->uid/delete", array("title" => t("Delete this bookmark from your list.")), $query, NULL, FALSE, TRUE);
}
/** @} End of addtogroup themeable **/
/********************************************************************
* Module Functions
********************************************************************/
/**
* The controller for managing bookmarks. Callback happens via menu().
*
* @return string Completely themed HTML page.
*/
function bookmarks_page() {
global $user;
$edit = $_POST;
$op = $_POST['op'];
switch (($op ? $op : arg(2))) {
case 'add':
$title = t('Create new bookmark');
if (arg(3) == 'quick') {
$edit = bookmarks_load_quicklink();
}
elseif (arg(3) == 'weblink') {
$edit = bookmarks_load_weblink(arg(4));
}
$output = drupal_get_form('bookmarks_form', $edit);
break;
case 'edit':
$title = t('Edit bookmark');
$output = ($url = urldecode($_GET['url'])) ? drupal_get_form('bookmarks_form',bookmarks_load($url)) : drupal_set_message(t('Bookmark cannot be edited, because it is not in your list.'), 'error');
break;
case 'delete':
$output = ($url = urldecode($_GET['url'])) ? bookmarks_delete($url) : drupal_set_message(t('Bookmark cannot be deleted, because no URL was specified.'), 'error');
break;
case t('Save'):
$title = t('Bookmarks');
if (bookmarks_validate($edit)) {
bookmarks_save($edit);
drupal_goto("bookmarks/$user->uid");
}
else {
$output = drupal_get_form('bookmarks_form', $edit);
}
break;
default:
$output = theme_bookmarks_overview();
}
drupal_set_title($title);
print theme('page', $output);
}
function bookmarks_delete($url) {
global $user;
db_query("DELETE FROM {bookmarks} WHERE uid = %d AND url = '%s'", $user->uid, $url);
drupal_set_message(t('Deleted bookmark.'));
if ($_GET['block']) { //User has most likely clicked delete via their bookmark block. Show them their originating page.
drupal_goto(bookmarks_get_canonical_path($url));
}
else {
return theme_bookmarks_overview();
}
}
function bookmarks_form($edit = null) {
$form['details'] = array(
'#type' => 'fieldset',
'#title' => t('Bookmark details'),
'#collapsible' => TRUE,
'#attributes' => array('class' => 'collapsible'),
);
$form['details']['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $edit['title'],
'#size' => 60,
'#maxlength' => 128,
'#description' => null,
'#attributes' => null,
'#required' => true,
);
$form['details']['type'] = array(
'#type' => 'hidden',
'#title' => t('Node type'),
'#default_value' => $edit['type'],
'#size' => 60,
'#maxlength' => 50,
'#description' => null,
'#attributes' => null,
'#required' => false,
);
$form['details']['teaser'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $edit['teaser'],
'#size' => 60,
'#rows' => 5,
'#maxlength' => 155,
'#description' => null,
'#attributes' => null,
'#required' => false,
);
$form['details']['url'] = array(
'#type' => 'textfield',
'#title' => t('URL'),
'#default_value' => $edit['url'],
'#size' => 60,
'#maxlength' => 128,
'#description' => t('Enter the address of the page you want to bookmark. This can be an internal or external reference. External bookmarks start with http://'),
'#attributes' => null,
'#required' => true,
);
$form['details']['old_url'] = array(
'#type' => 'hidden',
'#value' => urlencode($edit['url']),
);
$form['details']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function bookmarks_load($url) {
global $user;
return db_fetch_array(db_query("SELECT * FROM {bookmarks} WHERE uid = %d AND url = '%s'", $user->uid, $url));
}
function bookmarks_load_quicklink() {
$edit['url'] = bookmarks_get_canonical_path($url);
$explicit_uri = drupal_get_normal_path($edit['url']);
$uri_ary = explode('/', $explicit_uri);
$type = $uri_ary[0];
foreach ($uri_ary as $value) {
if (is_numeric($value)) {
$id = $value;
break;
}
}
/* Only display quicklink title for internal URLs */
if (is_numeric($id) && !strstr($edit['url'], 'http://')) {
switch ($type) {
case 'node':
$edit['title'] = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $id));
$edit['type'] = db_result(db_query('SELECT type FROM {node} WHERE nid = %d', $id));
$edit['teaser'] = db_result(db_query('SELECT teaser FROM {node_revisions} WHERE nid = %d', $id));
break;
case 'comment':
$edit['title'] = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $id));
break;
case 'user':
$edit['title'] = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $id));
break;
default:
/* Leave the title blank. The user will be prompted to enter a title. */
}
}
if (!$edit['title']) {
$edit['title'] = urldecode($_GET['title']);
}
return $edit;
}
function bookmarks_load_weblink($nid) {
return db_fetch_array(db_query('SELECT n.title, w.weblink url FROM {node} n, {weblink} w WHERE n.nid = w.nid AND n.nid = %d', $nid));
}
function theme_bookmarks_overview() {
global $user;
$header = array(
array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
array('data' => t('Node type'), 'field' => 'type'),
array('data' => t('Description'), 'field' => 'url'),
array('data' => t('Operations'), 'colspan' => 10)
);
$sql = 'SELECT url, title, type, teaser FROM {bookmarks} WHERE uid = '. db_escape_string($user->uid). tablesort_sql($header);
$result = pager_query($sql, 50);
while ($data = db_fetch_object($result)) {
$title = (strlen($data->url) > 50) ? substr($data->url, 0, 47). '...' : $data->url;
$rows[] = array(l($data->title, $data->url), $data->type , $data->teaser, ' '.l(t('edit'), "bookmarks/$user->uid/edit", null, 'url=' . urlencode($data->url)).' '. l(t('delete'), "bookmarks/$user->uid/delete", null, 'url=' . urlencode($data->url)));
}
$pager = theme('pager', null, 50, 0);
if (!empty($pager)) {
$rows[] = array(array('data' => $pager, 'colspan' => 3));
}
$output .= (count($rows) == 0) ? t('You have no bookmarks.') : theme('table', $header, $rows);
return $output;
}
function bookmarks_save($edit) {
global $user;
$edit['old_url'] = urldecode($edit['old_url']);
if ($edit['old_url'] && db_result(db_query("SELECT COUNT(uid) FROM {bookmarks} WHERE uid = %d AND url = '%s'", $user->uid, $edit['old_url']))) {
db_query("UPDATE {bookmarks} SET title = '%s', type = '%s', teaser = '%s', url = '%s' WHERE uid = %d AND url = '%s'", $edit['title'], $edit['type'], $edit['teaser'], $edit['url'], $user->uid, $edit['old_url']);
drupal_set_message(t('The bookmark has been updated.'));
}
else {
db_query("INSERT INTO {bookmarks} (uid, url, title, type, teaser) VALUES (%d, '%s', '%s', '%s', '%s')", $user->uid, $edit['url'], $edit['title'], $edit['type'], $edit['teaser']);
drupal_set_message(t('The link has been added to your bookmarks.'));
}
}
function bookmarks_validate($edit) {
$errors = array();
if (isset($edit['title']) && !$edit['title']) {
$errors['title'] = t('You must supply a title.');
}
if (isset($edit['url']) && !$edit['url']) {
$errors['url'] = t('You must supply an URL.');
}
foreach ($errors as $name => $message) {
form_set_error($name, $message);
}
return count($errors) == 0;
}
/**
* Return a relative URL that Drupal can recognize internally.
*/
function bookmarks_get_canonical_path($url) {
global $base_url;
$c_path = ltrim(str_replace($base_url, '', trim(referer_uri(), '/')), '/');
$c_path = str_replace('index.php', '', $c_path);
$c_path = str_replace('?q=', '', $c_path);
return $c_path;
}
/**
* Generate a link based on the URL being internal or external
*/
function _bookmarks_get_link($url, $title) {
return (preg_match("!^[a-zA-Z]+://!", $url)) ? "$title" : l($title, $url);
}