forked from yshiga/q2a-custom-vote-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qa-custom-vote-button-layer.php
212 lines (186 loc) · 6.23 KB
/
qa-custom-vote-button-layer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
class qa_html_theme_layer extends qa_html_theme_base
{
const TARGET_DATE = '2016-07-15';
const AVATAR_SIZE = 80;
public function head_css()
{
qa_html_theme_base::head_css();
$plugin_url = qa_path('qa-plugin/q2a-custom-vote-button/');
$css = $plugin_url . 'custom-vote-button.css';
$this->output('<link rel="stylesheet" type="text/css" href="'.$css.'">');
}
public function main_part($key, $part)
{
if ($this->template === 'user' && $key === 'form_activity') {
$newpart = $this->remove_downvote($part);
qa_html_theme_base::main_part($key, $newpart);
return;
}
qa_html_theme_base::main_part($key, $part);
}
public function voting_inner_html($post)
{
$this->vote_buttons($post);
$this->vote_count($post);
// モバイルでない かつ 投稿リスト内ではない
if (!qa_is_mobile_probably() && !$this->is_q_list($post)) {
$this->vote_avatars($post);
}
$this->vote_clear();
}
public function vote_buttons($post)
{
$this->output('<div class="qa-vote-buttons '.(($post['vote_view'] == 'updown') ? 'qa-vote-buttons-updown' : 'qa-vote-buttons-net').'">');
switch (@$post['vote_state'])
{
case 'voted_up':
$this->post_hover_button($post, 'vote_up_tags', '+', 'qa-vote-one-button qa-voted-up');
break;
case 'voted_up_disabled':
$this->post_disabled_button($post, 'vote_up_tags', '+', 'qa-vote-one-button qa-vote-up');
break;
case 'voted_down':
// $this->post_hover_button($post, 'vote_down_tags', '–', 'qa-vote-one-button qa-voted-down');
break;
case 'voted_down_disabled':
// $this->post_disabled_button($post, 'vote_down_tags', '–', 'qa-vote-one-button qa-vote-down');
break;
case 'up_only':
$this->post_hover_button($post, 'vote_up_tags', '+', 'qa-vote-first-button qa-vote-up');
// $this->post_disabled_button($post, 'vote_down_tags', '', 'qa-vote-second-button qa-vote-down');
break;
case 'enabled':
$this->post_hover_button($post, 'vote_up_tags', '+', 'qa-vote-first-button qa-vote-up');
// $this->post_hover_button($post, 'vote_down_tags', '–', 'qa-vote-second-button qa-vote-down');
break;
default:
$this->post_disabled_button($post, 'vote_up_tags', '', 'qa-vote-first-button qa-vote-up');
// $this->post_disabled_button($post, 'vote_down_tags', '', 'qa-vote-second-button qa-vote-down');
break;
}
$this->output('</div>');
}
public function vote_count($post)
{
$post['netvotes_view']['prefix'] = '';
qa_html_theme_base::vote_count($post);
}
public function body_footer()
{
qa_html_theme_base::body_footer();
if (!qa_is_mobile_probably() && $this->template === 'question') {
$plugin_url = qa_path('qa-plugin/q2a-custom-vote-button/');
$script = $plugin_url . 'custom-vote-button.js';
$this->output('<script type="text/javascript" src="'.$script.'"></script>');
}
}
/**
* 支持した人のアイコンを表示する
* @param array $post その投稿
* @return なし
*/
public function vote_avatars($post)
{
$voted_user_icons = $this->get_voted_user_icons($post);
$this->output('<div class="voted-avatar-list" >');
if (!empty($voted_user_icons)) {
$this->output('<ul>');
foreach ( $voted_user_icons as $icon ) {
$this->output('<li class="qa-voted-avatar">'.$icon.'<li>');
}
$this->output('</ul>');
}
$this->output('<div style="clear:both;"></div>');
$this->output('</div>');
}
/**
* 支持しているユーザーのユーザーIDを収得
* @param string $postid ポストID
* @return array ユーザーID
*/
private function get_voted_users($postid)
{
if (empty($postid)) {
return array();
}
$sql = "SELECT *
FROM ^users
WHERE userid
IN ( SELECT userid FROM ^uservotes WHERE postid = # AND vote = 1 )
";
return qa_db_read_all_assoc(qa_db_query_sub($sql, $postid));
}
/**
* 支持したユーザーのアバターアイコンを取得
* @param array $post その投稿
* @return array アバターアイコンのhtml
*/
private function get_voted_user_icons($post)
{
$result = array();
$postid = $post['raw']['postid'];
$created = $post['raw']['created'];
if (isset($postid) && $this->is_after_date($created, self::TARGET_DATE)) {
$users = array();
$users = $this->get_voted_users($postid);
foreach ($users as $user) {
if (QA_FINAL_EXTERNAL_USERS) {
$result[]= qa_get_external_avatar_html($user['userid'], self::AVATAR_SIZE, false);
} else {
$result[]= qa_get_user_avatar_html($user['flags'], $user['email'], $user['handle'],
$user['avatarblobid'], $user['avatarwidth'], $user['avatarheight'], self::AVATAR_SIZE);
}
}
}
return $result;
}
/**
* 投稿日指定日付より後かどうかを返す
* @param string $created 投稿日のタイムスタンプ
* @param string $target 指定日付 YYYY-MM-DD
* @return boolean 指定日付以降ならtrue
*/
private function is_after_date($created, $target)
{
$targetDay = new DateTime($target);
if ($created >= $targetDay->getTimestamp()) {
return true;
} else {
return false;
}
}
/**
* 質問リスト内の投稿かどうか
* @param array $post 現在の投稿
* @return boolean 投稿リスト内ならtrue
*/
private function is_q_list($post)
{
$keys = array_keys($post);
if (count($keys) <= 19) {
// ajax 呼び出しの場合keyの数が19個
return false;
}
foreach ($keys as $key) {
// 質問リストには c_list なし
if ($key === 'c_list') {
return false;
}
}
return true;
}
private function remove_downvote($part)
{
$points = $this->content['raw']['points'];
$upvotes = $points['aupvotes'] + $points['qupvotes'];
$innervalue = '<span class="qa-uf-user-upvotes">'.number_format($upvotes).'</span>';
$votegavevalue = (($upvotes == 1) ? qa_lang_html_sub('profile/1_up_vote', $innervalue, '1') : qa_lang_html_sub('profile/x_up_votes', $innervalue));
$part['fields']['votegave']['value'] = $votegavevalue;
$innervalue = '<span class="qa-uf-user-upvoteds">'.number_format($points['upvoteds']).'</span>';
$votegotvalue = ((@$userpoints['upvoteds'] == 1) ? qa_lang_html_sub('profile/1_up_vote', $innervalue, '1')
: qa_lang_html_sub('profile/x_up_votes', $innervalue));
$part['fields']['votegot']['value'] = $votegotvalue;
return $part;
}
}