Commit 62a4b84382a33535d5469ad0a87e4fb8281b6822
1 parent
bd5b1d78
[Orga] Chargement des événements via SQL
Contient un script permettant de créer la table sur le serveur, sera supprimé après éxecution au prochain commit.
Showing
3 changed files
with
177 additions
and
69 deletions
Show diff stats
... | ... | @@ -0,0 +1,69 @@ |
1 | +-- phpMyAdmin SQL Dump | |
2 | +-- version 4.2.7.1 | |
3 | +-- http://www.phpmyadmin.net | |
4 | +-- | |
5 | +-- Client : localhost | |
6 | +-- Généré le : Mar 11 Novembre 2014 à 22:27 | |
7 | +-- Version du serveur : 5.6.20 | |
8 | +-- Version de PHP : 5.5.15 | |
9 | + | |
10 | +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
11 | +SET time_zone = "+00:00"; | |
12 | + | |
13 | + | |
14 | +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
15 | +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
16 | +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
17 | +/*!40101 SET NAMES utf8 */; | |
18 | + | |
19 | +-- | |
20 | +-- Base de données : `crep` | |
21 | +-- | |
22 | + | |
23 | +-- -------------------------------------------------------- | |
24 | + | |
25 | +-- | |
26 | +-- Structure de la table `events` | |
27 | +-- | |
28 | + | |
29 | +CREATE TABLE IF NOT EXISTS `events` ( | |
30 | +`id` smallint(5) unsigned NOT NULL, | |
31 | + `creationTime` int(10) unsigned NOT NULL, | |
32 | + `nom` text CHARACTER SET utf8 NOT NULL, | |
33 | + `description` mediumtext CHARACTER SET utf8 NOT NULL, | |
34 | + `annule` tinyint(1) NOT NULL DEFAULT '0', | |
35 | + `valide` int(10) unsigned NOT NULL DEFAULT '0', | |
36 | + `duree` mediumint(8) unsigned NOT NULL DEFAULT '3600', | |
37 | + `supprime` tinyint(1) NOT NULL DEFAULT '0' | |
38 | +) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; | |
39 | + | |
40 | +-- | |
41 | +-- Contenu de la table `events` | |
42 | +-- | |
43 | + | |
44 | +INSERT INTO `events` (`id`, `creationTime`, `nom`, `description`, `annule`, `valide`, `duree`, `supprime`) VALUES | |
45 | +(1, 1415737327, 'Événement de test n°1', 'Description de l''événement de test n°1.', 0, 0, 7200, 0), | |
46 | +(2, 1415738888, 'Événement de test n°2', 'Description de l''événement de test n°2.\r\nNouvelle ligne et caractères $ρ∑⊂|⋀∪×.', 1, 0, 12345, 0); | |
47 | + | |
48 | +-- | |
49 | +-- Index pour les tables exportées | |
50 | +-- | |
51 | + | |
52 | +-- | |
53 | +-- Index pour la table `events` | |
54 | +-- | |
55 | +ALTER TABLE `events` | |
56 | + ADD PRIMARY KEY (`id`), ADD KEY `id` (`id`); | |
57 | + | |
58 | +-- | |
59 | +-- AUTO_INCREMENT pour les tables exportées | |
60 | +-- | |
61 | + | |
62 | +-- | |
63 | +-- AUTO_INCREMENT pour la table `events` | |
64 | +-- | |
65 | +ALTER TABLE `events` | |
66 | +MODIFY `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3; | |
67 | +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
68 | +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
69 | +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; | ... | ... |
... | ... | @@ -0,0 +1,47 @@ |
1 | +<?php | |
2 | + | |
3 | +// Script d'import de table SQL | |
4 | +// TEMPORAIRE | |
5 | +// de http://stackoverflow.com/questions/19751354/how-to-import-sql-file-in-mysql-database-using-php | |
6 | + | |
7 | +require('creds.php'); | |
8 | + | |
9 | +if (isset($_GET['KAMGF6QfHGl8GHypRCat'])) { | |
10 | + | |
11 | + // Name of the file | |
12 | + $filename = 'events.sql'; | |
13 | + | |
14 | + // Connect to MySQL server | |
15 | + mysql_connect(__MYSQL_HOSTNAME__, __MYSQL_USERNAME__, __MYSQL_PASSWORD__) or die('Error connecting to MySQL server: ' . mysql_error()); | |
16 | + | |
17 | + // Select database | |
18 | + mysql_select_db(__MYSQL_DATABASE__) or die('Error selecting MySQL database: ' . mysql_error()); | |
19 | + | |
20 | + // Temporary variable, used to store current query | |
21 | + $templine = ''; | |
22 | + | |
23 | + // Read in entire file | |
24 | + $lines = file($filename); | |
25 | + | |
26 | + // Loop through each line | |
27 | + foreach ($lines as $line) { | |
28 | + | |
29 | + // Skip it if it's a comment | |
30 | + if (substr($line, 0, 2) == '--' || $line == '') continue; | |
31 | + | |
32 | + // Add this line to the current segment | |
33 | + $templine.= $line; | |
34 | + | |
35 | + // If it has a semicolon at the end, it's the end of the query | |
36 | + if (substr(trim($line), -1, 1) == ';') { | |
37 | + | |
38 | + // Perform the query | |
39 | + mysql_query($templine) or print ('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />'); | |
40 | + | |
41 | + // Reset temp variable to empty | |
42 | + $templine = ''; | |
43 | + } | |
44 | + } | |
45 | + echo "Tables imported successfully"; | |
46 | +} | |
47 | +?> | |
0 | 48 | \ No newline at end of file | ... | ... |
orga.php
... | ... | @@ -28,8 +28,6 @@ if ($e_connecte) { |
28 | 28 | $droits = array('voir'); |
29 | 29 | } |
30 | 30 | |
31 | -$lastID = 0; // TODO Est-ce que cela mérite-t-il d'exister ? | |
32 | - | |
33 | 31 | class Evenement |
34 | 32 | { |
35 | 33 | // TODO Mettre tout en privé et utiliser __get et __set |
... | ... | @@ -46,19 +44,62 @@ class Evenement |
46 | 44 | public $dates = array(); |
47 | 45 | public $datesVotes = array(); |
48 | 46 | |
49 | - function __construct($id = 0) { | |
50 | - global $lastID; | |
51 | - if ($id == 0) { // Nouvel évènement | |
52 | - $lastID += 1; | |
53 | - $this->id = $lastID; | |
47 | + private static $bddOK = false; | |
48 | + private static $bdd = null; | |
49 | + public static $tout = array(); | |
50 | + | |
51 | + private static function connecterBDD() { | |
52 | + if (!Evenement::$bddOK) { | |
53 | + require_once("creds.php"); | |
54 | + try { | |
55 | + Evenement::$bdd = mysql_connect(__MYSQL_HOSTNAME__, __MYSQL_USERNAME__, __MYSQL_PASSWORD__); | |
56 | + mysql_query("SET NAMES 'utf8'"); | |
57 | + } catch(Exception $e) { | |
58 | + echo 'Nop connect'; | |
59 | + return; // TODO Message d'erreur digne de ce nom | |
60 | + } | |
61 | + if (mysql_select_db('crep', Evenement::$bdd)) { | |
62 | + Evenement::$bddOK = true; | |
63 | + } else { | |
64 | + echo 'Nop db'; | |
65 | + } | |
66 | + } | |
67 | + } | |
68 | + | |
69 | + public static function chargerTout() { | |
70 | + Evenement::$tout = array(); | |
71 | + Evenement::connecterBDD(); | |
72 | + $requete = 'SELECT id FROM events'; | |
73 | + // TODO SQL Protéger | |
74 | + $resultat = mysql_query($requete); | |
75 | + while ($row = mysql_fetch_assoc($resultat)) { | |
76 | + Evenement::$tout[] = new Evenement($row['id']); | |
77 | + } | |
78 | + } | |
79 | + | |
80 | + function __construct($id = null) { | |
81 | + Evenement::connecterBDD(); | |
82 | + | |
83 | + if ($id == null) { // Nouvel évènement | |
54 | 84 | $this->creationTime = time(); |
85 | + // TODO SQL Récupérer id (AUTOINCREMENT) | |
55 | 86 | } else { // Évènement existant : on charge |
56 | - // TODO SQL Select | |
57 | - $this->id = $lastID + 1; // TODO DEBUG SQL Récupération id | |
58 | - if ($this->id > $lastID) { | |
59 | - $lastID = $this->id; | |
87 | + $requete = 'SELECT id, creationTime, nom, description, annule, valide, duree, supprime FROM events WHERE id='.$id; | |
88 | + // TODO SQL Protéger | |
89 | + $resultat = mysql_query($requete); | |
90 | + if ($resultat && $row = mysql_fetch_assoc($resultat)) { | |
91 | + $this->id = $row['id']; | |
92 | + $this->creationTime = $row['creationTime']; | |
93 | + $this->nom = $row['nom']; | |
94 | + $this->description = $row['description']; | |
95 | + $this->annule = $row['annule']; | |
96 | + $this->valide = $row['valide']; | |
97 | + $this->duree = $row['duree']; | |
98 | + $this->supprime = $row['supprime']; | |
99 | + | |
100 | + } else { | |
101 | + echo 'Nop resultat'; | |
60 | 102 | } |
61 | - // TODO SQL Récupération du reste des propriétés | |
62 | 103 | } |
63 | 104 | } |
64 | 105 | |
... | ... | @@ -191,62 +232,12 @@ class Evenement |
191 | 232 | |
192 | 233 | } |
193 | 234 | |
194 | -# a_ : Récupérer depuis la base de donnée (ou pas) | |
195 | -function a_evenements() { | |
196 | - # DEBUG | |
197 | - $test1 = new Evenement; | |
198 | - $test1->duree = 12345; | |
199 | - $test1->nom = 'Évènement de test n°1'; | |
200 | - $test1->description = 'Description de l\'évènement de test n°1'; | |
201 | - $test1->valide = time(); | |
202 | - | |
203 | - $test2 = new Evenement; | |
204 | - $test2->nom = 'Évènement de test n°2'; | |
205 | - $test2->description = 'Description de l\'évènement de test n°2'; | |
206 | - $test2->duree = 36000; | |
207 | - $test2->valide = time(); | |
208 | - $test2->annule = true; | |
209 | - | |
210 | - $test3 = new Evenement; | |
211 | - $test3->nom = 'Évènement de test n°3'; | |
212 | - $test3->description = 'Description de l\'évènement de test n°3'; | |
213 | - $test3->dates[] = 1415482197; | |
214 | - $test3->datesVotes[] = 42; | |
215 | - $test3->dates[] = time(); | |
216 | - $test3->datesVotes[] = 5; | |
217 | - $test3->dates[] = time()+365*24*3600; | |
218 | - $test3->datesVotes[] = 1; | |
219 | - | |
220 | - $test4 = new Evenement; | |
221 | - $test4->nom = 'Évènement de test n°4'; | |
222 | - $test4->description = 'Description de l\'évènement de test n°4'; | |
223 | - $test4->dates[] = time(); | |
224 | - $test4->datesVotes[] = 5; | |
225 | - $test4->dates[] = time()+365*24*3600; | |
226 | - $test4->datesVotes[] = 1; | |
227 | - $test4->dates[] = time(); | |
228 | - $test4->annule = true; | |
229 | - | |
230 | - $test5 = new Evenement; | |
231 | - $test5->nom = 'Évènement de test n°5'; | |
232 | - $test5->description = 'Description de l\'évènement de test n°5'; | |
233 | - $test5->dates[] = time(); | |
234 | - $test5->datesVotes[] = 0; | |
235 | - $test5->supprime = true; | |
236 | - | |
237 | - $test6 = new Evenement; | |
238 | - $test6->nom = 'Évènement de test n°6'; | |
239 | - $test6->description = 'Description de l\'évènement de test n°6'; | |
240 | - $test6->valide = 1415452197; | |
241 | - | |
242 | - return array($test1, $test2, $test3, $test4, $test5, $test6); | |
243 | -} | |
235 | +Evenement::chargerTout(); | |
244 | 236 | |
245 | -$evenements = a_evenements(); | |
237 | +# a_ : Récupérer depuis la base de donnée (ou pas) | |
246 | 238 | |
247 | -function a_evenement($id) { | |
248 | - global $evenements; | |
249 | - foreach ($evenements as $evenement) { | |
239 | +function a_evenement($id) { // TODO Méthode statique à Evenement | |
240 | + foreach (Evenement::$tout as $evenement) { | |
250 | 241 | if ($evenement->id == $id) { |
251 | 242 | return $evenement; |
252 | 243 | } |
... | ... | @@ -324,7 +315,7 @@ $evenementsPlanifies = array(); |
324 | 315 | $evenementsAPlanifier = array(); |
325 | 316 | $evenementsPasses = array(); |
326 | 317 | |
327 | -foreach ($evenements as $evenement) { | |
318 | +foreach (Evenement::$tout as $evenement) { | |
328 | 319 | if (!$evenement->supprime) { |
329 | 320 | if ($evenement->valide) { |
330 | 321 | if ($evenement->passe()) { |
... | ... | @@ -345,9 +336,10 @@ if (!$e_connecte) { |
345 | 336 | <div class="alert alert-warning" role="alert">Connectez-vous afin de pouvoir agir sur les évènements.</div> |
346 | 337 | <?php |
347 | 338 | } |
348 | -?> | |
349 | -<?php | |
339 | + | |
340 | + | |
350 | 341 | if (in_array('voir', $droits)) { |
342 | +// TODO Message si catégorie vide | |
351 | 343 | ?> |
352 | 344 | <h3>Évènements plannifiés <?php if (in_array('ajouter', $droits)) { ?><button id="ev_ajouter_fixe" type="button" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Ajouter un évènement avec une date fixée</button><?php } ?></h3> |
353 | 345 | <ul id="ev_ul_planifies" class="list-group"> | ... | ... |