public function executeAction($parameters)
 {
     $user = $this->_websoccer->getUser();
     $clubId = $user->getClubId($this->_websoccer, $this->_db);
     // check if it is own player
     $player = PlayersDataService::getPlayerById($this->_websoccer, $this->_db, $parameters["id"]);
     if ($clubId != $player["team_id"]) {
         throw new Exception("nice try");
     }
     // if player is not happy at club, he does not want to extend at all
     $satisfaction = $player["player_strength_satisfaction"];
     if ($satisfaction < MINIMUM_SATISFACTION_FOR_EXTENSION) {
         throw new Exception($this->_i18n->getMessage("extend-contract_player_is_unhappy"));
     }
     // check if player is already on market
     if ($player["player_transfermarket"]) {
         throw new Exception($this->_i18n->getMessage("sell_player_already_on_list"));
     }
     // no salary decrease
     if ($parameters["salary"] < $player["player_contract_salary"]) {
         throw new Exception($this->_i18n->getMessage("extend-contract_lower_than_current_salary"));
     }
     $averageSalary = $this->getAverageSalary($player["player_strength"]);
     // if salary is already higher than average, then just expect 10% more
     if ($player["player_contract_salary"] > $averageSalary) {
         $salaryFactor = 1.1;
     } else {
         // make minimum salary dependent on happiness
         $salaryFactor = (200 - $satisfaction) / 100;
     }
     $salaryFactor = max(1.1, $salaryFactor);
     $minSalary = round($player["player_contract_salary"] * $salaryFactor);
     // the salary should be at least 90% of the average, except if this would douple the salary
     if ($averageSalary < $parameters["salary"] * 2) {
         $minSalaryOfAverage = round(0.9 * $averageSalary);
         $minSalary = max($minSalary, $minSalaryOfAverage);
     }
     if ($parameters["salary"] < $minSalary) {
         // decrease satisfaction
         $this->decreaseSatisfaction($player["player_id"], $player["player_strength_satisfaction"]);
         throw new Exception($this->_i18n->getMessage("extend-contract_salary_too_low"));
     }
     // check if club can pay this salary
     TeamsDataService::validateWhetherTeamHasEnoughBudgetForSalaryBid($this->_websoccer, $this->_db, $this->_i18n, $clubId, $parameters["salary"]);
     // check goal bonus
     $minGoalBonus = $player["player_contract_goalbonus"] * 1.3;
     if ($parameters["goal_bonus"] < $minGoalBonus) {
         throw new Exception($this->_i18n->getMessage("extend-contract_goalbonus_too_low"));
     }
     $this->updatePlayer($player["player_id"], $player["player_strength_satisfaction"], $parameters["salary"], $parameters["goal_bonus"], $parameters["matches"]);
     // reset inactivity
     UserInactivityDataService::resetContractExtensionField($this->_websoccer, $this->_db, $user->id);
     // success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("extend-contract_success"), ""));
     return null;
 }
 /**
  * (non-PHPdoc)
  * @see IActionController::executeAction()
  */
 public function executeAction($parameters)
 {
     // check if feature is enabled
     if (!$this->_websoccer->getConfig('transfermarket_enabled')) {
         return;
     }
     $user = $this->_websoccer->getUser();
     $clubId = $user->getClubId($this->_websoccer, $this->_db);
     $playerId = $parameters['id'];
     // check if user has a club
     if ($clubId < 1) {
         throw new Exception($this->_i18n->getMessage('error_action_required_team'));
     }
     // check if it is not own player
     $player = PlayersDataService::getPlayerById($this->_websoccer, $this->_db, $playerId);
     if ($user->id == $player['team_user_id']) {
         throw new Exception($this->_i18n->getMessage('transfer_bid_on_own_player'));
     }
     // check if player is still on transfer list
     if (!$player['player_transfermarket']) {
         throw new Exception($this->_i18n->getMessage('transfer_bid_player_not_on_list'));
     }
     // check that auction is not over
     $now = $this->_websoccer->getNowAsTimestamp();
     if ($now > $player['transfer_end']) {
         throw new Exception($this->_i18n->getMessage('transfer_bid_auction_ended'));
     }
     // player must accept the new salary
     $minSalary = $player['player_contract_salary'] * 1.1;
     if ($parameters['contract_salary'] < $minSalary) {
         throw new Exception($this->_i18n->getMessage('transfer_bid_salary_too_less'));
     }
     // check goal bonus
     $minGoalBonus = $player['player_contract_goalbonus'] * 1.1;
     if ($parameters['contract_goal_bonus'] < $minGoalBonus) {
         throw new Exception($this->_i18n->getMessage('transfer_bid_goalbonus_too_less'));
     }
     // check if user has been already traded too often with the other user
     if ($player['team_id'] > 0) {
         $noOfTransactions = TransfermarketDataService::getTransactionsBetweenUsers($this->_websoccer, $this->_db, $player['team_user_id'], $user->id);
         $maxTransactions = $this->_websoccer->getConfig('transfermarket_max_transactions_between_users');
         if ($noOfTransactions >= $maxTransactions) {
             throw new Exception($this->_i18n->getMessage('transfer_bid_too_many_transactions_with_user', $noOfTransactions));
         }
     }
     // get existing highest bid
     $highestBid = TransfermarketDataService::getHighestBidForPlayer($this->_websoccer, $this->_db, $parameters['id'], $player['transfer_start'], $player['transfer_end']);
     // with transfer-fee: check if own bid amount is higher than existing bid
     if ($player['team_id'] > 0) {
         $minBid = $player['transfer_min_bid'] - 1;
         if (isset($highestBid['amount'])) {
             $minBid = $highestBid['amount'];
         }
         if ($parameters['amount'] <= $minBid) {
             throw new Exception($this->_i18n->getMessage('transfer_bid_amount_must_be_higher', $minBid));
         }
         // without transfer fee: compare contract conditions
     } else {
         if (isset($highestBid['contract_matches'])) {
             // we compare the total income of the whole offered contract duraction
             $ownBidValue = $parameters['handmoney'] + $parameters['contract_matches'] * $parameters['contract_salary'];
             $opponentSalary = $highestBid['hand_money'] + $highestBid['contract_matches'] * $highestBid['contract_salary'];
             // consider goal bonus only for midfield and striker, assuming player scores 10 goals
             if ($player['player_position'] == 'midfield' || $player['player_position'] == 'striker') {
                 $ownBidValue += 10 * $parameters['contract_goal_bonus'];
                 $opponentSalary += 10 * $highestBid['contract_goalbonus'];
             }
             if ($ownBidValue <= $opponentSalary) {
                 throw new Exception($this->_i18n->getMessage('transfer_bid_contract_conditions_too_low'));
             }
         }
     }
     // check if budget is enough (hand money/fee + assume that the team consists of 20 players with same salary, then it should survive for 2 matches)
     TeamsDataService::validateWhetherTeamHasEnoughBudgetForSalaryBid($this->_websoccer, $this->_db, $this->_i18n, $clubId, $parameters['contract_salary']);
     // check if budget is enough for all current highest bids of user.
     $team = TeamsDataService::getTeamSummaryById($this->_websoccer, $this->_db, $clubId);
     $result = $this->_db->querySelect('SUM(abloese) + SUM(handgeld) AS bidsamount', $this->_websoccer->getConfig('db_prefix') . '_transfer_angebot', 'user_id = %d AND ishighest = \'1\'', $user->id);
     $bids = $result->fetch_array();
     $result->free();
     if (isset($bids['bidsamount']) && $parameters['handmoney'] + $parameters['amount'] + $bids['bidsamount'] >= $team['team_budget']) {
         throw new Exception($this->_i18n->getMessage('transfer_bid_budget_for_all_bids_too_less'));
     }
     // save bid
     $this->saveBid($playerId, $user->id, $clubId, $parameters);
     // mark previous highest bid as outbidden
     if (isset($highestBid['bid_id'])) {
         $this->_db->queryUpdate(array('ishighest' => '0'), $this->_websoccer->getConfig('db_prefix') . '_transfer_angebot', 'id = %d', $highestBid['bid_id']);
     }
     // notify outbidden user
     if (isset($highestBid['user_id']) && $highestBid['user_id']) {
         $playerName = strlen($player['player_pseudonym']) ? $player['player_pseudonym'] : $player['player_firstname'] . ' ' . $player['player_lastname'];
         NotificationsDataService::createNotification($this->_websoccer, $this->_db, $highestBid['user_id'], 'transfer_bid_notification_outbidden', array('player' => $playerName), 'transfermarket', 'transfer-bid', 'id=' . $playerId);
     }
     // success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage('transfer_bid_success'), ''));
     return null;
 }
 public function executeAction($parameters)
 {
     // check if feature is enabled
     if (!$this->_websoccer->getConfig("transferoffers_enabled")) {
         return;
     }
     $clubId = $this->_websoccer->getUser()->getClubId($this->_websoccer, $this->_db);
     // check if user has team
     if ($clubId == null) {
         throw new Exception($this->_i18n->getMessage("feature_requires_team"));
     }
     $player = PlayersDataService::getPlayerById($this->_websoccer, $this->_db, $this->_websoccer->getRequestParameter("id"));
     // check if player team has a manager
     if (!$player["team_user_id"]) {
         throw new Exception($this->_i18n->getMessage("transferoffer_err_nomanager"));
     }
     // check if player is already in one of user's teams
     if ($player["team_user_id"] == $this->_websoccer->getUser()->id) {
         throw new Exception($this->_i18n->getMessage("transferoffer_err_ownplayer"));
     }
     // check if player is unsellable or already on transfer market
     if ($player["player_unsellable"] || $player["player_transfermarket"]) {
         throw new Exception($this->_i18n->getMessage("transferoffer_err_unsellable"));
     }
     // check if there are open transfer offered by the user for manager of player
     $this->checkIfThereAreAlreadyOpenOffersFromUser($player["team_id"]);
     // check if user is allowed to send an alternative offer after previous offer had been rejected
     $this->checkIfUserIsAllowedToSendAlternativeOffers($player["player_id"]);
     // check player is allowed to transfer (Wechselsperre)
     $this->checkPlayersTransferStop($player["player_id"]);
     // check exchange player
     if ($parameters["exchangeplayer1"]) {
         $this->checkExchangePlayer($parameters["exchangeplayer1"]);
     }
     if ($parameters["exchangeplayer2"]) {
         $this->checkExchangePlayer($parameters["exchangeplayer2"]);
     }
     // check if team is above minimum number of players.
     if ($parameters["exchangeplayer1"] || $parameters["exchangeplayer2"]) {
         $teamSize = TeamsDataService::getTeamSize($this->_websoccer, $this->_db, $clubId);
         $numberOfSizeReduction = $parameters["exchangeplayer2"] ? 1 : 0;
         if ($teamSize < $this->_websoccer->getConfig("transfermarket_min_teamsize") - $numberOfSizeReduction) {
             throw new Exception($this->_i18n->getMessage("sell_player_teamsize_too_small", $teamSize));
         }
     }
     // check maximum number of transactions between same user within last 30 days
     $noOfTransactions = TransfermarketDataService::getTransactionsBetweenUsers($this->_websoccer, $this->_db, $player["team_user_id"], $this->_websoccer->getUser()->id);
     $maxTransactions = $this->_websoccer->getConfig("transfermarket_max_transactions_between_users");
     if ($noOfTransactions >= $maxTransactions) {
         throw new Exception($this->_i18n->getMessage("transfer_bid_too_many_transactions_with_user", $noOfTransactions));
     }
     // check if budget is enough to pay this amount and sum of other open offers
     $team = TeamsDataService::getTeamSummaryById($this->_websoccer, $this->_db, $clubId);
     $totalOffers = $this->getSumOfAllOpenOffers() + $parameters["amount"];
     if ($team["team_budget"] < $totalOffers) {
         throw new Exception($this->_i18n->getMessage("transferoffer_err_totaloffers_too_high"));
     }
     // check if club can pay this salary
     TeamsDataService::validateWhetherTeamHasEnoughBudgetForSalaryBid($this->_websoccer, $this->_db, $this->_i18n, $clubId, $player["player_contract_salary"]);
     // submit offer
     DirectTransfersDataService::createTransferOffer($this->_websoccer, $this->_db, $player["player_id"], $this->_websoccer->getUser()->id, $clubId, $player["team_user_id"], $player["team_id"], $parameters["amount"], $parameters["comment"], $parameters["exchangeplayer1"], $parameters["exchangeplayer2"]);
     // show success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("transferoffer_submitted_title"), $this->_i18n->getMessage("transferoffer_submitted_message")));
     return null;
 }