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

Contents of /contributions/modules/top_node/top_node.module

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


Revision 1.1 - (show annotations) (download) (as text)
Wed Jul 11 16:50:05 2007 UTC (2 years, 4 months ago) by eaton
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Top Node lets you set up custom url paths that display the first node from a view. This allows you to set up urls like news/today or comics/latest or even blog/random whose node change based on the content listed in the selected view.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Allows administrators to map arbitrary paths to the top nodes in selected views.
7 */
8
9 function top_node_menu($may_cache = FALSE) {
10 $items = array();
11 if ($may_cache) {
12 $items[] = array('path' => 'admin/build/top_node',
13 'title' => t('Top nodes'),
14 'callback' => 'top_node_admin_page',
15 'access' => user_access('administer top nodes'),
16 'description' => t('Set up url paths that display a node from a dynamically updated list. Useful for urls like news/today or comic/latest that should display a single node that changes regularly.'),
17 'type' => MENU_NORMAL_ITEM
18 );
19 $items[] = array('path' => 'admin/build/top_node/list',
20 'title' => t('List'),
21 'callback' => 'top_node_admin_page',
22 'access' => user_access('administer top nodes'),
23 'type' => MENU_DEFAULT_LOCAL_TASK,
24 'weight' => '-1'
25 );
26 $items[] = array('path' => 'admin/build/top_node/add',
27 'title' => t('Add'),
28 'callback' => 'top_node_admin_add_page',
29 'access' => user_access('administer top nodes'),
30 'type' => MENU_LOCAL_TASK
31 );
32 $items[] = array('path' => 'admin/build/top_node/edit',
33 'title' => t('Edit'),
34 'callback' => 'top_node_admin_edit_page',
35 'access' => user_access('administer top nodes'),
36 'type' => MENU_CALLBACK
37 );
38 } else {
39 $paths = variable_get('top_node_paths', _top_node_default_paths());
40 foreach ($paths as $path => $data) {
41 $items[] = array(
42 'path' => $path,
43 'callback' => '_top_node_show_node',
44 'callback arguments' => array($data, $path),
45 'access' => TRUE,
46 );
47 }
48 }
49 return $items;
50 }
51
52 function top_node_perm() {
53 return array('administer top nodes');
54 }
55
56 function top_node_admin_page() {
57 $paths = variable_get('top_node_paths', _top_node_default_paths());
58
59 $headers = array(t('Path'), t('View'), t('Cache'), t('Redirect'), '');
60 $items = array();
61 foreach ($paths as $path => $data) {
62 $items[] = array(
63 l($path, $path),
64 $data['view'],
65 !empty($data['cache']) ? t('Yes') : t('No'),
66 !empty($data['redirect']) ? t('Yes') : t('No'),
67 l(t('Edit'), 'admin/build/top_node/edit/'. str_replace('/', '|', $path)),
68 );
69 }
70 if (!empty($items)) {
71 $output = theme('table', $headers, $items, array("cellpadding" => "4"), t('Existing Top Node paths'));
72 }
73 else {
74 $output .= '<p>'. t('No Top Node paths have currently been defined.') .'</p>';
75 }
76 return $output;
77 }
78
79 function top_node_admin_add_page() {
80 drupal_set_title(t('Add Top Node path'));
81 return drupal_get_form('top_node_edit_form');
82 }
83
84 function top_node_admin_edit_page($editpath = NULL) {
85 $editpath = str_replace('|', '/', $editpath);
86 $path = array();
87
88 $paths = variable_get('top_node_paths', _top_node_default_paths());
89 if (!empty($paths[$editpath])) {
90 $path = $paths[$editpath];
91 }
92 $path['path'] = $editpath;
93
94 drupal_set_title(t('Edit Top Node path: !path', array('!path' => $editpath)));
95 return drupal_get_form('top_node_edit_form', $path);
96 }
97
98 function top_node_edit_form($data = array()) {
99 $form = array();
100 $form['path'] = array(
101 '#type' => 'textfield',
102 '#required' => TRUE,
103 '#title' => t('Path'),
104 '#description' => t('The URL path for this top node. For example, comic/latest.'),
105 '#default_value' => $data['path'],
106 );
107 $options = array();
108 include_once(drupal_get_path('module', 'views') . '/views_cache.inc');
109 $default_views = _views_get_default_views();
110
111 $res = db_query("SELECT name FROM {view_view} ORDER BY name");
112 while ($view = db_fetch_object($res)) {
113 $options[$view->name] = $view->name;
114 }
115
116 if(is_array($default_views)) {
117 foreach($default_views as $key => $view) {
118 $options[$key] = $view->name;
119 }
120 }
121
122 $form['view'] = array(
123 '#type' => 'select',
124 '#title' => t('View'),
125 '#required' => TRUE,
126 '#options' => $options,
127 '#description' => t('The view from which the top node should be selected.'),
128 '#default_value' => $data['view']
129 );
130
131 $form['args'] = array(
132 '#type' => 'textarea',
133 '#title' => t('View arguments'),
134 '#required' => FALSE,
135 '#rows' => 3,
136 '#description' => t('A return-delimited list of arguments to pass into the selected view.'),
137 '#default_value' => !empty($data['args']) ? implode("\n", $data['args']) : '',
138 );
139
140 $period = array(0 => t('Never'));
141 $period += drupal_map_assoc(array(600, 3600, 10800, 21600, 32400, 43200, 86400), 'format_interval');
142
143 $form['cache'] = array(
144 '#type' => 'select',
145 '#title' => t('Cache lifespan'),
146 '#options' => $period,
147 '#description' => t('On high-traffic sites, or sites where the top node at this path changes infrequently, caching the node can reduce database load considerably.'),
148 '#default_value' => $data['cache'],
149 );
150
151 $form['redirect'] = array(
152 '#type' => 'checkbox',
153 '#title' => t('Redirect to node page'),
154 '#description' => t("By default, a top node is displayed at the url path you define. When this option is selected, users will be redirected to the node's native path (node/1, for example)."),
155 '#default_value' => $data['redirect'],
156 );
157
158 $form['buttons']['submit'] = array(
159 '#type' => 'submit',
160 '#value' => t('Submit'),
161 );
162 if (!empty($data['path'])) {
163 $form['buttons']['delete'] = array(
164 '#type' => 'submit',
165 '#value' => t('Delete'),
166 );
167 }
168
169 return $form;
170 }
171
172 function top_node_edit_form_validate($form_id, $form_values) {
173 $path = 'http://www.example.com'. $form_values['path'];
174 if (!valid_url($path)) {
175 form_set_error('path', t('The URL path you specified is invalid.'));
176 }
177 }
178
179 function top_node_edit_form_submit($form_id, $form_values) {
180 $paths = variable_get('top_node_paths', _top_node_default_paths());
181 $path = $form_values['path'];
182
183 if ($form_values['op'] == t('Delete')) {
184 unset($paths[$path]);
185 }
186 else {
187 $paths[$path] = $form_values;
188
189 $paths[$path]['args'] = explode("\n", $form_values['args']);
190 unset($paths[$path]['path']);
191 unset($paths[$path]['op']);
192 }
193
194 variable_set('top_node_paths', $paths);
195 return 'admin/build/top_node';
196 }
197
198 function _top_node_default_paths() {
199 $paths = array();
200 $paths['top_node'] = array(
201 'view' => 'tracker',
202 'args' => array(),
203 'cache' => FALSE,
204 'redirect' => FALSE,
205 );
206 return $paths;
207 }
208
209 function _top_node_show_node($data = array(), $path) {
210 if ($data['cache'] && $cache = cache_get('top_node:'. $path)) {
211 $nid = $cache->data;
212 } else {
213 $view = views_get_view($data['view']);
214 $result = views_build_view('items', $view, $data['args'], FALSE, 1);
215 if (count($result['items'])) {
216 $nid = $result['items'][0]->nid;
217 }
218 if ($data['cache']) {
219 cache_set('top_node:'. $path, 'cache', $nid, time() + $data['cache']);
220 }
221 }
222
223 if ($data['redirect']) {
224 drupal_goto('node/'. $nid);
225 }
226
227 if ($node = node_load($nid)) {
228 return node_page_view($node);
229 }
230 else {
231 drupal_not_found();
232 }
233 }

  ViewVC Help
Powered by ViewVC 1.1.2