1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/Ride.php');
|
---|
4 | include_once(dirname(__FILE__).'/PageMain.php');
|
---|
5 | include_once(dirname(__FILE__).'/PageRideDetail.php');
|
---|
6 | include_once(dirname(__FILE__).'/PageRideList.php');
|
---|
7 | include_once(dirname(__FILE__).'/PageNewRide.php');
|
---|
8 |
|
---|
9 | class ModuleRide extends Module
|
---|
10 | {
|
---|
11 | function __construct($System)
|
---|
12 | {
|
---|
13 | parent::__construct($System);
|
---|
14 | $this->Name = 'Ride';
|
---|
15 | $this->Version = '1.0';
|
---|
16 | $this->Creator = 'Chronos';
|
---|
17 | $this->License = 'GNU/GPL';
|
---|
18 | $this->Description = 'Ride management';
|
---|
19 | $this->Dependencies = array('User', 'Map');
|
---|
20 | }
|
---|
21 |
|
---|
22 | function DoStart(): void
|
---|
23 | {
|
---|
24 | $this->System->RegisterPage([''], 'PageMain');
|
---|
25 | $this->System->RegisterPage(['nova-jizda'], 'PageNewRide');
|
---|
26 | $this->System->RegisterPage(['jizda'], 'PageRideDetail');
|
---|
27 | $this->System->RegisterPage(['jizdy'], 'PageRideList');
|
---|
28 | Core::Cast($this->System)->RegisterPageBarItem('TopLeft', 'Logo', array($this, 'ShowLogo'));
|
---|
29 | }
|
---|
30 |
|
---|
31 | function DoInstall(): void
|
---|
32 | {
|
---|
33 | $this->Database->query('CREATE TABLE `CarManufacturer` (
|
---|
34 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
35 | `Name` varchar(255) NOT NULL,
|
---|
36 | PRIMARY KEY (`Id`)
|
---|
37 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
|
---|
38 | $this->Database->query('CREATE TABLE `CarModel` (
|
---|
39 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
40 | `Name` varchar(255) NOT NULL,
|
---|
41 | `Manufacturer` int(11) NOT NULL,
|
---|
42 | PRIMARY KEY (`Id`),
|
---|
43 | KEY `Manufacturer` (`Manufacturer`)
|
---|
44 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
|
---|
45 | $this->Database->query('ALTER TABLE `CarModel`
|
---|
46 | ADD CONSTRAINT `CarModel_ibfk_1` FOREIGN KEY (`Manufacturer`) REFERENCES `CarManufacturer` (`Id`);');
|
---|
47 | $this->Database->query('CREATE TABLE `Car` (
|
---|
48 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
49 | `Owner` int(11) NOT NULL,
|
---|
50 | `Photo` int(11) DEFAULT NULL,
|
---|
51 | `Model` int(11) NOT NULL,
|
---|
52 | PRIMARY KEY (`Id`),
|
---|
53 | KEY `Owner` (`Owner`),
|
---|
54 | KEY `Model` (`Model`)
|
---|
55 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
|
---|
56 | $this->Database->query('ALTER TABLE `Car`
|
---|
57 | ADD CONSTRAINT `Car_ibfk_1` FOREIGN KEY (`Owner`) REFERENCES `User` (`Id`),
|
---|
58 | ADD CONSTRAINT `Car_ibfk_2` FOREIGN KEY (`Model`) REFERENCES `CarModel` (`Id`);');
|
---|
59 | $this->Database->query('CREATE TABLE `Ride` (
|
---|
60 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
61 | `Driver` int(11) NOT NULL,
|
---|
62 | `SeatCount` int(11) NOT NULL,
|
---|
63 | `Car` int(11) NOT NULL,
|
---|
64 | `Details` text NOT NULL,
|
---|
65 | PRIMARY KEY (`Id`),
|
---|
66 | KEY `Driver` (`Driver`),
|
---|
67 | KEY `Car` (`Car`)
|
---|
68 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
|
---|
69 | $this->Database->query('ALTER TABLE `Ride`
|
---|
70 | ADD CONSTRAINT `Ride_ibfk_1` FOREIGN KEY (`Driver`) REFERENCES `User` (`Id`),
|
---|
71 | ADD CONSTRAINT `Ride_ibfk_2` FOREIGN KEY (`Car`) REFERENCES `Car` (`Id`);');
|
---|
72 | $this->Database->query('CREATE TABLE `RideStop` (
|
---|
73 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
74 | `Ride` int(11) NOT NULL,
|
---|
75 | `Time` datetime NOT NULL,
|
---|
76 | `Place` varchar(255) NOT NULL,
|
---|
77 | `Sequence` int(11) NOT NULL,
|
---|
78 | `Price` int(11) NOT NULL,
|
---|
79 | `PassengerCount` int(11) NOT NULL,
|
---|
80 | PRIMARY KEY (`Id`),
|
---|
81 | KEY `Ride` (`Ride`)
|
---|
82 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
|
---|
83 | $this->Database->query('ALTER TABLE `RideStop`
|
---|
84 | ADD CONSTRAINT `RideStop_ibfk_1` FOREIGN KEY (`Ride`) REFERENCES `Ride` (`Id`);');
|
---|
85 | $this->Database->query('CREATE TABLE `Passenger` (
|
---|
86 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
87 | `Ride` int(11) NOT NULL,
|
---|
88 | `User` int(11) NOT NULL,
|
---|
89 | `SeatCount` int(11) NOT NULL,
|
---|
90 | `RideStopFrom` int(11) NOT NULL,
|
---|
91 | `RideStopTo` int(11) NOT NULL,
|
---|
92 | PRIMARY KEY (`Id`),
|
---|
93 | KEY `User` (`User`),
|
---|
94 | KEY `Ride` (`Ride`),
|
---|
95 | KEY `RideStopFrom` (`RideStopFrom`),
|
---|
96 | KEY `RideStopTo` (`RideStopTo`)
|
---|
97 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
|
---|
98 | $this->Database->query('ALTER TABLE `Passenger`
|
---|
99 | ADD CONSTRAINT `Passenger_ibfk_1` FOREIGN KEY (`Ride`) REFERENCES `Ride` (`Id`),
|
---|
100 | ADD CONSTRAINT `Passenger_ibfk_2` FOREIGN KEY (`User`) REFERENCES `User` (`Id`),
|
---|
101 | ADD CONSTRAINT `Passenger_ibfk_3` FOREIGN KEY (`RideStopFrom`) REFERENCES `RideStop` (`Id`),
|
---|
102 | ADD CONSTRAINT `Passenger_ibfk_4` FOREIGN KEY (`RideStopTo`) REFERENCES `RideStop` (`Id`);');
|
---|
103 | $this->InsertData();
|
---|
104 | }
|
---|
105 |
|
---|
106 | function InsertData()
|
---|
107 | {
|
---|
108 | $this->Database->query('INSERT INTO `CarManufacturer` (`Id`, `Name`) VALUES '.
|
---|
109 | '(NULL, "Škoda"), (NULL, "Nissan"), (NULL, "Mitsubishi"), (NULL, "Fiat"), '.
|
---|
110 | '(NULL, "Dacia"), (NULL, "KIA"), (NULL, "Hyundai"), (NULL, "Ford"), '.
|
---|
111 | '(NULL, "Opel"), (NULL, "Volkswagen"), (NULL, "Citroen"), (NULL, "Peugeot"), '.
|
---|
112 | '(NULL, "Tesla"), (NULL, "Audi"), (NULL, "BMW"), (NULL, "Chevrolet"), '.
|
---|
113 | '(NULL, "Suzuki"), (NULL, "Toyota"), (NULL, "Mercedes-benz"), (NULL, "Seat"),'.
|
---|
114 | '(NULL, "Volvo"), (NULL, "Renault"), (NULL, "Mazda");');
|
---|
115 | $this->Database->query('INSERT INTO `CarModel` (`Id`, `Name`, `Manufacturer`) VALUES '.
|
---|
116 | '(NULL, "Rapid", 1), (NULL, "Octavia", 1), (NULL, "Fabia", 1), (NULL, "Felicia", 1), '.
|
---|
117 | '(NULL, "Superb", 1);');
|
---|
118 | }
|
---|
119 |
|
---|
120 | function DoUninstall(): void
|
---|
121 | {
|
---|
122 | $this->Database->query('DROP TABLE IF EXISTS `Passenger`');
|
---|
123 | $this->Database->query('DROP TABLE IF EXISTS `RideStop`');
|
---|
124 | $this->Database->query('DROP TABLE IF EXISTS `Ride`');
|
---|
125 | $this->Database->query('DROP TABLE IF EXISTS `Car`');
|
---|
126 | $this->Database->query('DROP TABLE IF EXISTS `CarModel`');
|
---|
127 | $this->Database->query('DROP TABLE IF EXISTS `CarManufacturer`');
|
---|
128 | }
|
---|
129 |
|
---|
130 | function DoUpgrade(): string
|
---|
131 | {
|
---|
132 | return '';
|
---|
133 | }
|
---|
134 |
|
---|
135 | function InsertSampleData(): void
|
---|
136 | {
|
---|
137 | $Cities = array('Praha', 'Brno', 'Ostrava', 'Olomouc', 'Liberec', 'Plzeň',
|
---|
138 | 'Humpolec', 'Zlín', 'Přerov', 'České Budějovice', 'Jihlava', 'Ústí nad Labem',
|
---|
139 | 'Hradec Králové', 'Pardubice', 'Mohelnice', 'Břeclav', 'Znojmo', 'Semily',
|
---|
140 | 'Tábor', 'Opava', 'Karviná', 'Pelhřimov', 'Osoblaha', 'Broumov', 'Jirkov',
|
---|
141 | 'Aš', 'Kutná Hora', 'Kolín', 'Strakonice', 'Tachov', 'Domažlice', 'Beroun',
|
---|
142 | 'Most', 'Chomutov', 'Mělník', 'Mladá Boleslav', 'Svitavy', 'Šumperk',
|
---|
143 | 'Litomyšl', 'Chrudim', 'Blansko', 'Hodonín', 'Rosice', 'Uherský Brod',
|
---|
144 | 'Vsetín', 'Nový Jičín', 'Prostějov', 'Vyškov', 'Mohelnice', 'Ústí nad Orlicí');
|
---|
145 |
|
---|
146 | for ($I = 0; $I < 100; $I++)
|
---|
147 | {
|
---|
148 | $Stops = array_fill(0, random(5), '');
|
---|
149 | for ($J = 0; $J < count($Stops); $J++)
|
---|
150 | $Stops[$J] = $Cities[random(count($Cities))];
|
---|
151 |
|
---|
152 | $Ride = new Ride();
|
---|
153 | $StartTime = time() - random(60*60*24*365);
|
---|
154 | $StopCount = random(5);
|
---|
155 | $Ride->Add($Stops, $StartTime, $SeatCount);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | function ShowLogo()
|
---|
160 | {
|
---|
161 | $Output = '<img src="" width="200" height="50" alt="Logo"/> '.
|
---|
162 | '<a href="'.$this->System->Link('/jizdy/').'">Hledat jízdu</a> nebo '.
|
---|
163 | '<a href="'.$this->System->Link('/nova-jizda/').'">Nabídnout jízdu</a>';
|
---|
164 | return $Output;
|
---|
165 | }
|
---|
166 |
|
---|
167 | function UpdatePassengerCount($RideId)
|
---|
168 | {
|
---|
169 | $DbResult = $this->System->Database->select('RideStop', 'COUNT(*) AS RideStopCount', 'Ride='.$RideId);
|
---|
170 | $DbRow = $DbResult->fetch_assoc();
|
---|
171 |
|
---|
172 | $Stops = array_fill(0, $DbRow['RideStopCount'], 0);
|
---|
173 | $this->System->Database->update('RideStop', 'Ride='.$RideId, array('PassengerCount' => 0));
|
---|
174 | $DbResult = $this->System->Database->query('SELECT PassengerRideStopFrom.Sequence AS SequenceFrom, '.
|
---|
175 | 'PassengerRideStopTo.Sequence AS SequenceTo, Passenger.SeatCount FROM Passenger '.
|
---|
176 | 'LEFT JOIN RideStop AS PassengerRideStopFrom ON PassengerRideStopFrom.Id=Passenger.RideStopFrom '.
|
---|
177 | 'LEFT JOIN RideStop AS PassengerRideStopTo ON PassengerRideStopTo.Id=Passenger.RideStopTo '.
|
---|
178 | 'WHERE Passenger.Ride='.$RideId);
|
---|
179 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
180 | {
|
---|
181 | for ($I = $DbRow['SequenceFrom'] + 1; $I <= $DbRow['SequenceTo']; $I++)
|
---|
182 | {
|
---|
183 | $Stops[$I] = $Stops[$I] + $DbRow['SeatCount'];
|
---|
184 | }
|
---|
185 | }
|
---|
186 | foreach ($Stops as $Index => $Stop)
|
---|
187 | {
|
---|
188 | $this->System->Database->update('RideStop', 'Ride='.$RideId.' AND Sequence='.$Index, array('PassengerCount' => $Stop));
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | function time_elapsed($secs)
|
---|
194 | {
|
---|
195 | $bit = array(
|
---|
196 | 'y' => $secs / 31556926 % 12,
|
---|
197 | 'w' => $secs / 604800 % 52,
|
---|
198 | 'd' => $secs / 86400 % 7,
|
---|
199 | 'h' => $secs / 3600 % 24,
|
---|
200 | 'm' => $secs / 60 % 60,
|
---|
201 | 's' => $secs % 60
|
---|
202 | );
|
---|
203 |
|
---|
204 | $ret = array();
|
---|
205 | foreach ($bit as $k => $v)
|
---|
206 | if ($v > 0)$ret[] = $v . $k;
|
---|
207 |
|
---|
208 | return join(' ', $ret);
|
---|
209 | }
|
---|