forked from vergnet/site-accueil-insa
115 lines
3.7 KiB
PHP
Executable file
115 lines
3.7 KiB
PHP
Executable file
<?php
|
|
|
|
class Dao
|
|
{
|
|
|
|
private $conn;
|
|
|
|
private $debug = false;
|
|
|
|
private function get_debug_mode () {
|
|
$this->debug = file_exists(__DIR__.DIRECTORY_SEPARATOR."../DEBUG");
|
|
}
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
$this->get_debug_mode();
|
|
if ($this->debug) {
|
|
$username = 'phpmyadmin';
|
|
$password = $this->read_password();;
|
|
$dsn = 'mysql:dbname=phpmyadmin;host=127.0.0.1';
|
|
} else {
|
|
$username = 'accueil_insa';
|
|
$password = $this->read_password();
|
|
$dsn = 'mysql:dbname=accueil_insa;host=127.0.0.1';
|
|
}
|
|
try {
|
|
$this->conn = new PDO($dsn, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
|
|
} catch (PDOException $e) {
|
|
echo $e;
|
|
}
|
|
}
|
|
|
|
private function read_password()
|
|
{
|
|
if ($this->debug)
|
|
$real_path = __DIR__.DIRECTORY_SEPARATOR.".htpassdb_debug";
|
|
else
|
|
$real_path = __DIR__.DIRECTORY_SEPARATOR.".htpassdb";
|
|
$file = fopen($real_path, "r") or die("Unable to open DB password file!");;
|
|
$password = fgets($file);
|
|
fclose($file);
|
|
return trim($password);
|
|
}
|
|
|
|
public function get_score_team($team)
|
|
{
|
|
$sql = 'SELECT text, points FROM scores WHERE team = ?';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$team]);
|
|
return $cursor->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function save_scores($scores_json, $team)
|
|
{
|
|
$sql = 'DELETE FROM scores WHERE team = ?';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$team]);
|
|
foreach ($scores_json as $value) {
|
|
$sql = 'INSERT INTO scores (text, points, team) VALUES (?, ?, ?)';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$value['text'], $value['points'], $team]);
|
|
}
|
|
}
|
|
|
|
public function get_map_info($selector)
|
|
{
|
|
$sql = 'SELECT title, description FROM map_insa WHERE selector = ?';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$selector]);
|
|
return $cursor->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function get_map_selectors()
|
|
{
|
|
$sql = 'SELECT selector FROM map_insa';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute();
|
|
return $cursor->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function save_map_info($selector, $info_json)
|
|
{
|
|
$sql = 'DELETE FROM map_insa WHERE selector = ?';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$selector]);
|
|
$sql = 'INSERT INTO map_insa (title, description, selector) VALUES (?, ?, ?)';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$info_json['title'], $info_json['description'], $selector]);
|
|
}
|
|
|
|
public function get_activities_of_day($day)
|
|
{
|
|
$sql = 'SELECT * FROM planning_insa WHERE day = ?';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$day]);
|
|
return $cursor->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
public function save_day_activities($day, $info_json)
|
|
{
|
|
$sql = 'DELETE FROM planning_insa WHERE day = ?';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$day]);
|
|
|
|
foreach ($info_json as $value) {
|
|
$sql = 'INSERT INTO planning_insa (day, small_title, full_title, description, color, start, length) VALUES (?, ?, ?, ?, ?, ?, ?)';
|
|
$cursor = $this->conn->prepare($sql);
|
|
$cursor->execute([$value['day'], $value['small_title'], $value['full_title'], $value['description'], $value['color'], $value['start'], $value['length']]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|