/[drupal]/contributions/modules/stumble/stumble.module
ViewVC logotype

Contents of /contributions/modules/stumble/stumble.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Tue Mar 4 23:56:05 2008 UTC (20 months, 3 weeks ago) by davereid
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +57 -11 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_menu()
6 */
7 function stumble_menu($may_cache) {
8 $items = array();
9
10 if ($may_cache) {
11 $items[] = array(
12 'path' => 'stumble',
13 'title' => t('Stumble'),
14 'callback' => 'stumble_go',
15 'access' => user_access('access content'),
16 'type' => MENU_CALLBACK,
17 );
18 $items[] = array(
19 'path' => 'admin/settings/stumble',
20 'title' => 'Stumble',
21 'callback' => 'drupal_get_form',
22 'callback arguments' => array('stumble_admin_settings'),
23 'access' => user_access('administer site configuration'),
24 'type' => MENU_NORMAL_ITEM,
25 );
26 }
27
28 return $items;
29 }
30
31 /**
32 * Implementation of hook_link()
33 */
34 function stumble_link($type, $node = NULL, $teaser = FALSE) {
35 $links = array();
36
37 if ($type == 'node' && !$teaser && in_array($node->type, variable_get('stumble_node_types', array()))) {
38 $links['stumble'] = array(
39 'title' => t('Stumble'),
40 'href' => 'stumble/'. $node->type,
41 'attributes' => array(
42 'title' => 'Stumble to a random page on this site',
43 'class' => 'stumble',
44 ),
45 );
46 }
47
48 return $links;
49 }
50
51 /**
52 * Selects a random node and redirects the current request to that node.
53 * TODO: Make sure selected random isn't the current node?
54 */
55 function stumble_go($node_type = NULL) {
56 // Find the maximum node id for random number generation
57 $max_nid = db_result(db_query("SELECT MAX(nid) FROM {node}"));
58
59 // If we can freely stumble between node types, format the node types as a string for the SQL IN statement
60 if ($node_type == NULL || variable_get('stumble_free', FALSE) || !in_array($node_type, variable_get('stumble_node_types', array()))) {
61 $node_type = implode("', '", variable_get('stumble_node_types', array()));
62 }
63
64 // Run the query once looking greater than random number, and if a result
65 // is not found, look less than the same number.
66 $sql = "SELECT nid FROM {node} WHERE status = 1 AND nid >= %d AND type IN ('". $node_type ."') ORDER BY nid";
67 $random_nid = mt_rand(1, $max_nid);
68 $stumble_nid = db_result(db_query_range($sql, $random_nid, 0, 1));
69 if (empty($stumble_nid)) {
70 $sql = preg_replace('/>=/', '<', $sql);
71 $stumble_nid = db_result(db_query_range($sql, $random_nid, 0, 1));
72 }
73 if (!empty($stumble_nid)) {
74 drupal_goto('node/'. $stumble_nid);
75 }
76
77 // If no result was found, direct the user to the frontpage
78 //drupal_goto(variable_get('site_frontpage', 'node'));
79 }
80
81 function stumble_admin_settings() {
82 $form['stumble_node_types'] = array(
83 '#type' => 'checkboxes',
84 '#title' => t('Enable stumbling for the following content types'),
85 '#default_value' => variable_get('stumble_node_types', array()),
86 '#options' => node_get_types('names'),
87 );
88 $form['stumble_free'] = array(
89 '#type' => 'checkbox',
90 '#title' => t('Enable free stumbling between content types'),
91 '#default_value' => variable_get('stumble_free', FALSE),
92 '#description' => t('If enabled, stumbles may lead to different content types than the previously-viewed content. For example, a user viewing a story may then stumble to a page, blog entry, etc.'),
93 );
94 $form['array_filter'] = array('#type' => 'hidden');
95
96 return system_settings_form($form);
97 }

  ViewVC Help
Powered by ViewVC 1.1.2