public static function assign()
 {
     Controller::requirePermissions(["AdminAccessDashboard", "AdminTeams"]);
     Controller::requireFields("get", ["id", "section"], "/acp/league");
     $section = current(LeagueSection::get($_GET["section"]));
     TeamModel::update($_GET["id"], null, null, $section->id, $section->leagueId);
     Controller::addAlert(new Alert("success", "Team assigned to section successfully"));
     Controller::redirect("/acp/league/manage?id=" . $section->leagueId);
 }
 public static function delete()
 {
     Controller::requirePermissions(["AdminAccessDashboard"]);
     if (!array_key_exists("id", $_GET)) {
         Controller::redirect("/acp/league");
     }
     $section = current(LeagueSectionModel::get($_GET["id"]));
     // check permissions
     $visitor = User::getVisitor();
     if ($visitor->id != $section->getLeague()->managerId) {
         Controller::requirePermissions(["AdminAllLeagues"]);
     }
     try {
         $section->delete();
         Controller::addAlert(new Alert("success", "League section deleted successfully"));
     } catch (ObjectCannotBeDeletedException $e) {
         Controller::addAlert(new Alert("danger", "You cannot delete a section which has teams assigned to it. Please reassign the teams to an alternative section first"));
     }
     Controller::redirect("/acp/league/manage?id=" . $section->getLeague()->id);
 }
 /**
  * Construct a nice array of dates to fixtures
  *
  * @return array like such:
  * [
  *   "18/09/2015" => [
  *     \stdClass(
  *       $homeTeam,
  *       $awayTeam
  *     ),
  *     \stdClass(
  *       $homeTeam,
  *       $awayTeam
  *     )
  *   ]
  * ]
  */
 public function constructFixtures()
 {
     // first we grab all fixtures for the league
     $fixtures = Fixture::get(null, $this->id, true);
     // we'll also be needing all sections
     $sections = LeagueSection::get(null, $this->id);
     // now we iterate over and expand them into teams
     /**
      * @var $returnData
      * struc:
      * [
      *   "18/09/2015" => [
      *     \stdClass(
      *       $homeTeam,
      *       $awayTeam
      *     ),
      *     \stdClass(
      *       $homeTeam,
      *       $awayTeam
      *     )
      *   ]
      * ]
      */
     $returnData = [];
     foreach ($fixtures as $fixture) {
         if (!array_key_exists($fixture->playByDate, $returnData)) {
             $returnData[$fixture->playByDate] = [];
         }
         if ($fixture->homeTeamId && $fixture->awayTeamId) {
             // id vs id are easy peasy
             $obj = new \stdClass();
             $obj->homeTeam = $fixture->homeTeam->organization->name . " " . $fixture->homeTeam->designation;
             $obj->awayTeam = $fixture->awayTeam->organization->name . " " . $fixture->awayTeam->designation;
             $returnData[$fixture->playByDate][] = $obj;
         } else {
             foreach ($sections as &$section) {
                 // pass by reference is important! otherwise we lose the assigned teams on the next loop and have to re-query (very costly!)
                 $homeTeam = false;
                 $awayTeam = false;
                 $potentialTeams = $section->getAssignedTeams();
                 // dwbi this is quick cached
                 foreach ($potentialTeams as $potentialTeam) {
                     if ($fixture->homeTeamAssignedNumber == $potentialTeam->assignedNumber) {
                         $homeTeam = $potentialTeam->organization->name . " " . $potentialTeam->designation;
                     } else {
                         if ($fixture->awayTeamAssignedNumber == $potentialTeam->assignedNumber) {
                             $awayTeam = $potentialTeam->organization->name . " " . $potentialTeam->designation;
                         }
                     }
                 }
                 if ($homeTeam && $awayTeam) {
                     $obj = new \stdClass();
                     $obj->homeTeam = $homeTeam;
                     $obj->awayTeam = $awayTeam;
                     $returnData[$fixture->playByDate][] = $obj;
                 }
             }
         }
     }
     return $returnData;
 }
 /**
  * Get league section
  *
  * @return \sma\models\LeagueSection
  */
 public function getLeagueSection()
 {
     if (!$this->leagueSection) {
         $this->leagueSection = current(LeagueSection::get($this->leagueSectionId));
     }
     return $this->leagueSection;
 }