Issue #1081286 by Berdir: Fixed url prefix user/%user/messages is broken when private...
[project/privatemsg.git] / privatemsg.theme.inc
1 <?php
2
3 /**
4 * @file
5 * Theme functions for privatemsg.
6 */
7
8 /**
9 * @defgroup theming Theming documentation
10 *
11 * It is possible to theme many aspect of privatemsg with theme functions.
12 *
13 * For the thread list, so called theme patterns are used to allow flexible
14 * theming of the table and its columns (including columns added by other
15 * modules).
16 *
17 * Columns are defined with hook_privatemsg_header_info(). The default weight
18 * and if they are enabled or not can by set by default but can be overriden in
19 * the admin user interface.
20 *
21 * Additionally, a theme function needs to be defined that defaults to the name
22 * 'privatemsg_list_field__$key', where $key is the name of the header/column.
23 *
24 * Every theme function will receive the $thread array as single argument, which
25 * is everything that has been returned by the query built in
26 * privatemsg_sql_list(). The theme function should return either a string or a
27 * theme_table() compatible array.
28 *
29 * To change what information is available in that array, it is possible to
30 * either implement hook_privatemsg_sql_list_alter() or alter $form['#data'] of
31 * privatemsg_list(). See privatemsg_filter_form_privatemsg_list_alter() for an
32 * example.
33 *
34 * To override an already existing theme function, use the following structure:
35 * themename_privatemsg_list_field_fieldname. It is possible to change the
36 * header definition with hook_privatemsg_header_info_alter() but that is often
37 * not necessary.
38 */
39
40 /**
41 * @addtogroup theming
42 * @{
43 */
44
45 /**
46 * Theme the participants field.
47 */
48 function phptemplate_privatemsg_list_field__participants($thread) {
49 $participants = _privatemsg_generate_user_array($thread['participants'], -4);
50 $field = array();
51 $field['data'] = _privatemsg_format_participants($participants, 3, TRUE);
52 $field['class'] = 'privatemsg-list-participants';
53 return $field;
54 }
55
56 /**
57 * Theme the subject of the thread.
58 */
59 function phptemplate_privatemsg_list_field__subject($thread) {
60 $field = array();
61 $options = array();
62 $is_new = '';
63 if (!empty($thread['is_new'])) {
64 $is_new = theme_mark(MARK_NEW);
65 $options['fragment'] = 'new';
66 }
67 $field['data'] = l($thread['subject'], privatemsg_get_dynamic_url_prefix() . '/view/' . $thread['thread_id'], $options) . $is_new;
68 $field['class'] = 'privatemsg-list-subject';
69 return $field;
70 }
71
72 /**
73 * Theme the replies field.
74 */
75 function phptemplate_privatemsg_list_field__count($thread) {
76 $field = array();
77 $field['data'] = $thread['count'];
78 $options = array();
79 if (!empty($thread['is_new']) && $thread['is_new'] < $thread['count']) {
80 $options['fragment'] = 'new';
81 $field['data'] .= '<br />' . l((format_plural($thread['is_new'], '(1 new)', '(@count new)')), privatemsg_get_dynamic_url_prefix() . '/view/' . $thread['thread_id'], $options);
82 }
83 $field['class'] = 'privatemsg-list-count';
84 return $field;
85 }
86
87 /**
88 * Theme the last updated column.
89 */
90 function phptemplate_privatemsg_list_field__last_updated($thread) {
91 $field = array();
92 $field['data'] = format_date($thread['last_updated'], 'small');
93 $field['class'] = 'privatemsg-list-date';
94 return $field;
95 }
96
97 /**
98 * Theme the thread started column.
99 */
100 function phptemplate_privatemsg_list_field__thread_started($thread) {
101 $field = array();
102 $field['data'] = format_date($thread['thread_started'], 'small');
103 $field['class'] = 'privatemsg-list-date-started';
104 return $field;
105 }
106
107 /**
108 * Theme to display the privatemsg list.
109 *
110 * This theme builds a table with paging based on the data which has been built
111 * by the header and field theme patterns.
112 */
113 function theme_privatemsg_list($form) {
114 $has_posts = !empty($form['#data']);
115
116 drupal_add_css(drupal_get_path('module', 'privatemsg') .'/styles/privatemsg-list.css');
117
118 // Get the headers.
119 $headers = privatemsg_get_headers(TRUE);
120 $themed_rows = array();
121 // Check if there is atleast a single thread.
122 if ($has_posts) {
123 foreach ($form['#data'] as $thread_id => $data) {
124 // Theme the row.
125 $row = _privatemsg_list_thread($data);
126 $data = array();
127 // Render the checkbox.
128 $data[] = array('data' => drupal_render($form['threads'][$thread_id]), 'class' => 'privatemsg-list-select');
129
130 // Store the #rows data in the same order as the header is,
131 // the key property of the header refers to the field that belongs to it.
132 foreach ($headers as $key => $header) {
133 $data[] = isset($row['data'][$key]) ? $row['data'][$key] : '';
134 }
135 // Replace the data
136 $row['data'] = $data;
137 $themed_rows[] = $row;
138 }
139 // Add select all checkox to header array.
140 array_unshift($headers, theme('table_select_header_cell'));
141 }
142 else {
143 // Display a message if now messages are available.
144 $themed_rows[] = array(array('data' => t('No messages available.'), 'colspan' => count($headers)));
145 }
146
147 // Remove any data in header that we don't need anymore.
148 foreach ($headers as $id => $header) {
149 unset($headers[$id]['key']);
150 unset($headers[$id]['#weight']);
151 }
152
153 // Theme the table, pass all generated information to the table theme function.
154 $form['list'] = array('#value' => theme('table', $headers, $themed_rows, array('class' => 'privatemsg-list')), '#weight' => 5);
155 return drupal_render($form);
156 }
157
158 /**
159 * Theme a block which displays the number of new messages a user has.
160 */
161 function theme_privatemsg_new_block($count) {
162 if ($count == 0) {
163 $text = t('Click here to go to your messages.');
164 }
165 else {
166 $text = format_plural($count, 'You have a new message! Click here to read it.',
167 'You have @count new messages! Click here to read them.',
168 array('@count' => $count));
169 }
170
171 return l($text, privatemsg_get_dynamic_url_prefix(), array('attributes' => array('id' => 'privatemsg-new-link')));
172 }
173
174 /**
175 * Used to theme and display user recipients.
176 *
177 * Wrapper for theme_username() with a few additional options.
178 */
179 function theme_privatemsg_username($recipient, $options) {
180 if (!isset($recipient->uid)) {
181 $recipient->uid = $recipient->recipient;
182 }
183 if (!empty($options['plain'])) {
184 $name = $recipient->name;
185 if (!empty($options['unique'])) {
186 $name .= ' [user]';
187 }
188 return $name;
189 }
190 else {
191 return theme('username', $recipient);
192 }
193 }
194
195 /**
196 * Output the admin settings display fields and weight settings as a
197 * drag and drop sortable table.
198 */
199 function theme_privatemsg_admin_settings_display_fields($element = array()) {
200 $header = array(
201 array('data' => t('Field'), 'class' => 'field'),
202 array('data' => t('Enable'), 'class' => 'enable'),
203 array('data' => t('Weight'), 'class' => 'weight'),
204 );
205 $rows = array();
206 foreach (element_children($element['privatemsg_display_fields']) as $child) {
207 $row = array();
208
209 // Title.
210 $row[] = array('data' => $element['privatemsg_display_fields'][$child]['#title'], 'class' => 'field');
211 unset($element['privatemsg_display_fields'][$child]['#title']);
212
213 // Enable checkbox.
214 $row[] = array('data' => drupal_render($element['privatemsg_display_fields'][$child]), 'class' => 'enable');
215
216 // Weight selector.
217 unset($element['privatemsg_display_fields_weights'][$child]['#title']);
218 $element['privatemsg_display_fields_weights'][$child]['#attributes']['class'] = 'privatemsg-display-fields-weight';
219 $row[] = array(
220 'data' => drupal_render($element['privatemsg_display_fields_weights'][$child]),
221 'class' => 'weight',
222 );
223
224 $rows[] = array('data' => $row, 'class' => 'draggable');
225 }
226 if (!empty($rows)) {
227 drupal_add_tabledrag('privatemsg-list-display-fields', 'order', 'sibling', 'privatemsg-display-fields-weight');
228 return theme('table', $header, $rows, array('id' => 'privatemsg-list-display-fields')) . drupal_render($element);
229 }
230 else {
231 return drupal_render($element);
232 }
233 }
234
235 /**
236 * @}
237 */