-
Notifications
You must be signed in to change notification settings - Fork 0
/
bracket.php
477 lines (398 loc) · 12.5 KB
/
bracket.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
$pagetitle = "Fierceboard Bracket Contest";
$root = $_SERVER['DOCUMENT_ROOT'];
$worlds_bracket_home = $root . '/worldsbracket/';
require_once($worlds_bracket_home . "library/include.php");
include $worlds_bracket_home . 'library/BracketEntry.php';
$textInputFormat = "div_{0}_pos_{1}_text";
$idInputFormat = "div_{0}_pos_{1}_id";
$brackid = 0;
if(isset($_GET['id']))
{
$bracketid = $_GET['id'];
// get the bracket info/divisions
$bracket = $BRACKET_DATA_BO->getBracket($bracketid);
$pagetitle = $bracket['MatchName'] . ' Bracket';
}
// get the rounds for this brack (prelims/semis/finals)
$rounds = $BRACKET_DATA_BO->getBracketRounds($bracketid);
// get the current round id from the query string, otherwise default to 0
$roundname = '';
$roundid = 0;
if(isset($_GET['roundid']))
{
$roundid = $_GET['roundid'];
}
// if no query string, default to the first round in the bracket
if($roundid == 0)
{
$roundid = $rounds[0]['CompetitionRoundId'];
$roundname = $rounds[0]['CompetitionRoundName'];
}
else
{
// otherwise lookup the current round
foreach($rounds as $round)
{
if($round['CompetitionRoundId'] == $roundid)
{
$roundname = $round['CompetitionRoundName'];
}
}
}
// exclude partial/full paid bid winners from the web service
$atLargeOnly = $roundid == 1;
//update the title
$pagetitle = $pagetitle . ' - ' . $roundname;
// get all of the divisions for this round/bracket
$divisions = $BRACKET_DATA_BO->getBracketDivisionsByRound($bracketid, $roundid);
// on post back save the teams
if(isset($bracket) && isset($CURRENT_USER) &&
(isset($_POST["btnSubmit"]) || isset($_POST["btnSubmitNext"])))
{
foreach($divisions as $div)
{
$divId = $div['DivisionId'];
$entries = [];
for($i = 1; $i <= $div['NumEntries']; $i++)
{
$textInputName = str_replace('{1}', $i, str_replace('{0}', $divId, $textInputFormat));
$hiddenInputName = str_replace('{1}', $i, str_replace('{0}', $divId, $idInputFormat));
if(isset($_POST[$hiddenInputName]))
{
if(strlen($_POST[$hiddenInputName]) > 0)
{
$entry = new BracketEntry();
$entry->setUserId($CURRENT_USER["user_id"]);
$entry->setBracketId($bracketid);
$entry->setRoundId($roundid);
$entry->setDivisionId($divId);
$entry->setPosition($i);
$entry->setTeamId($_POST[$hiddenInputName]);
$entry->setTeamName($_POST[$textInputName]);
$entries[$i] = $entry;
}
}
}
$BRACKET_DATA_BO->addBracketEntries($entries, $bracketid, $roundid, $divId, $CURRENT_USER['user_id']);
$isSaved = True;
}
if(isset($_POST["btnSubmitNext"]))
{
// redirect
for($i = 0; $i < sizeof($rounds); $i++)
{
if($rounds[$i]['CompetitionRoundId'] == $roundid) {
break;
}
}
if($i == sizeof($rounds) - 1)
{
$i = 0;
}
else
{
$i = $i +1;
}
$href = 'bracket.php?id=' . $bracketid . '&roundid=' . $rounds[$i]['CompetitionRoundId'];
header("Location: " . $href);
}
}
// bracket start/end date
$currentTime = new DateTime();
$currentTime->setTimestamp(time());
$startDate = new DateTime();
$startDate->setTimestamp(strtotime($bracket['StartDate']));
$endDate = new DateTime();
$endDate->setTimestamp(strtotime($bracket['EndDate']));
include $worlds_bracket_home . 'header.php';
?>
<div id="wizard">
<div id="content">
<div class="menubar">
<div class="sidebar-toggler visible-xs">
<i class="ion-navicon"></i>
</div>
<div class="page-title">
<?php echo $pagetitle ?>
</div>
</div>
<div class="content-wrapper">
<?php
if(count($rounds) > 1)
{
?>
<div class="header" id="wizard-header">
<div class="steps clearfix">
<div>
<?php
// active class
foreach($rounds as $round)
{
$href = 'bracket.php?id=' . $bracketid . '&roundid=' . $round['CompetitionRoundId'];
$isactive = '';
if($roundid >= $round['CompetitionRoundId'])
{
$isactive = ' active ';
}
// for some reason this chunk is blowing up, so echo out the html instead
echo '<div class="step' . $isactive . '">';
echo '<a href="' . $href . '">';
echo $round['CompetitionRoundName'];
echo '</a>';
echo '<span></span>';
echo "</div>";
/*
?>
<div class="step <?php echo $isactive ?>">
<a href="<?php echo $href ?>">
<?php echo $round['CompetitionRoundName'] ?>
</a>
<span></span>
</div>
<?
*/
}
?>
</div>
</div>
</div>
<div style="margin-bottom:120px;">
</div>
<?php
}
if($currentTime < $startDate)
{
?>
<div class="alert alert-info" role="alert">
Bracket entries will open on <?php echo $startDate->format("M d, Y"); ?> and close on <?php echo $endDate->format("M d, Y"); ?>. In the mean time, you can research bid winners at <a href="http://www.theroadtoworlds.com" target="_blank">The Road to Worlds</a>.
</div>
<?php
}
else
{
if(isset($isSaved) && $isSaved == True)
{
?>
<div class="alert alert-success" role="alert">
Bracket saved!
</div>
<?php
}
if (!isset($CURRENT_USER)) {
echo '<div class="alert alert-warning" role="alert">Please <a href="' . $config['fierceboardUrl'] . 'login/">first log into fierceboard</a> to take part in the bracket contest</div>';
}
?>
<div class="alert alert-info" role="alert">
To select a team, start typing the team name in the textbox. A dropdown will appear with matching options. There may be a slight delay before the list of teams appears.
<BR/><br/>
If a team is missing, please message <a href="<?php echo $config['fierceboardUrl'] ?>conversations/add?to=Ashley" target="_blank">Ashley</a>.
<?php
if($roundid == 1)
{
echo "<br/><br/><strong>Reminder: Prelims are at-large teams only.</strong>";
}
?>
</div>
<div class="alert alert-warning" role="alert">
<?php
$scoringDetails = $divisions[0]['ScoringNotes'];
echo "<h4>Scoring Notes</h4>";
echo $scoringDetails;
?>
</div>
<div class="alert alert-danger" style="display:none;" id="errorBox" role="alert">
</div>
<form class="form-horizontal" method="post" name="bracket-form" id="bracket-form" enctype="multipart/form-data">
<?php
foreach($divisions as $div) {
$divId = $div['DivisionId'];
$divName = $div['DivisionName'];
echo '<div class="chart" data-division="' . $divId . '">';
if ($div['TieBreakOrder'] != "0")
{
echo "<h3>" . $divName . " <small>(Tie Breaker)</small></h3>";
}
else
{
echo "<h3>" . $divName . "</h3>";
}
$entries = $BRACKET_DATA_BO->getBracketEntriesDict($bracketid, $roundid, $divId, $CURRENT_USER["user_id"]);
for($i = 1; $i <= $div['NumEntries']; $i++)
{
$textInputName = str_replace('{1}', $i, str_replace('{0}', $divId, $textInputFormat));
$hiddenInputName = str_replace('{1}', $i, str_replace('{0}', $divId, $idInputFormat));
$textInputVal = "";
$hiddenInputVal = "";
if(isset($entries[$i]))
{
$textInputVal = $entries[$i]['TeamName'];
$hiddenInputVal = $entries[$i]['TeamId'];
}
?>
<div class="form-group">
<label class="col-sm-1 col-md-1 control-label">
<?php echo $i ?>.
</label>
<div class="col-sm-11 col-md-9">
<?php
if($currentTime > $endDate)
{
echo '<span class="form-control">' . $textInputVal . '</span>';
}
else
{
?>
<div class="input-wrapper">
<input type="text" class="form-control auto-complete" name="<?php echo $textInputName ?>" value="<?php echo $textInputVal ?>" data-division="<?php echo $divId?>" data-placement="<?php echo $i?>" />
<input type="hidden" class="hidden-ac-val" name="<?php echo $hiddenInputName ?>" value="<?php echo $hiddenInputVal ?>" data-division="<?php echo $divId ?>" data-placement="<?php echo $i?>" />
</div>
<?php
}
?>
</div>
</div>
<?php
}
echo '</div>';
}
if (isset($CURRENT_USER) && ($currentTime < $endDate))
{
?>
<input type="submit" text="save" class="btn btn-primary" name="btnSubmit" id="btnSubmit">
<input type="submit" value="Save and Next Round" class="btn btn-primary pull-right" name="btnSubmitNext" id="btnSubmitNext">
<?php
}
}
?>
</form>
</div>
</div>
<script>
function SortByName(a, b){
var aName = a.label.toLowerCase();
var bName = b.label.toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}
$(function() {
var dict = {};
$('#bracket-form .chart').each(function(i, el) {
// load the list of teams from RTW
var divId = $(el).data('division');
$.ajax({
url: "<?php echo $config['rtwServiceUrl'] ?>GetBidWinners?divisionid=" + divId,
dataType: "jsonp",
divisionId: divId,
divWrapper: $(el),
success: function( data ) {
<?php
if($atLargeOnly)
{
?>
data = $.grep(data, function(n, i){
return n.BidType == 'AtLarge';
});
<?php
}
?>
// store the data
dict[this.divisionId] = data;
// iterate over the controls and store info in the data attr
this.divWrapper.find('.input-wrapper').each(function(i, el) {
var textInput = $(el).find('.auto-complete').first();
var hiddenInput = $(el).find('.hidden-ac-val').first();
// default the team-name data attr to blank
textInput.data('team-name','');
if($.trim(hiddenInput.val()) == '' || hiddenInput.val() == '0') {
// if the hidden input is blank or 0, clear the text
hiddenInput.val('');
textInput.val('');
}
else {
// store the team name as a data attribute for validation later
var match = $.grep(data, function(n, i) {
return n.TeamId == hiddenInput.val();
});
if (match.length > 0) {
textInput.data('team-name', match[0].FullTeamName);
textInput.val(match[0].FullTeamName); // update the text box val if the data changed at RTW
}
}
});
}
});
});
$( ".auto-complete" ).each(function(i, el) {
// the input element
var el = $(el);
// the hidden input
var elVal = el.siblings('.hidden-ac-val').first();
// validate on blur
el.blur(function(){
if($(this).val() != $(this).data('team-name')) {
$(this).closest('.form-group').addClass('has-error');
}
else {
$(this).closest('.form-group').removeClass('has-error');
}
if($('.has-error').length > 0) {
$('#errorBox').show();
$('#errorBox').text('There is a problem with your team selection. Please fix the rows highlighted in red before you save.');
$("#btnSubmit").prop('disabled', true);
}
else {
$('#errorBox').hide();
$("#btnSubmit").prop('disabled', false);
}
});
// set up autocomplete
el.autocomplete({
select: function(e, ui) {
// set the hidden field value to the be the teamid and the label to be the text
e.preventDefault()
elVal.val(ui.item.value);
$(this).data('team-name', ui.item.label)
$(this).val(ui.item.label);
},
focus: function( e, ui ) {
e.preventDefault()
elVal.val(ui.item.value);
$(this).data('team-name', ui.item.label);
$(this).val(ui.item.label);
},
source: function(req, response) {
// get the division/data
var id = el.data('division');
data = dict[id];
// filter the data
var newData = $.grep(data, function(n, i){
return n.FullTeamName.toLowerCase().indexOf(req.term.toLowerCase()) > -1;
});
// now create a new array of data
var selectData = [];
for(var i = 0; i < newData.length; i++)
{
selectData.push({label: newData[i].FullTeamName, value: newData[i].TeamId, bidtype:newData[i].BidType});
}
// send the select data to the response
response( selectData.sort(SortByName));
},
create: function () {
$(this).data('ui-autocomplete')._renderItem = function (ul, item) {
var bidType = '';
if(item.bidtype == 'FullPaid')
bidType = 'Full Paid';
else if (item.bidtype == 'PartialPaid')
bidType = 'Partial Paid';
else if (item.bidtype == "AtLarge")
bidType = 'At Large';
return $('<li>')
.append('<a>' + item.label + '<span class="pull-right">(' + bidType + ')</span></a>')
.appendTo(ul);
};
}
})
});
});
</script>
<?php include $root . "/worldsbracket/" . 'footer.php'; ?>