author | ymh <ymh.work@gmail.com> |
Tue, 20 Mar 2018 15:02:40 +0100 | |
changeset 573 | 25f3d28f51b2 |
parent 541 | e756a8c72c3d |
permissions | -rwxr-xr-x |
541
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
<?php |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
/** |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
* @file |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
* Builds placeholder replacement tokens for values specific to Poll nodes. |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
*/ |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
/** |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
* Implements hook_token_info(). |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
*/ |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
function poll_token_info() { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
$node['poll-votes'] = array( |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
'name' => t("Poll votes"), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
'description' => t("The number of votes that have been cast on a poll."), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
$node['poll-winner'] = array( |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
'name' => t("Poll winner"), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
'description' => t("The winning poll answer."), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
$node['poll-winner-votes'] = array( |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
'name' => t("Poll winner votes"), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
'description' => t("The number of votes received by the winning poll answer."), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
$node['poll-winner-percent'] = array( |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
'name' => t("Poll winner percent"), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
'description' => t("The percentage of votes received by the winning poll answer."), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
$node['poll-duration'] = array( |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
'name' => t("Poll duration"), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
'description' => t("The length of time the poll is set to run."), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
return array( |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
'tokens' => array('node' => $node), |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
/** |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
* Implements hook_tokens(). |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
*/ |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
function poll_tokens($type, $tokens, array $data = array(), array $options = array()) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
$sanitize = !empty($options['sanitize']); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
if (isset($options['language'])) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
$url_options['language'] = $options['language']; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
$language_code = $options['language']->language; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
else { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
$language_code = NULL; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
$replacements = array(); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
if ($type == 'node' && !empty($data['node']) && $data['node']->type == 'poll') { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
$node = $data['node']; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
$total_votes = 0; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
$highest_votes = 0; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
foreach ($node->choice as $choice) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
if ($choice['chvotes'] > $highest_votes) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
$winner = $choice; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
$highest_votes = $choice['chvotes']; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
$total_votes = $total_votes + $choice['chvotes']; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
foreach ($tokens as $name => $original) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
switch ($name) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
case 'poll-votes': |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
$replacements[$original] = $total_votes; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
break; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
case 'poll-winner': |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
if (isset($winner)) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
$replacements[$original] = $sanitize ? filter_xss($winner['chtext']) : $winner['chtext']; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
else { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
$replacements[$original] = ''; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
break; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
case 'poll-winner-votes': |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
if (isset($winner)) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
$replacements[$original] = $winner['chvotes']; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
else { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
85 |
$replacements[$original] = ''; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
break; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
case 'poll-winner-percent': |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
if (isset($winner)) { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
$percent = ($winner['chvotes'] / $total_votes) * 100; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
$replacements[$original] = number_format($percent, 0); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
94 |
else { |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
95 |
$replacements[$original] = ''; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
break; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
99 |
case 'poll-duration': |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
100 |
$replacements[$original] = format_interval($node->runtime, 1, $language_code); |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
101 |
break; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
102 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
103 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
104 |
} |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
|
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
return $replacements; |
e756a8c72c3d
integrate drupal and correct build process. update version
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
107 |
} |