/**
  * (non-PHPdoc)
  * @see IModel::getTemplateParameters()
  */
 public function getTemplateParameters()
 {
     $teamId = $this->_websoccer->getUser()->getClubId($this->_websoccer, $this->_db);
     $stadium = StadiumsDataService::getStadiumByTeamId($this->_websoccer, $this->_db, $teamId);
     $construction = StadiumsDataService::getCurrentConstructionOrderOfTeam($this->_websoccer, $this->_db, $teamId);
     $upgradeCosts = array();
     if ($stadium) {
         $upgradeCosts["pitch"] = StadiumsDataService::computeUpgradeCosts($this->_websoccer, "pitch", $stadium);
         $upgradeCosts["videowall"] = StadiumsDataService::computeUpgradeCosts($this->_websoccer, "videowall", $stadium);
         $upgradeCosts["seatsquality"] = StadiumsDataService::computeUpgradeCosts($this->_websoccer, "seatsquality", $stadium);
         $upgradeCosts["vipquality"] = StadiumsDataService::computeUpgradeCosts($this->_websoccer, "vipquality", $stadium);
     }
     return array("stadium" => $stadium, "construction" => $construction, "upgradeCosts" => $upgradeCosts);
 }
 /**
  * (non-PHPdoc)
  * @see IActionController::executeAction()
  */
 public function executeAction($parameters)
 {
     $user = $this->_websoccer->getUser();
     $clubId = $user->getClubId($this->_websoccer, $this->_db);
     // verify that it is due
     $construction = StadiumsDataService::getCurrentConstructionOrderOfTeam($this->_websoccer, $this->_db, $clubId);
     if ($construction == NULL || $construction["deadline"] > $this->_websoccer->getNowAsTimestamp()) {
         throw new Exception($this->_i18n->getMessage("stadium_acceptconstruction_err_nonedue"));
     }
     // is completed?
     $pStatus["completed"] = $construction["builder_reliability"];
     $pStatus["notcompleted"] = 100 - $pStatus["completed"];
     $constructionResult = SimulationHelper::selectItemFromProbabilities($pStatus);
     // not completed: postpone deadline
     if ($constructionResult == "notcompleted") {
         $newDeadline = $this->_websoccer->getNowAsTimestamp() + $this->_websoccer->getConfig("stadium_construction_delay") * 24 * 3600;
         $this->_db->queryUpdate(array("deadline" => $newDeadline), $this->_websoccer->getConfig("db_prefix") . "_stadium_construction", "id = %d", $construction["id"]);
         // show warning alert
         $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_WARNING, $this->_i18n->getMessage("stadium_acceptconstruction_notcompleted_title"), $this->_i18n->getMessage("stadium_acceptconstruction_notcompleted_details")));
         // completed
     } else {
         // update stadium
         $stadium = StadiumsDataService::getStadiumByTeamId($this->_websoccer, $this->_db, $clubId);
         $columns = array();
         $columns["p_steh"] = $stadium["places_stands"] + $construction["p_steh"];
         $columns["p_sitz"] = $stadium["places_seats"] + $construction["p_sitz"];
         $columns["p_haupt_steh"] = $stadium["places_stands_grand"] + $construction["p_haupt_steh"];
         $columns["p_haupt_sitz"] = $stadium["places_seats_grand"] + $construction["p_haupt_sitz"];
         $columns["p_vip"] = $stadium["places_vip"] + $construction["p_vip"];
         $this->_db->queryUpdate($columns, $this->_websoccer->getConfig("db_prefix") . "_stadion", "id = %d", $stadium["stadium_id"]);
         // delete order
         $this->_db->queryDelete($this->_websoccer->getConfig("db_prefix") . "_stadium_construction", "id = %d", $construction["id"]);
         // create success message
         $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("stadium_acceptconstruction_completed_title"), $this->_i18n->getMessage("stadium_acceptconstruction_completed_details")));
     }
     return null;
 }
 /**
  * (non-PHPdoc)
  * @see IActionController::executeAction()
  */
 public function executeAction($parameters)
 {
     $user = $this->_websoccer->getUser();
     $teamId = $user->getClubId($this->_websoccer, $this->_db);
     if ($teamId < 1) {
         return null;
     }
     // any number entered?
     if (!$parameters["side_standing"] && !$parameters["side_seats"] && !$parameters["grand_standing"] && !$parameters["grand_seats"] && !$parameters["vip"]) {
         return null;
     }
     $stadium = StadiumsDataService::getStadiumByTeamId($this->_websoccer, $this->_db, $teamId);
     if (!$stadium) {
         return null;
     }
     // max limit exceeded?
     $seatsSide = $stadium["places_stands"] + $stadium["places_seats"] + $parameters["side_standing"] + $parameters["side_seats"];
     if ($seatsSide > $this->_websoccer->getConfig("stadium_max_side")) {
         throw new Exception($this->_i18n->getMessage("stadium_extend_err_exceed_max_side", $this->_websoccer->getConfig("stadium_max_side")));
     }
     $seatsGrand = $stadium["places_stands_grand"] + $stadium["places_seats_grand"] + $parameters["grand_standing"] + $parameters["grand_seats"];
     if ($seatsGrand > $this->_websoccer->getConfig("stadium_max_grand")) {
         throw new Exception($this->_i18n->getMessage("stadium_extend_err_exceed_max_grand", $this->_websoccer->getConfig("stadium_max_grand")));
     }
     $seatsVip = $stadium["places_vip"] + $parameters["vip"];
     if ($seatsVip > $this->_websoccer->getConfig("stadium_max_vip")) {
         throw new Exception($this->_i18n->getMessage("stadium_extend_err_exceed_max_vip", $this->_websoccer->getConfig("stadium_max_vip")));
     }
     // is construction already on-going?
     if (StadiumsDataService::getCurrentConstructionOrderOfTeam($this->_websoccer, $this->_db, $teamId) != NULL) {
         throw new Exception($this->_i18n->getMessage("stadium_extend_err_constructionongoing"));
     }
     if (isset($parameters["validate-only"]) && $parameters["validate-only"]) {
         return "stadium-extend-confirm";
     }
     // builder got selected? Illegal builder ID can only happen due to a bug or user input manipulation.
     $builderId = $this->_websoccer->getRequestParameter("offerid");
     $offers = StadiumsDataService::getBuilderOffersForExtension($this->_websoccer, $this->_db, $teamId, (int) $this->_websoccer->getRequestParameter("side_standing"), (int) $this->_websoccer->getRequestParameter("side_seats"), (int) $this->_websoccer->getRequestParameter("grand_standing"), (int) $this->_websoccer->getRequestParameter("grand_seats"), (int) $this->_websoccer->getRequestParameter("vip"));
     if ($builderId == NULL || !isset($offers[$builderId])) {
         throw new Exception("Illegal offer ID.");
     }
     // can user afford it?
     $offer = $offers[$builderId];
     $team = TeamsDataService::getTeamSummaryById($this->_websoccer, $this->_db, $teamId);
     $totalCosts = $offer["totalCosts"];
     if ($team["team_budget"] <= $totalCosts) {
         throw new Exception($this->_i18n->getMessage("stadium_extend_err_too_expensive"));
     }
     // try to debit premium fee
     if ($offer["builder_premiumfee"]) {
         PremiumDataService::debitAmount($this->_websoccer, $this->_db, $user->id, $offer["builder_premiumfee"], "extend-stadium");
     }
     // debit money
     BankAccountDataService::debitAmount($this->_websoccer, $this->_db, $teamId, $totalCosts, "stadium_extend_transaction_subject", $offer["builder_name"]);
     // create construction order
     $this->_db->queryInsert(array("team_id" => $teamId, "builder_id" => $builderId, "started" => $this->_websoccer->getNowAsTimestamp(), "deadline" => $offer["deadline"], "p_steh" => $parameters["side_standing"] ? $parameters["side_standing"] : 0, "p_sitz" => $parameters["side_seats"] ? $parameters["side_seats"] : 0, "p_haupt_steh" => $parameters["grand_standing"] ? $parameters["grand_standing"] : 0, "p_haupt_sitz" => $parameters["grand_seats"] ? $parameters["grand_seats"] : 0, "p_vip" => $parameters["vip"] ? $parameters["vip"] : 0), $this->_websoccer->getConfig("db_prefix") . "_stadium_construction");
     // success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("stadium_extend_success"), ""));
     // create action log manually here, ceause of this great "validate-only" idea...
     ActionLogDataService::createOrUpdateActionLog($this->_websoccer, $this->_db, $user->id, "extend-stadium");
     $seats = $parameters["side_standing"] + $parameters["side_seats"] + $parameters["grand_standing"] + $parameters["grand_seats"] + $parameters["vip"];
     BadgesDataService::awardBadgeIfApplicable($this->_websoccer, $this->_db, $user->id, 'stadium_construction_by_x', $seats);
     return "stadium";
 }