Changeset 8 for trunk/Modules/Ride


Ignore:
Timestamp:
Jun 1, 2023, 12:18:18 AM (19 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
Location:
trunk/Modules/Ride
Files:
5 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Ride/Ride.php

    r7 r8  
    11<?php
    22
    3 include_once(dirname(__FILE__).'/RideModel.php');
     3class Ride extends Model
     4{
     5  function Add($Track, $SeatCount, $Car, $Details)
     6  {
     7    $this->Database->insert('Ride', array('SeatCount' => $SeatCount, 'Details' => $Details,
     8      'Car' => $Car, 'Driver' => $this->System->ModuleManager->Modules['User']->User['Id']));
     9    $RideId = $this->Database->insert_id;
    410
    5 class ModuleRide extends AppModule
    6 {
    7   function __construct($System)
    8   {
    9     parent::__construct($System);
    10     $this->Name = 'Ride';
    11     $this->Version = '1.0';
    12     $this->Creator = 'Chronos';
    13     $this->License = 'GNU/GPL';
    14     $this->Description = 'Ride management';
    15     $this->Dependencies = array('User', 'Map');
    16   }
    17 
    18   function DoStart()
    19   {
    20     $this->System->RegisterPage('', 'PageMain');
    21     $this->System->RegisterPage('nova-jizda', 'PageNewRide');
    22     $this->System->RegisterPage('jizda', 'PageRideDetail');
    23     $this->System->RegisterPage('jizdy', 'PageRideList');
    24     $this->System->RegisterPageBarItem('TopLeft', 'Logo', array($this, 'ShowLogo'));
    25   }
    26 
    27   function DoInstall()
    28   {
    29     $this->Database->query('CREATE TABLE `CarManufacturer` (
    30   `Id` int(11) NOT NULL AUTO_INCREMENT,
    31   `Name` varchar(255) NOT NULL,
    32   PRIMARY KEY (`Id`)
    33 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
    34     $this->Database->query('CREATE TABLE `CarModel` (
    35   `Id` int(11) NOT NULL AUTO_INCREMENT,
    36   `Name` varchar(255) NOT NULL,
    37   `Manufacturer` int(11) NOT NULL,
    38   PRIMARY KEY (`Id`),
    39   KEY `Manufacturer` (`Manufacturer`)
    40 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
    41     $this->Database->query('ALTER TABLE `CarModel`
    42     ADD CONSTRAINT `CarModel_ibfk_1` FOREIGN KEY (`Manufacturer`) REFERENCES `CarManufacturer` (`Id`);');
    43     $this->Database->query('CREATE TABLE `Car` (
    44   `Id` int(11) NOT NULL AUTO_INCREMENT,
    45   `Owner` int(11) NOT NULL,
    46   `Photo` int(11) DEFAULT NULL,
    47   `Model` int(11) NOT NULL,
    48   PRIMARY KEY (`Id`),
    49   KEY `Owner` (`Owner`),
    50   KEY `Model` (`Model`)
    51 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
    52    $this->Database->query('ALTER TABLE `Car`
    53   ADD CONSTRAINT `Car_ibfk_1` FOREIGN KEY (`Owner`) REFERENCES `User` (`Id`),
    54   ADD CONSTRAINT `Car_ibfk_2` FOREIGN KEY (`Model`) REFERENCES `CarModel` (`Id`);');
    55      $this->Database->query('CREATE TABLE `Ride` (
    56   `Id` int(11) NOT NULL AUTO_INCREMENT,
    57   `Driver` int(11) NOT NULL,
    58   `SeatCount` int(11) NOT NULL,
    59   `Car` int(11) NOT NULL,
    60   `Details` text NOT NULL,
    61   PRIMARY KEY (`Id`),
    62   KEY `Driver` (`Driver`),
    63   KEY `Car` (`Car`)
    64 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
    65     $this->Database->query('ALTER TABLE `Ride`
    66   ADD CONSTRAINT `Ride_ibfk_1` FOREIGN KEY (`Driver`) REFERENCES `User` (`Id`),
    67   ADD CONSTRAINT `Ride_ibfk_2` FOREIGN KEY (`Car`) REFERENCES `Car` (`Id`);');
    68     $this->Database->query('CREATE TABLE `RideStop` (
    69   `Id` int(11) NOT NULL AUTO_INCREMENT,
    70   `Ride` int(11) NOT NULL,
    71   `Time` datetime NOT NULL,
    72   `Place` varchar(255) NOT NULL,
    73   `Sequence` int(11) NOT NULL,
    74   `Price` int(11) NOT NULL,
    75   `PassengerCount` int(11) NOT NULL,
    76   PRIMARY KEY (`Id`),
    77   KEY `Ride` (`Ride`)
    78 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
    79     $this->Database->query('ALTER TABLE `RideStop`
    80   ADD CONSTRAINT `RideStop_ibfk_1` FOREIGN KEY (`Ride`) REFERENCES `Ride` (`Id`);');
    81    $this->Database->query('CREATE TABLE `Passenger` (
    82   `Id` int(11) NOT NULL AUTO_INCREMENT,
    83   `Ride` int(11) NOT NULL,
    84   `User` int(11) NOT NULL,
    85   `SeatCount` int(11) NOT NULL,
    86   `RideStopFrom` int(11) NOT NULL,
    87   `RideStopTo` int(11) NOT NULL,
    88   PRIMARY KEY (`Id`),
    89   KEY `User` (`User`),
    90   KEY `Ride` (`Ride`),
    91   KEY `RideStopFrom` (`RideStopFrom`),
    92   KEY `RideStopTo` (`RideStopTo`)
    93 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
    94    $this->Database->query('ALTER TABLE `Passenger`
    95   ADD CONSTRAINT `Passenger_ibfk_1` FOREIGN KEY (`Ride`) REFERENCES `Ride` (`Id`),
    96   ADD CONSTRAINT `Passenger_ibfk_2` FOREIGN KEY (`User`) REFERENCES `User` (`Id`),
    97   ADD CONSTRAINT `Passenger_ibfk_3` FOREIGN KEY (`RideStopFrom`) REFERENCES `RideStop` (`Id`),
    98   ADD CONSTRAINT `Passenger_ibfk_4` FOREIGN KEY (`RideStopTo`) REFERENCES `RideStop` (`Id`);');
    99     $this->InsertData();
    100   }
    101 
    102   function InsertData()
    103   {
    104     $this->Database->query('INSERT INTO `CarManufacturer` (`Id`, `Name`) VALUES '.
    105       '(NULL, "Škoda"), (NULL, "Nissan"), (NULL, "Mitsubishi"), (NULL, "Fiat"), '.
    106       '(NULL, "Dacia"), (NULL, "KIA"), (NULL, "Hyundai"), (NULL, "Ford"), '.
    107       '(NULL, "Opel"), (NULL, "Volkswagen"), (NULL, "Citroen"), (NULL, "Peugeot"), '.
    108       '(NULL, "Tesla"), (NULL, "Audi"), (NULL, "BMW"), (NULL, "Chevrolet"), '.
    109       '(NULL, "Suzuki"), (NULL, "Toyota"), (NULL, "Mercedes-benz"), (NULL, "Seat"),'.
    110       '(NULL, "Volvo"), (NULL, "Renault"), (NULL, "Mazda");');
    111     $this->Database->query('INSERT INTO `CarModel` (`Id`, `Name`, `Manufacturer`) VALUES '.
    112       '(NULL, "Rapid", 1), (NULL, "Octavia", 1), (NULL, "Fabia", 1), (NULL, "Felicia", 1), '.
    113       '(NULL, "Superb", 1);');
    114   }
    115 
    116   function DoUninstall()
    117   {
    118     $this->Database->query('DROP TABLE IF EXISTS `Passenger`');
    119     $this->Database->query('DROP TABLE IF EXISTS `RideStop`');
    120     $this->Database->query('DROP TABLE IF EXISTS `Ride`');
    121     $this->Database->query('DROP TABLE IF EXISTS `Car`');
    122     $this->Database->query('DROP TABLE IF EXISTS `CarModel`');
    123     $this->Database->query('DROP TABLE IF EXISTS `CarManufacturer`');
    124   }
    125 
    126   function DoUpgrade()
    127   {
    128 
    129   }
    130 
    131   function InsertSampleData()
    132   {
    133     $Cities = array('Praha', 'Brno', 'Ostrava', 'Olomouc', 'Liberec', 'Plzeň',
    134       'Humpolec', 'Zlín', 'Přerov', 'České Budějovice', 'Jihlava', 'Ústí nad Labem',
    135       'Hradec Králové', 'Pardubice', 'Mohelnice', 'Břeclav', 'Znojmo', 'Semily',
    136       'Tábor', 'Opava', 'Karviná', 'Pelhřimov', 'Osoblaha', 'Broumov', 'Jirkov',
    137       'Aš', 'Kutná Hora', 'Kolín', 'Strakonice', 'Tachov', 'Domažlice', 'Beroun',
    138       'Most', 'Chomutov', 'Mělník', 'Mladá Boleslav', 'Svitavy', 'Šumperk',
    139       'Litomyšl', 'Chrudim', 'Blansko', 'Hodonín', 'Rosice', 'Uherský Brod',
    140       'Vsetín', 'Nový Jičín', 'Prostějov', 'Vyškov', 'Mohelnice', 'Ústí nad Orlicí');
    141 
    142     for ($I = 0; $I < 100; $I++)
     11    foreach($_SESSION['Track'] as $Index => $TrackItem)
    14312    {
    144       $Stops = array_fill(0, random(5), '');
    145       for ($J = 0; $J < count($Stops); $J++)
    146         $Stops[$J] = $Cities[random(count($Cities))];
    147 
    148       $Ride = new Ride();
    149       $StartTime = time() - random(60*60*24*365);
    150       $StopCount = random(5);
    151       $Ride->Add($Stops, $StartTime, $SeatCount);
     13      $this->Database->insert('RideStop', array(
     14        'Ride' => $RideId,
     15        'Place' => $TrackItem['Destination'],
     16        'Time' => TimeToMysqlDateTime($TrackItem['Time']),
     17        'Price' => $TrackItem['Price'],
     18        'Sequence' => $Index,
     19        'PassengerCount' => 0,
     20      ));
    15221    }
    15322  }
    15423
    155   function ShowLogo()
     24  function GetTrack()
    15625  {
    157     $Output = '<img src="" width="200" height="50" alt="Logo"/> '.
    158       '<a href="'.$this->System->Link('/jizdy/').'">Hledat jízdu</a> nebo '.
    159       '<a href="'.$this->System->Link('/nova-jizda/').'">Nabídnout jízdu</a>';
    160     return $Output;
     26    return($Track);
    16127  }
    16228
    163   function UpdatePassengerCount($RideId)
     29  function Remove($Id)
    16430  {
    165     $DbResult = $this->System->Database->select('RideStop', 'COUNT(*) AS RideStopCount', 'Ride='.$RideId);
    166     $DbRow = $DbResult->fetch_assoc();
    167 
    168     $Stops = array_fill(0, $DbRow['RideStopCount'], 0);
    169     $this->System->Database->update('RideStop', 'Ride='.$RideId, array('PassengerCount' => 0));
    170     $DbResult = $this->System->Database->query('SELECT PassengerRideStopFrom.Sequence AS SequenceFrom, '.
    171         'PassengerRideStopTo.Sequence AS SequenceTo, Passenger.SeatCount FROM Passenger '.
    172         'LEFT JOIN RideStop AS PassengerRideStopFrom ON PassengerRideStopFrom.Id=Passenger.RideStopFrom '.
    173         'LEFT JOIN RideStop AS PassengerRideStopTo ON PassengerRideStopTo.Id=Passenger.RideStopTo '.
    174         'WHERE Passenger.Ride='.$RideId);
    175     while ($DbRow = $DbResult->fetch_assoc())
    176     {
    177       for ($I = $DbRow['SequenceFrom'] + 1; $I <= $DbRow['SequenceTo']; $I++)
    178       {
    179         $Stops[$I] = $Stops[$I] + $DbRow['SeatCount'];
    180       }
    181     }
    182     foreach ($Stops as $Index => $Stop)
    183     {
    184       $this->System->Database->update('RideStop', 'Ride='.$RideId.' AND Sequence='.$Index, array('PassengerCount' => $Stop));
    185     }
     31    $this->Database->delete('Passenger', 'Ride='.$Id);
     32    $this->Database->delete('RideStop', 'Ride='.$Id);
     33    $this->Database->delete('Ride', 'Id='.$Id);
    18634  }
    18735}
    188 
    189 class PageRideDetail extends Page
    190 {
    191   function ShowDetail()
    192   {
    193     $RideId = $_GET['id'];
    194     $DbResult = $this->Database->query('SELECT Ride.Details, Ride.Id, Ride.SeatCount, '.
    195       'RideStopFrom.Id AS RideStopFromId, RideStopTo.Id AS RideStopToId, '.
    196       '(SELECT Name FROM User WHERE User.Id=Ride.Driver) AS DriverName, '.
    197       'CONCAT(CarManufacturer.Name, " ", CarModel.Name) AS CarName FROM Ride '.
    198       'JOIN RideStop AS RideStopFrom ON (RideStopFrom.Ride = Ride.Id) AND (RideStopFrom.Place LIKE "%'.$_GET['from'].'%") '.
    199       'JOIN RideStop AS RideStopTo ON (RideStopTo.Ride = Ride.Id) AND (RideStopTo.Place LIKE "%'.$_GET['to'].'%") '.
    200         'LEFT JOIN Car ON Car.Id = Ride.Car '.
    201       'LEFT JOIN CarModel ON CarModel.Id = Car.Model '.
    202       'LEFT JOIN CarManufacturer ON CarManufacturer.Id = CarModel.Manufacturer '.
    203       'WHERE Ride.Id='.$RideId);
    204     $DbRow = $DbResult->fetch_assoc();
    205     $Track = array();
    206     $Stops = array();
    207     $DbResult2 = $this->Database->query('SELECT * FROM RideStop WHERE Ride='.$DbRow['Id'].' ORDER BY Sequence');
    208     while ($DbRow2 = $DbResult2->fetch_assoc())
    209     {
    210       $Track[] = $DbRow2;
    211       $Stops[] = $DbRow2['Place'];
    212     }
    213 
    214     $Output = '<h3>Detail jízdy</h3><table>'.
    215       '<tr><td>Trasa:</td><td>'.implode(' → ', $Stops).'</td></tr>'.
    216       '<tr><td>Odjezd z:</td><td>'.$Track[0]['Place'].'</td></tr>'.
    217       '<tr><td>Příjezd do:</td><td>'.$Track[count($Track) - 1]['Place'].'</td></tr>'.
    218       '<tr><td>Čas odjezdu:</td><td>'.$Track[0]['Time'].'</td></tr>'.
    219       '<tr><td>Podrobnosti:</td><td>'.$DbRow['Details'].'</td></tr>';
    220     $Output .= '</table>';
    221     $Output .= '<form action="?step=2" method="post">'.
    222       'Počet míst: <input type="text" name="seats" value="1"/>'.
    223       '<input type="hidden" name="id" value="'.$RideId.'"/>'.
    224       '<input type="hidden" name="fromid" value="'.$DbRow['RideStopFromId'].'"/>'.
    225       '<input type="hidden" name="toid" value="'.$DbRow['RideStopToId'].'"/>'.
    226       '<input type="submit" value="Rezervovat"/>'.
    227       '</form>';
    228 
    229     $Output .= '<h3>Cestující</h3>'.
    230       '<table class="WideTable">';
    231 
    232     // Load passengers table
    233     $Table = array_fill(0, count($Track), array_fill(0, $DbRow['SeatCount'], NULL));
    234     $Passengers = array();
    235     $DbResult = $this->System->Database->query('SELECT User.Name AS UserName, PassengerRideStopFrom.Sequence AS SequenceFrom, '.
    236       'PassengerRideStopTo.Sequence AS SequenceTo FROM Passenger '.
    237       'LEFT JOIN RideStop AS PassengerRideStopFrom ON PassengerRideStopFrom.Id=Passenger.RideStopFrom '.
    238       'LEFT JOIN RideStop AS PassengerRideStopTo ON PassengerRideStopTo.Id=Passenger.RideStopTo '.
    239       'LEFT JOIN User ON User.Id=Passenger.User '.
    240       'WHERE Passenger.Ride='.$RideId);
    241     while ($Passenger = $DbResult->fetch_assoc())
    242     {
    243       $Passengers[] = $Passenger;
    244       $Seat = 0;
    245       do {
    246         $Check = true;
    247         for ($I = $Passenger['SequenceFrom']; $I < $Passenger['SequenceTo']; $I++)
    248         {
    249           if ($Table[$I][$Seat] != 0) $Check = false;
    250         }
    251         if (!$Check) $Seat++;
    252       } while (!$Check);
    253       for ($I = $Passenger['SequenceFrom']; $I < $Passenger['SequenceTo']; $I++)
    254         $Table[$I][$Seat] = count($Passengers) - 1;
    255     }
    256 
    257     $Output .= '<tr><th>Zastávka / Čas</th><th style="width: 80px">Řidič</th>';
    258     for ($I = 0; $I < $DbRow['SeatCount']; $I++)
    259       $Output .= '<th style="width: 80px">Cestující</th>';
    260     $Output .= '</tr>';
    261     foreach ($Track as $Index => $TrackItem)
    262     {
    263       $Output .= '<tr><td style="height: 100px">'.$TrackItem['Place'].'<br/>'.$TrackItem['Time'].'</td>';
    264       if ($Index == 0) $Output .= '<td rowspan="'.count($Track).'">Řidič</td>';
    265       for ($I = 0; $I < $DbRow['SeatCount']; $I++)
    266       {
    267         if (!is_null($Table[$Index][$I])) {
    268           $Passenger = $Passengers[$Table[$Index][$I]];
    269           if ($Index == $Passenger['SequenceFrom'])
    270           $Output .= '<td rowspan="'.($Passenger['SequenceTo'] - $Passenger['SequenceFrom']).'">'.$Passenger['UserName'].'</td>';
    271         } else {
    272           // Show unused space
    273           if (($Index == 0) or ((($Index - 1) >= 0) and (!is_null($Table[$Index - 1][$I]))))
    274           {
    275             $J = 1;
    276             while ((($Index + $J) < count($Table)) and is_null($Table[$Index + $J][$I])) $J++;
    277             $Output .= '<td rowspan="'.$J.'">&nbsp;</td>';
    278           }
    279         }
    280       }
    281       $Output .= '</tr>';
    282     }
    283     $Output .= '</table>';
    284 
    285 
    286     return $Output;
    287   }
    288 
    289   function Show()
    290   {
    291     if (!array_key_exists('step', $_GET)) $Output = $this->ShowDetail();
    292     else {
    293       if ($_GET['step'] == 2) {
    294         $this->System->Database->insert('Passenger', array(
    295           'Ride' => $_POST['id'],
    296           'User' => $this->System->User->User['Id'],
    297           'SeatCount' => $_POST['seats'],
    298           'RideStopFrom' => $_POST['fromid'],
    299           'RideStopTo' => $_POST['toid'],
    300         ));
    301         $this->System->ModuleManager->Modules['Ride']->UpdatePassengerCount($_POST['id']);
    302         $Output = 'Rezervace dokončena';
    303       }
    304     }
    305     return $Output;
    306   }
    307 }
    308 
    309 class PageMain extends Page
    310 {
    311   function Show()
    312   {
    313     if (array_key_exists('from', $_GET)) $PlaceFrom = $_GET['from'];
    314       else $PlaceFrom = '';
    315     if (array_key_exists('to', $_GET)) $PlaceTo = $_GET['to'];
    316       else $PlaceTo = '';
    317     if (array_key_exists('time', $_GET)) $Time = $_GET['time'];
    318       else $Time = '';
    319     $Output = '<table style="width: 100%; margin-top: 100px; margin-bottom: 100px;"><tr><td style="text-align: center">'.
    320       '<div><h3>Vyhledat jízdu</h3>'.
    321       '<form style="display: inline;" method="get" action="'.$this->System->Link('/jizdy/').'">'.
    322       'Odkud: <input type="text" name="from" value="'.$PlaceFrom.'"/> '.
    323       'Kam: <input type="text" name="to" value="'.$PlaceTo.'"/> '.
    324       'Čas: <input type="text" name="time" value="'.$Time.'"/> '.
    325       '<input type="submit" value="Hledat"/>'.
    326       '</form></div></td></tr></table>';
    327     return $Output;
    328   }
    329 }
    330 
    331 class PageRideList extends Page
    332 {
    333   function Show()
    334   {
    335     if (array_key_exists('from', $_GET)) $PlaceFrom = $_GET['from'];
    336       else $PlaceFrom = '';
    337     if (array_key_exists('to', $_GET)) $PlaceTo = $_GET['to'];
    338       else $PlaceTo = '';
    339     if (array_key_exists('time', $_GET)) $Time = $_GET['time'];
    340       else $Time = '';
    341     $Output = '<table style="width: 100%"><tr><td style="text-align: center;">'.
    342       '<form style="display: inline;" method="get" action="'.$this->System->Link('/').'">'.
    343       'Odkud: <input type="text" name="from" value="'.$PlaceFrom.'"/> '.
    344       'Kam: <input type="text" name="to" value="'.$PlaceTo.'"/> '.
    345       'Čas: <input type="text" name="time" value="'.$Time.'"/> '.
    346       '<input type="submit" value="Hledat"/>'.
    347       '</form></td></tr></table>';
    348     if (array_key_exists('from', $_GET) and array_key_exists('to', $_GET)) {
    349       $Output .= $this->ShowRides($_GET['from'], $_GET['to']);
    350     } else $Output .= $this->ShowRides('', '');
    351     return $Output;
    352   }
    353 
    354   function ShowRides($PlaceFrom, $PlaceTo)
    355   {
    356     $Output = '<h3>Jízdy:</h3>';
    357     $Output .= '<table>';
    358     $Query = 'SELECT Ride.Id, Ride.SeatCount, User.Name AS DriverName, User.BirthDate, '.
    359       'CONCAT(CarManufacturer.Name, " ", CarModel.Name) AS CarName ';
    360     if ((trim($PlaceFrom) != '') and (trim($PlaceTo != '')))
    361       $Query .= ', (SELECT COALESCE(MAX(RideStop.PassengerCount), 0) FROM RideStop '.
    362       'WHERE (RideStop.Ride=Ride.Id) AND (RideStop.Sequence > RideStopFrom.Sequence) AND '.
    363       '(RideStop.Sequence <= RideStopTo.Sequence)) AS PassengerCount ';
    364     else
    365       $Query .= ', (SELECT COALESCE(MAX(RideStop.PassengerCount), 0) FROM RideStop '.
    366        'WHERE (RideStop.Ride=Ride.Id)) AS PassengerCount ';
    367 
    368     $Query .= 'FROM Ride '.
    369       'LEFT JOIN Car ON Car.Id = Ride.Car '.
    370       'LEFT JOIN CarModel ON CarModel.Id = Car.Model '.
    371       'LEFT JOIN CarManufacturer ON CarManufacturer.Id = CarModel.Manufacturer '.
    372       'LEFT JOIN User ON User.Id=Ride.Driver ';
    373     if ((trim($PlaceFrom) != '') and (trim($PlaceTo != '')))
    374       $Query .= 'JOIN RideStop AS RideStopFrom ON (RideStopFrom.Ride = Ride.Id) AND (RideStopFrom.Place LIKE "%'.$PlaceFrom.'%") '.
    375         'JOIN RideStop AS RideStopTo ON (RideStopTo.Ride = Ride.Id) AND (RideStopTo.Place LIKE "%'.$PlaceTo.'%") '.
    376         'WHERE (RideStopFrom.Sequence < RideStopTo.Sequence)';
    377     $Query .= 'LIMIT 10';
    378     $DbResult = $this->Database->query($Query);
    379     while ($DbRow = $DbResult->fetch_assoc())
    380     {
    381       $Stops = array();
    382       $Price = 0;
    383       $DbResult2 = $this->Database->query('SELECT * FROM RideStop WHERE Ride='.$DbRow['Id'].' ORDER BY Sequence');
    384       while ($DbRow2 = $DbResult2->fetch_assoc())
    385       {
    386         $Stops[] = $DbRow2['Place'];
    387         $Price += $DbRow2['Price'];
    388       }
    389 
    390       if ($DbRow['BirthDate'] != '') $Age = ' ('.round((time() - MysqlDateToTime($DbRow['BirthDate'])) / (3600*24*364.25)).' let)';
    391         else $Age = '';
    392       $Output .= '<tr><td>Řidič:</td><td>'.$DbRow['DriverName'].$Age.'</td>'.
    393         '<td colspan="4"><a href="'.$this->System->Link('/jizda/?id='.$DbRow['Id'].'&from='.$PlaceFrom.'&to='.$PlaceTo).'">Ukázat</a></td></tr>'.
    394         '<tr><td>Trasa:</td><td>'.implode(' → ', $Stops).'</td></tr>'.
    395         '<tr><td>Cena:</td><td>'.$Price.' Kč</td></tr>'.
    396         '<tr><td>Volných míst:</td><td>'.($DbRow['SeatCount'] - $DbRow['PassengerCount']).'</td></tr>'.
    397         '<tr><td>Auto:</td><td>'.$DbRow['CarName'].'</td></tr>'.
    398         '<tr><td>&nbsp;</td></tr>';
    399     }
    400     $Output .= '</table>';
    401 
    402     return $Output;
    403   }
    404 }
    405 
    406 function time_elapsed($secs)
    407 {
    408   $bit = array(
    409     'y' => $secs / 31556926 % 12,
    410     'w' => $secs / 604800 % 52,
    411     'd' => $secs / 86400 % 7,
    412     'h' => $secs / 3600 % 24,
    413     'm' => $secs / 60 % 60,
    414     's' => $secs % 60
    415   );
    416 
    417   $ret = array();
    418   foreach ($bit as $k => $v)
    419     if ($v > 0)$ret[] = $v . $k;
    420 
    421   return join(' ', $ret);
    422 }
    423 
    424 class PageNewRide extends Page
    425 {
    426   function Show()
    427   {
    428     if (!array_key_exists('step', $_GET))
    429     {
    430       $Output = '<script>
    431 function insertAfter(referenceNode, newNode)
    432 {
    433     referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
    434 }
    435 
    436 function add_before(name)
    437 {
    438     var objTo = document.getElementById(name)
    439     var divtest = document.createElement("div");
    440     divtest.innerHTML = \'<tr><td>Zastávka:</td><td><input type="text" name="ridestop1"/></td></tr>\';
    441 
    442     objTo.parentNode.insertBefore(divtest, objTo.childNodes[0])
    443 }
    444 
    445 function add_after(name)
    446 {
    447     var objTo = document.getElementById(name)
    448     var divtest = document.createElement("div");
    449     divtest.innerHTML = \'<tr><td>Zastávka:</td><td><input type="text" name="ridestop1"/></td></tr>\';
    450 
    451     insertAfter(objTo, divtest)
    452 }
    453           </script>'.
    454         '<form method="get" action="'.$this->System->Link('/nova-jizda/').'">'.
    455         '<table>'.
    456         '<tr><td colspan="2"><strong>Trasa:</strong></td></tr>'.
    457 
    458         '<tr><td>Počátek:</td><td><input type="text" name="from"/></td></tr>'.
    459         '<tr><td>Zastávka:</td><td><input type="text" name="middle"/></td></tr>'.
    460         '<tr><td>Konec:</td><td><input type="text" name="to"/></td></tr>'.
    461 
    462         '<tr><td colspan="2"><br><strong>Parametry jízdy:</strong></td></tr>'.
    463         '<tr><td>Čas odjezdu:</td><td><input type="text" name="time-from"/></td></tr>'.
    464         '<tr><td>Počet míst pro cestující:</td><td><input type="text" name="seats"/></td></tr>'.
    465         '</table>'.
    466         '<input type="hidden" name="step" value="2"/><br/>'.
    467         '<input type="submit" value="Pokračovat"/>'.
    468         '</form>';
    469     } else {
    470       if ($_GET['step'] == '2') {
    471         $TimeFrom = strtotime($_GET['time-from']);
    472         $Output = '<table style="width: 100%"><tr><td>';
    473         $Output .= '<form method="post" action="'.$this->System->Link('/nova-jizda/?step=3').'">'.
    474         '<input type="hidden" name="from" value="'.$_GET['from'].'"/><br/>'.
    475         '<input type="hidden" name="to" value="'.$_GET['to'].'"/><br/>'.
    476         '<input type="hidden" name="time-from" value="'.$_GET['time-from'].'"/><br/>'.
    477         '<input type="hidden" name="seats" value="'.$_GET['seats'].'"/><br/>'.
    478         '<input type="hidden" name="step" value="3"/><br/>'.
    479         'Popis:<br/><textarea name="desc"></textarea><br/>'.
    480         'Auto: <select name="car">';
    481         $DbResult = $this->Database->query('SELECT Car.Id, CONCAT(CarManufacturer.Name, " ", CarModel.Name) AS ModelName FROM Car '.
    482           'LEFT JOIN CarModel ON CarModel.Id=Car.Model '.
    483           'LEFT JOIN CarManufacturer ON CarManufacturer.Id = CarModel.Manufacturer '.
    484           'WHERE Car.Owner='.$this->System->User->User['Id']);
    485         while ($DbRow = $DbResult->fetch_assoc())
    486         {
    487           $Output .= '<option value="'.$DbRow['Id'].'"/>'.$DbRow['ModelName'].'</option>';
    488         }
    489         $Output .= '</select>'.
    490           '<input type="submit" value="Vytvořit"/>'.
    491           '</form>';
    492         $Map = new MapGoogle($this->System);
    493         $Map->Key = 'AIzaSyDQ-tOjP9mO8ZpSEuBc5_oCRg30rpQZiBI';
    494         $Map->Width = 400;
    495         $Map->Height = 300;
    496         $Map->Path = array($_GET['from'], $_GET['to']);
    497         //$Map->Path = array('Praha', 'Brno', 'Zlín', 'Vsetín');
    498         $Track = $Map->Route();
    499         $Output .= '<h3>Trasa</h3>';
    500         $PricePerKm = 0.8;
    501         $Output .= '<table class="WideTable"><tr><th>Úsek</th><th>Odjezd</th><th>Trvání</th><th>Vzdálenost [km]</th><th>Cena [Kč]</th></tr>';
    502         foreach ($Track as $Index => $TrackItem)
    503         {
    504           if ($Index > 0)
    505           {
    506             $Track[$Index]['Time'] = $TimeFrom + $Track[$Index - 1]['Duration'];
    507             $RelDistance = $TrackItem['Distance'] - $Track[$Index - 1]['Distance'];
    508             $RelDuration = $TrackItem['Duration'] - $Track[$Index - 1]['Duration'];
    509             $Track[$Index]['Price'] = round($PricePerKm * $RelDistance / 1000);
    510             $Output .= '<tr><td>'.$Map->Path[$Index - 1].' → '.$TrackItem['Destination'].'</td>'.
    511               '<td>'.TimeToMysqlDateTime($Track[$Index]['Time']).'</td>'.
    512               '<td>'.time_elapsed($RelDuration).'</td>'.
    513               '<td>'.round($RelDistance / 1000).'</td>'.
    514               '<td>'.$Track[$Index]['Price'].'</td></tr>';
    515 
    516           } else {
    517             $Track[$Index]['Time'] = $TimeFrom;
    518             $Track[$Index]['Price'] = 0;
    519           }
    520         }
    521         $Output .= '<tr><td>'.$Map->Path[0].' → '.$Map->Path[count($Map->Path) - 1].'</td>'.
    522           '<td>'.TimeToMysqlDateTime($Track[0]['Time']).'</td>'.
    523           '<td>'.time_elapsed($Track[count($Track) - 1]['Duration']).'</td>'.
    524           '<td>'.round($Track[count($Track) - 1]['Distance'] / 1000).'</td>'.
    525           '<td>'.round($PricePerKm * $Track[count($Track) - 1]['Distance'] / 1000).'</td></tr>';
    526         $Output .= '</table><br/>';
    527         $Output .= 'Vzdálenost: '.round($Track[count($Track) - 1]['Distance'] / 1000, 1).' km<br/>';
    528         $Output .= 'Délka jízdy: '.time_elapsed($Track[count($Track) - 1]['Duration']).'<br/>';
    529         $Output .= '</td><td>';
    530         $Map->Key = 'AIzaSyDwKYuEdkXAl9Y2RyDLf5m03n-ItHaqdKs';
    531         $Output .= $Map->Show();
    532         $Output .= '</td></tr></table>';
    533         $_SESSION['Track'] = $Track;
    534         $_SESSION['SeatCount'] = $_GET['seats'];
    535       } else
    536       if ($_GET['step'] == '3') {
    537         $this->Database->insert('Ride', array(
    538           'Driver' => $this->System->User->User['Id'],
    539           'SeatCount' => $_POST['seats'],
    540           'Car' => $_POST['car'],
    541           'Details' => $_POST['desc'],
    542         ));
    543         $RideId = $this->Database->insert_id;
    544         foreach ($_SESSION['Track'] as $Index => $TrackItem)
    545         {
    546           $this->Database->insert('RideStop', array(
    547             'Ride' => $RideId,
    548             'Place' => $TrackItem['Destination'],
    549             'Time' => TimeToMysqlDateTime($TrackItem['Time']),
    550             'Price' => $TrackItem['Price'],
    551             'Sequence' => $Index,
    552             'PassengerCount' => 0,
    553           ));
    554         }
    555         $Output = 'Nová jízda přidána.';
    556       }
    557     }
    558     return $Output;
    559   }
    560 }
    561 
Note: See TracChangeset for help on using the changeset viewer.