From 7c18add7708dee37fbd16faa569c6ef8c14bdcbc Mon Sep 17 00:00:00 2001 From: TheMrNomis Date: Thu, 3 Dec 2015 10:05:20 +0100 Subject: [PATCH 1/7] added getEventsByDateAndCategories --- getEvents.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/getEvents.php b/getEvents.php index e60f680..ca0e47b 100755 --- a/getEvents.php +++ b/getEvents.php @@ -96,6 +96,34 @@ function getEventsByDate($db, $date) } } +/** + * @brief queries the database for the events on a certain date, where the categories are matched + * @param $db: the PDO connection to the database + * @param $date: the date to search for + * @param $categories: an array containing the IDs of the categories + * @return a list of all the events of the different categories at the date $date + */ +function getEventsByDateAndCategories($db, $date, $categories) +{ + try + { + $request = $db->prepare('SELECT events.id, events.titre, events.localisation, events.dtstart, events.dtend, events.description FROM events, categorie WHERE events.categorie = categorie.id AND (dtstart <= :date AND dtend >= :date) AND events.categorie IN :categories'); + $request->execute(array( + 'date'=>date("Y-m-d",$date), + 'categories'=>implode(',', array_map('intval', $categories)) + )); + $result = $request->fetchAll(); + $request->closeCursor(); + return $result; + } + catch(PDOException $e) + { + //NOTE: change $e->getMessage() by an error message before going to production + echo($e->getMessage()); + die(); + } +} + /** * @brief queries the database for the events after a certain date * @param $db: the PDO connection to the database From 34b1235749a95ed546d8505a9f28af7efd027c86 Mon Sep 17 00:00:00 2001 From: TheMrNomis Date: Thu, 3 Dec 2015 10:08:53 +0100 Subject: [PATCH 2/7] renames getEvents.php to databaseOperations.php --- getEvents.php => databaseOperations.php | 0 export.php | 2 +- index.php | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename getEvents.php => databaseOperations.php (100%) diff --git a/getEvents.php b/databaseOperations.php similarity index 100% rename from getEvents.php rename to databaseOperations.php diff --git a/export.php b/export.php index 5382e43..0a522f3 100644 --- a/export.php +++ b/export.php @@ -13,7 +13,7 @@ function icaldate($date) return date('Ymd',$dt).'T'. date('His',$dt). 'Z'; } -include_once('getEvents.php'); +include_once('databaseOperations.php'); $db = connect(); $events = getEventsSince($db, strtotime('last monday +'.$weekOffset.' weeks')); diff --git a/index.php b/index.php index 885936f..8b135d1 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,6 @@ Date: Thu, 3 Dec 2015 10:19:50 +0100 Subject: [PATCH 3/7] added getCategories --- databaseOperations.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/databaseOperations.php b/databaseOperations.php index ca0e47b..bec526b 100755 --- a/databaseOperations.php +++ b/databaseOperations.php @@ -25,6 +25,29 @@ function connect() } } +/** + * @brief queries all the categories from the dabase + * @param $db: the PDO connection to the database + * @return al list of all the categories (id, name) + */ +function getCategories($db) +{ + try + { + $request = $db->prepare('SELECT id, nom_cat, sous_cat FROM categorie ORDER BY sous_cat ASC'); + $request->execute(); + $result = $request->fetchAll(); + $request->closeCursor(); + return $result; + } + catch(PDOException $e) + { + //NOTE: change $e->getMessage() by an error message before going to production + echo($e->getMessage()); + die(); + } +} + /** * @brief queries all the events from the database * @param $db: the PDO connection to the database From ee1028e2a32589a9509b5ee86efb7bbef52f7cee Mon Sep 17 00:00:00 2001 From: TheMrNomis Date: Thu, 3 Dec 2015 10:45:44 +0100 Subject: [PATCH 4/7] loading categories --- index.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.php b/index.php index 8b135d1..f6aa766 100644 --- a/index.php +++ b/index.php @@ -32,6 +32,8 @@ if(isset($_GET['w'])&&is_numeric($_GET['w'])) else $weekOffset = 0; +$categories = getCategories($db); + ?> From 5662410747c2bcacea4409b64d5e92b7b6922a6c Mon Sep 17 00:00:00 2001 From: TheMrNomis Date: Thu, 3 Dec 2015 11:10:17 +0100 Subject: [PATCH 5/7] minor SQL schema update (nullable informations) --- makedatabase.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/makedatabase.sql b/makedatabase.sql index 39904e5..a761bf6 100644 --- a/makedatabase.sql +++ b/makedatabase.sql @@ -1,7 +1,7 @@ -CREATE TABLE sous_categorie(sous_cat_id INTEGER PRIMARY KEY, sous_cat_titre VARCHAR(255)); +CREATE TABLE sous_categorie(sous_cat_id INTEGER PRIMARY KEY, sous_cat_tab INTEGER NOT NULL, sous_cat_titre VARCHAR(255) NOT NULL); -CREATE TABLE categorie(cat_id INTEGER PRIMARY KEY, cat_titre VARCHAR(255), sous_cat_id INTEGER, FOREIGN KEY(sous_cat_id) REFERENCES sous_categorie(sous_cat_id)); +CREATE TABLE categorie(cat_id INTEGER PRIMARY KEY, cat_titre VARCHAR(255) NOT NULL, sous_cat_id INTEGER NOT NULL, FOREIGN KEY(sous_cat_id) REFERENCES sous_categorie(sous_cat_id)); -CREATE TABLE event(event_id INTEGER PRIMARY KEY, event_titre VARCHAR(255), event_localisation VARCHAR(255), event_dtstart DATETIME, event_dtend DATETIME, event_description TEXT, event_url VARCHAR(255)); +CREATE TABLE event(event_id INTEGER PRIMARY KEY, event_titre VARCHAR(255) NOT NULL, event_localisation VARCHAR(255) NOT NULL, event_dtstart DATETIME NOT NULL, event_dtend DATETIME NOT NULL, event_description TEXT NOT NULL, event_url VARCHAR(255) NULL); CREATE TABLE eventCategorie(event_id INTEGER, cat_id INTEGER, FOREIGN KEY(event_id) REFERENCES event(event_id), FOREIGN KEY(cat_id) REFERENCES categorie(cat_id)); From 32e33f0438a85dfb081b9209b340cdac6562725a Mon Sep 17 00:00:00 2001 From: TheMrNomis Date: Thu, 3 Dec 2015 11:09:19 +0100 Subject: [PATCH 6/7] updated database operations to the new database schema --- databaseOperations.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/databaseOperations.php b/databaseOperations.php index bec526b..8a52e0c 100755 --- a/databaseOperations.php +++ b/databaseOperations.php @@ -34,7 +34,7 @@ function getCategories($db) { try { - $request = $db->prepare('SELECT id, nom_cat, sous_cat FROM categorie ORDER BY sous_cat ASC'); + $request = $db->prepare('SELECT * FROM categorie NATURAL JOIN sous_categorie ORDER BY sous_cat_tab ASC, sous_cat_id ASC'); $request->execute(); $result = $request->fetchAll(); $request->closeCursor(); @@ -57,7 +57,7 @@ function getAllEvents($db) { try { - $request = $db->prepare('SELECT events.id, events.titre, events.localisation, events.dtstart, events.dtend, events.description FROM events, categorie WHERE events.categorie = categorie.id '); + $request = $db->prepare('SELECT * FROM event NATURAL JOIN eventCategorie NATURAL JOIN categorie'); $request->execute(array('date'=>date("Y-m-d",$date))); $result = $request->fetchAll(); $request->closeCursor(); @@ -81,7 +81,25 @@ function getOneEvent($db, $id) { try { - $request = $db->prepare('SELECT events.id, events.titre, events.localisation, events.dtstart, events.dtend, events.description FROM events, categorie WHERE events.categorie = categorie.id AND events.id = ?'); + $request = $db->prepare('SELECT * FROM event WHERE event_id = ?'); + $request->execute(array($id)); + $result = $request->fetch(); + $request->closeCursor(); + return $result; + } + catch(PDOException $e) + { + //NOTE: change $e->getMessage() by an error message before going to production + echo($e->getMessage()); + die(); + } +} + +function getCategoriesForOneEvent($db, $eventId) +{ + try + { + $request = $db->prepare('SELECT * FROM eventCategorie NATURAL JOIN categorie NATURAL JOIN sous_categorie ORDER BY sous_cat_tab ASC, sous_cat_id ASC WHERE event_id = ?'); $request->execute(array($id)); $result = $request->fetch(); $request->closeCursor(); @@ -105,7 +123,7 @@ function getEventsByDate($db, $date) { try { - $request = $db->prepare('SELECT events.id, events.titre, events.localisation, events.dtstart, events.dtend, events.description FROM events, categorie WHERE events.categorie = categorie.id AND (dtstart <= :date AND dtend >= :date)'); + $request = $db->prepare('SELECT * FROM event WHERE (event_dtstart <= :date AND event_dtend >= :date)'); $request->execute(array('date'=>date("Y-m-d",$date))); $result = $request->fetchAll(); $request->closeCursor(); @@ -130,7 +148,7 @@ function getEventsByDateAndCategories($db, $date, $categories) { try { - $request = $db->prepare('SELECT events.id, events.titre, events.localisation, events.dtstart, events.dtend, events.description FROM events, categorie WHERE events.categorie = categorie.id AND (dtstart <= :date AND dtend >= :date) AND events.categorie IN :categories'); + $request = $db->prepare('SELECT * FROM event NATURAL JOIN eventCategorie NATURAL JOIN categorie WHERE (dtstart <= :date AND dtend >= :date) AND events.categorie IN (:categories)'); $request->execute(array( 'date'=>date("Y-m-d",$date), 'categories'=>implode(',', array_map('intval', $categories)) @@ -157,7 +175,7 @@ function getEventsSince($db,$date) { try { - $request = $db->prepare('SELECT events.id, events.titre, events.localisation, events.dtstart, events.dtend, events.description FROM events, categorie WHERE events.categorie = categorie.id AND (events.dtstart >= :date OR events.dtend >= :date)'); + $request = $db->prepare('SELECT * FROM event WHERE (event_dtstart >= :date OR event_dtend >= :date)'); $request->execute(array('date'=>date("Y-m-d",$date))); $result = $request->fetchAll(); $request->closeCursor(); From c98c60c604b16f135921edcf2dcadad4e1e8db67 Mon Sep 17 00:00:00 2001 From: TheMrNomis Date: Thu, 3 Dec 2015 11:09:41 +0100 Subject: [PATCH 7/7] removed deprecated functions --- databaseOperations.php | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/databaseOperations.php b/databaseOperations.php index 8a52e0c..d102b17 100755 --- a/databaseOperations.php +++ b/databaseOperations.php @@ -188,30 +188,4 @@ function getEventsSince($db,$date) die(); } } - -/** - * @deprecated - */ -function getEvents() -{ - //FUTURE: delete this function - $pdo = connect(); - $stmt = $pdo->prepare('SELECT titre, localisation, dtstart, dtend, description, url FROM events, categorie WHERE events.categorie = categorie.id'); - $stmt->execute(); - $result = $stmt->fetchAll(); - return $result; -} - -/** - * @deprecated - */ -function getEvent($id) -{ - //FUTURE: delete this function - $pdo = connect(); - $stmt = $pdo->prepare('SELECT titre, localisation, dtstart, dtend, description, url FROM events WHERE id = ?'); - $stmt->execute(array($id)); - $result = $stmt->fetch(); - return $result; -} ?>