-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
875 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE DATABASE IF NOT EXISTS `mini`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE `mini`.`song` ( | ||
`id` int(11) NOT NULL AUTO_INCREMENT, | ||
`artist` text COLLATE utf8_unicode_ci NOT NULL, | ||
`track` text COLLATE utf8_unicode_ci NOT NULL, | ||
`link` text COLLATE utf8_unicode_ci, | ||
PRIMARY KEY (`id`), | ||
UNIQUE KEY `id` (`id`) | ||
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
INSERT INTO `mini`.`song` (`id`, `artist`, `track`, `link`) VALUES | ||
(1, 'Dena', 'Cash, Diamond Ring, Swimming Pools', 'http://www.youtube.com/watch?v=r4CDc9yCAqE'), | ||
(2, 'Jessy Lanza', 'Kathy Lee', 'http://vimeo.com/73455369'), | ||
(3, 'The Orwells', 'In my Bed (live)', 'http://www.youtube.com/watch?v=8tA_2qCGnmE'), | ||
(4, 'L''Orange & Stik Figa', 'Smoke Rings', 'https://www.youtube.com/watch?v=Q5teohMyGEY'), | ||
(5, 'Labyrinth Ear', 'Navy Light', 'http://www.youtube.com/watch?v=a9qKkG7NDu0'), | ||
(6, 'Bon Hiver', 'Wolves (Kill them with Colour Remix)', 'http://www.youtube.com/watch?v=5GXAL5mzmyw'), | ||
(7, 'Detachments', 'Circles (Martyn Remix)', 'http://www.youtube.com/watch?v=UzS7Gvn7jJ0'), | ||
(8, 'Dillon & Dirk von Loetzow', 'Tip Tapping (Live at ZDF Aufnahmezustand)', 'https://www.youtube.com/watch?v=hbrOLsgu000'), | ||
(9, 'Dillon', 'Contact Us (Live at ZDF Aufnahmezustand)', 'https://www.youtube.com/watch?v=E6WqTL2Up3Y'), | ||
(10, 'Tricky', 'Hey Love (Promo Edit)', 'http://www.youtube.com/watch?v=OIsCGdW49OQ'), | ||
(11, 'Compuphonic', 'Sunset feat. Marques Toliver (DJ T. Remix)', 'http://www.youtube.com/watch?v=Ue5ZWSK9r00'), | ||
(12, 'Ludovico Einaudi', 'Divenire (live @ Royal Albert Hall London)', 'http://www.youtube.com/watch?v=X1DRDcGlSsE'), | ||
(13, 'Maxxi Soundsystem', 'Regrets we have no use for (Radio1 Rip)', 'https://soundcloud.com/maxxisoundsystem/maxxi-soundsystem-ft-name-one'), | ||
(14, 'Beirut', 'Nantes (Fredo & Thang Remix)', 'https://www.youtube.com/watch?v=ojV3oMAgGgU'), | ||
(15, 'Buku', 'All Deez', 'http://www.youtube.com/watch?v=R0bN9JRIqig'), | ||
(16, 'Pilocka Krach', 'Wild Pete', 'http://www.youtube.com/watch?v=4wChP_BEJ4s'), | ||
(17, 'Mount Kimbie', 'Here to stray (live at Pitchfork Music Festival Paris)', 'http://www.youtube.com/watch?v=jecgI-zEgIg'), | ||
(18, 'Kool Savas', 'King of Rap (2012) / Ein Wunder', 'http://www.youtube.com/watch?v=mTqc6UTG1eY&hd=1'), | ||
(19, 'Chaim feat. Meital De Razon', 'Love Rehab (Original Mix)', 'http://www.youtube.com/watch?v=MJT1BbNFiGs'), | ||
(20, 'Emika', 'Searching', 'http://www.youtube.com/watch?v=oscuSiHfbwo'), | ||
(21, 'Emika', 'Sing to me', 'http://www.youtube.com/watch?v=k9sDBZm8pgk'), | ||
(22, 'George Fitzgerald', 'Thinking of You', 'http://www.youtube.com/watch?v=-14B8l49iKA'), | ||
(23, 'Disclosure', 'You & Me (Flume Edit)', 'http://www.youtube.com/watch?v=OUkkaqSNduU'), | ||
(24, 'Crystal Castles', 'Doe Deer', 'http://www.youtube.com/watch?v=zop0sWrKJnQ'), | ||
(25, 'Tok Tok vs. Soffy O.', 'Missy Queens Gonna Die', 'http://www.youtube.com/watch?v=EN0Tnw5zy6w'), | ||
(26, 'Fink', 'Maker (Synapson Remix)', 'http://www.youtube.com/watch?v=Dyd-cUkj4Nk'), | ||
(27, 'Flight Facilities (ft. Christine Hoberg)', 'Clair De Lune', 'http://www.youtube.com/watch?v=Jcu1AHaTchM'), | ||
(28, 'Karmon', 'Turning Point (Original Mix)', 'https://www.youtube.com/watch?v=-tB-zyLSPEo'), | ||
(29, 'Shuttle Life', 'The Birds', 'http://www.youtube.com/watch?v=-I3m3cWDEtM'), | ||
(30, 'Santé', 'Homegirl (Rampa Mix)', 'http://www.youtube.com/watch?v=fnhMNOWxLYw'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace Mini\Model; | ||
|
||
use PDO; | ||
|
||
class Model | ||
{ | ||
/** | ||
* The database connection | ||
* @var PDO | ||
*/ | ||
private $db; | ||
|
||
/** | ||
* When creating the model, the configs for database connection creation are needed | ||
* @param $config | ||
*/ | ||
function __construct($config) | ||
{ | ||
// PDO db connection statement preparation | ||
$dsn = 'mysql:host=' . $config['db_host'] . ';dbname=' . $config['db_name'] . ';port=' . $config['db_port']; | ||
|
||
// note the PDO::FETCH_OBJ, returning object ($result->id) instead of array ($result["id"]) | ||
// @see http://php.net/manual/de/pdo.construct.php | ||
$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING); | ||
|
||
// create new PDO db connection | ||
$this->db = new PDO($dsn, $config['db_user'], $config['db_pass'], $options); | ||
} | ||
|
||
public function getAmountOfSongs() | ||
{ | ||
$sql = "SELECT COUNT(id) AS amount_of_songs FROM song"; | ||
$query = $this->db->prepare($sql); | ||
$query->execute(); | ||
|
||
return $query->fetch()->amount_of_songs; | ||
} | ||
|
||
/** | ||
* Get all songs from database | ||
*/ | ||
public function getAllSongs() | ||
{ | ||
$sql = "SELECT id, artist, track, link FROM song"; | ||
$query = $this->db->prepare($sql); | ||
$query->execute(); | ||
// fetchAll() is the PDO method that gets all result rows, here in object-style because we defined this in | ||
// core/controller.php! If you prefer to get an associative array as the result, then do | ||
// $query->fetchAll(PDO::FETCH_ASSOC); or change core/controller.php's PDO options to | ||
// $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ... | ||
return $query->fetchAll(); | ||
} | ||
|
||
/** | ||
* Add a song to database | ||
* Please note that it's not necessary to "clean" our input in any way. With PDO all input is escaped properly | ||
* automatically. We also don't use strip_tags() etc. here so we keep the input 100% original (so it's possible | ||
* to save HTML and JS to the database, which is a valid use case). Data will only be cleaned when putting it out | ||
* in the views (see the views for more info). | ||
* @param string $artist Artist | ||
* @param string $track Track | ||
* @param string $link Link | ||
*/ | ||
public function addSong($artist, $track, $link) | ||
{ | ||
$sql = "INSERT INTO song (artist, track, link) VALUES (:artist, :track, :link)"; | ||
$query = $this->db->prepare($sql); | ||
$parameters = array(':artist' => $artist, ':track' => $track, ':link' => $link); | ||
// useful for debugging: you can see the SQL behind above construction by using: | ||
// echo '[ PDO DEBUG ]: ' . \PdoDebugger::show($sql, $parameters); exit(); | ||
$query->execute($parameters); | ||
} | ||
|
||
/** | ||
* Delete a song in the database | ||
* Please note: this is just an example! In a real application you would not simply let everybody | ||
* add/update/delete stuff! | ||
* @param int $song_id Id of song | ||
*/ | ||
public function deleteSong($song_id) | ||
{ | ||
$sql = "DELETE FROM song WHERE id = :song_id"; | ||
$query = $this->db->prepare($sql); | ||
$parameters = array(':song_id' => $song_id); | ||
// useful for debugging: you can see the SQL behind above construction by using: | ||
// echo '[ PDO DEBUG ]: ' . \PdoDebugger::show($sql, $parameters); exit(); | ||
$query->execute($parameters); | ||
} | ||
|
||
|
||
/** | ||
* Get a song from database | ||
*/ | ||
public function getSong($song_id) | ||
{ | ||
$sql = "SELECT id, artist, track, link FROM song WHERE id = :song_id LIMIT 1"; | ||
$query = $this->db->prepare($sql); | ||
$parameters = array(':song_id' => $song_id); | ||
// useful for debugging: you can see the SQL behind above construction by using: | ||
// echo '[ PDO DEBUG ]: ' . \PdoDebugger::show($sql, $parameters); exit(); | ||
$query->execute($parameters); | ||
// fetch() is the PDO method that get exactly one result | ||
return $query->fetch(); | ||
} | ||
|
||
/** | ||
* Update a song in database | ||
* Please note that it's not necessary to "clean" our input in any way. With PDO all input is escaped properly | ||
* automatically. We also don't use strip_tags() etc. here so we keep the input 100% original (so it's possible | ||
* to save HTML and JS to the database, which is a valid use case). Data will only be cleaned when putting it out | ||
* in the views (see the views for more info). | ||
* @param string $artist Artist | ||
* @param string $track Track | ||
* @param string $link Link | ||
* @param int $song_id Id | ||
*/ | ||
public function updateSong($artist, $track, $link, $song_id) | ||
{ | ||
$sql = "UPDATE song SET artist = :artist, track = :track, link = :link WHERE id = :song_id"; | ||
$query = $this->db->prepare($sql); | ||
$parameters = array(':artist' => $artist, ':track' => $track, ':link' => $link, ':song_id' => $song_id); | ||
// useful for debugging: you can see the SQL behind above construction by using: | ||
// echo '[ PDO DEBUG ]: ' . \PdoDebugger::show($sql, $parameters); exit(); | ||
$query->execute($parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="description" content=""> | ||
<title>MINI 2</title> | ||
<!-- css --> | ||
<link href="/css/style.css" rel="stylesheet"> | ||
<link href='//fonts.googleapis.com/css?family=Oswald:400,300,700' rel='stylesheet' type='text/css'> | ||
<!-- jQuery --> | ||
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script> | ||
<!-- our JavaScript --> | ||
<script src="/js/application.js"></script> | ||
</head> | ||
<body> | ||
|
||
<!-- logo --> | ||
<div class="logo">MINI</div> | ||
|
||
<!-- navigation --> | ||
<div class="navigation"> | ||
<a href="/">home</a> | ||
<a href="/subpage">subpage</a> | ||
<a href="/subpage/deeper">subpage/deeper</a> | ||
<a href="/songs">songs</a> | ||
</div> | ||
|
||
<!-- main content, will be injected via Twig --> | ||
{% block content %} {% endblock %} | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends "_base.twig" %} | ||
{% block content %} | ||
<div class="container"> | ||
<h1>/</h1> | ||
<p> | ||
Everything inside this box comes from view/index.twig.<br/> | ||
Everything outside this box comes from view/_base.twig (which is the surrounding layout template). | ||
</p> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% extends "_base.twig" %} | ||
{% block content %} | ||
<div class="container"> | ||
<h1>Edit a song</h1> | ||
<!-- add song form --> | ||
<div> | ||
<form action="/songs/updatesong" method="POST"> | ||
<input autofocus type="text" name="artist" value="{{ song.artist }}" required /> | ||
<input type="text" name="track" value="{{ song.track }}" required /> | ||
<input type="text" name="link" value="{{ song.link }}" /> | ||
<input type="hidden" name="song_id" value="{{ song.id }}" /> | ||
<input type="submit" name="submit_update_song" value="Update" /> | ||
</form> | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{% extends "_base.twig" %} | ||
{% block content %} | ||
<div class="container"> | ||
<h1>/songs</h1> | ||
<div> | ||
Everything inside this box comes from view/songs.twig.<br/> | ||
Everything outside this box comes from view/_base.twig (which is the surrounding layout template). | ||
</div> | ||
<!-- add song form --> | ||
<div class="box"> | ||
<h2>Add a song</h2> | ||
<form action="/songs/addsong" method="POST"> | ||
<input type="text" name="artist" value="" placeholder="Artist" required /> | ||
<input type="text" name="track" value="" placeholder="Track" required /> | ||
<input type="text" name="link" value="" placeholder="Link" /> | ||
<input type="submit" name="submit_add_song" value="Add this" /> | ||
</form> | ||
</div> | ||
<!-- main content output --> | ||
<div class="box"> | ||
<h2>List of songs ({{ amount_of_songs }} items)</h2> | ||
<div> | ||
To fill this little demo application with some real data I've simply added (again) some tracks: | ||
Favourites of the last months, gems from my vinyl crate, tracks of friends and excellent trash too. :) | ||
</div> | ||
<div> | ||
<table> | ||
<thead> | ||
<tr> | ||
<td>Id</td> | ||
<td>Artist</td> | ||
<td>Track</td> | ||
<td>Link</td> | ||
<td>DELETE</td> | ||
<td>EDIT</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for song in songs %} | ||
<tr> | ||
<td>{{ song.id }}</td> | ||
<td>{{ song.artist }}</td> | ||
<td>{{ song.track }}</td> | ||
<td> | ||
<a href="{{ song.link }}">{{ song.link }}</a> | ||
</td> | ||
<td><a href="/songs/deletesong/{{ song.id }}">delete</a></td> | ||
<td><a href="/songs/editsong/{{ song.id }}">edit</a></td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
<div class="box"> | ||
<h3>Ajax demo</h3> | ||
<div> | ||
Click here to get the amount of songs via Ajax (will be displayed in #javascript-ajax-result-box): | ||
<button id="javascript-ajax-button">Get data via AJAX</button> | ||
<div id="javascript-ajax-result-box"></div> | ||
</div> | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends "_base.twig" %} | ||
{% block content %} | ||
<div class="container"> | ||
<h1>/subpage/deeper</h1> | ||
<p> | ||
Everything inside this box comes from view/subpage.deeper.twig.<br/> | ||
Everything outside this box comes from view/_base.twig (which is the surrounding layout template). | ||
</p> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends "_base.twig" %} | ||
{% block content %} | ||
<div class="container"> | ||
<h1>/subpage</h1> | ||
<p> | ||
Everything inside this box comes from view/subpage.twig.<br/> | ||
Everything outside this box comes from view/_base.twig (which is the surrounding layout template). | ||
</p> | ||
</div> | ||
{% endblock %} |
Oops, something went wrong.