| 1 |
<?php
|
| 2 |
// $Id: bookmarks.module,v 1.37.2.3 2006/04/13 15:45:05 unconed Exp $
|
| 3 |
|
| 4 |
/********************************************************************
|
| 5 |
* Drupal Hooks
|
| 6 |
********************************************************************/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_block().
|
| 10 |
*/
|
| 11 |
function bookmarks_block($op = "list", $delta = 0) {
|
| 12 |
if ($op == 'list') {
|
| 13 |
$blocks[0]['info'] = t('User bookmarks');
|
| 14 |
return $blocks;
|
| 15 |
}
|
| 16 |
elseif ($op == 'view') {
|
| 17 |
switch($delta) {
|
| 18 |
case 0:
|
| 19 |
return theme('bookmarks_block');
|
| 20 |
break;
|
| 21 |
}
|
| 22 |
return $block;
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implementation of hook_help().
|
| 28 |
*/
|
| 29 |
function bookmarks_help($section) {
|
| 30 |
switch ($section) {
|
| 31 |
case 'admin/modules#description':
|
| 32 |
return t("Enables users to bookmark any page on the site.");
|
| 33 |
|
| 34 |
case 'user/bookmarks':
|
| 35 |
return t('A bookmark is a link to an address (URL) on the internet.');
|
| 36 |
break;
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
/**
|
| 41 |
* Implementation of hook_menu().
|
| 42 |
*/
|
| 43 |
function bookmarks_menu($may_cache) {
|
| 44 |
global $user;
|
| 45 |
$items = array();
|
| 46 |
$access = user_access('access bookmarks');
|
| 47 |
|
| 48 |
// Main menu item
|
| 49 |
$items[] = array('path' => "bookmarks/$user->uid", 'title' => t('my bookmarks'),
|
| 50 |
'callback' => 'bookmarks_page', 'access' => $access);
|
| 51 |
|
| 52 |
// Top level tabs
|
| 53 |
$items[] = array('path' => "bookmarks/$user->uid/overview", 'title' => t('list'),
|
| 54 |
'access' => $access, 'weight' => -10, 'type' => MENU_DEFAULT_LOCAL_TASK);
|
| 55 |
$items[] = array('path' => "bookmarks/$user->uid/add", 'title' => t('add bookmark'),
|
| 56 |
'callback' => 'bookmarks_page', 'access' => $access, 'weight' => 10,
|
| 57 |
'type' => MENU_LOCAL_TASK);
|
| 58 |
|
| 59 |
return $items;
|
| 60 |
}
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Implementation of hook_perm().
|
| 64 |
*/
|
| 65 |
function bookmarks_perm() {
|
| 66 |
return array('access bookmarks');
|
| 67 |
}
|
| 68 |
|
| 69 |
/********************************************************************
|
| 70 |
* Theme Hooks
|
| 71 |
********************************************************************/
|
| 72 |
|
| 73 |
/**
|
| 74 |
@addtogroup themeable
|
| 75 |
|
| 76 |
Bookmarks module specific theme functions.
|
| 77 |
@{
|
| 78 |
**/
|
| 79 |
|
| 80 |
/**
|
| 81 |
Returns an user's bookmarks block.
|
| 82 |
|
| 83 |
@return array the paramter to pass to the block function.
|
| 84 |
**/
|
| 85 |
|
| 86 |
function theme_bookmarks_block() {
|
| 87 |
|
| 88 |
global $user;
|
| 89 |
|
| 90 |
// Do not let anonymous users have bookmarks, even if the admin decides this
|
| 91 |
if ($user->uid != 0 && user_access('access bookmarks')) {
|
| 92 |
|
| 93 |
$result = db_query('SELECT url, title FROM {bookmarks} WHERE uid = %d ORDER BY title', $user->uid);
|
| 94 |
$bookmarks = array();
|
| 95 |
while ($data = db_fetch_object($result)) {
|
| 96 |
$bookmarks[] = '<div class="icon">'. theme('bookmarks_delete', $data->url) .'</div>'. _bookmarks_get_link($data->url, $data->title);
|
| 97 |
}
|
| 98 |
|
| 99 |
// Print bookmarks list as an item list
|
| 100 |
$output = (count($bookmarks) ? theme('item_list', $bookmarks) : t('You have no bookmarks.'));
|
| 101 |
$links['bookmarks_quick_link'] = array(
|
| 102 |
'title' => t("quick link"),
|
| 103 |
'href' => "bookmarks/$user->uid/add/quick",
|
| 104 |
'attributes' => array('title' => t('Bookmark the current page.')), 'title='. urlencode(drupal_get_title())
|
| 105 |
);
|
| 106 |
|
| 107 |
$links['bookmarks_manage'] = array (
|
| 108 |
'title' => t("manage"),
|
| 109 |
'href' => "bookmarks/$user->uid"
|
| 110 |
);
|
| 111 |
|
| 112 |
$output .= '<div class="links">'. theme("links", $links) .'</div>';
|
| 113 |
|
| 114 |
return array(
|
| 115 |
'subject' => t('%username\'s bookmarks', array('%username' => $user->name)),
|
| 116 |
'content' => $output
|
| 117 |
);
|
| 118 |
|
| 119 |
}
|
| 120 |
|
| 121 |
// Not a logged in user or has no rights
|
| 122 |
else {
|
| 123 |
return FALSE;
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
Returns a bookmarks delete icon.
|
| 129 |
|
| 130 |
@param bookmark_url The URL of the page to remove
|
| 131 |
@return string the delete icon to be emitted
|
| 132 |
**/
|
| 133 |
function theme_bookmarks_delete($url) {
|
| 134 |
global $user;
|
| 135 |
|
| 136 |
$query = 'url=' . urlencode($url). '&block=1';
|
| 137 |
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);
|
| 138 |
}
|
| 139 |
/** @} End of addtogroup themeable **/
|
| 140 |
|
| 141 |
/********************************************************************
|
| 142 |
* Module Functions
|
| 143 |
********************************************************************/
|
| 144 |
|
| 145 |
/**
|
| 146 |
* The controller for managing bookmarks. Callback happens via menu().
|
| 147 |
*
|
| 148 |
* @return string Completely themed HTML page.
|
| 149 |
*/
|
| 150 |
function bookmarks_page() {
|
| 151 |
global $user;
|
| 152 |
|
| 153 |
$edit = $_POST;
|
| 154 |
$op = $_POST['op'];
|
| 155 |
|
| 156 |
switch (($op ? $op : arg(2))) {
|
| 157 |
|
| 158 |
case 'add':
|
| 159 |
$title = t('Create new bookmark');
|
| 160 |
if (arg(3) == 'quick') {
|
| 161 |
$edit = bookmarks_load_quicklink();
|
| 162 |
}
|
| 163 |
elseif (arg(3) == 'weblink') {
|
| 164 |
$edit = bookmarks_load_weblink(arg(4));
|
| 165 |
}
|
| 166 |
$output = drupal_get_form('bookmarks_form', $edit);
|
| 167 |
break;
|
| 168 |
|
| 169 |
case 'edit':
|
| 170 |
$title = t('Edit bookmark');
|
| 171 |
$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');
|
| 172 |
break;
|
| 173 |
|
| 174 |
case 'delete':
|
| 175 |
$output = ($url = urldecode($_GET['url'])) ? bookmarks_delete($url) : drupal_set_message(t('Bookmark cannot be deleted, because no URL was specified.'), 'error');
|
| 176 |
break;
|
| 177 |
|
| 178 |
case t('Save'):
|
| 179 |
$title = t('Bookmarks');
|
| 180 |
if (bookmarks_validate($edit)) {
|
| 181 |
bookmarks_save($edit);
|
| 182 |
drupal_goto("bookmarks/$user->uid");
|
| 183 |
}
|
| 184 |
else {
|
| 185 |
$output = drupal_get_form('bookmarks_form', $edit);
|
| 186 |
}
|
| 187 |
break;
|
| 188 |
|
| 189 |
default:
|
| 190 |
$output = bookmarks_overview();
|
| 191 |
}
|
| 192 |
|
| 193 |
drupal_set_title($title);
|
| 194 |
print theme('page', $output);
|
| 195 |
}
|
| 196 |
|
| 197 |
function bookmarks_delete($url) {
|
| 198 |
global $user;
|
| 199 |
|
| 200 |
db_query("DELETE FROM {bookmarks} WHERE uid = %d AND url = '%s'", $user->uid, $url);
|
| 201 |
drupal_set_message(t('Deleted bookmark.'));
|
| 202 |
|
| 203 |
if ($_GET['block']) { //User has most likely clicked delete via their bookmark block. Show them their originating page.
|
| 204 |
drupal_goto(bookmarks_get_canonical_path($url));
|
| 205 |
}
|
| 206 |
else {
|
| 207 |
return bookmarks_overview();
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
function bookmarks_form($edit = null) {
|
| 212 |
$form['details'] = array(
|
| 213 |
'#type' => 'fieldset',
|
| 214 |
'#title' => t('Bookmark details'),
|
| 215 |
);
|
| 216 |
$form['details']['title'] = array(
|
| 217 |
'#type' => 'textfield',
|
| 218 |
'#title' => t('Title'),
|
| 219 |
'#default_value' => $edit['title'],
|
| 220 |
'#size' => 60,
|
| 221 |
'#maxlength' => 128,
|
| 222 |
'#description' => null,
|
| 223 |
'#attributes' => null,
|
| 224 |
'#required' => true,
|
| 225 |
);
|
| 226 |
$form['details']['url'] = array(
|
| 227 |
'#type' => 'textfield',
|
| 228 |
'#title' => t('URL'),
|
| 229 |
'#default_value' => $edit['url'],
|
| 230 |
'#size' => 60,
|
| 231 |
'#maxlength' => 128,
|
| 232 |
'#description' => t('Enter the address of the page you want to bookmark. This can be an internal or external reference.'),
|
| 233 |
'#attributes' => null,
|
| 234 |
'#required' => true,
|
| 235 |
);
|
| 236 |
$form['details']['old_url'] = array(
|
| 237 |
'#type' => 'hidden',
|
| 238 |
'#value' => urlencode($edit['url']),
|
| 239 |
);
|
| 240 |
$form['details']['submit'] = array(
|
| 241 |
'#type' => 'submit',
|
| 242 |
'#value' => t('Save'),
|
| 243 |
);
|
| 244 |
return $form;
|
| 245 |
}
|
| 246 |
|
| 247 |
function bookmarks_load($url) {
|
| 248 |
global $user;
|
| 249 |
return db_fetch_array(db_query("SELECT * FROM {bookmarks} WHERE uid = %d AND url = '%s'", $user->uid, $url));
|
| 250 |
}
|
| 251 |
|
| 252 |
function bookmarks_load_quicklink() {
|
| 253 |
|
| 254 |
$edit['url'] = bookmarks_get_canonical_path($url);
|
| 255 |
$explicit_uri = drupal_get_normal_path($edit['url']);
|
| 256 |
|
| 257 |
$uri_ary = explode('/', $explicit_uri);
|
| 258 |
$type = $uri_ary[0];
|
| 259 |
foreach ($uri_ary as $value) {
|
| 260 |
if (is_numeric($value)) {
|
| 261 |
$id = $value;
|
| 262 |
break;
|
| 263 |
}
|
| 264 |
}
|
| 265 |
|
| 266 |
/* Only display quicklink title for internal URLs */
|
| 267 |
if (is_numeric($id) && !strstr($edit['url'], 'http://')) {
|
| 268 |
switch ($type) {
|
| 269 |
case 'node':
|
| 270 |
$edit['title'] = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $id));
|
| 271 |
break;
|
| 272 |
case 'comment':
|
| 273 |
$edit['title'] = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $id));
|
| 274 |
break;
|
| 275 |
case 'user':
|
| 276 |
$edit['title'] = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $id));
|
| 277 |
break;
|
| 278 |
default:
|
| 279 |
/* Leave the title blank. The user will be prompted to enter a title. */
|
| 280 |
}
|
| 281 |
}
|
| 282 |
|
| 283 |
if (!$edit['title']) {
|
| 284 |
$edit['title'] = urldecode($_GET['title']);
|
| 285 |
}
|
| 286 |
|
| 287 |
return $edit;
|
| 288 |
}
|
| 289 |
|
| 290 |
function bookmarks_load_weblink($nid) {
|
| 291 |
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));
|
| 292 |
}
|
| 293 |
|
| 294 |
function bookmarks_overview() {
|
| 295 |
|
| 296 |
global $user;
|
| 297 |
|
| 298 |
$header = array(
|
| 299 |
array('data' => t('title'), 'field' => 'title', 'sort' => 'asc'),
|
| 300 |
array('data' => t('link'), 'field' => 'url'),
|
| 301 |
array('data' => t('operations'), 'colspan' => 2)
|
| 302 |
);
|
| 303 |
$sql = 'SELECT url, title FROM {bookmarks} WHERE uid = '. db_escape_string($user->uid). tablesort_sql($header);
|
| 304 |
$result = pager_query($sql, 50);
|
| 305 |
|
| 306 |
while ($data = db_fetch_object($result)) {
|
| 307 |
$title = (strlen($data->url) > 50) ? substr($data->url, 0, 47). '...' : $data->url;
|
| 308 |
$rows[] = array($data->title, _bookmarks_get_link($data->url, $title), ' '.l(t('edit'), "bookmarks/$user->uid/edit", null, 'url=' . urlencode($data->url)).' '. l(t('delete'), "bookmarks/$user->uid/delete", null, 'url=' . urlencode($data->url)));
|
| 309 |
}
|
| 310 |
|
| 311 |
$pager = theme('pager', null, 50, 0);
|
| 312 |
if (!empty($pager)) {
|
| 313 |
$rows[] = array(array('data' => $pager, 'colspan' => 3));
|
| 314 |
}
|
| 315 |
|
| 316 |
$output .= (count($rows) == 0) ? t('You have no bookmarks.') : theme('table', $header, $rows);
|
| 317 |
return $output;
|
| 318 |
}
|
| 319 |
|
| 320 |
function bookmarks_save($edit) {
|
| 321 |
global $user;
|
| 322 |
|
| 323 |
$edit['old_url'] = urldecode($edit['old_url']);
|
| 324 |
if ($edit['old_url'] && db_result(db_query("SELECT COUNT(uid) FROM {bookmarks} WHERE uid = %d AND url = '%s'", $user->uid, $edit['old_url']))) {
|
| 325 |
db_query("UPDATE {bookmarks} SET title = '%s', url = '%s' WHERE uid = %d AND url = '%s'", $edit['title'], $edit['url'], $user->uid, $edit['old_url']);
|
| 326 |
drupal_set_message(t('The bookmark has been updated.'));
|
| 327 |
}
|
| 328 |
else {
|
| 329 |
db_query("INSERT INTO {bookmarks} (uid, url, title) VALUES (%d, '%s', '%s')", $user->uid, $edit['url'], $edit['title']);
|
| 330 |
drupal_set_message(t('The link has been added to your bookmarks.'));
|
| 331 |
}
|
| 332 |
}
|
| 333 |
|
| 334 |
function bookmarks_validate($edit) {
|
| 335 |
$errors = array();
|
| 336 |
|
| 337 |
if (isset($edit['title']) && !$edit['title']) {
|
| 338 |
$errors['title'] = t('You must supply a title.');
|
| 339 |
}
|
| 340 |
if (isset($edit['url']) && !$edit['url']) {
|
| 341 |
$errors['url'] = t('You must supply an URL.');
|
| 342 |
}
|
| 343 |
|
| 344 |
foreach ($errors as $name => $message) {
|
| 345 |
form_set_error($name, $message);
|
| 346 |
}
|
| 347 |
return count($errors) == 0;
|
| 348 |
}
|
| 349 |
|
| 350 |
/**
|
| 351 |
* Return a relative URI that Drupal can recognize internally.
|
| 352 |
*/
|
| 353 |
function bookmarks_get_canonical_path($url) {
|
| 354 |
global $base_url;
|
| 355 |
|
| 356 |
$c_path = ltrim(str_replace($base_url, '', trim(referer_uri(), '/')), '/');
|
| 357 |
$c_path = str_replace('index.php', '', $c_path);
|
| 358 |
$c_path = str_replace('?q=', '', $c_path);
|
| 359 |
|
| 360 |
return $c_path;
|
| 361 |
}
|
| 362 |
|
| 363 |
/**
|
| 364 |
* Generate a link based on the URL being internal or external
|
| 365 |
*/
|
| 366 |
function _bookmarks_get_link($url, $title) {
|
| 367 |
return (preg_match("!^[a-zA-Z]+://!", $url)) ? "<a href=\"$url\">$title</a>" : l($title, $url);
|
| 368 |
}
|
| 369 |
|
| 370 |
?>
|