commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / views / includes / analyze.inc
1 <?php
2
3 /**
4 * @file
5 * Contains the view analyze tool code.
6 *
7 * This tool is a small plugin manager to perform analysis on a view and
8 * report results to the user. This tool is meant to let modules that
9 * provide data to Views also help users properly use that data by
10 * detecting invalid configurations. Views itself comes with only a
11 * small amount of analysis tools, but more could easily be added either
12 * by modules or as patches to Views itself.
13 */
14
15 /**
16 * Analyze a review and return the results.
17 *
18 * @return
19 * An array of analyze results organized into arrays keyed by 'ok',
20 * 'warning' and 'error'.
21 */
22 function views_analyze_view(&$view) {
23 $view->init_display();
24 $messages = module_invoke_all('views_analyze', $view);
25
26 return $messages;
27 }
28
29 /**
30 * Format the analyze result into a message string.
31 *
32 * This is based upon the format of drupal_set_message which uses separate
33 * boxes for "ok", "warning" and "error".
34 */
35 function views_analyze_format_result($view, $messages) {
36 if (empty($messages)) {
37 $messages = array(views_ui_analysis(t('View analysis can find nothing to report.'), 'ok'));
38 }
39
40 $types = array('ok' => array(), 'warning' => array(), 'error' => array());
41 foreach ($messages as $message) {
42 if (empty($types[$message['type']])) {
43 $types[$message['type']] = array();
44 }
45 $types[$message['type']][] = $message['message'];
46 }
47
48 $output = '';
49 foreach ($types as $type => $messages) {
50 $type .= ' messages';
51 $message = '';
52 if (count($messages) > 1) {
53 $message = theme('item_list', array('items' => $messages));
54 }
55 elseif ($messages) {
56 $message = array_shift($messages);
57 }
58
59 if ($message) {
60 $output .= "<div class=\"$type\">$message</div>";
61 }
62 }
63
64 return $output;
65 }
66
67 /**
68 * Format an analysis message.
69 *
70 * This tool should be called by any module responding to the analyze hook
71 * to properly format the message. It is usually used in the form:
72 * @code
73 * $ret[] = views_ui_analysis(t('This is the message'), 'ok');
74 * @endcode
75 *
76 * The 'ok' status should be used to provide information about things
77 * that are acceptable. In general analysis isn't interested in 'ok'
78 * messages, but instead the 'warning', which is a category for items
79 * that may be broken unless the user knows what he or she is doing,
80 * and 'error' for items that are definitely broken are much more useful.
81 *
82 * @param $messages
83 * The message to report.
84 * @param $type
85 * The type of message. This should be "ok", "warning" or "error". Other
86 * values can be used but how they are treated by the output routine
87 * is undefined.
88 */
89 function views_ui_analysis($message, $type = 'error') {
90 return array('message' => $message, 'type' => $type);
91 }
92
93 /**
94 * Implements hook_views_analyze().
95 *
96 * This is the basic views analysis that checks for very minimal problems.
97 * There are other analysis tools in core specific sections, such as
98 * node.views.inc as well.
99 */
100 function views_ui_views_analyze($view) {
101 $ret = array();
102 // Check for something other than the default display:
103 if (count($view->display) < 2) {
104 $ret[] = views_ui_analysis(t('This view has only a default display and therefore will not be placed anywhere on your site; perhaps you want to add a page or a block display.'), 'warning');
105 }
106 // You can give a page display the same path as an alias existing in the
107 // system, so the alias will not work anymore. Report this to the user,
108 // because he probably wanted something else.
109 foreach ($view->display as $id => $display) {
110 if (empty($display->handler)) {
111 continue;
112 }
113 if ($display->handler->has_path() && $path = $display->handler->get_option('path')) {
114 $normal_path = drupal_get_normal_path($path);
115 if ($path != $normal_path) {
116 $ret[] = views_ui_analysis(t('You have configured display %display with a path which is an path alias as well. This might lead to unwanted effects so better use an internal path.', array('%display' => $display->display_title)), 'warning');
117 }
118 }
119 }
120
121 return $ret;
122 }