Projet

Général

Profil

Paste
Télécharger (6,65 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / privatemsg / views / views_handler_field_privatemsg_link.inc @ 13755f8d

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the privatemsg link views field handler.
6
 */
7

    
8
/**
9
 * Provides a configurable link to the new message form for a specific user.
10
 */
11
class views_handler_field_privatemsg_link extends views_handler_field {
12

    
13
  /**
14
   * Add uid as a additional field.
15
   */
16
  function construct() {
17
    parent::construct();
18
    $this->additional_fields['uid'] = 'uid';
19
  }
20

    
21
  /**
22
   * Define our additional configuration setting.
23
   */
24
  function option_definition() {
25
    $options = parent::option_definition();
26
    $options['text'] = array('default' => t('Send message'), 'translatable' => TRUE);
27
    $options['subject'] = array('default' => '', 'translatable' => TRUE);
28
    $options['return'] = array('default' => TRUE, 'translatable' => FALSE);
29
    $options['custom_destination'] = array('default' => '', 'translatable' => FALSE);
30
    return $options;
31
  }
32

    
33
  /**
34
   * Define the configuration form for our textfield.
35
   */
36
  function options_form(&$form, &$form_state) {
37
    $form['label'] = array(
38
      '#type' => 'textfield',
39
      '#title' => t('Label'),
40
      '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
41
      '#description' => t('The label for this field that will be displayed to end users if the style requires it.'),
42
    );
43

    
44
    $form['text'] = array(
45
      '#type' => 'textfield',
46
      '#title' => t('Text to display'),
47
      '#default_value' => isset($this->options['text']) ? $this->options['text'] : '',
48
      '#description' => t('Define the text to use for the link title. You can use replacement tokens to insert any existing field output.'),
49
    );
50

    
51
    $form['subject'] = array(
52
      '#type' => 'textfield',
53
      '#title' => t('Pre-filled subject'),
54
      '#default_value' => isset($this->options['subject']) ? $this->options['subject'] : '',
55
      '#description' => t('Define the subject that will be pre-filled in the send message form. You can use replacement tokens to insert any existing field output.'),
56
    );
57

    
58
    $form['return'] = array(
59
      '#type' => 'checkbox',
60
      '#title' => t('Return to view after message was sent.'),
61
      '#default_value' => $this->options['return'],
62
      '#description' => t('Should the user be redirected back to the current view when the message was sent.'),
63
    );
64

    
65
    $form['custom_destination'] = array(
66
      '#type' => 'textfield',
67
      '#title' => t('Custom destination'),
68
      '#default_value' => $this->options['custom_destination'],
69
      '#description' => t('If non-empty, users will be forwared to the given url. You can use replacement tokens to insert any existing field output.'),
70
      '#states' => array(
71
        'visible' => array(
72
          "input[name='options[return]']" => array('checked' => TRUE),
73
        ),
74
      ),
75
    );
76

    
77
    // Get a list of the available fields and arguments for token replacement.
78
    $options = array();
79
    foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
80
      $options[t('Fields')]["[$field]"] = $handler->ui_name();
81
      // We only use fields up to (and including) this one.
82
      if ($field == $this->options['id']) {
83
        break;
84
      }
85
    }
86
    $count = 0; // This lets us prepare the key as we want it printed.
87
    foreach ($this->view->display_handler->get_handlers('argument') as $handler) {
88
      $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
89
      $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
90
    }
91

    
92
    // Add documentation about the new message token. Note that this is not
93
    // a real token that will be replaced but it is handled in
94
    // privatemsg_send_submit().
95
    $options[t('Privatemsg')]['[new-message]'] = t('This will redirect to the newly sent message.');
96

    
97
    $this->document_self_tokens($options[t('Fields')]);
98

    
99
    // Default text.
100
    $output = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
101
    // We have some options, so make a list.
102
    if (!empty($options)) {
103
      $output = t('<p>The following substitution patterns are available for this display. Use the pattern shown on the left to display the value indicated on the right. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
104
      foreach (array_keys($options) as $type) {
105
        if (!empty($options[$type])) {
106
          $items = array();
107
          foreach ($options[$type] as $key => $value) {
108
            $items[] = $key . ' == ' . $value;
109
          }
110
          $output .= theme('item_list',
111
            array(
112
              'items' => $items,
113
              'type' => $type
114
            ));
115
        }
116
      }
117
    }
118

    
119
    $form['help'] = array(
120
      '#id' => 'views-tokens-help',
121
      '#markup' => '<div><fieldset id="views-tokens-help"><legend>' . t('Replacement patterns') . '</legend>' . $output . '</fieldset></div>',
122
    );
123
  }
124

    
125
  /**
126
   * Renders our field, displays a link if the user is allowed to.
127
   */
128
  function render($values) {
129
    if (isset($values->{$this->aliases['uid']})) {
130
      $uid = $values->{$this->aliases['uid']};
131
    }
132
    else {
133
      return '';
134
    }
135

    
136
    $text = t('Send message');
137
    if (!empty($this->options['text'])) {
138
      $tokens = $this->get_render_tokens($this);
139
      $text = strip_tags(strtr($this->options['text'], $tokens));
140
    }
141
    $subject = NULL;
142
    if (!empty($this->options['subject'])) {
143
      $tokens = $this->get_render_tokens($this);
144
      $subject = strip_tags(strtr($this->options['subject'], $tokens));
145
    }
146

    
147
    $options = array();
148
    if ($this->options['return']) {
149
      if (!empty($this->options['custom_destination'])) {
150
        $tokens = $this->get_render_tokens($this);
151
        $destination = strip_tags(strtr($this->options['custom_destination'], $tokens));
152
        $options['query'] = array('destination' => $destination);
153
      }
154
      else {
155
        $options['query'] = drupal_get_destination();
156
      }
157
    }
158

    
159
    $data = '';
160
    if (($recipient = user_load($uid)) && ($url = privatemsg_get_link(array($recipient), NULL, $subject))) {
161
      $data = l($text, $url, $options);
162
    }
163
    return $data;
164
  }
165

    
166
  /**
167
   * Only display the column for users with the appropriate permission.
168
   */
169
  function access() {
170
    return privatemsg_user_access('write privatemsg');
171
  }
172

    
173
  /**
174
   * Just do some basic checks, don't add "privatemsg_link" as field.
175
   */
176
  function query() {
177
    $this->ensure_my_table();
178
    $this->add_additional_fields();
179
  }
180
}