Add PDO-activity for reading events from database.
This commit is contained in:
parent
2949aba2bb
commit
72b3a1f535
@ -15,30 +15,29 @@ class Agenda {
|
||||
const THIS_MONTH = 3;
|
||||
const NEXT_MONTH = 4;
|
||||
|
||||
public function __construct() {
|
||||
$this->activities = [
|
||||
new NerdepitsaActivity,
|
||||
new AnimekveldActivity,
|
||||
];
|
||||
public function __construct($activities) {
|
||||
$this->activities = $activities;
|
||||
}
|
||||
|
||||
public function getEventsBetween(DateTimeImmutable $from, DateTimeImmutable $to) {
|
||||
$results = [[], []];
|
||||
do {
|
||||
$run = false;
|
||||
for($i = 0; $i < sizeof($this->activities); $i++) {
|
||||
if (sizeof($results[$i])) {
|
||||
$date = end($results[$i])->getStop();
|
||||
$results = [];
|
||||
for($i = 0; $i < sizeof($this->activities); $i++) {
|
||||
$result = [];
|
||||
do {
|
||||
$run = false;
|
||||
if (sizeof($result)) {
|
||||
$date = end($result)->getStop();
|
||||
} else {
|
||||
$date = $from;
|
||||
}
|
||||
$next = $this->activities[$i]->getNextEventFrom($date);
|
||||
if (isset($next) && $next->getStart() < $to) {
|
||||
$results[$i][] = $this->activities[$i]->getNextEventFrom($date);
|
||||
$result[] = $this->activities[$i]->getNextEventFrom($date);
|
||||
$run = true;
|
||||
}
|
||||
}
|
||||
} while ($run);
|
||||
} while ($run);
|
||||
$results[] = $result;
|
||||
}
|
||||
$result = [];
|
||||
foreach($results as $a) foreach($a as $b) $result[] = $b;
|
||||
usort($result, function($a, $b) {
|
||||
|
66
src/pvv/side/dbactivity.php
Normal file
66
src/pvv/side/dbactivity.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace pvv\side;
|
||||
|
||||
use \DateTimeImmutable;
|
||||
use \PDO;
|
||||
|
||||
class DBActivity implements RepeatingActivity {
|
||||
|
||||
public function __construct($dsn, $username, $password) {
|
||||
$this->pdo = new PDO($dsn, $username, $password);
|
||||
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
public function getNextEventFrom(DateTimeImmutable $date) {
|
||||
$query = 'SELECT name,start,stop,organiser,location FROM events WHERE start > :date ORDER BY start ASC LIMIT 1';
|
||||
return $this->retrieve($date, $query);
|
||||
}
|
||||
|
||||
public function getPreviousEventFrom(DateTimeImmutable $date) {
|
||||
$query = 'SELECT name,start,stop,organiser,location FROM events WHERE start < :date ORDER BY start DESC LIMIT 1';
|
||||
return $this->retrieve($date, $query);
|
||||
}
|
||||
|
||||
private function retrieve($date, $query) {
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
$stmt->execute(['date' => $date->format('Y-m-d H:i:s')]);
|
||||
if ($result = $stmt->fetch(PDO::FETCH_ASSOC)){
|
||||
$ev = new OnceEvent(
|
||||
$result['name'],
|
||||
DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $result['start']),
|
||||
DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $result['stop']),
|
||||
$result['organiser'],
|
||||
$result['location']
|
||||
);
|
||||
return $ev;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getName() /* : string */ {
|
||||
return "Database";
|
||||
}
|
||||
|
||||
public function getLocation() /* : Location */ {
|
||||
return "Location";
|
||||
}
|
||||
|
||||
public function getOrganiser() /* : User */ {
|
||||
return "User";
|
||||
}
|
||||
|
||||
/*
|
||||
public function getAllEvents(){
|
||||
global $url, $user,$pass,$db;
|
||||
$events = array();
|
||||
$mysqli = new mysqli($url,$user,$pass,$db);
|
||||
$result = $mysqli->query("SELECT name,start,stop,organiser,location FROM events");
|
||||
while($row = $result->fetch_assoc()){
|
||||
$ev = new OnceEvent($row['name'],$row['start'],$row['stop'],$row['organiser'],$row['location']);
|
||||
array_push($events,$ev);
|
||||
}
|
||||
#array_sort($events);
|
||||
return $events;
|
||||
}
|
||||
*/
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace pvv\side;
|
||||
|
||||
Class OnceEvent implements Event {
|
||||
Class OnceEvent extends Event {
|
||||
private $name, $start, $end, $org, $loc;
|
||||
public function __construct($name,$start,$end,$org, $loc){
|
||||
public function __construct($name,\DateTimeImmutable $start,\DateTimeImmutable $end,$org, $loc){
|
||||
$this->name = $name;
|
||||
$this->start = $start;
|
||||
$this->end = $end;
|
||||
|
@ -1,4 +1,8 @@
|
||||
<?php require '../src/_autoload.php'; date_default_timezone_set('Europe/Oslo') ?><!DOCTYPE html>
|
||||
<?php
|
||||
require '../src/_autoload.php';
|
||||
date_default_timezone_set('Europe/Oslo');
|
||||
//include __DIR__.'/../sql_config.php';
|
||||
?><!DOCTYPE html>
|
||||
<html lang="no">
|
||||
<title>Programvareverkstedet</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
@ -40,7 +44,12 @@
|
||||
<?php $translation = ['i dag', 'i morgen', 'denne uka', 'denne måned', 'neste måned'] ?>
|
||||
<?php $counter1 = 0; ?>
|
||||
<?php $counter2 = 0; ?>
|
||||
<?php foreach((new \pvv\side\Agenda())->getNextDays() as $period => $events) if ($events && $counter1 < 2 && $counter2 < 10) { $counter1++ ?>
|
||||
<?php $agenda = new \pvv\side\Agenda([
|
||||
new \pvv\side\social\NerdepitsaActivity,
|
||||
new \pvv\side\social\AnimekveldActivity,
|
||||
new \pvv\side\DBActivity('sqlite:../pvv.sqlite', null, null),
|
||||
]); ?>
|
||||
<?php foreach($agenda->getNextDays() as $period => $events) if ($events && $counter1 < 2 && $counter2 < 10) { $counter1++ ?>
|
||||
<li><p><?= $translation[$period] ?> <span><?= reset($events)->getStart()->format('Y-m-d'); ?></span></p>
|
||||
<ul>
|
||||
<?php foreach($events as $event) { $counter2++ ?>
|
||||
|
Loading…
Reference in New Issue
Block a user