Name = 'Ride'; $this->Version = '1.0'; $this->Creator = 'Chronos'; $this->License = 'GNU/GPL'; $this->Description = 'Ride management'; $this->Dependencies = array('User', 'Map'); } function DoStart(): void { $this->System->RegisterPage([''], 'PageMain'); $this->System->RegisterPage(['nova-jizda'], 'PageNewRide'); $this->System->RegisterPage(['jizda'], 'PageRideDetail'); $this->System->RegisterPage(['jizdy'], 'PageRideList'); Core::Cast($this->System)->RegisterPageBarItem('TopLeft', 'Logo', array($this, 'ShowLogo')); } function DoInstall(): void { $this->Database->query('CREATE TABLE `CarManufacturer` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(255) NOT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'); $this->Database->query('CREATE TABLE `CarModel` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(255) NOT NULL, `Manufacturer` int(11) NOT NULL, PRIMARY KEY (`Id`), KEY `Manufacturer` (`Manufacturer`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'); $this->Database->query('ALTER TABLE `CarModel` ADD CONSTRAINT `CarModel_ibfk_1` FOREIGN KEY (`Manufacturer`) REFERENCES `CarManufacturer` (`Id`);'); $this->Database->query('CREATE TABLE `Car` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Owner` int(11) NOT NULL, `Photo` int(11) DEFAULT NULL, `Model` int(11) NOT NULL, PRIMARY KEY (`Id`), KEY `Owner` (`Owner`), KEY `Model` (`Model`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'); $this->Database->query('ALTER TABLE `Car` ADD CONSTRAINT `Car_ibfk_1` FOREIGN KEY (`Owner`) REFERENCES `User` (`Id`), ADD CONSTRAINT `Car_ibfk_2` FOREIGN KEY (`Model`) REFERENCES `CarModel` (`Id`);'); $this->Database->query('CREATE TABLE `Ride` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Driver` int(11) NOT NULL, `SeatCount` int(11) NOT NULL, `Car` int(11) NOT NULL, `Details` text NOT NULL, PRIMARY KEY (`Id`), KEY `Driver` (`Driver`), KEY `Car` (`Car`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'); $this->Database->query('ALTER TABLE `Ride` ADD CONSTRAINT `Ride_ibfk_1` FOREIGN KEY (`Driver`) REFERENCES `User` (`Id`), ADD CONSTRAINT `Ride_ibfk_2` FOREIGN KEY (`Car`) REFERENCES `Car` (`Id`);'); $this->Database->query('CREATE TABLE `RideStop` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Ride` int(11) NOT NULL, `Time` datetime NOT NULL, `Place` varchar(255) NOT NULL, `Sequence` int(11) NOT NULL, `Price` int(11) NOT NULL, `PassengerCount` int(11) NOT NULL, PRIMARY KEY (`Id`), KEY `Ride` (`Ride`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'); $this->Database->query('ALTER TABLE `RideStop` ADD CONSTRAINT `RideStop_ibfk_1` FOREIGN KEY (`Ride`) REFERENCES `Ride` (`Id`);'); $this->Database->query('CREATE TABLE `Passenger` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Ride` int(11) NOT NULL, `User` int(11) NOT NULL, `SeatCount` int(11) NOT NULL, `RideStopFrom` int(11) NOT NULL, `RideStopTo` int(11) NOT NULL, PRIMARY KEY (`Id`), KEY `User` (`User`), KEY `Ride` (`Ride`), KEY `RideStopFrom` (`RideStopFrom`), KEY `RideStopTo` (`RideStopTo`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'); $this->Database->query('ALTER TABLE `Passenger` ADD CONSTRAINT `Passenger_ibfk_1` FOREIGN KEY (`Ride`) REFERENCES `Ride` (`Id`), ADD CONSTRAINT `Passenger_ibfk_2` FOREIGN KEY (`User`) REFERENCES `User` (`Id`), ADD CONSTRAINT `Passenger_ibfk_3` FOREIGN KEY (`RideStopFrom`) REFERENCES `RideStop` (`Id`), ADD CONSTRAINT `Passenger_ibfk_4` FOREIGN KEY (`RideStopTo`) REFERENCES `RideStop` (`Id`);'); $this->InsertData(); } function InsertData() { $this->Database->query('INSERT INTO `CarManufacturer` (`Id`, `Name`) VALUES '. '(NULL, "Škoda"), (NULL, "Nissan"), (NULL, "Mitsubishi"), (NULL, "Fiat"), '. '(NULL, "Dacia"), (NULL, "KIA"), (NULL, "Hyundai"), (NULL, "Ford"), '. '(NULL, "Opel"), (NULL, "Volkswagen"), (NULL, "Citroen"), (NULL, "Peugeot"), '. '(NULL, "Tesla"), (NULL, "Audi"), (NULL, "BMW"), (NULL, "Chevrolet"), '. '(NULL, "Suzuki"), (NULL, "Toyota"), (NULL, "Mercedes-benz"), (NULL, "Seat"),'. '(NULL, "Volvo"), (NULL, "Renault"), (NULL, "Mazda");'); $this->Database->query('INSERT INTO `CarModel` (`Id`, `Name`, `Manufacturer`) VALUES '. '(NULL, "Rapid", 1), (NULL, "Octavia", 1), (NULL, "Fabia", 1), (NULL, "Felicia", 1), '. '(NULL, "Superb", 1);'); } function DoUninstall(): void { $this->Database->query('DROP TABLE IF EXISTS `Passenger`'); $this->Database->query('DROP TABLE IF EXISTS `RideStop`'); $this->Database->query('DROP TABLE IF EXISTS `Ride`'); $this->Database->query('DROP TABLE IF EXISTS `Car`'); $this->Database->query('DROP TABLE IF EXISTS `CarModel`'); $this->Database->query('DROP TABLE IF EXISTS `CarManufacturer`'); } function DoUpgrade(): string { return ''; } function InsertSampleData(): void { $Cities = array('Praha', 'Brno', 'Ostrava', 'Olomouc', 'Liberec', 'Plzeň', 'Humpolec', 'Zlín', 'Přerov', 'České Budějovice', 'Jihlava', 'Ústí nad Labem', 'Hradec Králové', 'Pardubice', 'Mohelnice', 'Břeclav', 'Znojmo', 'Semily', 'Tábor', 'Opava', 'Karviná', 'Pelhřimov', 'Osoblaha', 'Broumov', 'Jirkov', 'Aš', 'Kutná Hora', 'Kolín', 'Strakonice', 'Tachov', 'Domažlice', 'Beroun', 'Most', 'Chomutov', 'Mělník', 'Mladá Boleslav', 'Svitavy', 'Šumperk', 'Litomyšl', 'Chrudim', 'Blansko', 'Hodonín', 'Rosice', 'Uherský Brod', 'Vsetín', 'Nový Jičín', 'Prostějov', 'Vyškov', 'Mohelnice', 'Ústí nad Orlicí'); for ($I = 0; $I < 100; $I++) { $Stops = array_fill(0, random(5), ''); for ($J = 0; $J < count($Stops); $J++) $Stops[$J] = $Cities[random(count($Cities))]; $Ride = new Ride(); $StartTime = time() - random(60*60*24*365); $StopCount = random(5); $Ride->Add($Stops, $StartTime, $SeatCount); } } function ShowLogo() { $Output = 'Logo '. 'Hledat jízdu nebo '. 'Nabídnout jízdu'; return $Output; } function UpdatePassengerCount($RideId) { $DbResult = $this->System->Database->select('RideStop', 'COUNT(*) AS RideStopCount', 'Ride='.$RideId); $DbRow = $DbResult->fetch_assoc(); $Stops = array_fill(0, $DbRow['RideStopCount'], 0); $this->System->Database->update('RideStop', 'Ride='.$RideId, array('PassengerCount' => 0)); $DbResult = $this->System->Database->query('SELECT PassengerRideStopFrom.Sequence AS SequenceFrom, '. 'PassengerRideStopTo.Sequence AS SequenceTo, Passenger.SeatCount FROM Passenger '. 'LEFT JOIN RideStop AS PassengerRideStopFrom ON PassengerRideStopFrom.Id=Passenger.RideStopFrom '. 'LEFT JOIN RideStop AS PassengerRideStopTo ON PassengerRideStopTo.Id=Passenger.RideStopTo '. 'WHERE Passenger.Ride='.$RideId); while ($DbRow = $DbResult->fetch_assoc()) { for ($I = $DbRow['SequenceFrom'] + 1; $I <= $DbRow['SequenceTo']; $I++) { $Stops[$I] = $Stops[$I] + $DbRow['SeatCount']; } } foreach ($Stops as $Index => $Stop) { $this->System->Database->update('RideStop', 'Ride='.$RideId.' AND Sequence='.$Index, array('PassengerCount' => $Stop)); } } } function time_elapsed($secs) { $bit = array( 'y' => $secs / 31556926 % 12, 'w' => $secs / 604800 % 52, 'd' => $secs / 86400 % 7, 'h' => $secs / 3600 % 24, 'm' => $secs / 60 % 60, 's' => $secs % 60 ); $ret = array(); foreach ($bit as $k => $v) if ($v > 0)$ret[] = $v . $k; return join(' ', $ret); }