/[drupal]/contributions/modules/user_display/user_display.module
ViewVC logotype

Contents of /contributions/modules/user_display/user_display.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sat Jul 19 23:12:33 2008 UTC (16 months, 1 week ago) by sun
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of User Display API.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Provides a centralized and highly configurable API for displaying users.
7 */
8
9 require_once(drupal_get_path('module', 'user_display') .'/user_display.theme.inc');
10
11 define('USER_DISPLAY_NO_PICTURE', 0);
12 define('USER_DISPLAY_DEFAULT_PICTURE', -1);
13
14 /**
15 * Return configured User Display styles.
16 */
17 function user_display_styles() {
18 return variable_get('user_display_styles', array());
19 }
20
21 /**
22 * Return user display styles as FAPI options.
23 */
24 function user_display_styles_options() {
25 static $options;
26 if (isset($options)) {
27 return $options;
28 }
29 $styles = user_display_styles();
30 $options = array();
31 foreach ($styles as $id => $style) {
32 $options[$id] = $style['name'];
33 }
34 asort($options);
35 return $options;
36 }
37
38 /**
39 * Return a User Display style by id.
40 */
41 function user_display_style($id) {
42 $styles = user_display_styles();
43 return isset($styles[$id]) ? $styles[$id] : FALSE;
44 }
45
46 /**
47 * Save a User Display style.
48 */
49 function user_display_style_save($style) {
50 $styles = user_display_styles();
51 // Replace an existing style.
52 if (isset($style['id']) && isset($styles[$style['id']])) {
53 $styles[$style['id']] = $style;
54 }
55 // Add a new style.
56 else {
57 $new_id = count($styles);
58 $style['id'] = $new_id;
59 $styles[$new_id] = $style;
60 }
61 variable_set('user_display_styles', $styles);
62 return $style;
63 }
64
65 /**
66 * Delete a User Display style.
67 */
68 function user_display_style_delete($style) {
69 $styles = user_display_styles();
70 if (isset($style['id']) && isset($styles[$style['id']])) {
71 unset($styles[$style['id']]);
72 variable_set('user_display_styles', $styles);
73 }
74 }
75
76 /**
77 * Return available user display elements.
78 */
79 function user_display_elements() {
80 return module_invoke_all('user_display', 'elements');
81 }
82
83 /**
84 * Implementation of hook_form_alter().
85 */
86 function user_display_form_alter($form_id, &$form) {
87 switch ($form_id) {
88 case 'user_edit':
89 $form['#validate']['user_display_user_edit_validate'] = array();
90 $form['#submit']['user_display_user_edit_submit'] = array();
91 break;
92
93 case 'user_admin_settings':
94 $presets = user_display_imagecache_presets();
95 $settings = variable_get('user_display', array());
96
97 $form['pictures']['user_display']['#tree'] = TRUE;
98 $form['pictures']['user_display']['default'] = array(
99 '#type' => 'select',
100 '#title' => t('Default ImageCache preset used for user pictures'),
101 '#default_value' => isset($settings['default']) ? $settings['default'] : USER_DISPLAY_NO_PICTURE,
102 '#options' => $presets,
103 '#description' => t('This sets the default user picture size throughout the site.'),
104 );
105 $form['pictures']['user_display']['default_profile'] = array(
106 '#type' => 'select',
107 '#title' => t('ImageCache preset used for user profile picture'),
108 '#default_value' => isset($settings['default_profile']) ? $settings['default_profile'] : USER_DISPLAY_NO_PICTURE,
109 '#options' => $presets,
110 '#description' => t("This sets the picture size when viewing a user's profile page."),
111 );
112 $form['pictures']['user_picture_min_dimensions'] = array(
113 '#type' => 'textfield',
114 '#title' => t('Picture minimum dimensions'),
115 '#default_value' => variable_get('user_picture_min_dimensions', '10x10'),
116 '#size' => 15,
117 '#maxlength' => 10,
118 '#description' => t('Minimum dimensions for pictures, in pixels.')
119 );
120 $form['#submit'] = array('user_display_user_admin_settings_submit' => array()) + $form['#submit'];
121 break;
122 }
123 }
124
125 function user_display_user_admin_settings_submit($form_id, $form_values) {
126 $settings = variable_get('user_display', array());
127 $settings = array_merge($settings, $form_values['user_display']);
128 variable_set('user_display', $settings);
129 unset($form_values['user_display']);
130 }
131
132 function user_display_user_edit_validate($form_id, $form_values) {
133 if (file_check_upload('picture_upload') && !form_get_errors()) {
134 // Validate minimum picture size.
135 $info = image_get_info($form_values['picture']);
136 list($minwidth, $minheight) = explode('x', variable_get('user_picture_min_dimensions', '10x10'));
137 if ($info['width'] < $minwidth || $info['height'] < $minheight) {
138 form_set_error('picture_upload', t('The uploaded image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_min_dimensions', '10x10'))));
139 }
140 }
141 }
142
143 /**
144 * Check for a new or deleted user picture and remove the ImageCache derivatives
145 * accordingly.
146 */
147 function user_display_user_edit_submit($form_id, $form_values) {
148 if (file_check_upload('picture_upload') || $form_values['picture_delete']) {
149 imagecache_image_flush($form_values['picture']);
150 }
151 }
152
153 /**
154 * Menu callback; return the module settings form.
155 */
156 function user_display_settings() {
157 $settings = variable_get('user_display', array());
158 $presets = user_display_imagecache_presets();
159
160 // $form['user_display'] = array(
161 // '#tree' => TRUE,
162 // '#type' => 'fieldset',
163 // '#title' => ucfirst(str_replace('_', ' ', $module)),
164 // '#collapsible' => TRUE,
165 // '#collapsed' => FALSE,
166 // );
167 $form['user_display']['#tree'] = TRUE;
168
169 foreach (module_implements('user_display', TRUE) as $module) {
170 // Get available display classes.
171 $classes = module_invoke($module, 'user_display', 'info');
172 foreach ($classes as $class => $info) {
173 $form['user_display'][$class] = array(
174 '#type' => 'select',
175 '#title' => t('@name preset', array('@name' => $info['name'])),
176 '#default_value' => isset($settings[$class]) ? $settings[$class] : USER_DISPLAY_NO_PICTURE,
177 '#options' => $presets,
178 '#description' => $info['description'],
179 );
180 }
181 }
182
183 return system_settings_form($form);
184 }
185
186 /**
187 * @defgroup user_display_api User Display API
188 * @{
189 */
190
191 /**
192 * Implementation of hook_user_display().
193 */
194 function user_display_user_display($op) {
195 switch ($op) {
196 case 'elements':
197 $items = array(
198 'user_display_username' => array(
199 '#type' => 'select',
200 '#title' => t('Display user name'),
201 '#options' => array(0 => t('Without link'), 1 => t('Linked')),
202 '#default_value' => 1,
203 '#category' => 'name',
204 ),
205 'user_display_imagecache' => array(
206 '#type' => 'select',
207 '#title' => t('Display user picture using ImageCache'),
208 '#options' => user_display_imagecache_presets(),
209 '#default_value' => USER_DISPLAY_DEFAULT_PICTURE,
210 '#category' => 'picture',
211 ),
212 'user_display_onlinestatus' => array(
213 '#title' => t('Display online status'),
214 '#category' => 'decorators',
215 ),
216 'user_display_access' => array(
217 '#title' => t('Display last online time of user'),
218 '#category' => 'statistics',
219 ),
220 'user_display_nodes' => array(
221 '#title' => t('Display total nodes of user'),
222 '#category' => 'statistics',
223 ),
224 'user_display_comments' => array(
225 '#title' => t('Display total comments of user'),
226 '#category' => 'statistics',
227 ),
228 );
229 return $items;
230
231 case 'classes':
232 // @todo Can we register classes for each view implementing a user name
233 // or user picture? -- or migrate to a t()-like registry?
234 $classes = array(
235 'default' => array(
236 'name' => t('Default'),
237 'description' => t('This class applies to all core user pictures.')
238 ),
239 'default_profile' => array(
240 'name' => t('User profile page'),
241 'description' => t("This class applies to all user pictures displayed on the user profile page.")
242 ),
243 );
244 // Node classes (phptemplate.engine).
245 foreach (node_get_types() as $type) {
246 foreach (array('teaser', 'page') as $view) {
247 foreach (array('' => '', '_submitted' => ': Submitted', '_picture' => ': Picture') as $subtype => $subtitle) {
248 $classes['node_'. $type->type .'_'. $view . $subtype] = array(
249 'name' => t('Node: @title (@view@subtitle)', array('@title' => $type->name, '@view' => drupal_ucfirst($view), '@subtitle' => $subtitle)),
250 );
251 }
252 }
253 }
254 return $classes;
255 }
256 }
257
258 /**
259 * Render a user name.
260 *
261 * @param $linked
262 * Whether to output a link to the user profile.
263 */
264 function user_display_username($account, $style, $linked = 1) {
265 // Shorten the name when it is too long or it will break many tables.
266 if (drupal_strlen($account->name) > 20) {
267 $name = drupal_substr($account->name, 0, 15) .'...';
268 }
269 else {
270 $name = $account->name;
271 }
272 if ($linked && user_access('access user profiles')) {
273 $name = l($name, 'user/'. $account->uid, array('title' => t('View user profile.')));
274 }
275 else {
276 $name = check_plain($name);
277 }
278 return array(
279 'content' => '<span class="user-name">'. $name .'</span>',
280 'class' => array('name'),
281 );
282 }
283
284 /**
285 * Render a user picture using ImageCache.
286 *
287 * This is a bit awkward, but it's the only way without duplicating
288 * theme_user_picture().
289 *
290 * @param $preset
291 * An ImageCache preset to use.
292 */
293 function user_display_imagecache($account, $style, $preset) {
294 return array(
295 'content' => theme('user_picture', $account, NULL, $preset),
296 'class' => array('picture', $preset),
297 );
298 }
299
300 /**
301 * Return available ImageCache presets.
302 */
303 function user_display_imagecache_presets() {
304 $presets = array();
305 $presets[USER_DISPLAY_NO_PICTURE] = t('- Hide picture -');
306 $presets[USER_DISPLAY_DEFAULT_PICTURE] = t('- Default picture -');
307
308 // ImageCache v2 API.
309 $imagecache_presets = module_invoke('imagecache', 'presets');
310 foreach ($imagecache_presets as $id => $info) {
311 $presets[$info['presetname']] = $info['presetname'];
312 }
313 return $presets;
314 }
315
316 /**
317 * Render a user's online status.
318 */
319 function user_display_onlinestatus($account, $style) {
320 if ($account->uid) {
321 // Buddylist already queries online status of users.
322 if (!isset($account->online)) {
323 // Try not to pollute MySQL's query cache with ever changing timestamps.
324 $interval = floor((time() - variable_get('user_block_seconds_online', 900)) / 60) * 60;
325 $status = db_result(db_query('SELECT timestamp FROM {sessions} WHERE uid = %d AND timestamp >= %d', $account->uid, $interval));
326 }
327 else {
328 $status = $account->online;
329 }
330 return array(
331 'content' => '<div class="user-status">'. theme('user_display_onlinestatus', $account, $status) .'</div>',
332 'class' => array('status', $status ? 'online' : 'offline'),
333 );
334 }
335 }
336
337 /**
338 * Render an online status indicator image.
339 *
340 * @param $account
341 * A user account object.
342 * @param $status
343 * TRUE if the user is online currently.
344 */
345 function theme_user_display_onlinestatus($account, $status) {
346 $image = drupal_get_path('module', 'user_display') .'/images/'. ($status ? 'online' : 'offline') .'.png';
347 $alt = $status ? t('Online') : t('Offline');
348 $title = $status ? t('@name is online.', array('@name' => $account->name)) : t('@name is offline.', array('@name' => $account->name));
349 return theme('image', $image, $alt, $title, array(), FALSE);
350 }
351
352 /**
353 * Render last access time of a user.
354 */
355 function user_display_access($account, $style) {
356 if (!isset($account->access)) {
357 $account->access = db_result(db_query("SELECT access FROM {user} WHERE uid = %d", $account->uid));
358 }
359 return array(
360 'content' => '<div class="user-access"><span class="label">'. t('Last access') .':</span> '. format_date($account->access, 'small') .'</div>',
361 'class' => array('stats'),
362 );
363 }
364
365 /**
366 * Render total count of nodes of a user.
367 *
368 * @param $type
369 * An internal content-type name to query.
370 */
371 function user_display_nodes($account, $style, $type) {
372 $count = db_result(db_query("SELECT COUNT(nid) FROM {node} WHERE type = '%s' AND uid = %d", $type, $account->uid));
373 $name = node_get_types('name', $type);
374 return array(
375 'content' => '<div class="user-'. $type .'s">'. format_plural($count, strtr('1 @type', array('@type' => $name)), strtr('@count @types', array('@type' => $name))) .'</div>',
376 'class' => array('stats'),
377 );
378 }
379
380 /**
381 * Render total count of comments of a user.
382 */
383 function user_display_comments($account, $style) {
384 $count = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE uid = %d", $account->uid));
385 return array(
386 'content' => '<div class="user-comments">'. format_plural($count, '1 comment', '@count comments') .'</div>',
387 'class' => array('stats'),
388 );
389 }
390
391 /**
392 * @} End of "defgroup user_display_api".
393 */
394
395 /**
396 * @ingroup views_support Views support
397 * @{
398 */
399
400 /**
401 * Implementation of hook_views_tables_alter().
402 */
403 function user_display_views_tables_alter(&$tables) {
404 // Add user display style select to user name field and change handler.
405 $tables['users']['fields']['name']['handler'] = 'user_display_views_handler_field_username';
406 $tables['users']['fields']['name']['option'] = array(
407 '#type' => 'select',
408 '#options' => user_display_styles_options(),
409 );
410 // Add user display style select to user picture field and change handler.
411 $tables['users']['fields']['uid']['handler'] = 'user_display_views_handler_field_user_picture';
412 $tables['users']['fields']['uid']['option'] = array(
413 '#type' => 'select',
414 '#options' => user_display_styles_options(),
415 );
416 }
417
418 /**
419 * Format a field as user name. Needs to have 'uid' available,
420 * specified in the field info.
421 */
422 function user_display_views_handler_field_username($fieldinfo, $fielddata, $value, $data) {
423 $obj = new stdClass();
424 $uidfield = $fielddata['tablename'] . '_' . $fieldinfo['uid'];
425 if (!$value && $data->$uidfield) {
426 $obj = user_load(array('uid' => $data->$uidfield));
427 }
428 else {
429 // $obj->name = $value;
430 // $obj->uid = $data->$uidfield;
431 $obj = user_load(array('uid' => $data->$uidfield));
432 }
433 return theme('username', $obj, $fielddata['options']);
434 }
435
436 /**
437 * Format a field as user picture. Needs to have 'uid' available,
438 * specified in the field info.
439 */
440 function user_display_views_handler_field_user_picture($fieldinfo, $fielddata, $value, $data) {
441 $account = user_load(array('uid' => $value));
442 if ($account !== FALSE) {
443 return theme('username', $account, $fielddata['options']);
444 }
445 }
446
447 /**
448 * @} End of "ingroup views_support".
449 */
450

  ViewVC Help
Powered by ViewVC 1.1.2