/
Match.php
134 lines (114 loc) · 4.14 KB
/
Match.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
<?php
/**
* Sports Match Administrator
*
* Copyright © 2014-2015, Jack P. Harley, jackpharley.com
* All Rights Reserved
*/
namespace sma\controllers;
use DateTime;
use sma\Database;
use sma\exceptions\DuplicateException;
use sma\models\League;
use sma\models\MatchReport;
use sma\models\Player;
use sma\models\Team;
use sma\models\User;
use sma\models\Alert;
use sma\Controller;
use sma\View;
use sma\models\Match as MatchModel;
class Match {
public static function submit() {
Controller::requirePermissions(["SubmitMatchReports"]);
if (empty($_POST)) {
View::load("match/submit.twig", [
"leagues" => League::get(),
"players" => Player::get()
]);
}
else {
// basic input validation
Controller::requireFields("post", ["date", "league", "reporter-team", "reporter-score",
"opposing-team", "opposing-score"], "/match/submit");
$datetime = DateTime::createFromFormat("Y-m-d", $_POST["date"]);
$epoch = $datetime->getTimestamp();
if (($datetime === false) || (array_sum($datetime->getLastErrors())) || ($epoch > time()) || ((time()-$epoch)>(3600*24*365))) {
Controller::addAlert(new Alert("danger", "You did not enter a valid date, please try again."));
Controller::redirect("/match/submit");
}
// check authorization of user to file reports on behalf of reporting team
$reporterTeam = current(Team::get($_POST["reporter-team"]));
$visitor = User::getVisitor();
if ($visitor->organizationId != $reporterTeam->organizationId)
Controller::requirePermissions(["SubmitMatchReportsForAnyTeam"]);
// start determining the data for insertion
if ($_POST["location"] == "home") { // reporting team is home
$homeTeamId = $_POST["reporter-team"];
$homeScore = $_POST["reporter-score"];
$awayTeamId = $_POST["opposing-team"];
$awayScore = $_POST["opposing-score"];
}
else {
$awayTeamId = $_POST["reporter-team"];
$awayScore = $_POST["reporter-score"];
$homeTeamId = $_POST["opposing-team"];
$homeScore = $_POST["opposing-score"];
}
// transaction
Database::getConnection()->beginTransaction();
// attempt to pull an existing match record or add a new one
$match = current(MatchModel::get(null, $_POST["date"], $_POST["league"], $homeTeamId, $awayTeamId));
if ($match)
$matchId = $match->id;
else
$matchId = MatchModel::add($_POST["date"], $_POST["league"], $homeTeamId, $awayTeamId);
try {
MatchReport::add($matchId, $_POST["reporter-team"], $visitor->id, $homeScore,
$awayScore);
}
catch (DuplicateException $e) {
Database::getConnection()->rollBack();
Controller::addAlert(new Alert("danger", "You have already submitted a report for that match!"));
Controller::redirect("/match/submit");
}
if (!$match)
$match = current(MatchModel::get($matchId));
$players = $reporterTeam->getPlayers();
foreach($players as $player) {
if (array_key_exists("player" . $player->id, $_POST))
$match->addParticipatingPlayer($reporterTeam->id, $player->id);
}
for($i=1;$i<=8;$i++) {
if ((array_key_exists("additional-player" . $i, $_POST)) && ($_POST["additional-player" . $i]))
$match->addParticipatingPlayer($reporterTeam->id, null, $_POST["additional-player" . $i]);
}
// commit
Database::getConnection()->commit();
// attempt reconciliation
$matches = MatchModel::get($matchId);
current($matches)->attemptReportReconciliation();
Controller::addAlert(new Alert("success", "Match report submitted successfully!"));
Controller::redirect("/match/record?id=" . $matchId);
}
}
public static function submitted() {
Controller::requirePermissions(["SubmitMatchReports"]);
$visitor = User::getVisitor();
$teams = Team::get(null, $visitor->organizationId);
$teamIds = [];
foreach($teams as $team)
$teamIds[] = $team->id;
$reports = MatchReport::get(null, null, null, $teamIds, 25);
View::load("match/submitted.twig", [
"organizationReports" => $reports,
"userReports" => MatchReport::get(null, null, $visitor->id, null, 25)
]);
}
public static function record() {
$match = current(MatchModel::get($_GET["id"]));
View::load("match/record.twig", [
"match" => $match
]);
}
}