Projet

Général

Profil

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

root / drupal7 / sites / all / modules / flag / includes / views / flag_handler_field_ops.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the flag Ops field handler.
6
 */
7

    
8
/**
9
 * Views field handler for the Flag operations links (flag/unflag).
10
 *
11
 * @ingroup views
12
 */
13
class flag_handler_field_ops extends views_handler_field {
14

    
15
  /**
16
   * Returns the flag object associated with our field.
17
   *
18
   * A field is in some relationship. This function reaches out for this
19
   * relationship and reads its 'flag' option, which holds the flag name.
20
   */
21
  function get_flag() {
22
    // When editing a view it's possible to delete the relationship (either by
23
    // error or to later recreate it), so we have to guard against a missing
24
    // one.
25
    if (isset($this->view->relationship[$this->options['relationship']])) {
26
      return $this->view->relationship[$this->options['relationship']]->get_flag();
27
    }
28
  }
29

    
30
  /**
31
   * Return the the relationship we're linked to. That is, the alias for its
32
   * table (which is suitbale for use with the various methods of the 'query'
33
   * object).
34
   */
35
  function get_parent_relationship() {
36
    $parent = $this->view->relationship[$this->options['relationship']]->options['relationship'];
37
    if (!$parent || $parent == 'none') {
38
      return NULL; // Base query table.
39
    }
40
    else {
41
      return $this->view->relationship[$parent]->alias;
42
    }
43
  }
44

    
45
  function option_definition() {
46
    $options = parent::option_definition();
47
    $options['link_type'] = array('default' => '');
48
    return $options;
49
  }
50

    
51
  function options_form(&$form, &$form_state) {
52
    parent::options_form($form, $form_state);
53

    
54
    $form['link_type'] = array(
55
      '#type' => 'radios',
56
      '#title' => t('Link type'),
57
      '#options' => array('' => t('Use flag link settings')) + _flag_link_type_options(),
58
      '#default_value' => $this->options['link_type'],
59
    );
60
  }
61

    
62
  /**
63
   * Override base ::query(). The purpose here is to make it possible for the
64
   * render() method to know two things: what's the content ID, and whether
65
   * it's flagged.
66
   */
67
  function query() {
68
    if (!($flag = $this->get_flag())) {
69
      return; // Error message is printed in render().
70
    }
71
    $info = $flag->get_views_info();
72
    $parent = $this->get_parent_relationship();
73

    
74
    // Find out if the content is flagged. We can't just peek at some field in
75
    // our loaded table because it doesn't always reflect the user browsing the
76
    // page. So we explicitly add the flagging table to find this out.
77
    // If the relationship is created with 'Current User' checked, we probably
78
    // already have the appropriate join. We just need the appropriate table
79
    // alias for that join. We look in the relationship settings to see if
80
    // user_scope is set to 'current' to determine this.
81
    $relationship = $this->view->relationship[$this->options['relationship']];
82
    if (isset($relationship->options['user_scope']) && $relationship->options['user_scope'] == 'current') {
83
      $table_alias = $relationship->alias;
84
    }
85
    // Otherwise, let's set up the alias, keeping it unique for this flag in
86
    // case there are multiple flag relationships in a single view.
87
    else {
88
      $table_alias = 'flagging_current_user_' . $flag->fid;
89
    }
90
    // Now that we have the table alias, let's see if it's already been joined
91
    // to. If it hasn't, we'll set up a join.
92
    if (!isset($this->query->table_queue[$table_alias])) {
93
      $join = new views_join();
94
      $join->construct('flagging', $info['views table'], $info['join field'], 'entity_id');
95
      $join->extra[] = array(
96
        'field' => 'fid',
97
        'value' => $flag->fid,
98
        'numeric' => TRUE,
99
      );
100
      if (!$flag->global) {
101
        $join->extra[] = array(
102
          'field' => 'uid',
103
          'value' => '***CURRENT_USER***',
104
          'numeric' => TRUE,
105
        );
106
        $join->extra[] = array(
107
          'field' => 'sid',
108
          'value' => '***FLAG_CURRENT_USER_SID***',
109
          'numeric' => TRUE,
110
        );
111
      }
112
      $table_alias = $this->query->add_table($table_alias, $parent, $join);
113
    }
114
    $this->aliases['is_flagged'] = $this->query->add_field($table_alias, 'entity_id');
115

    
116
    // Next, find out the content ID. We can't add_field() on this table
117
    // (flagging), because its entity_id may be NULL (in case no user has
118
    // flagged this content, and it's a LEFT JOIN). So we reach to the parent
119
    // relationship and add_field() *its* content ID column.
120
    $left_table = $this->view->relationship[$this->options['relationship']]->table_alias;
121
    $this->aliases['entity_id'] = $this->query->add_field($left_table, $info['join field']);
122
  }
123

    
124
  /**
125
   * Find out if the flag applies to each item seen on the page. It's done in a
126
   * separate DB query to to avoid complexity and to make 'many to one' tests
127
   * (e.g. checking user roles) possible without causing duplicate rows.
128
   */
129
  function pre_render(&$values) {
130
    if (!($flag = $this->get_flag())) {
131
      return; // Error message is printed in render().
132
    }
133

    
134
    $ids = array();
135
    foreach ($values as $row) {
136
      $entity_id = $row->{$this->aliases['entity_id']};
137
      $is_flagged = $row->{$this->aliases['is_flagged']};
138
      if (isset($entity_id)) {
139
        $ids[$entity_id] = $is_flagged ? 'unflag' : 'flag';
140
      }
141
    }
142
    $this->flag_applies = $ids ? $flag->access_multiple($ids) : array();
143
  }
144

    
145
  function render($values) {
146
    if (!($flag = $this->get_flag())) {
147
      return t('Missing flag'); // get_flag() itself will print a more detailed message.
148
    }
149

    
150
    $entity_id = $values->{$this->aliases['entity_id']};
151
    $is_flagged = $values->{$this->aliases['is_flagged']};
152

    
153
    if (empty($this->flag_applies[$entity_id])) {
154
      // Flag does not apply to this content.
155
      return;
156
    }
157

    
158
    if (!empty($this->options['link_type'])) {
159
      $flag->link_type = $this->options['link_type'];
160
    }
161
    return $flag->theme($is_flagged ? 'unflag' : 'flag', $entity_id);
162
  }
163
}