/
Team.php
152 lines (128 loc) · 5.03 KB
/
Team.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
<?php
/**
* Sports Match Administrator
*
* Copyright © 2014-2015, Jack P. Harley, jackpharley.com
* All Rights Reserved
*/
namespace sma\controllers;
use sma\ErrorHandler;
use sma\exceptions\DuplicateException;
use sma\models\League;
use sma\models\Player;
use sma\models\User;
use sma\models\Alert;
use sma\Controller;
use sma\View;
use sma\models\Team as TeamModel;
use sma\models\Organization;
class Team {
public static function register() {
Controller::requirePermissions(["RegisterTeamsForOwnOrganization",
"RegisterTeamsForAnyOrganization"], "any");
if (empty($_POST)) {
$teams = (User::getVisitor()->organizationId) ? TeamModel::get(null, User::getVisitor()->organizationId) : null;
View::load("team/register_form.twig", [
"organizations" => Organization::get(),
"designations" => TeamModel::getValidDesignations(),
"leagues" => League::get(),
"teams" => $teams
]);
}
else {
// add the team
if (User::getVisitor()->checkPermissions(["RegisterTeamsForAnyOrganization"]))
$organizationId = $_POST["organization-id"];
else
$organizationId = User::getVisitor()->organizationId;
try {
if (ALLOW_TEAM_REGISTRANTS_TO_SELECT_LEAGUE)
$teamId = TeamModel::add($organizationId, $_POST["designation"], User::getVisitor()->id, $_POST["league-id"]);
else
$teamId = TeamModel::add($organizationId, $_POST["designation"], User::getVisitor()->id);
}
catch (DuplicateException $e) {
Controller::addAlert(new Alert("danger", "You cannot register more than one team with the same name. " .
"To edit an existing team please use the edit button beside the team in the Registered Teams box."));
Controller::redirect("/team/register");
}
// add the players
$exemptsAdded = 0;
for ($i = 1; array_key_exists("player" . $i, $_POST); $i++) {
if ($_POST["player" . $i]) {
if (isset($_POST["player" . $i . "e"])) {
if ($exemptsAdded < MAX_EXEMPTS) {
$makeExempt = true;
$exemptsAdded++;
}
else {
$makeExempt = false;
Controller::addAlert(new Alert("warning", "You attempted to star " . $_POST["player" . $i] . " but you had already starred " .
MAX_EXEMPTS . " other players, which is the maxmimum allowed, thus " . $_POST["player" . $i] . "was not starred"));
}
}
else {
$makeExempt = false;
}
try {
Player::add($_POST["player" . $i], $teamId, $makeExempt);
}
catch (DuplicateException $e) {
Controller::addAlert(new Alert("info", "You entered the name " .
$_POST["player" . $i] . " more than once, only the first entry was" .
"added to the database"));
}
}
}
Controller::addAlert(new Alert("success",
"You have successfully registered your team and its details are shown below. You can come back to this area up until the freeze date and make changes."));
Controller::redirect("/team/edit?id=" . $teamId);
}
}
public static function edit() {
$team = current(TeamModel::get($_GET["id"]));
if (!User::getVisitor()->checkPermissions(["RegisterTeamsForAnyOrganization"])) {
Controller::requirePermissions(["RegisterTeamsForOwnOrganization"]);
if ($team->organizationId != User::getVisitor()->organizationId)
ErrorHandler::forbidden();
}
if (!empty($_POST)) {
TeamModel::update($_POST["id"], null, $_POST["designation"]);
Controller::addAlert(new Alert("success", "Team details updated successfully"));
$team = current(TeamModel::get($_GET["id"]));
}
View::load("team/edit.twig", [
"team" => $team
]);
}
public static function addplayer() {
Controller::requireFields("post", ["name", "team"], "/acp/team");
$team = current(TeamModel::get($_POST["team"]));
if (!User::getVisitor()->checkPermissions(["RegisterTeamsForAnyOrganization"])) {
Controller::requirePermissions(["RegisterTeamsForOwnOrganization"]);
if ($team->organizationId != User::getVisitor()->organizationId)
ErrorHandler::forbidden();
}
Player::add($_POST["name"], $team->id, false);
Controller::addAlert(new Alert("success", "Player added successfully"));
Controller::redirect("/team/edit?id=" . $team->id);
}
public static function updateplayer() {
Controller::requireFields("get", ["id"], "/acp/team");
$player = current(Player::get($_GET["id"]));
if (!User::getVisitor()->checkPermissions(["RegisterTeamsForAnyOrganization"])) {
Controller::requirePermissions(["RegisterTeamsForOwnOrganization"]);
if ($player->getTeam()->organizationId != User::getVisitor()->organizationId)
ErrorHandler::forbidden();
}
if (($_GET["exempt"] == 1) && (!$player->exempt)) {
if ($player->getTeam()->getNumberOfExemptPlayers() >= MAX_EXEMPTS) {
Controller::addAlert(new Alert("danger", "You have already starred the maximum number of players"));
Controller::redirect("/team/edit?id=" . $player->getTeam()->id);
}
}
Player::update($player->id, null, (bool) $_GET["exempt"]);
Controller::addAlert(new Alert("success", "Player updated successfully"));
Controller::redirect("/team/edit?id=" . $player->getTeam()->id);
}
}