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 | $message = ''; |
---|
51 | if (count($messages) > 1) { |
---|
52 | $message = theme('item_list', $messages); |
---|
53 | } |
---|
54 | else if ($messages) { |
---|
55 | $message = array_shift($messages); |
---|
56 | } |
---|
57 | |
---|
58 | if ($message) { |
---|
59 | $output .= "<div class=\"$type\">$message</div>"; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | return $output; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Format an analysis message. |
---|
68 | * |
---|
69 | * This tool should be called by any module responding to the analyze hook |
---|
70 | * to properly format the message. It is usually used in the form: |
---|
71 | * @code |
---|
72 | * $ret[] = views_ui_analysis(t('This is the message'), 'ok'); |
---|
73 | * @endcode |
---|
74 | * |
---|
75 | * The 'ok' status should be used to provide information about things |
---|
76 | * that are acceptable. In general analysis isn't interested in 'ok' |
---|
77 | * messages, but instead the 'warning', which is a category for items |
---|
78 | * that may be broken unless the user knows what he or she is doing, |
---|
79 | * and 'error' for items that are definitely broken are much more useful. |
---|
80 | * |
---|
81 | * @param $messages |
---|
82 | * The message to report. |
---|
83 | * @param $type |
---|
84 | * The type of message. This should be "ok", "warning" or "error". Other |
---|
85 | * values can be used but how they are treated by the output routine |
---|
86 | * is undefined. |
---|
87 | */ |
---|
88 | function views_ui_analysis($message, $type = 'error') { |
---|
89 | return array('message' => $message, 'type' => $type); |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Implementation of hook_views_analyze(). |
---|
94 | * |
---|
95 | * This is the basic views analysis that checks for very minimal problems. |
---|
96 | * There are other analysis tools in core specific sections, such as |
---|
97 | * node.views.inc as well. |
---|
98 | */ |
---|
99 | function views_ui_views_analyze($view) { |
---|
100 | $ret = array(); |
---|
101 | // Check for something other than the default display: |
---|
102 | if (count($view->display) < 2) { |
---|
103 | $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'); |
---|
104 | } |
---|
105 | foreach ($view->display as $display_id => $display) { |
---|
106 | if (empty($display->handler) || !empty($display->handler->broken)) { |
---|
107 | $ret[] = views_ui_analysis(t('Display plugin @plugin is not available.', array('@plugin' => $display->display_plugin)), 'error'); |
---|
108 | } |
---|
109 | |
---|
110 | $plugin = $display->handler->get_plugin('style'); |
---|
111 | if ($plugin) { |
---|
112 | $plugin->init($view, $display); |
---|
113 | if ($validate_messages = $plugin->validate()) { |
---|
114 | foreach ($validate_messages as $validate_message) { |
---|
115 | $ret[] = views_ui_analysis(t('Style plugin @plugin: @message', array('@plugin' => $plugin_name, '@message' => $validate_message))); |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | else { |
---|
120 | $ret[] = views_ui_analysis(t('Style plugin @plugin is not available.', array('@plugin' => $plugin_name)), 'error'); |
---|
121 | } |
---|
122 | |
---|
123 | foreach (views_object_types() as $type => $info) { |
---|
124 | $handlers = $display->handler->get_handlers($type); |
---|
125 | if ($handlers) { |
---|
126 | foreach ($handlers as $id => $handler) { |
---|
127 | if ($validate_messages = $handler->validate()) { |
---|
128 | foreach ($validate_messages as $message) { |
---|
129 | $ret[] = views_ui_analysis("$display_id: $id: $message", 'error'); |
---|
130 | } |
---|
131 | } |
---|
132 | if ($handler->broken()) { |
---|
133 | $ret[] = views_ui_analysis(t('@type handler @table.@field is not available.', array( |
---|
134 | '@type' => $info['stitle'], |
---|
135 | '@table' => $handler->table, |
---|
136 | '@field' => $handler->field, |
---|
137 | )), 'error'); |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | return $ret; |
---|
145 | } |
---|