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

Contents of /contributions/modules/viewtheme/viewtheme.module

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


Revision 1.1 - (show annotations) (download) (as text)
Wed Feb 28 04:27:35 2007 UTC (2 years, 8 months ago) by njivy
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit: Using views, change themes for node pages
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_help()
6 */
7 function viewtheme_help($section) {
8 switch ($section) {
9 case 'admin/build/viewtheme':
10 return '<p>'. t('When a node page is displayed, the rows below will be evaluated from top to bottom. The first match determines what theme a person will see. A match occurs when a node is in the view. If no views match, the default theme (%theme) will be used.', array('%theme' => variable_get('theme_default', 'garland'))) .'</p>';
11 break;
12 }
13 }
14
15 /**
16 * Implementation of hook_menu()
17 */
18 function viewtheme_menu($may_cache) {
19 $items = array();
20 if ($may_cache) {
21 $items[] = array('path' => 'admin/build/viewtheme',
22 'title' => t('View - Theme'),
23 'callback' => 'drupal_get_form',
24 'callback arguments' => array('viewtheme_admin_form'),
25 'access' => user_access('administer view themes'),
26 );
27 }
28 return $items;
29 }
30
31
32 /**
33 * Implementation of hook_perm()
34 */
35 function viewtheme_perm() {
36 return array('administer view themes');
37 }
38
39
40 /**
41 * Implementation of hook_nodeapi()
42 */
43 function viewtheme_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
44 if ($op == 'load') {
45 // TODO: Caching!
46
47 $result = db_query('SELECT vid,theme FROM {view_theme} ORDER BY weight');
48 while ($row = db_fetch_object($result)) {
49 $view = views_get_view($row->vid);
50 $view = views_build_view('queries', $view);
51
52 if (strpos($view['countquery'], ' WHERE ')) {
53 $query = $view['countquery'] ."AND node.nid = {$node->nid}";
54 }
55 else {
56 $query = $view['countquery'] . "WHERE node.nid = {$node->nid}";
57 }
58
59 $search = db_result(db_query($query));
60 if ($search) {
61 // The system default is 0.
62 if($row->theme) {
63 global $custom_theme;
64 $custom_theme = $row->theme;
65
66 // TODO: Caching!
67 }
68 return;
69 }
70 }
71 }
72 }
73
74 /**
75 * Administrative settings form
76 */
77 function viewtheme_admin_form() {
78 $themes = system_theme_data();
79 ksort($themes);
80 $options[0] = t('System default');
81 foreach ($themes as $theme) {
82 if (!$theme->status) {
83 continue;
84 }
85 $options[$theme->name] = $theme->name;
86 }
87
88 for ($i = -10; $i < 11; $i++) {
89 $weights[$i] = $i;
90 }
91
92 $form['vids']['#tree'] = TRUE;
93 $result = db_query("SELECT v.vid, v.name, v.description, vt.weight, vt.theme, vt.enabled FROM {view_view} v LEFT JOIN {view_theme} vt ON v.vid = vt.vid ORDER BY vt.weight, v.name");
94 while ($view = db_fetch_object($result)) {
95 $form['vids'][$view->vid]['edit_view'] = array(
96 '#value' => l(t('edit view'), 'admin/build/views/edit/'. $view->vid),
97 );
98 $form['vids'][$view->vid]['enabled'] = array(
99 '#type' => 'checkbox',
100 '#default_value' => $view->enabled,
101 );
102 $form['vids'][$view->vid]['name'] = array(
103 '#value' => check_plain($view->name),
104 );
105 $form['vids'][$view->vid]['description'] = array(
106 '#value' => check_plain($view->description),
107 );
108 $form['vids'][$view->vid]['theme'] = array(
109 '#type' => 'select',
110 '#default_value' => $view->theme,
111 '#options' => $options,
112 );
113 $form['vids'][$view->vid]['weight'] = array(
114 '#type' => 'select',
115 '#default_value' => (is_numeric($view->weight) ? $view->weight: 0),
116 '#options' => $weights,
117 );
118 }
119 $form['submit'] = array(
120 '#type' => 'submit',
121 '#value' => t('Submit'),
122 );
123 $form['#submit']['viewtheme_admin_form_submit'] = array();
124
125 return $form;
126 }
127
128 function viewtheme_admin_form_submit($form_id, $form_values) {
129 db_query('DELETE FROM {view_theme}');
130 foreach ($form_values['vids'] as $vid => $values) {
131 db_query("INSERT INTO {view_theme} (vid, theme, weight, enabled) VALUES (%d, '%s', %d, %d)", $vid, $values['theme'], $values['weight'], $values['enabled']);
132 }
133 drupal_set_message(t('The settings were updated.'));
134 cache_clear_all();
135 }
136
137 function theme_viewtheme_admin_form($form) {
138 $enabled_rows = array();
139 $disabled_rows = array();
140 foreach (element_children($form['vids']) as $vid => $row) {
141 if ($form['vids'][$vid]['enabled']['#default_value']) {
142 $enabled_rows[] = array(
143 drupal_render($form['vids'][$vid]['enabled']),
144 drupal_render($form['vids'][$vid]['name']),
145 drupal_render($form['vids'][$vid]['description']),
146 drupal_render($form['vids'][$vid]['theme']),
147 drupal_render($form['vids'][$vid]['weight']),
148 drupal_render($form['vids'][$vid]['edit_view']),
149 );
150 }
151 else {
152 $disabled_rows[] = array(
153 drupal_render($form['vids'][$vid]['enabled']),
154 drupal_render($form['vids'][$vid]['name']),
155 drupal_render($form['vids'][$vid]['description']),
156 drupal_render($form['vids'][$vid]['theme']),
157 drupal_render($form['vids'][$vid]['weight']),
158 drupal_render($form['vids'][$vid]['edit_view']),
159 );
160 }
161 }
162
163 $rows[] = array(array('data' => t('Enabled'), 'class' => 'region', 'colspan' => 6));
164 $rows = array_merge($rows, $enabled_rows);
165 $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => 6));
166 $rows = array_merge($rows, $disabled_rows);
167
168 $header = array('', t('View'), t('Description'), t('Theme'), t('Weight'), '');
169 $output = theme('table', $header, $rows, array('id' => 'viewtheme'));
170 $output .= drupal_render($form);
171 return $output;
172 }

  ViewVC Help
Powered by ViewVC 1.1.2