1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Themeable function callbacks for diff.module. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Theme function to display the revisions formular with means to select |
---|
10 | * two revisions. |
---|
11 | */ |
---|
12 | function theme_diff_node_revisions($form) { |
---|
13 | $output = ''; |
---|
14 | |
---|
15 | // Overview table: |
---|
16 | $header = array( |
---|
17 | t('Revision'), |
---|
18 | array('data' => drupal_render($form['submit']), 'colspan' => 2), |
---|
19 | array('data' => t('Operations'), 'colspan' => 2) |
---|
20 | ); |
---|
21 | if (isset($form['info']) && is_array($form['info'])) { |
---|
22 | foreach (element_children($form['info']) as $key) { |
---|
23 | $row = array(); |
---|
24 | if (isset($form['operations'][$key][0])) { |
---|
25 | // Note: even if the commands for revert and delete are not permitted, |
---|
26 | // the array is not empty since we set a dummy in this case. |
---|
27 | $row[] = drupal_render($form['info'][$key]); |
---|
28 | $row[] = drupal_render($form['diff']['old'][$key]); |
---|
29 | $row[] = drupal_render($form['diff']['new'][$key]); |
---|
30 | $row[] = drupal_render($form['operations'][$key][0]); |
---|
31 | $row[] = drupal_render($form['operations'][$key][1]); |
---|
32 | $rows[] = $row; |
---|
33 | } |
---|
34 | else { |
---|
35 | // its the current revision (no commands to revert or delete) |
---|
36 | $row[] = array('data' => drupal_render($form['info'][$key]), 'class' => 'revision-current'); |
---|
37 | $row[] = array('data' => drupal_render($form['diff']['old'][$key]), 'class' => 'revision-current'); |
---|
38 | $row[] = array('data' => drupal_render($form['diff']['new'][$key]), 'class' => 'revision-current'); |
---|
39 | $row[] = array('data' => theme('placeholder', t('current revision')), 'class' => 'revision-current', 'colspan' => '2'); |
---|
40 | $rows[] = array( |
---|
41 | 'data' => $row, |
---|
42 | 'class' => 'error', |
---|
43 | ); |
---|
44 | } |
---|
45 | } |
---|
46 | } |
---|
47 | $output .= theme('table', $header, $rows); |
---|
48 | $output .= drupal_render($form); |
---|
49 | return $output; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Theme functions |
---|
54 | */ |
---|
55 | |
---|
56 | /** |
---|
57 | * Return a themed table. This is a modified version of theme_table, adding |
---|
58 | * colgroup tag and col tag options. |
---|
59 | * |
---|
60 | * @param $header |
---|
61 | * An array containing the table headers. Each element of the array can be |
---|
62 | * either a localized string or an associative array with the following keys: |
---|
63 | * - "data": The localized title of the table column. |
---|
64 | * - "field": The database field represented in the table column (required if |
---|
65 | * user is to be able to sort on this column). |
---|
66 | * - "sort": A default sort order for this column ("asc" or "desc"). |
---|
67 | * - Any HTML attributes, such as "colspan", to apply to the column header cell. |
---|
68 | * @param $rows |
---|
69 | * An array of table rows. Every row is an array of cells, or an associative |
---|
70 | * array with the following keys: |
---|
71 | * - "data": an array of cells |
---|
72 | * - Any HTML attributes, such as "class", to apply to the table row. |
---|
73 | * |
---|
74 | * Each cell can be either a string or an associative array with the following keys: |
---|
75 | * - "data": The string to display in the table cell. |
---|
76 | * - "header": Indicates this cell is a header. |
---|
77 | * - Any HTML attributes, such as "colspan", to apply to the table cell. |
---|
78 | * |
---|
79 | * Here's an example for $rows: |
---|
80 | * @verbatim |
---|
81 | * $rows = array( |
---|
82 | * // Simple row |
---|
83 | * array( |
---|
84 | * 'Cell 1', 'Cell 2', 'Cell 3' |
---|
85 | * ), |
---|
86 | * // Row with attributes on the row and some of its cells. |
---|
87 | * array( |
---|
88 | * 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => 'funky' |
---|
89 | * ) |
---|
90 | * ); |
---|
91 | * @endverbatim |
---|
92 | * |
---|
93 | * @param $attributes |
---|
94 | * An array of HTML attributes to apply to the table tag. |
---|
95 | * @param $caption |
---|
96 | * A localized string to use for the <caption> tag. |
---|
97 | * @param $cols |
---|
98 | * An array of table colum groups. Every column group is an array of columns, |
---|
99 | * or an associative array with the following keys: |
---|
100 | * - "data": an array of cells |
---|
101 | * - Any HTML attributes, such as "class", to apply to the table column group. |
---|
102 | * |
---|
103 | * Each column can be either an empty array or associative array with the following keys: |
---|
104 | * - Any HTML attributes, such as "class", to apply to the table column group. |
---|
105 | * |
---|
106 | * Here's an example for $cols: |
---|
107 | * @verbatim |
---|
108 | * $cols = array( |
---|
109 | * // Simple colgroup. |
---|
110 | * array(), |
---|
111 | * // Simple colgroup with attributes. |
---|
112 | * array( |
---|
113 | * 'data' => array(), 'colspan' => 2, 'style' => 'color: green;', |
---|
114 | * ), |
---|
115 | * // Simple colgroup with one col. |
---|
116 | * array( |
---|
117 | * array(), |
---|
118 | * ), |
---|
119 | * // Colgroup with attributes on the colgroup and some of its cols. |
---|
120 | * array( |
---|
121 | * 'data' => array(array('class' => 'diff-marker'), array('colspan' => 2)), 'class' => 'funky', |
---|
122 | * ), |
---|
123 | * ); |
---|
124 | * @endverbatim |
---|
125 | * |
---|
126 | * The HTML will look as follows: |
---|
127 | * @verbatim |
---|
128 | * <table> |
---|
129 | * <!-- Simple colgroup. --> |
---|
130 | * <colgroup /> |
---|
131 | * |
---|
132 | * <!-- Simple colgroup with attributes. --> |
---|
133 | * <colgroup colspan="2" style="color: green;" /> |
---|
134 | * |
---|
135 | * <!-- Simple colgroup with one col. --> |
---|
136 | * <colgroup> |
---|
137 | * <col /> |
---|
138 | * </colgroup> |
---|
139 | * |
---|
140 | * <!-- Colgroup with attributes on the colgroup and some of its cols. --> |
---|
141 | * <colgroup class="funky"> |
---|
142 | * <col class="diff-marker" /> |
---|
143 | * <col colspan="2" /> |
---|
144 | * </colgroup> |
---|
145 | * ... |
---|
146 | * </table> |
---|
147 | * @endverbatim |
---|
148 | * |
---|
149 | * @return |
---|
150 | * An HTML string representing the table. |
---|
151 | */ |
---|
152 | function theme_diff_table($header, $rows, $attributes = array(), $caption = NULL, $cols = array()) { |
---|
153 | $output = '<table'. drupal_attributes($attributes) .">\n"; |
---|
154 | |
---|
155 | if (isset($caption)) { |
---|
156 | $output .= '<caption>'. $caption ."</caption>\n"; |
---|
157 | } |
---|
158 | |
---|
159 | // Format the table columns: |
---|
160 | if (count($cols)) { |
---|
161 | foreach ($cols as $number => $col) { |
---|
162 | $attributes = array(); |
---|
163 | |
---|
164 | // Check if we're dealing with a simple or complex column |
---|
165 | if (isset($col['data'])) { |
---|
166 | foreach ($col as $key => $value) { |
---|
167 | if ($key == 'data') { |
---|
168 | $cells = $value; |
---|
169 | } |
---|
170 | else { |
---|
171 | $attributes[$key] = $value; |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | else { |
---|
176 | $cells = $col; |
---|
177 | } |
---|
178 | |
---|
179 | // Build colgroup |
---|
180 | if (is_array($cells) && count($cells)) { |
---|
181 | $output .= ' <colgroup'. drupal_attributes($attributes) .'>'; |
---|
182 | $i = 0; |
---|
183 | foreach ($cells as $cell) { |
---|
184 | $output .= ' <col'. drupal_attributes($cell) .' />'; |
---|
185 | } |
---|
186 | $output .= " </colgroup>\n"; |
---|
187 | } |
---|
188 | else { |
---|
189 | $output .= ' <colgroup'. drupal_attributes($attributes) ." />\n"; |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | // Format the table header: |
---|
195 | if (count($header)) { |
---|
196 | $ts = tablesort_init($header); |
---|
197 | $output .= ' <thead><tr>'; |
---|
198 | foreach ($header as $cell) { |
---|
199 | $cell = tablesort_header($cell, $header, $ts); |
---|
200 | $output .= _theme_table_cell($cell, TRUE); |
---|
201 | } |
---|
202 | $output .= " </tr></thead>\n"; |
---|
203 | } |
---|
204 | |
---|
205 | // Format the table rows: |
---|
206 | $output .= "<tbody>\n"; |
---|
207 | if (count($rows)) { |
---|
208 | $flip = array('even' => 'odd', 'odd' => 'even'); |
---|
209 | $class = 'even'; |
---|
210 | foreach ($rows as $number => $row) { |
---|
211 | $attributes = array(); |
---|
212 | |
---|
213 | // Check if we're dealing with a simple or complex row |
---|
214 | if (isset($row['data'])) { |
---|
215 | foreach ($row as $key => $value) { |
---|
216 | if ($key == 'data') { |
---|
217 | $cells = $value; |
---|
218 | } |
---|
219 | else { |
---|
220 | $attributes[$key] = $value; |
---|
221 | } |
---|
222 | } |
---|
223 | } |
---|
224 | else { |
---|
225 | $cells = $row; |
---|
226 | } |
---|
227 | |
---|
228 | // Add odd/even class |
---|
229 | $class = $flip[$class]; |
---|
230 | if (isset($attributes['class'])) { |
---|
231 | $attributes['class'] .= ' '. $class; |
---|
232 | } |
---|
233 | else { |
---|
234 | $attributes['class'] = $class; |
---|
235 | } |
---|
236 | |
---|
237 | // Build row |
---|
238 | $output .= ' <tr'. drupal_attributes($attributes) .'>'; |
---|
239 | $i = 0; |
---|
240 | foreach ($cells as $cell) { |
---|
241 | $cell = tablesort_cell($cell, $header, $ts, $i++); |
---|
242 | $output .= _theme_table_cell($cell); |
---|
243 | } |
---|
244 | $output .= " </tr>\n"; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | $output .= "</tbody></table>\n"; |
---|
249 | return $output; |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * Theme function for a header line in the diff. |
---|
254 | */ |
---|
255 | function theme_diff_header_line($lineno) { |
---|
256 | return '<strong>'. t('Line %lineno', array('%lineno' => $lineno)) .'</strong>'; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * Theme function for a content line in the diff. |
---|
261 | */ |
---|
262 | function theme_diff_content_line($line) { |
---|
263 | return '<div>'. $line .'</div>'; |
---|
264 | } |
---|
265 | |
---|
266 | /** |
---|
267 | * Theme function for an empty line in the diff. |
---|
268 | */ |
---|
269 | function theme_diff_empty_line($line) { |
---|
270 | return $line; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * Theme function for inline diff form. |
---|
275 | */ |
---|
276 | function theme_diff_inline_form($form) { |
---|
277 | drupal_add_css(drupal_get_path('module', 'diff') .'/diff.css'); |
---|
278 | return drupal_render($form); |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Display inline diff metadata. |
---|
283 | */ |
---|
284 | function theme_diff_inline_metadata($node) { |
---|
285 | drupal_add_css(drupal_get_path('module', 'diff') .'/diff.css'); |
---|
286 | $output = "<div class='diff-inline-metadata clear-block'>"; |
---|
287 | $output .= "<div class='diff-inline-byline'>"; |
---|
288 | $output .= t('Updated by !name on @date', array( |
---|
289 | '!name' => theme('username', $node), |
---|
290 | '@date' => format_date($node->revision_timestamp, 'small'), |
---|
291 | )); |
---|
292 | $output .= "</div>"; |
---|
293 | $output .= "<div class='diff-inline-legend clear-block'>"; |
---|
294 | $output .= "<label>". t('Legend') ."</label>"; |
---|
295 | $output .= theme('diff_inline_chunk', t('Added'), 'add'); |
---|
296 | $output .= theme('diff_inline_chunk', t('Changed'), 'change'); |
---|
297 | $output .= theme('diff_inline_chunk', t('Deleted'), 'delete'); |
---|
298 | $output .= "</div>"; |
---|
299 | $output .= "</div>"; |
---|
300 | return $output; |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * Theme a span of changed text in an inline diff display. |
---|
305 | */ |
---|
306 | function theme_diff_inline_chunk($text, $type = NULL) { |
---|
307 | switch ($type) { |
---|
308 | case 'add': |
---|
309 | return "<span class='diff-added'>{$text}</span>"; |
---|
310 | case 'change': |
---|
311 | return "<span class='diff-changed'>{$text}</span>"; |
---|
312 | case 'delete': |
---|
313 | return "<span class='diff-deleted'>{$text}</span>"; |
---|
314 | default: |
---|
315 | return $text; |
---|
316 | } |
---|
317 | } |
---|