-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
241 lines (204 loc) · 8.45 KB
/
action.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* DokuWiki Plugin gameteam (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Michal Koutný <[email protected]>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC'))
die();
class action_plugin_gameteam extends DokuWiki_Action_Plugin {
/**
* @var helper_plugin_gameteam
*/
private $helper;
public function __construct() {
$this->helper = $this->loadHelper('gameteam');
}
/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @return void
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_html_loginform_output');
$controller->register_hook('HTML_REGISTERFORM_OUTPUT', 'BEFORE', $this, 'handle_html_registerform_output');
$controller->register_hook('HTML_RESENDPWDFORM_OUTPUT', 'BEFORE', $this, 'handle_html_resendpwdform_output');
$controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handle_html_updateprofileform_output');
$controller->register_hook('AUTH_USER_CHANGE', 'BEFORE', $this, 'handle_auth_user_change');
$controller->register_hook('MAIL_MESSAGE_SEND', 'BEFORE', $this, 'handle_mail_message_send');
}
/**
* [Custom event handler which performs action]
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return void
*/
public function handle_html_loginform_output(Doku_Event &$event, $param) {
$form = $event->data;
$pos = $form->findElementByAttribute('name', 'u');
$el = & $form->getElementAt($pos);
$el['_text'] = $this->getLang('teamno');
}
public function handle_html_registerform_output(Doku_Event &$event, $param) {
$form = $event->data;
$this->modify_user_form($form);
}
public function handle_html_resendpwdform_output(Doku_Event &$event, $param) {
$form = $event->data;
$pos = $form->findElementByAttribute('name', 'login');
$el = & $form->getElementAt($pos);
$el['_text'] = $this->getLang('teamno');
}
public function handle_html_updateprofileform_output(Doku_Event &$event, $param) {
$user = $_SERVER['REMOTE_USER'];
$defaultVolumeId = $this->getConf('volume_id');
list($volumeId, $teamIdVolume) = $this->helper->parseUsername($user, $defaultVolumeId);
$form = $event->data;
$team = $this->helper->select('team', array(
'team_id_volume' => $teamIdVolume,
'volume_id' => $volumeId,
));
$l = $this->getConf('vs_length');
$m = pow(10, $l);
$vs = str_pad($teamIdVolume % $m, $l, '0', STR_PAD_LEFT);
$info = array(
'Tým' => $team['name'],
'Účet' => $this->getConf('account'),
'Variabilní symbol' => $this->getConf('vs_prefix') . $vs,
);
if ($team['state'] == auth_plugin_gameteam::STATE_PAID) {
$info['Stav'] = 'Zaplaceno';
} else if ($team['state'] == auth_plugin_gameteam::STATE_REGISTERED) {
$info['Stav'] = 'Nezaplaceno';
}
$elInfo = '<h2>Informace o platbě</h2><ul>';
foreach ($info as $name => $value) {
$elInfo .= '<li>' . $name . ': <strong>' . hsc($value) . '</strong></li>';
}
$elInfo .= '</ul>';
$elInfo .= '<h2>Úprava informací</h2>';
if ($this->getConf('show_payment')) {
$form->insertElement(0, $elInfo);
}
$this->modify_user_form($form);
}
public function handle_auth_user_change(Doku_Event &$event, $param) {
global $INPUT;
$data = & $event->data;
$type = $data['type'];
$fields = array();
foreach (json_decode($this->getConf('teamfields'), true) as $name => $fieldSpec) {
$fields[$name] = $INPUT->post->str($name);
}
switch ($type) {
case 'create':
$data['params'][] = array(); // no groups
$data['params'][] = $fields; // additional parameters
break;
case 'modify':
$data['params'][] = $fields; // additional parameters
break;
}
}
public function handle_mail_message_send(Doku_Event &$event, $param) {
$filename = metaFN('gameteam_mails', 'txt');
$data = $event->data;
$f = fopen($filename, 'a+');
fwrite($f, "Email to " . $data['to'] . ", result: " . (int) $data['success'] . ".\n");
fwrite($f, $data['body']);
fwrite($f, "\n\n\n");
fclose($f);
}
private function modify_user_form(Doku_Form $form) {
global $INPUT;
$pos = $form->findElementByAttribute('name', 'login');
$form->replaceElement($pos, null);
$form->addHidden('login', auth_plugin_gameteam::LOGIN_PLACEHOLDER);
$pos = $form->findElementByAttribute('name', 'fullname');
$el = & $form->getElementAt($pos);
$el['_text'] = $this->getLang('teamname');
$pos = $form->findElementByAttribute('name', 'email');
$el = & $form->getElementAt($pos);
$el['_text'] = $this->getLang('contactmail');
// submit button
$pos = $form->findElementByAttribute('type', 'submit');
$submit = $form->getElementAt($pos);
$form->replaceElement($pos, null);
// reset button
$pos = $form->findElementByAttribute('type', 'reset');
if ($pos) {
$reset = $form->getElementAt($pos);
$form->replaceElement($pos, null);
}
// load team info
$username = $_SERVER['REMOTE_USER'];
$teamInfo = array();
if ($username) {
$teamInfo = $this->loadTeamInfo($username);
}
// custom fields
$form->startFieldset($this->getLang('teaminfo'));
$fieldspec = json_decode($this->getConf('teamfields'), true);
$defaultSpec = array(
'default' => null,
'type' => 'text',
'attrs' => array(),
);
foreach ($fieldspec as $name => $spec) {
$spec = array_merge($defaultSpec, $spec);
if (array_key_exists($name, $teamInfo)) {
$spec['default'] = $teamInfo[$name];
}
$value = $INPUT->post->str($name, $spec['default'], true);
switch ($spec['type']) {
case 'bool':
if ($value) {
$spec['attrs']['checked'] = 'checked';
}
$field = form_makeCheckboxField($name, '1', $spec['label'], '', 'block', $spec['attrs']);
break;
case 'int':
$field = form_makeField('number', $name, $value, $spec['label'], '', 'block', $spec['attrs']);
break;
default:
$field = form_makeTextField($name, $value, $spec['label'], '', 'block', $spec['attrs']);
break;
}
$form->addElement($field);
}
$form->endFieldset();
$form->addElement($submit);
$form->addElement($reset);
}
private function loadTeamInfo($username) {
$defaultVolumeId = $this->getConf('volume_id');
list($volumeId, $teamIdVolume) = $this->helper->parseUsername($username, $defaultVolumeId);
$connection = $this->helper->getConnection();
$stmt = $connection->prepare('select * from team where volume_id = :volume_id and team_id_volume = :team_id_volume');
$stmt->bindValue('volume_id', $volumeId);
$stmt->bindValue('team_id_volume', $teamIdVolume);
$stmt->execute();
$teamInfo = $stmt->fetch();
if (!$teamInfo) {
$teamInfo = array();
}
$memberInfo = array();
$i = 0;
$stmt = $connection->prepare('select * from player where team_id = :team_id order by player_id');
$stmt->bindValue('team_id', $teamInfo['team_id']);
$stmt->execute();
while ($row = $stmt->fetch()) {
++$i;
foreach ($row as $column => $value) {
$memberInfo[$column . '_' . $i] = $value;
}
}
return array_merge($memberInfo, $teamInfo);
}
}
// vim:ts=4:sw=4:et: