Projet

Général

Profil

Paste
Télécharger (2,83 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / poll / poll.tokens.inc @ 76597ebf

1
<?php
2

    
3
/**
4
 * @file
5
 * Builds placeholder replacement tokens for values specific to Poll nodes.
6
 */
7

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function poll_token_info() {
12
  $node['poll-votes'] = array(
13
    'name' => t("Poll votes"),
14
    'description' => t("The number of votes that have been cast on a poll."),
15
  );
16
  $node['poll-winner'] = array(
17
    'name' => t("Poll winner"),
18
    'description' => t("The winning poll answer."),
19
  );
20
  $node['poll-winner-votes'] = array(
21
    'name' => t("Poll winner votes"),
22
    'description' => t("The number of votes received by the winning poll answer."),
23
  );
24
  $node['poll-winner-percent'] = array(
25
    'name' => t("Poll winner percent"),
26
    'description' => t("The percentage of votes received by the winning poll answer."),
27
  );
28
  $node['poll-duration'] = array(
29
    'name' => t("Poll duration"),
30
    'description' => t("The length of time the poll is set to run."),
31
  );
32

    
33
  return array(
34
    'tokens' => array('node' => $node),
35
  );
36
}
37

    
38
/**
39
 * Implements hook_tokens().
40
 */
41
function poll_tokens($type, $tokens, array $data = array(), array $options = array()) {
42
  $sanitize = !empty($options['sanitize']);
43
  if (isset($options['language'])) {
44
    $url_options['language'] = $options['language'];
45
    $language_code = $options['language']->language;
46
  }
47
  else {
48
    $language_code = NULL;
49
  }
50

    
51
  $replacements = array();
52

    
53
  if ($type == 'node' && !empty($data['node']) && $data['node']->type == 'poll') {
54
    $node = $data['node'];
55

    
56
    $total_votes = 0;
57
    $highest_votes = 0;
58
    foreach ($node->choice as $choice) {
59
      if ($choice['chvotes'] > $highest_votes) {
60
        $winner = $choice;
61
        $highest_votes = $choice['chvotes'];
62
      }
63
      $total_votes = $total_votes + $choice['chvotes'];
64
    }
65
    foreach ($tokens as $name => $original) {
66
      switch ($name) {
67
        case 'poll-votes':
68
          $replacements[$original] = $total_votes;
69
          break;
70

    
71
        case 'poll-winner':
72
          if (isset($winner)) {
73
            $replacements[$original] = $sanitize ? filter_xss($winner['chtext']) : $winner['chtext'];
74
          }
75
          else {
76
            $replacements[$original] = '';
77
          }
78
          break;
79

    
80
        case 'poll-winner-votes':
81
          if (isset($winner)) {
82
            $replacements[$original] = $winner['chvotes'];
83
          }
84
          else {
85
            $replacements[$original] = '';
86
          }
87
          break;
88

    
89
        case 'poll-winner-percent':
90
          if (isset($winner)) {
91
            $percent = ($winner['chvotes'] / $total_votes) * 100;
92
            $replacements[$original] = number_format($percent, 0);
93
          }
94
          else {
95
            $replacements[$original] = '';
96
          }
97
          break;
98

    
99
        case 'poll-duration':
100
          $replacements[$original] = format_interval($node->runtime, 1, $language_code);
101
          break;
102
      }
103
    }
104
  }
105

    
106
  return $replacements;
107
}