| 1 |
<?php
|
| 2 |
// $Id: bookmarks.module,v 1.2 2006/02/15 17:04:49 deekayen Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Lets users keep bookmarks
|
| 7 |
*
|
| 8 |
* @todo There's some code/functionality duplication to eliminate
|
| 9 |
* Callbacks are underutilized
|
| 10 |
*/
|
| 11 |
|
| 12 |
/********************************************************************
|
| 13 |
* Drupal Hooks
|
| 14 |
********************************************************************/
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_block().
|
| 18 |
*/
|
| 19 |
function bookmarks_block($op = 'list', $delta = 0) {
|
| 20 |
if ($op == 'list') {
|
| 21 |
$blocks[0]['info'] = t('User bookmarks');
|
| 22 |
return $blocks;
|
| 23 |
}
|
| 24 |
elseif ($op == 'view') {
|
| 25 |
switch($delta) {
|
| 26 |
case 0:
|
| 27 |
return theme('bookmarks_block');
|
| 28 |
break;
|
| 29 |
}
|
| 30 |
return $block;
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of hook_help().
|
| 36 |
*/
|
| 37 |
function bookmarks_help($section) {
|
| 38 |
switch ($section) {
|
| 39 |
case 'admin/modules#description':
|
| 40 |
return t('Enables users to bookmark any page on the site.');
|
| 41 |
|
| 42 |
case 'user/bookmarks':
|
| 43 |
return t('A bookmark is a link to an address (URL) on the internet.');
|
| 44 |
break;
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Implementation of hook_menu().
|
| 50 |
*/
|
| 51 |
function bookmarks_menu($may_cache) {
|
| 52 |
global $user;
|
| 53 |
$items = array();
|
| 54 |
$access = user_access('access bookmarks');
|
| 55 |
|
| 56 |
// Main menu item
|
| 57 |
$items[] = array('path' => "bookmarks/$user->uid", 'title' => t('my bookmarks'),
|
| 58 |
'callback' => 'bookmarks_page', 'access' => $access);
|
| 59 |
|
| 60 |
// Top level tabs
|
| 61 |
$items[] = array('path' => "bookmarks/$user->uid/overview", 'title' => t('list'),
|
| 62 |
'access' => $access, 'weight' => -10, 'type' => MENU_DEFAULT_LOCAL_TASK);
|
| 63 |
$items[] = array('path' => "bookmarks/$user->uid/add", 'title' => t('add bookmark'),
|
| 64 |
'callback' => 'bookmarks_page', 'access' => $access, 'weight' => 10,
|
| 65 |
'type' => MENU_LOCAL_TASK);
|
| 66 |
$items[] = array('path' => "bookmarks/$user->uid/delete", 'title' => t('delete bookmark'),
|
| 67 |
'callback' => 'bookmarks_delete', 'access' => $access, 'type' => MENU_CALLBACK);
|
| 68 |
$items[] = array('path' => "bookmarks/$user->uid/folders", 'title' => t('folders'),
|
| 69 |
'callback' => 'bookmarks_page', 'access' => $access, 'weight' => 15,
|
| 70 |
'type' => MENU_LOCAL_TASK);
|
| 71 |
$items[] = array('path' => "bookmarks/$user->uid/addfolder", 'title' => t('add folder'),
|
| 72 |
'callback' => 'bookmarks_page', 'access' => $access, 'weight' => 20,
|
| 73 |
'type' => MENU_LOCAL_TASK);
|
| 74 |
$items[] = array('path' => "bookmarks/$user->uid/deletefolder", 'title' => t('delete folder'),
|
| 75 |
'callback' => 'bookmarks_folder_delete', 'access' => $access, 'type' => MENU_CALLBACK);
|
| 76 |
$items[] = array('path' => "bookmarks/$user->uid/config", 'title' => t('bookmark preferences'),
|
| 77 |
'callback' => 'bookmarks_page', 'access' => $access, 'weight' => 25,
|
| 78 |
'type' => MENU_LOCAL_TASK);
|
| 79 |
return $items;
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Implementation of hook_perm().
|
| 84 |
*/
|
| 85 |
function bookmarks_perm() {
|
| 86 |
return array('access bookmarks');
|
| 87 |
}
|
| 88 |
|
| 89 |
/********************************************************************
|
| 90 |
* Theme Hooks
|
| 91 |
********************************************************************/
|
| 92 |
|
| 93 |
/**
|
| 94 |
@addtogroup themeable
|
| 95 |
|
| 96 |
Bookmarks module specific theme functions.
|
| 97 |
@{
|
| 98 |
**/
|
| 99 |
|
| 100 |
/**
|
| 101 |
Returns an user's bookmarks block.
|
| 102 |
|
| 103 |
@return array the paramter to pass to the block function.
|
| 104 |
**/
|
| 105 |
function theme_bookmarks_block() {
|
| 106 |
|
| 107 |
global $user;
|
| 108 |
|
| 109 |
// Do not let anonymous users have bookmarks, even if the admin decides this
|
| 110 |
if ($user->uid != 0 && user_access('access bookmarks')) {
|
| 111 |
|
| 112 |
$result = db_query('SELECT url, title FROM {bookmarks} WHERE uid = %d ORDER BY title', $user->uid);
|
| 113 |
$bookmarks = array();
|
| 114 |
while ($data = db_fetch_object($result)) {
|
| 115 |
$bookmarks[] = '<div class="icon">'. theme('bookmarks_delete', $data->url) .'</div>'. _bookmarks_get_link($data->url, $data->title, true);
|
| 116 |
}
|
| 117 |
|
| 118 |
// Print bookmarks list as an item list
|
| 119 |
$output = (count($bookmarks) ? theme('item_list', $bookmarks) : t('You have no bookmarks.'));
|
| 120 |
$links = array(
|
| 121 |
l(t('quick link'), "bookmarks/$user->uid/add/quick", array('title' => t('Bookmark the current page.')), 'title='. urlencode(drupal_get_title())),
|
| 122 |
l(t('manage'), "bookmarks/$user->uid")
|
| 123 |
);
|
| 124 |
$output .= '<div class="links">'. theme('links', $links) .'</div>';
|
| 125 |
|
| 126 |
return array(
|
| 127 |
'subject' => t('%username\'s bookmarks', array('%username' => $user->name)),
|
| 128 |
'content' => $output
|
| 129 |
);
|
| 130 |
}
|
| 131 |
|
| 132 |
// Not a logged in user or has no rights
|
| 133 |
else {
|
| 134 |
return FALSE;
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
| 138 |
/**
|
| 139 |
Returns a bookmarks delete icon.
|
| 140 |
|
| 141 |
@param bookmark_url The URL of the page to remove
|
| 142 |
@return string the delete icon to be emitted
|
| 143 |
**/
|
| 144 |
function theme_bookmarks_delete($url) {
|
| 145 |
global $user;
|
| 146 |
|
| 147 |
$query = 'url=' . urlencode($url). '&block=1';
|
| 148 |
return l(theme('image', 'modules/bookmarks/trash.gif', t('delete')), "bookmarks/$user->uid/delete", array("title" => t("Delete this bookmark from your list.")), $query, NULL, FALSE, TRUE);
|
| 149 |
}
|
| 150 |
/** @} End of addtogroup themeable **/
|
| 151 |
|
| 152 |
/********************************************************************
|
| 153 |
* Module Functions
|
| 154 |
********************************************************************/
|
| 155 |
|
| 156 |
/**
|
| 157 |
* The controller for managing bookmarks. Callback happens via menu().
|
| 158 |
*
|
| 159 |
* @return string Completely themed HTML page.
|
| 160 |
*/
|
| 161 |
function bookmarks_page() {
|
| 162 |
global $user;
|
| 163 |
|
| 164 |
$edit = $_POST['edit'];
|
| 165 |
$op = $_POST['op'];
|
| 166 |
|
| 167 |
switch (($op ? $op : arg(2))) {
|
| 168 |
case 'add':
|
| 169 |
$title = t('Create new bookmark');
|
| 170 |
if (arg(3) == 'quick') {
|
| 171 |
$edit = bookmarks_load_quicklink();
|
| 172 |
}
|
| 173 |
elseif (arg(3) == 'weblink') {
|
| 174 |
$edit = bookmarks_load_weblink(arg(4));
|
| 175 |
}
|
| 176 |
$output = bookmarks_form($edit);
|
| 177 |
break;
|
| 178 |
|
| 179 |
case 'edit':
|
| 180 |
$title = t('Edit bookmark');
|
| 181 |
$output = ($url = urldecode($_GET['url'])) ? bookmarks_form(bookmarks_load($url)) : drupal_set_message(t('Bookmark cannot be edited, because it is not in your list.'), 'error');
|
| 182 |
break;
|
| 183 |
|
| 184 |
case t('Save'):
|
| 185 |
$title = t('Bookmarks');
|
| 186 |
if (bookmarks_validate($edit)) {
|
| 187 |
bookmarks_save($edit);
|
| 188 |
drupal_goto("bookmarks/$user->uid");
|
| 189 |
}
|
| 190 |
else {
|
| 191 |
$output = bookmarks_form($edit);
|
| 192 |
}
|
| 193 |
break;
|
| 194 |
|
| 195 |
case 'folders' :
|
| 196 |
$title = t('Bookmark folders');
|
| 197 |
$output = bookmarks_folder_overview();
|
| 198 |
break;
|
| 199 |
|
| 200 |
case 'addfolder':
|
| 201 |
$title = t('Create new folder');
|
| 202 |
$output = _bookmarks_folder_form($edit);
|
| 203 |
break;
|
| 204 |
|
| 205 |
case 'folderedit':
|
| 206 |
$title = t('Edit folder');
|
| 207 |
$output = ($fid = $_GET['fid']) ? _bookmarks_folder_form(_bookmarks_folder_load($fid)) : drupal_set_message(t('Folder cannot be edited, because it is not in your list.'), 'error');
|
| 208 |
break;
|
| 209 |
|
| 210 |
case 'config':
|
| 211 |
$title = t('Configure bookmark preferences');
|
| 212 |
$output = _bookmarks_config();
|
| 213 |
break;
|
| 214 |
|
| 215 |
case t('Save folder'):
|
| 216 |
$title = t('Folder');
|
| 217 |
_bookmarks_folder_save($edit);
|
| 218 |
drupal_goto("bookmarks/$user->uid/folders");
|
| 219 |
break;
|
| 220 |
|
| 221 |
case t('Save preferences'):
|
| 222 |
$title = t('Boomark preferences');
|
| 223 |
_bookmarks_config_save($edit);
|
| 224 |
drupal_goto("bookmarks/$user->uid");
|
| 225 |
break;
|
| 226 |
|
| 227 |
default:
|
| 228 |
$output = bookmarks_overview();
|
| 229 |
}
|
| 230 |
|
| 231 |
drupal_set_title($title);
|
| 232 |
print theme('page', $output);
|
| 233 |
}
|
| 234 |
|
| 235 |
function bookmarks_delete() {
|
| 236 |
global $user;
|
| 237 |
|
| 238 |
if(!$_GET['url'] && $_GET['url'] != '0') {
|
| 239 |
drupal_set_message(t('Bookmark cannot be deleted, because no URL was specified.'), 'error');
|
| 240 |
return drupal_goto("bookmarks/{$user->uid}");
|
| 241 |
}
|
| 242 |
|
| 243 |
$form['url'] = array('#type' => 'hidden', '#value' => $_GET['url']);
|
| 244 |
return confirm_form('bookmarks_delete_confirm', $form, t('Are you sure you want to delete this bookmark?'), "bookmarks/{$user->uid}", '', t('Delete'), t('Cancel'));
|
| 245 |
}
|
| 246 |
|
| 247 |
function bookmarks_delete_confirm_submit($form_id, $form_values) {
|
| 248 |
global $user;
|
| 249 |
|
| 250 |
db_query("DELETE FROM {bookmarks} WHERE uid = %d AND url = '%s'", $user->uid, urldecode($form_values['url']));
|
| 251 |
drupal_set_message(t('Deleted bookmark.'));
|
| 252 |
|
| 253 |
return "bookmarks/{$user->uid}";
|
| 254 |
}
|
| 255 |
|
| 256 |
/**
|
| 257 |
* Menu callback; confirm deletion of folder.
|
| 258 |
*
|
| 259 |
* @todo name the folder in the question
|
| 260 |
*/
|
| 261 |
function bookmarks_folder_delete() {
|
| 262 |
global $user;
|
| 263 |
|
| 264 |
if(!is_numeric($_GET['fid'])) {
|
| 265 |
drupal_set_message(t('Folder cannot be deleted, because no folder ID was specified.'), 'error');
|
| 266 |
return drupal_goto("bookmarks/{$user->uid}");
|
| 267 |
}
|
| 268 |
|
| 269 |
$form['fid'] = array('#type' => 'hidden', '#value' => (int)$_GET['fid']);
|
| 270 |
return confirm_form('bookmarks_folder_delete_confirm', $form, t('Are you sure you want to delete this folder?'), "bookmarks/{$user->uid}", '', t('Delete'), t('Cancel'));
|
| 271 |
}
|
| 272 |
|
| 273 |
/**
|
| 274 |
* Delete a folder, all its children, and the associated bookmarks
|
| 275 |
*
|
| 276 |
* @param int $fid
|
| 277 |
* @return string
|
| 278 |
*/
|
| 279 |
function bookmarks_folder_delete_confirm_submit($form_id, $form_values, $fid = 0, $child = false) {
|
| 280 |
global $user;
|
| 281 |
|
| 282 |
if($child) {
|
| 283 |
$result = db_query('SELECT f.fid FROM {bookmarks_folders} f WHERE f.uid = %d AND f.fid = %d', $user->uid, $fid);
|
| 284 |
while($data = db_fetch_object($result)) {
|
| 285 |
db_query('DELETE FROM {bookmarks} WHERE uid = %d AND fid = %d', $user->uid, $data->fid);
|
| 286 |
db_query('DELETE FROM {bookmarks_folders} WHERE uid = %d AND fid = %d', $user->uid, $data->fid);
|
| 287 |
bookmarks_folder_delete_confirm_submit(NULL, NULL, $data->fid, true);
|
| 288 |
}
|
| 289 |
}
|
| 290 |
else {
|
| 291 |
if(!is_numeric($form_values['fid'])) {
|
| 292 |
drupal_set_message(t('Folder ID is not the correct format!', 'error'));
|
| 293 |
}
|
| 294 |
else {
|
| 295 |
|
| 296 |
db_query('DELETE FROM {bookmarks} WHERE uid = %d AND fid = %d', $user->uid, $form_values['fid']);
|
| 297 |
db_query('DELETE FROM {bookmarks_folders} WHERE uid = %d AND fid = %d', $user->uid, $form_values['fid']);
|
| 298 |
bookmarks_folder_delete_confirm_submit(NULL, NULL, $form_values['fid'], true);
|
| 299 |
drupal_set_message(t('Deleted bookmark folder.'));
|
| 300 |
}
|
| 301 |
|
| 302 |
return "bookmarks/{$user->uid}";
|
| 303 |
}
|
| 304 |
}
|
| 305 |
|
| 306 |
function _bookmarks_folder_select_options() {
|
| 307 |
$folders = _bookmarks_folder_child(true);
|
| 308 |
if ($edit != NULL) {
|
| 309 |
$current_name = $folderss[$edit['fid']];
|
| 310 |
$current_childs = array($edit['fid'] => $current_name);
|
| 311 |
_bookmarks_folder_child_loop($edit['fid'], $current_childs, (strpos($current_name, '>') / 2));
|
| 312 |
$folders = array_diff_assoc($folders, $current_childs);
|
| 313 |
}
|
| 314 |
return $folders;
|
| 315 |
}
|
| 316 |
|
| 317 |
function bookmarks_form($edit = null) {
|
| 318 |
$form['details'] = array(
|
| 319 |
'#type' => 'fieldset',
|
| 320 |
'#title' => t('Bookmark details'),
|
| 321 |
);
|
| 322 |
$form['details']['title'] = array(
|
| 323 |
'#type' => 'textfield',
|
| 324 |
'#title' => t('Title'),
|
| 325 |
'#default_value' => $edit['title'],
|
| 326 |
'#size' => 60,
|
| 327 |
'#maxlength' => 128,
|
| 328 |
'#description' => null,
|
| 329 |
'#attributes' => null,
|
| 330 |
'#required' => true,
|
| 331 |
);
|
| 332 |
$form['details']['url'] = array(
|
| 333 |
'#type' => 'textfield',
|
| 334 |
'#title' => t('URL'),
|
| 335 |
'#default_value' => $edit['url'],
|
| 336 |
'#size' => 60,
|
| 337 |
'#maxlength' => 128,
|
| 338 |
'#description' => t('Enter the address of the page you want to bookmark. This can be an internal or external reference.'),
|
| 339 |
'#attributes' => null,
|
| 340 |
'#required' => true,
|
| 341 |
);
|
| 342 |
$form['details']['fid'] = array(
|
| 343 |
'#type' => 'select',
|
| 344 |
'#title' => t('Parent folder'),
|
| 345 |
'#options' => _bookmarks_folder_select_options(),
|
| 346 |
'#default_value' => $edit['fid'],
|
| 347 |
'#multiple' => false,
|
| 348 |
'#required' => true
|
| 349 |
);
|
| 350 |
$form['old_url'] = array(
|
| 351 |
'#type' => 'hidden',
|
| 352 |
'#value' => urlencode($edit['url']),
|
| 353 |
);
|
| 354 |
$form['submit'] = array(
|
| 355 |
'#type' => 'submit',
|
| 356 |
'#value' => t('Save'),
|
| 357 |
);
|
| 358 |
return drupal_get_form('bookmarks_form', $form);
|
| 359 |
}
|
| 360 |
|
| 361 |
function _bookmarks_folder_form($edit = null) {
|
| 362 |
$form['details'] = array(
|
| 363 |
'#type' => 'fieldset',
|
| 364 |
'#title' => t('Folder details')
|
| 365 |
);
|
| 366 |
$form['details']['name'] = array(
|
| 367 |
'#type' => 'textfield',
|
| 368 |
'#title' => t('Folder name'),
|
| 369 |
'#default_value' => $edit['name'],
|
| 370 |
'#size' => 60,
|
| 371 |
'#maxlength' => 128,
|
| 372 |
'#required' => true
|
| 373 |
);
|
| 374 |
$form['details']['f_parent_id'] = array(
|
| 375 |
'#type' => 'select',
|
| 376 |
'#title' => t('Parent folder'),
|
| 377 |
'#options' => _bookmarks_folder_select_options(),
|
| 378 |
'#default_value' => $edit['f_parent_id'],
|
| 379 |
'#multiple' => false,
|
| 380 |
'#required' => true
|
| 381 |
);
|
| 382 |
$form['fid'] = array(
|
| 383 |
'#type' => 'hidden',
|
| 384 |
'#value' => $edit['fid']
|
| 385 |
);
|
| 386 |
$form['submit'] = array(
|
| 387 |
'#type' => 'submit',
|
| 388 |
'#value' => 'Save folder'
|
| 389 |
);
|
| 390 |
|
| 391 |
return drupal_get_form('bookmarks_folder_form', $form);
|
| 392 |
}
|
| 393 |
|
| 394 |
/**
|
| 395 |
* lets individuals set preferences for bookmarks
|
| 396 |
*
|
| 397 |
*/
|
| 398 |
function _bookmarks_config() {
|
| 399 |
$form['link_display'] = array(
|
| 400 |
'#type' => 'radios',
|
| 401 |
'#title' => t('Link display'),
|
| 402 |
'#default_value' => _bookmarks_user_pref('link_display', 0),
|
| 403 |
'#options' => array(t('Title and link separate'), t('Title as link')),
|
| 404 |
'#description' => t('Either show the bookmark name as a link, or the title with the URL as a separate link')
|
| 405 |
);
|
| 406 |
$form['submit'] = array(
|
| 407 |
'#type' => 'submit',
|
| 408 |
'#value' => 'Save preferences'
|
| 409 |
);
|
| 410 |
|
| 411 |
return drupal_get_form('_bookmarks_config', $form);
|
| 412 |
}
|
| 413 |
|
| 414 |
function _bookmarks_user_pref($var, $default) {
|
| 415 |
static $options;
|
| 416 |
|
| 417 |
if(!isset($options)) {
|
| 418 |
$options = db_result(db_query("SELECT options FROM {bookmarks_prefs} WHERE uid = %d", $GLOBALS['user']->uid));
|
| 419 |
}
|
| 420 |
$definitions = array(
|
| 421 |
'link_display' => 1 // display link title as link
|
| 422 |
);
|
| 423 |
return $options ? $options & $definitions[$var] : $default;
|
| 424 |
}
|
| 425 |
|
| 426 |
/**
|
| 427 |
* Write prefs to db
|
| 428 |
*
|
| 429 |
* @see _bookmarks_config()
|
| 430 |
* @param array $edit
|
| 431 |
*/
|
| 432 |
function _bookmarks_config_save($edit) {
|
| 433 |
global $user;
|
| 434 |
|
| 435 |
$options = 0;
|
| 436 |
$options |= $edit['link_display'];
|
| 437 |
|
| 438 |
if (db_result(db_query("SELECT COUNT(options) FROM {bookmarks_prefs} WHERE uid = %d", $user->uid))) {
|
| 439 |
db_query("UPDATE {bookmarks_prefs} SET options = %d WHERE uid = %d", $options, $user->uid);
|
| 440 |
} else {
|
| 441 |
db_query("INSERT INTO {bookmarks_prefs} (uid, options) VALUES (%d, %d)", $user->uid, $options);
|
| 442 |
}
|
| 443 |
drupal_set_message(t('Bookmark preferences have been updated.'));
|
| 444 |
}
|
| 445 |
|
| 446 |
function bookmarks_load($url) {
|
| 447 |
global $user;
|
| 448 |
return db_fetch_array(db_query("SELECT * FROM {bookmarks} WHERE uid = %d AND url = '%s'", $user->uid, $url));
|
| 449 |
}
|
| 450 |
|
| 451 |
function _bookmarks_folder_load($fid) {
|
| 452 |
global $user;
|
| 453 |
return db_fetch_array(db_query("SELECT * FROM {bookmarks_folders} WHERE uid = %d AND fid = %d", $user->uid, $fid));
|
| 454 |
}
|
| 455 |
|
| 456 |
function bookmarks_load_quicklink() {
|
| 457 |
|
| 458 |
$edit['url'] = bookmarks_get_canonical_path($url);
|
| 459 |
$explicit_uri = drupal_get_normal_path($edit['url']);
|
| 460 |
|
| 461 |
$uri_ary = explode('/', $explicit_uri);
|
| 462 |
$type = $uri_ary[0];
|
| 463 |
foreach ($uri_ary as $value) {
|
| 464 |
if (is_numeric($value)) {
|
| 465 |
$id = $value;
|
| 466 |
break;
|
| 467 |
}
|
| 468 |
}
|
| 469 |
|
| 470 |
/* Only display quicklink title for internal URLs */
|
| 471 |
if (is_numeric($id) && !strstr($edit['url'], 'http://')) {
|
| 472 |
switch ($type) {
|
| 473 |
case 'node':
|
| 474 |
$edit['title'] = db_result(db_query('SELECT title FROM {node} n WHERE nid = %d', $id));
|
| 475 |
break;
|
| 476 |
case 'comment':
|
| 477 |
$edit['title'] = db_result(db_query('SELECT subject FROM {comments} WHERE cid = %d', $id));
|
| 478 |
break;
|
| 479 |
case 'user':
|
| 480 |
$edit['title'] = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $id));
|
| 481 |
break;
|
| 482 |
default:
|
| 483 |
/* Leave the title blank. The user will be prompted to enter a title. */
|
| 484 |
}
|
| 485 |
}
|
| 486 |
|
| 487 |
if (!$edit['title']) {
|
| 488 |
$edit['title'] = urldecode($_GET['title']);
|
| 489 |
}
|
| 490 |
|
| 491 |
return $edit;
|
| 492 |
}
|
| 493 |
|
| 494 |
function bookmarks_load_weblink($nid) {
|
| 495 |
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));
|
| 496 |
}
|
| 497 |
|
| 498 |
function bookmarks_overview() {
|
| 499 |
global $user;
|
| 500 |
|
| 501 |
$link_display = _bookmarks_user_pref('link_display', 0);
|
| 502 |
|
| 503 |
$output = _bookmarks_overview_folders($link_display);
|
| 504 |
|
| 505 |
$header = array();
|
| 506 |
$header[] = array('data' => t('title'), 'field' => 'title', 'sort' => 'asc');
|
| 507 |
if(!$link_display) {
|
| 508 |
$header[] = array('data' => t('link'), 'field' => 'url');
|
| 509 |
}
|
| 510 |
$header[] = array('data' => t('operations'), 'colspan' => 2);
|
| 511 |
|
| 512 |
$sql = 'SELECT b.url, b.title FROM {bookmarks} b WHERE b.uid = '. db_escape_string($user->uid) .' AND b.fid = 0 '. tablesort_sql($header);
|
| 513 |
$result = pager_query($sql, 50);
|
| 514 |
|
| 515 |
$rows = array();
|
| 516 |
|
| 517 |
while ($data = db_fetch_object($result)) {
|
| 518 |
if($link_display) {
|
| 519 |
$rows[] = array(_bookmarks_get_link($data->url, $data->title, $link_display), ' '.l(t('edit'), "bookmarks/$user->uid/edit", null, 'url=' . urlencode($data->url)).' '. l(t('delete'), "bookmarks/$user->uid/delete", null, 'url=' . urlencode($data->url)));
|
| 520 |
} else {
|
| 521 |
$rows[] = array($data->title, _bookmarks_get_link($data->url, $data->title, $link_display), ' '.l(t('edit'), "bookmarks/$user->uid/edit", null, 'url=' . urlencode($data->url)).' '. l(t('delete'), "bookmarks/$user->uid/delete", null, 'url=' . urlencode($data->url)));
|
| 522 |
}
|
| 523 |
}
|
| 524 |
|
| 525 |
$pager = theme('pager', null, 50, 0, tablesort_pager());
|
| 526 |
if (!empty($pager)) {
|
| 527 |
$rows[] = array(array('data' => $pager, 'colspan' => $link_display ? 3 : 4));
|
| 528 |
}
|
| 529 |
|
| 530 |
$output .= (count($rows) == 0) ? t('You have no bookmarks.') : theme('table', $header, $rows);
|
| 531 |
return $output;
|
| 532 |
}
|
| 533 |
|
| 534 |
function _bookmarks_overview_folders($link_display, $fid = 0) {
|
| 535 |
global $user;
|
| 536 |
|
| 537 |
$form = array();
|
| 538 |
// having all these queries probably isn't the most efficient way of doing this
|
| 539 |
$f_result = db_query('SELECT f.fid, f.name FROM {bookmarks_folders} f WHERE f.f_parent_id = %d AND f.uid = %d', $fid, $user->uid);
|
| 540 |
while ($folder_data = db_fetch_object($f_result)) {
|
| 541 |
if(!isset($form[$folder_data->fid])) {
|
| 542 |
$form[$folder_data->fid] = array(
|
| 543 |
'#type' => 'fieldset',
|
| 544 |
'#title' => $folder_data->name,
|
| 545 |
'#collapsible' => true,
|
| 546 |
'#collapsed' => true
|
| 547 |
);
|
| 548 |
// start table
|
| 549 |
}
|
| 550 |
if($child = _bookmarks_overview_folders($link_display, $folder_data->fid)) {
|
| 551 |
$form[$folder_data->fid]['child'] = array(
|
| 552 |
'#value' => $child
|
| 553 |
);
|
| 554 |
}
|
| 555 |
if($bm_result = db_query('SELECT b.url, b.title FROM {bookmarks} b WHERE b.uid = %d AND b.fid = %d', $user->uid, $folder_data->fid)) {
|
| 556 |
$header = array();
|
| 557 |
$header[] = array('data' => t('title'), 'field' => 'title', 'sort' => 'asc');
|
| 558 |
if(!$link_display) {
|
| 559 |
$header[] = array('data' => t('link'), 'field' => 'url');
|
| 560 |
}
|
| 561 |
$header[] = array('data' => t('operations'), 'colspan' => 2);
|
| 562 |
|
| 563 |
$rows = array();
|
| 564 |
|
| 565 |
while ($data = db_fetch_object($bm_result)) {
|
| 566 |
if($link_display) {
|
| 567 |
$rows[] = array(_bookmarks_get_link($data->url, $data->title, $link_display),' '.l(t('edit'), "bookmarks/$user->uid/edit", null, 'url=' . urlencode($data->url)).' '. l(t('delete'), "bookmarks/$user->uid/delete", null, 'url=' . urlencode($data->url)));
|
| 568 |
} else {
|
| 569 |
$rows[] = array($data->title, _bookmarks_get_link($data->url, $data->title, $link_display), ' '.l(t('edit'), "bookmarks/$user->uid/edit", null, 'url=' . urlencode($data->url)).' '. l(t('delete'), "bookmarks/$user->uid/delete", null, 'url=' . urlencode($data->url)));
|
| 570 |
}
|
| 571 |
}
|
| 572 |
$form[$folder_data->fid][$data->url] = array(
|
| 573 |
'#value' => theme('table', $header, $rows)
|
| 574 |
);
|
| 575 |
}
|
| 576 |
}
|
| 577 |
return drupal_get_form('bookmark_folders', $form);
|
| 578 |
}
|
| 579 |
|
| 580 |
function bookmarks_folder_overview() {
|
| 581 |
global $user;
|
| 582 |
|
| 583 |
$header = array(
|
| 584 |
array('data' => t('name')),
|
| 585 |
array('data' => t('operations'), 'colspan' => 2)
|
| 586 |
);
|
| 587 |
|
| 588 |
$folders = _bookmarks_folder_child();
|
| 589 |
|
| 590 |
while(list($key, $value) = each($folders)) {
|
| 591 |
$name = (strlen($value) > 50) ? substr($value, 0, 47). '...' : $value;
|
| 592 |
$rows[] = array($value, ' '.l(t('edit'), "bookmarks/$user->uid/folderedit", null, 'fid=' . $key).' '. l(t('delete'), "bookmarks/$user->uid/deletefolder", null, 'fid=' . $key));
|
| 593 |
}
|
| 594 |
|
| 595 |
$output .= (count($rows) == 0) ? t('You have no folders.') : theme('table', $header, $rows);
|
| 596 |
return $output;
|
| 597 |
}
|
| 598 |
|
| 599 |
function _bookmarks_folder_child($inc_top = false) {
|
| 600 |
global $user;
|
| 601 |
|
| 602 |
$folder_tree = $inc_top == true ? array(0 => '['. t('Top level') .']') : array();
|
| 603 |
|
| 604 |
$result = db_query('SELECT f.fid, f.name FROM {bookmarks_folders} f WHERE f.f_parent_id = 0 AND f.uid = %d', $user->uid);
|
| 605 |
while ($data = db_fetch_object($result)) {
|
| 606 |
if($_GET['fid'] != $data->fid) {
|
| 607 |
$folder_tree[$data->fid] = $data->name;
|
| 608 |
_bookmarks_folder_child_loop($data->fid, $folder_tree, 0);
|
| 609 |
}
|
| 610 |
}
|
| 611 |
return $folder_tree;
|
| 612 |
}
|
| 613 |
|
| 614 |
function _bookmarks_folder_child_loop($fid, &$folder_array, $i) {
|
| 615 |
global $user;
|
| 616 |
|
| 617 |
$i++; // should this be static?
|
| 618 |
$result = db_query('SELECT f.fid, f.name FROM {bookmarks_folders} f WHERE f.f_parent_id = %d AND f.uid = %d', $fid, $user->uid);
|
| 619 |
while ($data = db_fetch_object($result)) {
|
| 620 |
$folder_array[$data->fid] = str_repeat('-', 2*$i) . ' ' . $data->name;
|
| 621 |
if($data->fid != $fid) {
|
| 622 |
_bookmarks_folder_child_loop($data->fid, $folder_array, $i);
|
| 623 |
}
|
| 624 |
}
|
| 625 |
}
|
| 626 |
|
| 627 |
function bookmarks_save($edit) {
|
| 628 |
global $user;
|
| 629 |
|
| 630 |
$edit['old_url'] = urldecode($edit['old_url']);
|
| 631 |
if ($edit['old_url'] && db_result(db_query("SELECT COUNT(b.uid) FROM {bookmarks} b WHERE b.uid = %d AND b.url = '%s'", $user->uid, $edit['old_url']))) {
|
| 632 |
db_query("UPDATE {bookmarks} SET fid = %d, title = '%s', url = '%s' WHERE uid = %d AND url = '%s'", $edit['fid'], $edit['title'], $edit['url'], $user->uid, $edit['old_url']);
|
| 633 |
drupal_set_message(t('The bookmark has been updated.'));
|
| 634 |
}
|
| 635 |
else {
|
| 636 |
db_query("INSERT INTO {bookmarks} (uid, fid, url, title) VALUES (%d, %d, '%s', '%s')", $user->uid, $edit['fid'], $edit['url'], $edit['title']);
|
| 637 |
drupal_set_message(t('The link has been added to your bookmarks.'));
|
| 638 |
}
|
| 639 |
}
|
| 640 |
|
| 641 |
function _bookmarks_folder_save($edit) {
|
| 642 |
global $user;
|
| 643 |
|
| 644 |
if (db_result(db_query("SELECT COUNT(fid) FROM {bookmarks_folders} WHERE fid = %d AND uid = %d", $edit['fid'], $user->uid))) {
|
| 645 |
db_query("UPDATE {bookmarks_folders} SET name = '%s', f_parent_id = %d WHERE uid = %d AND fid = %d", $edit['name'], $edit['f_parent_id'], $user->uid, $edit['fid']);
|
| 646 |
drupal_set_message(t('The folder has been updated.'));
|
| 647 |
} else {
|
| 648 |
db_query("INSERT INTO {bookmarks_folders} (uid, f_parent_id, name) VALUES (%d, %d, '%s')", $user->uid, $edit['f_parent_id'], $edit['name']);
|
| 649 |
drupal_set_message(t('The folder has been added to your bookmarks.'));
|
| 650 |
}
|
| 651 |
}
|
| 652 |
|
| 653 |
/**
|
| 654 |
* Administration settings to turn on password reminder or not
|
| 655 |
*
|
| 656 |
* @return array
|
| 657 |
*/
|
| 658 |
function bookmarks_settings() {
|
| 659 |
$form = array();
|
| 660 |
$form['bookmarks_link_crop_size'] = array(
|
| 661 |
'#type' => 'textfield',
|
| 662 |
'#title' => t('Link crop length'),
|
| 663 |
'#description' => t('The display of links will be cropped to this length in characters.'),
|
| 664 |
'#size' => 5,
|
| 665 |
'#maxlength' => 5,
|
| 666 |
'#default_value' => variable_get('bookmarks_link_crop_size', 50)
|
| 667 |
);
|
| 668 |
return $form;
|
| 669 |
}
|
| 670 |
|
| 671 |
function bookmarks_validate($edit) {
|
| 672 |
$errors = array();
|
| 673 |
|
| 674 |
if (isset($edit['title']) && !$edit['title']) {
|
| 675 |
$errors['title'] = t('You must supply a title.');
|
| 676 |
}
|
| 677 |
if (isset($edit['url']) && !$edit['url']) {
|
| 678 |
$errors['url'] = t('You must supply an URL.');
|
| 679 |
}
|
| 680 |
|
| 681 |
foreach ($errors as $name => $message) {
|
| 682 |
form_set_error($name, $message);
|
| 683 |
}
|
| 684 |
return count($errors) == 0;
|
| 685 |
}
|
| 686 |
|
| 687 |
/**
|
| 688 |
* Return a relative URI that Drupal can recognize internally.
|
| 689 |
*
|
| 690 |
* @return string
|
| 691 |
*/
|
| 692 |
function bookmarks_get_canonical_path($url) {
|
| 693 |
global $base_url;
|
| 694 |
|
| 695 |
$c_path = ltrim(str_replace($base_url, '', trim(referer_uri(), '/')), '/');
|
| 696 |
$c_path = str_replace('index.php', '', $c_path);
|
| 697 |
$c_path = str_replace('?q=', '', $c_path);
|
| 698 |
|
| 699 |
return $c_path;
|
| 700 |
}
|
| 701 |
|
| 702 |
/**
|
| 703 |
* Generate a link based on the URL being internal or external
|
| 704 |
*
|
| 705 |
* @return string
|
| 706 |
*/
|
| 707 |
function _bookmarks_get_link($url, $title, $link_display = false) {
|
| 708 |
$crop = variable_get('bookmarks_link_crop_size', 50);
|
| 709 |
if($link_display) {
|
| 710 |
$title = (strlen($title) > $crop) ? substr($title, 0, $crop - 3). '...' : $title;
|
| 711 |
} else {
|
| 712 |
$title = (strlen($url) > $crop) ? substr($url, 0, $crop - 3). '...' : $url;
|
| 713 |
}
|
| 714 |
return (preg_match("!^[a-zA-Z]+://!", $url)) ? "<a href=\"$url\" title=\"$url\">$title</a>" : l($title, $url);
|
| 715 |
}
|
| 716 |
|
| 717 |
?>
|