Projet

Général

Profil

Paste
Télécharger (3,74 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / user / views_handler_field_user_picture.inc @ 651307cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_user_picture.
6
 */
7

    
8
/**
9
 * Field handler to provide simple renderer that allows using a themed user link.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_field_user_picture extends views_handler_field {
14
  function construct() {
15
    parent::construct();
16
    $this->additional_fields['uid'] = 'uid';
17
    $this->additional_fields['name'] = 'name';
18
    $this->additional_fields['mail'] = 'mail';
19
  }
20

    
21
  function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
22
    if ($inline) {
23
      return 'span';
24
    }
25
    if ($none_supported) {
26
      if ($this->options['element_type'] === '0') {
27
        return '';
28
      }
29
    }
30
    if ($this->options['element_type']) {
31
      return check_plain($this->options['element_type']);
32
    }
33
    if ($default_empty) {
34
      return '';
35
    }
36
    if (isset($this->definition['element type'])) {
37
      return $this->definition['element type'];
38
    }
39

    
40
    return 'div';
41
  }
42

    
43
  function option_definition() {
44
    $options = parent::option_definition();
45
    $options['link_photo_to_profile'] = array('default' => TRUE, 'bool' => TRUE);
46
    $options['image_style'] = array('default' => '');
47
    return $options;
48
  }
49

    
50
  function options_form(&$form, &$form_state) {
51
    parent::options_form($form, $form_state);
52
    $form['link_photo_to_profile'] = array(
53
      '#title' => t("Link to user's profile"),
54
      '#description' => t("Link the user picture to the user's profile"),
55
      '#type' => 'checkbox',
56
      '#default_value' => $this->options['link_photo_to_profile'],
57
    );
58

    
59
    if (module_exists('image')) {
60
      $styles = image_styles();
61
      $style_options = array('' => t('Default'));
62
      foreach ($styles as $style) {
63
        $style_options[$style['name']] = $style['name'];
64
      }
65

    
66
      $form['image_style'] = array(
67
        '#title' => t('Image style'),
68
        '#description' => t('Using <em>Default</em> will use the site-wide image style for user pictures set in the <a href="!account-settings">Account settings</a>.', array('!account-settings' => url('admin/config/people/accounts', array('fragment' => 'edit-personalization')))),
69
        '#type' => 'select',
70
        '#options' => $style_options,
71
        '#default_value' => $this->options['image_style'],
72
      );
73
    }
74
  }
75

    
76
  function render($values) {
77
    if ($this->options['image_style'] && module_exists('image')) {
78
      // @todo: Switch to always using theme('user_picture') when it starts
79
      // supporting image styles. See http://drupal.org/node/1021564
80
      if ($picture_fid = $this->get_value($values)) {
81
        $picture = file_load($picture_fid);
82
        $picture_filepath = $picture->uri;
83
      }
84
      else {
85
        $picture_filepath = variable_get('user_picture_default', '');
86
      }
87
      if (file_valid_uri($picture_filepath)) {
88
        $output = theme('image_style', array('style_name' => $this->options['image_style'], 'path' => $picture_filepath));
89
        if ($this->options['link_photo_to_profile'] && user_access('access user profiles')) {
90
          $uid = $this->get_value($values, 'uid');
91
          $output = l($output, "user/$uid", array('html' => TRUE));
92
        }
93
      }
94
      else {
95
        $output = '';
96
      }
97
    }
98
    else {
99
      // Fake an account object.
100
      $account = new stdClass();
101
      if ($this->options['link_photo_to_profile']) {
102
        // Prevent template_preprocess_user_picture from adding a link
103
        // by not setting the uid.
104
        $account->uid = $this->get_value($values, 'uid');
105
      }
106
      $account->name = $this->get_value($values, 'name');
107
      $account->mail = $this->get_value($values, 'mail');
108
      $account->picture = $this->get_value($values);
109
      $output = theme('user_picture', array('account' => $account));
110
    }
111

    
112
    return $output;
113
  }
114
}