--- favorite_nodes.module 2006-07-10 15:17:45.000000000 -0400 +++ favorite_nodes.module.new 2006-11-29 16:47:43.000000000 -0500 @@ -3,6 +3,12 @@ define('FAVORITE_NODES_NODE_TYPE', 'favorite_nodes_node_type_'); define('FAVORITE_NODES_PERM_ADD', 'create favorite nodes'); define('FAVORITE_NODES_PERM_VIEW', 'view favorite nodes'); +define('FAVORITE_NODES_BLOCK', 'favorite_nodes_block_type_'); +define('FAVORITE_NODES_BLOCK_LIMIT', 'favorite_nodes_block_limit'); +define('FAVORITE_NODES_BLOCK_TITLE', 'favorite_nodes_block_title'); +define('FAVORITE_NODES_PAGE_LIMIT', 'favorite_nodes_page_limit'); +define('FAVORITE_NODES_PROFILE_LIMIT', 'favorite_nodes_profile_limit'); +define('FAVORITE_NODES_PAGE_TYPE', 'favorite_nodes_page_type'); function favorite_nodes_help($section) { switch ($section) { @@ -46,9 +52,108 @@ return $items; } +function favorite_nodes_block($op = 'list', $delta = 0, $edit = array()) { + global $user; + + switch ($op) { + case 'list': + return array(array('info' => t('Favorite nodes'))); + + case 'view': + if (user_access(FAVORITE_NODES_PERM_VIEW)) { + $block = array(); + $block['subject'] = variable_get(FAVORITE_NODES_BLOCK_TITLE, t('Favorites')); + $block['content'] = ''; + + $node_types = node_get_types(); + natcasesort($node_types); + + foreach ($node_types as $type => $name) { + if (variable_get(FAVORITE_NODES_NODE_TYPE . $type, 0)) { + $uid = intval($user->uid); + $favorites = favorite_nodes_get($uid, $type, variable_get(FAVORITE_NODES_BLOCK_LIMIT, 5)); + $items = array(); + if (count($favorites) > 0) { + foreach ($favorites as $favorite) { + $items[] = l($favorite->title, 'node/' . $favorite->nid); + } + $block['content'] .= theme('item_list', $items, variable_get(FAVORITE_NODES_BLOCK . $type, $name)); + } + $sql = "SELECT COUNT(*) FROM {node} n INNER JOIN {favorite_nodes} f USING(nid) WHERE n.type = '%s' AND f.uid = %d ORDER by f.last DESC"; + $count = db_result(db_query($sql, $type, $user->uid)); + if ($count > variable_get(FAVORITE_NODES_BLOCK_LIMIT, 5)) { + $block['content'] .= '
' . "\n"; + $block['content'] .= l(t('More Favorite %types', array('%types' => variable_get(FAVORITE_NODES_BLOCK . $type, $name))), 'favorite_nodes/view/' . $user->uid . '/' . $type); + $block['content'] .= '
' . "\n"; + } + } + } + } + return $block; + + case 'configure': + $form = array(); + $form['titles'] = array( + '#type' => 'fieldset', + '#title' => t('Titles'), + '#collapsible' => true, + '#collapsed' => false, + ); + $form['titles']['title'] = array( + '#type' => 'textfield', + '#title' => t('Block title'), + '#size' => 60, + '#description' => t('This title will be displayed at the top of the block.'), + '#default_value' => variable_get(FAVORITE_NODES_BLOCK_TITLE, t('Favorites')), + ); + $form['limit'] = array( + '#type' => 'textfield', + '#title' => t('Number of favorites to display'), + '#size' => 4, + '#description' => t('Up to this many favorites of each type of content will be displayed in the block. If there are no marked favorites of a type, then that type won\'t show up.'), + '#default_value' => variable_get(FAVORITE_NODES_BLOCK_LIMIT, 5), + ); + $node_types = node_get_types(); + natcasesort($node_types); + + $form['titles']['subtitles'] = array( + '#type' => 'fieldset', + '#title' => t('Type subtitles'), + '#collapsible' => true, + '#collapsed' => true, + ); + foreach ($node_types as $type => $name) { + if (variable_get(FAVORITE_NODES_NODE_TYPE . $type, 0)) { + $form['titles']['subtitles'][FAVORITE_NODES_BLOCK . $type] = array( + '#type' => 'textfield', + '#title' => t('Subtitle for the %name content type', array('%name' => $name)), + '#size' => 60, + '#description' => t('Within the block, any links to content of the %name type will be categorized under this subtitle.', array('%name' => $name)), + '#default_value' => variable_get(FAVORITE_NODES_BLOCK . $type, $name), + ); + } + } + return $form; + + case 'save': + variable_set(FAVORITE_NODES_BLOCK_TITLE, $edit['title']); + variable_set(FAVORITE_NODES_BLOCK_LIMIT, $edit['limit']); + $node_types = node_get_types(); + foreach ($node_types as $type => $name) { + if (variable_get(FAVORITE_NODES_NODE_TYPE . $type, 0)) { + variable_set(FAVORITE_NODES_BLOCK . $type, $edit[FAVORITE_NODES_BLOCK . $type]); + } + } + break; + + default: + break; + } +} + function favorite_nodes_link($type, $node = null, $teaser = false) { + global $user; $links = array(); - if ($type == 'node' && !$teaser) { if (variable_get(FAVORITE_NODES_NODE_TYPE . $node->type, 0)) { if (user_access(FAVORITE_NODES_PERM_ADD)) { @@ -58,6 +163,7 @@ else { if (user_access(FAVORITE_NODES_PERM_VIEW)) { $links[] = t('in favorites'); + $links[] = l(t('remove from favorites'), "favorite_nodes/remove/$node->nid"); } } } @@ -67,12 +173,43 @@ } function favorite_nodes_settings() { + $set = 'page'; + $form[$set] = array( + '#type' => 'fieldset', + '#title' => t('Favorites Page Settings'), + '#collapsible' => true, + '#collapsed' => false, + ); + $form[$set][FAVORITE_NODES_PAGE_LIMIT] = array( + '#type' => 'textfield', + '#title' => t('Favorite Nodes Page Limit'), + '#default_value' => variable_get(FAVORITE_NODES_PAGE_LIMIT, 10), + '#description' => t('How many items to display on a single page of marked favorites.'), + ); + $form[$set][FAVORITE_NODES_PROFILE_LIMIT] = array( + '#type' => 'textfield', + '#title' => t('Favorite Nodes Profile Limit'), + '#default_value' => variable_get(FAVORITE_NODES_PROFILE_LIMIT, 3), + '#description' => t('How many items per type to display on the profile page.'), + ); + $form[$set][FAVORITE_NODES_PAGE_TYPE] = array( + '#type' => 'select', + '#title' => t('Type of Page Display for Favorite Nodes'), + '#options' => array( + 'table' => 'Table', + 'teasers' => 'Teasers', + ), + '#default_value' => variable_get(FAVORITE_NODES_PAGE_TYPE, 'table'), + '#description' => t('How should favorites be displayed on the favorite nodes page?'), + ); $set = 'types'; $form[$set] = array( '#type' => 'fieldset', '#title' => t('Enable favorites for these node types'), + '#collapsible' => true, + '#collapsed' => false, ); - + foreach(node_get_types() as $type => $name) { $form[$set][FAVORITE_NODES_NODE_TYPE . $type] = array( '#type' => 'checkbox', @@ -89,12 +226,17 @@ global $user; $nid = (int)arg(2); - db_query("INSERT INTO {favorite_nodes} (nid, uid, last) VALUES (%d, %d, %d)", $nid, $user->uid, time()); - - drupal_set_message(t('The node was added to your favorites')); - - drupal_goto("node/$nid"); -} + if ($nid) { + $node = node_load($nid); + if ($node->nid) { + db_query("DELETE FROM {favorite_nodes} WHERE nid=%d AND uid=%d", $nid, $user->uid); + db_query("INSERT INTO {favorite_nodes} (nid, uid, last) VALUES (%d, %d, %d)", $nid, $user->uid, time()); + drupal_set_message(t('The %node was added to your favorites.', array('%node' => _node_names('name', $node)))); + drupal_goto("node/$nid"); + } + } + drupal_not_found(); +} function favorite_nodes_delete() { global $user; @@ -108,11 +250,27 @@ function favorite_nodes_view() { $uid = (int)arg(2); $type = arg(3); - print theme('page', theme('favorite_nodes_view', _favorite_nodes_get($uid, $type))); + print theme('page', theme('favorite_nodes_view_' . variable_get(FAVORITE_NODES_PAGE_TYPE, 'table'), favorite_nodes_get($uid, $type), $uid, $type)); } -function theme_favorite_nodes_view($list = array()) { +function theme_favorite_nodes_view_teasers($list = array(), $uid = NULL, $type = NULL) { + $user = user_load(array('uid' => $uid)); + $output .= '

' . t('Favorite %type for %user', array('%type' => variable_get(FAVORITE_NODES_BLOCK . $type, $name), '%user' => theme('username', $user))) . '

' . "\n"; + + if (isset($list)) { + foreach($list as $nid => $data) { + $node = node_load($nid); + $output .= node_view($node, true); + } + } + $output .= theme('pager'); + + return $output; +} + +function theme_favorite_nodes_view_table($list = array(), $uid = NULL, $type = NULL) { global $user; + $account = user_load(array('uid' => $uid)); $rows = array(); $header = array(t('Title'), t('Added'), t('Operations')); @@ -130,19 +288,33 @@ $rows[] = array('data' => $result); } } - - return theme('table', $header, $rows); + + $output .= '

' . t('Favorite %type for %user', array('%type' => variable_get(FAVORITE_NODES_BLOCK . $type, $name), '%user' => theme('username', $account))) . '

' . "\n"; + $output .= theme('table', $header, $rows); + $output .= theme('pager'); + + return $output; } function favorite_nodes_user($op, &$edit, &$user, $category = null) { switch ($op) { case 'view': $fav_list = array(); + $node_types = node_get_types(); + natcasesort($node_types); foreach(node_get_types() as $type => $name) { if (variable_get(FAVORITE_NODES_NODE_TYPE . $type, 0)) { + $uid = intval($user->uid); + $favorites = favorite_nodes_get($uid, $type, variable_get(FAVORITE_NODES_BLOCK_LIMIT, 5)); + $items = array(); + if (count($favorites) > 0) { + foreach ($favorites as $favorite) { + $items[] = l($favorite->title, 'node/' . $favorite->nid); + } + } $fav_list[] = array( - 'title' => $type, - 'value' => l(t("Favorite $name"), "favorite_nodes/view/$user->uid/$type"), + 'title' => $name, + 'value' => theme('item_list', $items), ); } } @@ -171,11 +343,30 @@ db_query("DELETE FROM {favorite_nodes} WHERE uid = %d", $uid); } -function _favorite_nodes_get($uid, $type = null) { - $sql = "SELECT n.nid, n.title, f.uid, f.last FROM {node} n INNER JOIN {favorite_nodes} f USING(nid) WHERE n.type = '%s' AND f.uid = %d ORDER by f.last DESC"; - $result = db_query($sql, $type, $uid); +function favorite_nodes_get($uid, $type = NULL, $limit = NULL) { + if (is_null($limit)) { + $limit = variable_get(FAVORITE_NODES_PAGE_LIMIT, 10); + } + $row = array(); + if ($type && variable_get(FAVORITE_NODES_NODE_TYPE . $type, 0)) { + $sql = "SELECT n.nid, n.title, f.uid, f.last FROM {node} n INNER JOIN {favorite_nodes} f USING(nid) WHERE n.type = '%s' AND f.uid = %d ORDER by f.last DESC"; + + $result = pager_query($sql, $limit, 0, NULL, $type, $uid); + if ($result && db_num_rows($result) > 0) { + while ($data = db_fetch_object($result)) { + $row[$data->nid] = $data; + } + } + } + return $row; +} + +function favorite_nodes_get_users($nid) { + $sql = "SELECT u.*, f.last FROM {users} u INNER JOIN {favorite_nodes} f USING(uid) WHERE f.nid = %d ORDER by f.last DESC"; + $result = db_query($sql, $nid); + $row = array(); while ($data = db_fetch_object($result)) { - $row[$data->nid] = $data; + $row[$data->uid] = $data; } return $row;