Changeset 3


Ignore:
Timestamp:
Sep 8, 2016, 11:03:23 PM (8 years ago)
Author:
chronos
Message:
  • Added: Calculare available seats for passenger registration.
  • Added: Show table of passengers for a ride.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/.htaccess

    r1 r3  
    77RewriteCond  %{REQUEST_FILENAME}  !-f
    88RewriteCond  %{REQUEST_FILENAME}  !-d
    9 RewriteRule   ^(.*)$ %{CONTEXT_PREFIX}/index.php?$1
     9RewriteRule   ^(.*)$ %{CONTEXT_PREFIX}index.php?$1
    1010
    1111#ErrorDocument 403 "index.php?page=403"
  • trunk/Application/Core.php

    r1 r3  
    3030    $this->ShowPage = true;
    3131    $this->BaseURL = $_SERVER['SCRIPT_NAME'];
    32     if(substr($this->BaseURL, -10, 10) == '/index.php')
    33       $this->BaseURL = substr($this->BaseURL, 0, -10);
     32    $BaseScriptName = '/index.php';
     33    if(substr($this->BaseURL, -strlen($BaseScriptName), strlen($BaseScriptName)) == $BaseScriptName)
     34      $this->BaseURL = substr($this->BaseURL, 0, -strlen($BaseScriptName));
    3435    $this->FormManager = new FormManager($this->Database);
    3536  }
     
    6263
    6364    $this->RegisterPageBar('Top');
     65    $this->RegisterPageBar('TopLeft');
    6466
    6567    // Register and start existing modules
     
    164166  {
    165167    Header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
    166     return('<h3 align="center">'.T('Required page not found'));
     168    return('<h3 align="center">'.T('Required page not found').'</h3>');
    167169  }
    168170}
  • trunk/Application/View.php

    r1 r3  
    3535  {
    3636    $Output = '<div>';
     37    $Output .= '<div class="MenuItem">';
     38    $Bar = '';
     39    foreach($this->System->Bars['TopLeft'] as $BarItem)
     40      $Bar .= call_user_func($BarItem);
     41      if(trim($Bar) != '') $Output .= $Bar;
     42      else $Output .= '&nbsp;';
     43    $Output .= '</div><div class="MenuItem2">';
    3744    $Bar = '';
    3845    foreach($this->System->Bars['Top'] as $BarItem)
     
    4047      if(trim($Bar) != '') $Output .= $Bar;
    4148      else $Output .= '&nbsp;';
    42     $Output .= '</div>';
     49    $Output .= '</div></div>';
    4350    return($Output);
    4451  }
  • trunk/Modules/Ride/Ride.php

    r1 r3  
    1919    $this->System->RegisterPage('nova-jizda', 'PageNewRide');
    2020    $this->System->RegisterPage('jizda', 'PageRideDetail');
     21    $this->System->RegisterPageBarItem('TopLeft', 'Logo', array($this, 'ShowLogo'));
    2122  }
    2223
     
    6970  `Sequence` int(11) NOT NULL,
    7071  `Price` int(11) NOT NULL,
     72  `PassengerCount` int(11) NOT NULL,
    7173  PRIMARY KEY (`Id`),
    7274  KEY `Ride` (`Ride`)
     
    123125
    124126  }
     127
     128  function ShowLogo()
     129  {
     130    $Output = '<img src="" width="200" height="50" alt="Logo"/> '.
     131      '<a href="'.$this->System->Link('/hledat/').'">Hledat jízdu</a> nebo '.
     132      '<a href="'.$this->System->Link('/nabídnout/').'">Nabídnout jízdu</a>';
     133    return($Output);
     134  }
     135
     136  function UpdatePassengerCount($RideId)
     137  {
     138    $DbResult = $this->System->Database->select('RideStop', 'COUNT(*) AS RideStopCount', 'Ride='.$RideId);
     139    $DbRow = $DbResult->fetch_assoc();
     140
     141    $Stops = array_fill(0, $DbRow['RideStopCount'], 0);
     142    $this->System->Database->update('RideStop', 'Ride='.$RideId, array('PassengerCount' => 0));
     143    $DbResult = $this->System->Database->query('SELECT PassengerRideStopFrom.Sequence AS SequenceFrom, '.
     144        'PassengerRideStopTo.Sequence AS SequenceTo, Passenger.SeatCount FROM Passenger '.
     145        'LEFT JOIN RideStop AS PassengerRideStopFrom ON PassengerRideStopFrom.Id=Passenger.RideStopFrom '.
     146        'LEFT JOIN RideStop AS PassengerRideStopTo ON PassengerRideStopTo.Id=Passenger.RideStopTo '.
     147        'WHERE Passenger.Ride='.$RideId);
     148    while($DbRow = $DbResult->fetch_assoc())
     149    {
     150      for($I = $DbRow['SequenceFrom'] + 1; $I <= $DbRow['SequenceTo']; $I++)
     151      {
     152        $Stops[$I] = $Stops[$I] + $DbRow['SeatCount'];
     153      }
     154    }
     155    foreach($Stops as $Index => $Stop)
     156    {
     157      $this->System->Database->update('RideStop', 'Ride='.$RideId.' AND Sequence='.$Index, array('PassengerCount' => $Stop));
     158    }
     159  }
    125160}
     161
    126162class PageRideDetail extends Page
    127163{
    128164  function ShowDetail()
    129165  {
     166    $RideId = $_GET['id'];
    130167    $DbResult = $this->Database->query('SELECT Ride.Details, Ride.Id, Ride.SeatCount, '.
    131168      'RideStopFrom.Id AS RideStopFromId, RideStopTo.Id AS RideStopToId, '.
     
    137174      'LEFT JOIN CarModel ON CarModel.Id = Car.Model '.
    138175      'LEFT JOIN CarManufacturer ON CarManufacturer.Id = CarModel.Manufacturer '.
    139       'WHERE Ride.Id='.$_GET['id']);
     176      'WHERE Ride.Id='.$RideId);
    140177    $DbRow = $DbResult->fetch_assoc();
    141178    $Track = array();
     
    157194    $Output .= '<form action="?step=2" method="post">'.
    158195      'Počet míst: <input type="text" name="seats" value="1"/>'.
    159       '<input type="hidden" name="id" value="'.$_GET['id'].'"/>'.
     196      '<input type="hidden" name="id" value="'.$RideId.'"/>'.
    160197      '<input type="hidden" name="fromid" value="'.$DbRow['RideStopFromId'].'"/>'.
    161198      '<input type="hidden" name="toid" value="'.$DbRow['RideStopToId'].'"/>'.
     
    166203      '<table class="WideTable">';
    167204
    168 /*    $DbResult2 = $this->Database->query('SELECT * FROM RideStop WHERE Ride='.$DbRow['Id'].' ORDER BY Sequence');
    169     while($DbRow2 = $DbResult2->fetch_assoc())
    170     {
    171       $Track[] = $DbRow2;
    172       $Stops[] = $DbRow2['Place'];
    173     }
    174   */
     205    // Load passengers table
     206    $Table = array_fill(0, count($Track), array_fill(0, $DbRow['SeatCount'], NULL));
     207    $Passengers = array();
     208    $DbResult = $this->System->Database->query('SELECT User.Name AS UserName, PassengerRideStopFrom.Sequence AS SequenceFrom, '.
     209      'PassengerRideStopTo.Sequence AS SequenceTo FROM Passenger '.
     210      'LEFT JOIN RideStop AS PassengerRideStopFrom ON PassengerRideStopFrom.Id=Passenger.RideStopFrom '.
     211      'LEFT JOIN RideStop AS PassengerRideStopTo ON PassengerRideStopTo.Id=Passenger.RideStopTo '.
     212      'LEFT JOIN User ON User.Id=Passenger.User '.
     213      'WHERE Passenger.Ride='.$RideId);
     214    while($Passenger = $DbResult->fetch_assoc())
     215    {
     216      $Passengers[] = $Passenger;
     217      $Seat = 0;
     218      do {
     219        $Check = true;
     220        for($I = $Passenger['SequenceFrom']; $I < $Passenger['SequenceTo']; $I++)
     221        {
     222          if($Table[$I][$Seat] != 0) $Check = false;
     223        }
     224        if (!$Check) $Seat++;
     225      } while(!$Check);
     226      for($I = $Passenger['SequenceFrom']; $I < $Passenger['SequenceTo']; $I++)
     227        $Table[$I][$Seat] = count($Passengers) - 1;
     228    }
     229
     230    $Output .= '<tr><th>Zastávka / Čas</th><th>Řidič</th>';
     231    for ($I = 0; $I < $DbRow['SeatCount']; $I++)
     232      $Output .= '<th>Cestující</th>';
     233    $Output .= '</tr>';
    175234    foreach($Track as $Index => $TrackItem)
    176235    {
    177       $Output .= '<tr><td>'.$TrackItem['Place'].'<br/>'.$TrackItem['Time'].'</td>';
     236      $Output .= '<tr><td style="height: 100px">'.$TrackItem['Place'].'<br/>'.$TrackItem['Time'].'</td>';
     237      if($Index == 0) $Output .= '<td rowspan="'.count($Track).'">Řidič</td>';
    178238      for ($I = 0; $I < $DbRow['SeatCount']; $I++)
    179239      {
    180         $Output .= '<td>'.'s'.'</td>';
     240        if (!is_null($Table[$Index][$I])) {
     241          $Passenger = $Passengers[$Table[$Index][$I]];
     242          if ($Index == $Passenger['SequenceFrom'])
     243          $Output .= '<td rowspan="'.($Passenger['SequenceTo'] - $Passenger['SequenceFrom']).'">'.$Passenger['UserName'].'</td>';
     244        } else {
     245          // Show unused space
     246          if(($Index == 0) or ((($Index - 1) >= 0) and (!is_null($Table[$Index - 1][$I]))))
     247          {
     248            $J = 1;
     249            while((($Index + $J) < count($Table)) and is_null($Table[$Index + $J][$I])) $J++;
     250            $Output .= '<td rowspan="'.$J.'">&nbsp;</td>';
     251          }
     252        }
    181253      }
    182254      $Output .= '</tr>';
     
    200272          'RideStopTo' => $_POST['toid'],
    201273        ));
     274        $this->System->ModuleManager->Modules['Ride']->UpdatePassengerCount($_POST['id']);
    202275        $Output = 'Rezervace dokončena';
    203276      }
     
    223296    if (array_key_exists('from', $_GET) and array_key_exists('to', $_GET)) {
    224297      $Output .= $this->ShowRides($_GET['from'], $_GET['to']);
    225     }
     298    } else $Output .= $this->ShowRides('', '');
    226299    return($Output);
    227300  }
     
    231304    $Output = '<h3>Jízdy:</h3>';
    232305    $Output .= '<table>';
    233     if((trim($PlaceFrom) == '') or (trim($PlaceTo == ''))) return "Zadejte odkud kam.";
    234     $DbResult = $this->Database->query('SELECT Ride.Id, Ride.SeatCount, (SELECT Name FROM User WHERE User.Id=Ride.Driver) AS DriverName, '.
    235       'CONCAT(CarManufacturer.Name, " ", CarModel.Name) AS CarName FROM Ride '.
    236       'JOIN RideStop AS RideStopFrom ON (RideStopFrom.Ride = Ride.Id) AND (RideStopFrom.Place LIKE "%'.$PlaceFrom.'%") '.
    237       'JOIN RideStop AS RideStopTo ON (RideStopTo.Ride = Ride.Id) AND (RideStopTo.Place LIKE "%'.$PlaceTo.'%") '.
     306    $Query = 'SELECT Ride.Id, Ride.SeatCount, (SELECT Name FROM User WHERE User.Id=Ride.Driver) AS DriverName, '.
     307      'CONCAT(CarManufacturer.Name, " ", CarModel.Name) AS CarName ';
     308    if((trim($PlaceFrom) != '') and (trim($PlaceTo != '')))
     309      $Query .= ', (SELECT COALESCE(MAX(RideStop.PassengerCount), 0) FROM RideStop '.
     310      'WHERE (RideStop.Ride=Ride.Id) AND (RideStop.Sequence > RideStopFrom.Sequence) AND '.
     311      '(RideStop.Sequence <= RideStopTo.Sequence)) AS PassengerCount ';
     312    else
     313      $Query .= ', (SELECT COALESCE(MAX(RideStop.PassengerCount), 0) FROM RideStop '.
     314       'WHERE (RideStop.Ride=Ride.Id)) AS PassengerCount ';
     315
     316    $Query .= 'FROM Ride '.
    238317      'LEFT JOIN Car ON Car.Id = Ride.Car '.
    239318      'LEFT JOIN CarModel ON CarModel.Id = Car.Model '.
    240       'LEFT JOIN CarManufacturer ON CarManufacturer.Id = CarModel.Manufacturer '.
    241       'WHERE RideStopFrom.Sequence < RideStopTo.Sequence '.
    242       'LIMIT 10');
     319      'LEFT JOIN CarManufacturer ON CarManufacturer.Id = CarModel.Manufacturer ';
     320    if((trim($PlaceFrom) != '') and (trim($PlaceTo != '')))
     321      $Query .= 'JOIN RideStop AS RideStopFrom ON (RideStopFrom.Ride = Ride.Id) AND (RideStopFrom.Place LIKE "%'.$PlaceFrom.'%") '.
     322        'JOIN RideStop AS RideStopTo ON (RideStopTo.Ride = Ride.Id) AND (RideStopTo.Place LIKE "%'.$PlaceTo.'%") '.
     323        'WHERE (RideStopFrom.Sequence < RideStopTo.Sequence)';
     324    $Query .= 'LIMIT 10';
     325    $DbResult = $this->Database->query($Query);
    243326    while($DbRow = $DbResult->fetch_assoc())
    244327    {
     
    251334        $Price += $DbRow2['Price'];
    252335      }
    253       $AvailSpace = $DbRow['SeatCount'];
    254       $DbResult2 = $this->Database->query('SELECT * FROM Passenger WHERE Ride='.$DbRow['Id']);
    255       while($DbRow2 = $DbResult2->fetch_assoc())
    256       {
    257         $AvailSpace -= $DbRow2['SeatCount'];
    258       }
    259336
    260337      $Output .= '<tr><td>Řidič:</td><td>'.$DbRow['DriverName'].'</td>'.
    261         '<td colspan="4"><a href="'.$this->System->Link('/jizda/?id='.$DbRow['Id'].'&from='.$_GET['from'].'&to='.$_GET['to']).'">Ukázat</a></td></tr>'.
     338        '<td colspan="4"><a href="'.$this->System->Link('/jizda/?id='.$DbRow['Id'].'&from='.$PlaceFrom.'&to='.$PlaceTo).'">Ukázat</a></td></tr>'.
    262339        '<tr><td>Trasa:</td><td>'.implode(' → ', $Stops).'</td></tr>'.
    263340        '<tr><td>Cena:</td><td>'.$Price.' Kč</td></tr>'.
    264         '<tr><td>Volných míst:</td><td>'.$AvailSpace.'</td></tr>'.
    265         '<tr><td>Auto:</td><td>'.$DbRow['CarName'].'</td></tr>';
     341        '<tr><td>Volných míst:</td><td>'.($DbRow['SeatCount'] - $DbRow['PassengerCount']).'</td></tr>'.
     342        '<tr><td>Auto:</td><td>'.$DbRow['CarName'].'</td></tr>'.
     343        '<tr><td>&nbsp;</td></tr>';
    266344    }
    267345    $Output .= '</table';
     
    331409        $Map->Width = 400;
    332410        $Map->Height = 300;
    333         $Map->Path = array($_GET['from'], 'Pardubice', $_GET['to']);
    334         $Map->Path = array('Praha', 'Brno', 'Zlín', 'Vsetín');
     411        $Map->Path = array($_GET['from'], $_GET['to']);
     412        //$Map->Path = array('Praha', 'Brno', 'Zlín', 'Vsetín');
    335413        $Track = $Map->Route();
    336414        $Output .= '<h3>Trasa</h3>';
Note: See TracChangeset for help on using the changeset viewer.