Skip to content

Commit

Permalink
Merge pull request #1 from HelloAsso/setupdbconnexionandtablecreation
Browse files Browse the repository at this point in the history
set database connexion and table creation
  • Loading branch information
lemarty authored Aug 19, 2024
2 parents 36f7fe2 + a515481 commit e1ab7b4
Show file tree
Hide file tree
Showing 511 changed files with 97,460 additions and 24 deletions.
26 changes: 19 additions & 7 deletions .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
DBURL=
DBNAME=
DBUSER=
DBPASSWORD=
ADMIN_USER=
ADMIN_PASSWORD=
ADMIN_IPS=
ENVIRONMENT=LOCAL

DBURL=https://phpmyadmin.cluster014.hosting.ovh.net/index.php?pma_username=socialgotwitch&pma_servername=socialgotwitch.mysql.db
DBNAME=socialgotwitch
DBUSER=socialgotwitch
DBPASSWORD=Q22yTCTDyZJnFEC

DBURL_LOCAL=localhost
DBNAME_LOCAL=socialgotwitch
DBUSER_LOCAL=socialgotwitch
DBPASSWORD_LOCAL=As9d!f4$sd5fJHU

IMAGES_FOLDER=images/charity_stream/
SOUNDS_FOLDER=sounds/charity_stream/
BLOB_URL_LOCAL=https://stockagehelloassopreprod.blob.core.windows.net/
BLOB_URL_DEV=https://stockagehelloassopreprod.blob.core.windows.net/
BLOB_URL_RC=https://stockagehelloassopreprod.blob.core.windows.net/
BLOB_URL_PROD=https://stockagehelloassoprod.blob.core.windows.net/
BLOB_URL_SANDBOX=https://stockagehelloassoprod.blob.core.windows.net/
46 changes: 30 additions & 16 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
<?php

require 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
$dotenv->load();

// Déterminer l'environnement actuel
$environment = $_ENV['ENVIRONMENT'];
$isLocal = $environment == "LOCAL";

// Configurer les paramètres de connexion en fonction de l'environnement
$host = $isLocal ? $_ENV['DBURL_LOCAL'] : $_ENV['DBURL'];
$dbname = $isLocal ? $_ENV['DBNAME_LOCAL'] : $_ENV['DBNAME'];
$user = $isLocal ? $_ENV['DBUSER_LOCAL'] : $_ENV['DBUSER'];
$password = $isLocal ? $_ENV['DBPASSWORD_LOCAL'] : $_ENV['DBPASSWORD'];

$blob_url = $_ENV['BLOB_URL_' . $environment];
$blob_images_folder = $_ENV['IMAGES_FOLDER'];
$blob_sounds_folder = $_ENV['SOUNDS_FOLDER'];

$ADMIN_USER = $_ENV['ADMIN_USER'];
$ADMIN_PASSWORD = $_ENV['ADMIN_PASSWORD'];
// Options de connexion
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];

$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_SSL_CA => '',
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
);
try {
$db = new PDO(
"mysql:host=$host;dbname=$dbname;charset=utf8mb4",
$user,
$password,
$options
);
} catch (PDOException $e) {
echo "Erreur de connexion : " . $e->getMessage();
}

$db = new PDO(
'mysql:host=' . $_ENV['DBURL'] . ';dbname=' . $_ENV['DBNAME'] . ';charset=utf8mb4',
$_ENV['DBUSER'],
$_ENV['DBPASSWORD'] ?? null,
$options
);

?>
86 changes: 86 additions & 0 deletions db_helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

function GetCharityStreamsList($db) {
// Requête pour récupérer les charity streams
$stmt = $db->query('SELECT id, owner_email, title, guid FROM charity_stream');
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

function GetDonationGoalWidgetByGuid($db, $guidBinary) {
$stmt = $db->prepare('SELECT * FROM widget_donation_goal_bar WHERE charity_stream_guid = ? LIMIT 1');
$stmt->execute([$guidBinary]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}

function GetAlertBoxWidgetByGuid($db, $guidBinary) {
$stmt = $db->prepare('SELECT * FROM widget_alert_box WHERE charity_stream_guid = ? LIMIT 1');
$stmt->execute([$guidBinary]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
function UpdateDonationGoalWidget($db, $guidBinary, $data) {
$stmt = $db->prepare('
UPDATE widget_donation_goal_bar
SET text_color = ?, bar_color = ?, background_color = ?, goal = ?, last_update = CURRENT_TIMESTAMP(6)
WHERE charity_stream_guid = ?
');
$stmt->execute([
$data['text_color'],
$data['bar_color'],
$data['background_color'],
$data['goal'],
$guidBinary
]);
}

function UpdateAlertBoxWidget($db, $guidBinary, $data) {
$stmt = $db->prepare('
UPDATE widget_alert_box
SET image = ?, alert_duration = ?, message_template = ?, sound = ?, sound_volume = ?, last_update = CURRENT_TIMESTAMP(6)
WHERE charity_stream_guid = ?
');
$stmt->execute([
$data['image'],
$data['alert_duration'],
$data['message_template'],
$data['sound'],
$data['sound_volume'],
$guidBinary
]);
}

function CreateCharityStream($db, $guid, $owner_email, $form_id, $title, $creation_date, $last_update) {
// Insérer le nouveau Charity Stream
$query = 'INSERT INTO charity_stream (guid, owner_email, form_id, title, state, creation_date, last_update)
VALUES (:guid, :owner_email, :form_id, :title, 1, :creation_date, :last_update)';
$stmt = $db->prepare($query);
$stmt->execute([
':guid' => hex2bin($guid),
':owner_email' => $owner_email,
':form_id' => $form_id,
':title' => $title,
':creation_date' => $creation_date,
':last_update' => $last_update,
]);

// Insérer un widget_donation_goal_bar associé avec des valeurs par défaut
$query = 'INSERT INTO widget_donation_goal_bar (charity_stream_guid, goal, text_color, bar_color, background_color, creation_date, last_update)
VALUES (:guid, 1000, "#FFFFFF", "#FF0000", "#000000", :creation_date, :last_update)';
$stmt = $db->prepare($query);
$stmt->execute([
':guid' => hex2bin($guid),
':creation_date' => $creation_date,
':last_update' => $last_update,
]);

// Insérer un widget_alert_box associé avec des valeurs par défaut
$query = 'INSERT INTO widget_alert_box (charity_stream_guid, image, alert_duration, message_template, sound, sound_volume, creation_date, last_update)
VALUES (:guid, "default_image.gif", 5, "{name} donated {amount} via Twitch Charity", "default_sound.mp3", 50, :creation_date, :last_update)';
$stmt = $db->prepare($query);
$stmt->execute([
':guid' => hex2bin($guid),
':creation_date' => $creation_date,
':last_update' => $last_update,
]);
}


84 changes: 83 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1 +1,83 @@
<?php require 'config.php' ?>
<?php
require 'config.php';
require 'db_helpers.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_charity_stream'])) {
$owner_email = $_POST['owner_email'];
$form_id = $_POST['form_id'];
$title = $_POST['title'];

$guid = bin2hex(random_bytes(16)); // Génère un GUID
$creation_date = date('Y-m-d H:i:s');
$last_update = $creation_date;

CreateCharityStream($db, $guid, $owner_email, $form_id, $title, $creation_date, $last_update);

// Redirection ou autre action après la création
header("Location: index.php");
exit();
}

// Utilisation de la fonction GetCharityStreamsList pour récupérer les données
$charityStreams = GetCharityStreamsList($db);
?>

<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Administration des Charity Streams</title>
<link href="node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body class="bg-light">
<div class="container">
<h1 class="my-4 text-center">Administration des Charity Streams</h1>

<!-- Formulaire de création de Charity Stream -->
<div class="my-4 p-4 bg-white rounded shadow-sm">
<h3>Créer un nouveau Charity Stream</h3>
<form method="POST">
<div class="mb-3">
<label for="owner_email" class="form-label">Owner Email</label>
<input type="email" class="form-control" id="owner_email" name="owner_email" required>
</div>
<div class="mb-3">
<label for="form_id" class="form-label">Form ID</label>
<input type="text" class="form-control" id="form_id" name="form_id" required>
</div>
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<button type="submit" class="btn btn-success" name="create_charity_stream">Créer Charity Stream</button>
</form>
</div>

<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>Owner Email</th>
<th>Title</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($charityStreams as $stream): ?>
<tr>
<td><?php echo htmlspecialchars($stream['id']); ?></td>
<td><?php echo htmlspecialchars($stream['owner_email']); ?></td>
<td><?php echo htmlspecialchars($stream['title']); ?></td>
<td>
<a href="widget_edit.php?charity_stream_id=<?php echo bin2hex($stream['guid']); ?>" class="btn btn-primary">Edit Widgets</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</body>
</html>

15 changes: 15 additions & 0 deletions migration/create_table_charity_stream.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE charity_stream (
id INT AUTO_INCREMENT PRIMARY KEY,
guid BINARY(16) NOT NULL UNIQUE, -- UUID en format binaire
title VARCHAR(500), -- Titre avec une longueur maximale de 500 caractères
owner_email VARCHAR(255) NOT NULL, -- Email de l'utilisateur
form_id VARCHAR(255) NOT NULL, -- Identifiant du formulaire
state TINYINT(1) NOT NULL, -- État comme un entier (1 pour Enabled, 0 pour Disabled)
creation_date TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), -- Date de création avec précision à la microseconde
last_update TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) NOT NULL -- Date de dernière mise à jour avec précision à la microseconde
);

-- Index pour optimiser les requêtes sur certaines colonnes
CREATE INDEX idx_owner_email ON charity_stream(owner_email);
CREATE INDEX idx_form_id ON charity_stream(form_id);
CREATE INDEX idx_state ON charity_stream(state);
15 changes: 15 additions & 0 deletions migration/create_table_widget_alert_box.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE widget_alert_box (
id INT AUTO_INCREMENT PRIMARY KEY, -- Identifiant unique, auto-incrémenté
charity_stream_guid BINARY(16) NOT NULL, -- UUID en format binaire, pour faire référence à `charity_stream`
image VARCHAR(255) NOT NULL, -- URL ou chemin vers l'image
alert_duration INT NOT NULL, -- Durée de l'alerte en secondes
message_template TEXT NOT NULL, -- Modèle de message, texte libre
sound VARCHAR(255) NOT NULL, -- Chemin ou nom du fichier son
sound_volume INT NOT NULL, -- Volume du son (0 à 100)
creation_date TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), -- Date de création avec précision à la microseconde
last_update TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) NOT NULL, -- Date de dernière mise à jour avec précision à la microseconde
CONSTRAINT fk_charity_stream_guid_alert_box FOREIGN KEY (charity_stream_guid) REFERENCES charity_stream(guid) -- Clé étrangère renommée pour éviter le conflit
);

CREATE INDEX idx_charity_stream_guid ON widget_alert_box(charity_stream_guid);
CREATE INDEX idx_alert_duration ON widget_alert_box(alert_duration);
15 changes: 15 additions & 0 deletions migration/create_table_widget_donation_goal_bar.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE widget_donation_goal_bar (
id INT AUTO_INCREMENT PRIMARY KEY, -- Identifiant unique, auto-incrémenté
charity_stream_guid BINARY(16) NOT NULL, -- UUID en format binary, pour faire référence à `charity_stream`
text_color CHAR(7) NOT NULL, -- Code couleur en format hexadécimal (#RRGGBB)
bar_color CHAR(7) NOT NULL, -- Code couleur en format hexadécimal (#RRGGBB)
background_color CHAR(7) NOT NULL, -- Code couleur en format hexadécimal (#RRGGBB)
goal INT NOT NULL, -- Objectif de dons, représenté par un entier
creation_date TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), -- Date de création avec précision à la microseconde
last_update TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) NOT NULL, -- Date de dernière mise à jour avec précision à la microseconde
CONSTRAINT fk_charity_stream_guid_donation_goal_bar FOREIGN KEY (charity_stream_guid) REFERENCES charity_stream(guid)
);


CREATE INDEX idx_charity_stream_id ON widget_donation_goal_bar(charity_stream_guid);
CREATE INDEX idx_goal ON widget_donation_goal_bar(goal);
35 changes: 35 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions node_modules/@popperjs/core/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e1ab7b4

Please sign in to comment.