Initial start at view analysis tool
[project/views.git] / views_ui.module
1 <?php
2 // $Id$
3 /**
4 * @file views_ui.module
5 * Provide structure for the administrative interface to Views.
6 */
7
8 function views_ui_menu() {
9 $items = array();
10
11 // Minor code reduction technique
12 $base = array(
13 'access callback' => 'user_access',
14 'access arguments' => array('administer views'),
15 'file' => 'includes/admin.inc',
16 );
17
18 $callback = $base + array('type' => MENU_CALLBACK);
19
20 $convert = array('file' => 'includes/convert.inc') + $base;
21
22 $items['admin/build/views'] = $base + array(
23 'title' => 'Views',
24 'page callback' => 'views_ui_list_views',
25 'description' => 'Views are customized lists of content on your system; they are highly configurable and give you control over how lists of content are presented.',
26 'type' => MENU_NORMAL_ITEM
27 );
28 $items['admin/build/views/list'] = $base + array(
29 'title' => 'List',
30 'page callback' => 'views_ui_list_views',
31 'type' => MENU_DEFAULT_LOCAL_TASK,
32 'weight' => '-1'
33 );
34 $items['admin/build/views/add'] = $base + array(
35 'title' => 'Add',
36 'page callback' => 'views_ui_add_page',
37 'type' => MENU_LOCAL_TASK
38 );
39 $items['admin/build/views/import'] = $base + array(
40 'title' => 'Import',
41 'page callback' => 'drupal_get_form',
42 'page arguments' => array('views_ui_import_page'),
43 'type' => MENU_LOCAL_TASK
44 );
45 $items['admin/build/views/tools'] = $base + array(
46 'title' => 'Tools',
47 'page callback' => 'drupal_get_form',
48 'page arguments' => array('views_ui_admin_tools'),
49 'type' => MENU_LOCAL_TASK
50 );
51 $items['admin/build/views/tools/basic'] = $base + array(
52 'title' => 'Basic',
53 'page callback' => 'drupal_get_form',
54 'page arguments' => array('views_ui_admin_tools'),
55 'type' => MENU_DEFAULT_LOCAL_TASK,
56 'weight' => -10,
57 );
58
59 $items['admin/build/views/tools/convert'] = $convert + array(
60 'title' => 'Convert',
61 'description' => 'Convert stored Views 1 views.',
62 'page callback' => 'views_ui_admin_convert',
63 'type' => MENU_LOCAL_TASK,
64 'weight' => 1,
65 );
66 $items['admin/build/views1/delete'] = $convert + array(
67 'title' => 'Delete view',
68 'page callback' => 'drupal_get_form',
69 'page arguments' => array('views_ui_delete1_confirm', 4),
70 'type' => MENU_CALLBACK,
71 );
72 $items['admin/build/views1/convert'] = $convert + array(
73 'title' => 'Convert view',
74 'page callback' => 'views_ui_convert1',
75 'page arguments' => array(4),
76 'type' => MENU_CALLBACK,
77 );
78
79 $items['admin/build/views/delete/%views_ui_cache'] = $callback + array(
80 'title' => 'Delete view',
81 'page callback' => 'drupal_get_form',
82 'page arguments' => array('views_ui_delete_confirm', 4),
83 );
84 $items['admin/build/views/break-lock/%views_ui_cache'] = $callback + array(
85 'title' => 'Delete view',
86 'page callback' => 'drupal_get_form',
87 'page arguments' => array('views_ui_break_lock_confirm', 4),
88 );
89 $items['admin/build/views/export/%views_ui_cache'] = $callback + array(
90 'page callback' => 'drupal_get_form',
91 'page arguments' => array('views_ui_export_page', 4),
92 );
93 $items['admin/build/views/clone/%views_ui_cache'] = $callback + array(
94 'page callback' => 'views_ui_clone_page',
95 'page arguments' => array(4),
96 );
97 $items['admin/build/views/enable/%views_ui_default'] = $callback + array(
98 'page callback' => 'views_ui_enable_page',
99 'page arguments' => array(4),
100 );
101 $items['admin/build/views/disable/%views_ui_default'] = $callback + array(
102 'page callback' => 'views_ui_disable_page',
103 'page arguments' => array(4),
104 );
105
106 // Many line items for editing a view.
107 $items['admin/build/views/edit/%views_ui_cache'] = $base + array(
108 'title' => 'Edit',
109 'page callback' => 'views_ui_edit_page',
110 'page arguments' => array(4),
111 'type' => MENU_LOCAL_TASK
112 );
113 // lots of little edit form pieces.
114 $items['admin/build/views/%views_ui_js/analyze/%views_ui_cache'] = $callback + array(
115 'page callback' => 'views_ui_analyze_view',
116 'page arguments' => array(3, 5),
117 );
118 $items['admin/build/views/%views_ui_js/details/%views_ui_cache'] = $callback + array(
119 'page callback' => 'views_ui_edit_details',
120 'page arguments' => array(3, 5),
121 );
122 $items['admin/build/views/%views_ui_js/add-display/%views_ui_cache'] = $callback + array(
123 'page callback' => 'views_ui_add_display',
124 'page arguments' => array(3, 5),
125 );
126 $items['admin/build/views/%views_ui_js/remove-display/%views_ui_cache'] = $callback + array(
127 'page callback' => 'views_ui_remove_display',
128 'page arguments' => array(3, 5),
129 );
130 $items['admin/build/views/%views_ui_js/rearrange/%views_ui_cache'] = $callback + array(
131 'page callback' => 'views_ui_rearrange_type',
132 'page arguments' => array(3, 5),
133 );
134 $items['admin/build/views/%views_ui_js/config-type/%views_ui_cache'] = $callback + array(
135 'page callback' => 'views_ui_config_type',
136 'page arguments' => array(3, 5),
137 );
138 $items['admin/build/views/%views_ui_js/add-item/%views_ui_cache'] = $callback + array(
139 'page callback' => 'views_ui_add_item',
140 'page arguments' => array(3, 5),
141 );
142 $items['admin/build/views/%views_ui_js/config-item/%views_ui_cache'] = $callback + array(
143 'page callback' => 'views_ui_config_item',
144 'page arguments' => array(3, 5),
145 );
146 $items['admin/build/views/%views_ui_js/config-item-extra/%views_ui_cache'] = $callback + array(
147 'page callback' => 'views_ui_config_item_extra',
148 'page arguments' => array(3, 5),
149 );
150 // display specific parameters
151 $items['admin/build/views/%views_ui_js/display/%views_ui_cache'] = $callback + array(
152 'page callback' => 'views_ui_edit_display',
153 'page arguments' => array(3, 5),
154 );
155 // Special style plugin stuff for arguments
156 $items['admin/build/views/%views_ui_js/change-style/%views_ui_cache'] = $callback + array(
157 'page callback' => 'views_ui_change_style',
158 'page arguments' => array(3, 5),
159 );
160 $items['admin/build/views/%views_ui_js/config-style/%views_ui_cache'] = $callback + array(
161 'page callback' => 'views_ui_config_style',
162 'page arguments' => array(3, 5),
163 );
164 // Live preview
165 $items['admin/build/views/%views_ui_js/preview/%views_ui_cache'] = $callback + array(
166 'page callback' => 'views_ui_preview',
167 'page arguments' => array(3, 5),
168 );
169
170 // autocompletes for handlers and such
171 $items['admin/build/views/autocomplete/user'] = $callback + array(
172 'page callback' => 'views_ui_autocomplete_user',
173 );
174
175
176 return $items;
177 }
178
179 function views_ui_help($path, $arg) {
180 switch ($path) {
181 case 'admin/build/views/tools/convert':
182 return '<p>' . t('The converter will make a best-effort attempt to convert a Views 1 view to Views 2. This conversion is not reliable; you will very likely have to make adjustments to your view to get it to match. You can import Views 1 views through the normal Import tab.') . '</p>';
183 }
184 }
185
186 /*
187 * Implementation of hook_perm()
188 */
189 function views_ui_perm() {
190 return array('administer views');
191 }
192
193 function views_ui_theme() {
194 $path = drupal_get_path('module', 'views');
195 require_once "./$path/includes/admin.inc";
196
197 return array(
198 'views_ui_edit_view' => array(
199 'arguments' => array('view' => NULL),
200 'template' => 'views-ui-edit-view',
201 'path' => "$path/theme",
202 ),
203 'views_ui_edit_tab' => array(
204 'arguments' => array('view' => NULL, 'display' => NULL),
205 'template' => 'views-ui-edit-tab',
206 'path' => "$path/theme",
207 ),
208 'views_ui_edit_item' => array(
209 'arguments' => array('type' => NULL, 'view' => NULL, 'display' => NULL),
210 'template' => 'views-ui-edit-item',
211 'path' => "$path/theme",
212 ),
213 'views_ui_rearrange_form' => array(
214 'arguments' => array('form' => NULL),
215 'file' => '/includes/admin.inc',
216 ),
217 'views_tabset' => array(
218 'arguments' => array('tabs' => NULL),
219 'file' => '/includes/tabs.inc',
220 ),
221 'views_tab' => array(
222 'arguments' => array('body' => NULL),
223 'file' => '/includes/tabs.inc',
224 ),
225
226 'views_ui_style_plugin_table' => array(
227 'arguments' => array('form' => NULL),
228 ),
229
230 );
231 }
232
233 /**
234 * Specialized menu callback to load a view either out of the cache or just
235 * load it.
236 */
237 function views_ui_cache_load($name) {
238 views_include('cache');
239 views_include('view');
240 $view = views_object_cache_get('view', $name);
241
242 if (empty($view)) {
243 $v = views_get_view($name);
244 if ($v) {
245 $view = drupal_clone($v);
246 }
247
248 if (!empty($view)) {
249 // Check to see if someone else is already editing this view.
250 global $user;
251 $view->locked = db_fetch_object(db_query("SELECT s.uid, v.updated FROM {views_object_cache} v INNER JOIN {sessions} s ON v.sid = s.sid WHERE s.uid != %d and v.name = '%s' and v.obj = 'view' ORDER BY v.updated ASC", $user->uid, $view->name));
252 }
253 }
254
255 if (empty($view)) {
256 return FALSE;
257 }
258
259 else {
260 return $view;
261 }
262 }
263
264 /**
265 * Specialized cache function to add a flag to our view, include an appropriate
266 * include, and cache more easily.
267 */
268 function views_ui_cache_set(&$view) {
269 if (!empty($view->locked)) {
270 drupal_set_message('Changes cannot be made to a locked view.', 'error');
271 return;
272 }
273 views_include('cache');
274 $view->changed = TRUE; // let any future object know that this view has changed.
275
276 // Unset handlers; we don't want to write these into the cache
277 unset($view->display_handler);
278 unset($view->current_display);
279 unset($view->default_display);
280 foreach (array_keys($view->display) as $id) {
281 unset($view->display[$id]->handler);
282 unset($view->display[$id]->default_display);
283 }
284 views_object_cache_set('view', $view->name, $view);
285 }
286
287
288 /**
289 * Specialized menu callback to load a view that is only a default
290 * view.
291 */
292 function views_ui_default_load($name) {
293 $view = views_get_view($name);
294 if ($view->type == t('Default')) {
295 return $view;
296 }
297
298 return FALSE;
299 }
300
301 /**
302 * Check to see if the incoming menu item is js capable or not.
303 */
304 function views_ui_js_load($js) {
305 if ($js == 'ajax') {
306 return TRUE;
307 }
308 return 0;
309 }