144 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			144 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| $app = require '../../../app/inc.php';
 | |
| 
 | |
| $model = $app->model('Teamtable');
 | |
| 
 | |
| if (!isset($_GET['item']))
 | |
| {
 | |
|     $app->session->flash('Kunne ikke endre lag: ID ikke definert som GET parameter', 'danger');
 | |
|     $app->redirect('index.php');
 | |
| }
 | |
| $id = $_GET['item'];
 | |
| 
 | |
| // Id must be a number
 | |
| if (!is_numeric($id))
 | |
| {
 | |
|     $app->session->flash('Kunne ikke endre lag: ID må være tall', 'danger');
 | |
|     $app->redirect('index.php');
 | |
| }
 | |
| 
 | |
| // Check if team with supplied ID exists
 | |
| $currentTeam = $model->getTeamByID($id);
 | |
| if (!$currentTeam)
 | |
| {
 | |
|     $app->session->flash("Kunne ikke endre lag: ID $id finnes ikke", "danger");
 | |
|     $app->redirect('index.php');
 | |
| }
 | |
| 
 | |
| if ($_SERVER['REQUEST_METHOD'] === 'POST')
 | |
| {
 | |
|     // Check that all parameters are present
 | |
|     if (!isset(
 | |
|             $_POST['LagNavn'],
 | |
|             $_POST['Bedrift'],
 | |
|             $_POST['Kortnummer'],
 | |
|             $_POST['Lagleder'],
 | |
|             $_POST['Telefon'],
 | |
|             $_POST['Deltagere'],
 | |
|             $_POST['Runder'],
 | |
|         )
 | |
|     )
 | |
|     {
 | |
|         $app->session->flash("Kunne ikke endre lag: Ikke alle POST parametere er til stede!", "danger");
 | |
|         $app->redirect('./');
 | |
|     }
 | |
| 
 | |
|     $LagNavn    = $_POST['LagNavn'];
 | |
|     $Bedrift    = $_POST['Bedrift'];
 | |
|     $Kortnummer = $_POST['Kortnummer'];
 | |
|     $Lagleder   = $_POST['Lagleder'];
 | |
|     $Telefon    = $_POST['Telefon'];
 | |
|     $Deltagere  = $_POST['Deltagere'];
 | |
|     $Runder     = $_POST['Runder'];
 | |
| 
 | |
|     //====Validate Input====//
 | |
| 
 | |
|     $validationError = FALSE;
 | |
|     $template = $model->emptyTeamTemplate;
 | |
| 
 | |
|     // LagNavn
 | |
|     if (!strlen($LagNavn)) {
 | |
|         $LagNavn = $template['LagNavn']; 
 | |
|     }
 | |
|     if (strlen($LagNavn) > 32) {
 | |
|         $validationError = TRUE;
 | |
|     }
 | |
| 
 | |
|     // Bedrift
 | |
|     if (!strlen($Bedrift)) {
 | |
|         $Bedrift = $template['Bedrift'];
 | |
|     }
 | |
|     if (strlen($Bedrift) > 32) {
 | |
|         $validationError = TRUE;
 | |
|     }
 | |
| 
 | |
|     // Kortnummer
 | |
|     if (!strlen($Kortnummer)) {
 | |
|         $Kortnummer = $template['Kortnummer'];
 | |
|     }
 | |
|     if (strlen($Kortnummer) > 32) {
 | |
|         $validationError = TRUE;
 | |
|     }
 | |
| 
 | |
|     // Lagleder
 | |
|     if (!strlen($Lagleder)) {
 | |
|         $Lagleder = $template['Lagleder'];
 | |
|     }
 | |
|     if (strlen($Lagleder) > 32) {
 | |
|         $validationError = TRUE;
 | |
|     }
 | |
| 
 | |
|     // Telefon
 | |
|     if (!strlen($Telefon)) {
 | |
|         $Telefon = $template['Telefon'];
 | |
|     }
 | |
|     if (strlen((string) $Telefon) > 32) {
 | |
|         $validationError = TRUE;
 | |
|     }
 | |
| 
 | |
|     // Deltagere
 | |
|     if (!strlen($Deltagere)) {
 | |
|         $Deltagere = $template['Deltagere'];
 | |
|     }
 | |
|     if (strlen((string) $Deltagere) > 32) {
 | |
|         $validationError = TRUE;
 | |
|     }
 | |
| 
 | |
|     // Runder
 | |
|     if (!strlen($Runder)) {
 | |
|         $Runder = $template['Runder'];
 | |
|     }
 | |
|     if (strlen((string) $Runder) > 32) {
 | |
|         $validationError = TRUE;
 | |
|     }
 | |
| 
 | |
| 
 | |
|     if (!$validationError) {
 | |
|         // All is good! Lets update the team details
 | |
|         $model->updateTeamByID(
 | |
|             $id, 
 | |
|             $LagNavn,
 | |
|             $Bedrift,
 | |
|             $Kortnummer,
 | |
|             $Lagleder,
 | |
|             $Telefon,
 | |
|             $Deltagere,
 | |
|             $Runder,
 | |
|         );
 | |
| 
 | |
|         $app->session->flash('Lagret endringer for lag: '.$LagNavn, 'success');
 | |
|         $app->redirect('index.php');   
 | |
|     } else {
 | |
|         $app->session->flash('Kunne ikke endre lag: Validerings feil!', 'danger');
 | |
|     }
 | |
| }
 | |
| 
 | |
| // Escape all values
 | |
| foreach ($currentTeam as $key => $value)
 | |
| {
 | |
|     $currentTeam[$key] = htmlspecialchars($currentTeam[$key]);
 | |
| }
 | |
| 
 | |
| $app->view('template/header', ['title' => 'Endre lagdetaljer']);
 | |
| $app->view('pages/teamtable/edit/update', ["team" => $currentTeam]);
 | |
| $app->view('template/footer'); |