Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed non static method call & Added query string support #67

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions application/Controller/SongsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@

class SongsController
{

/** @var object Query String (Key: Value pairs)
* Example: /Controller/action/?artist=Singer who Sings&track=Song of Awesomeness&link=http://example.com
*/
private $query_string = null;

/**
* Class Constructor
* Accepts a $query_string object as a parameter or if none
* defaults it to null. Then assigns the object to the private
* $query_string variable.
*/
function __construct($query_string = null)
{
$this->query_string = $query_string;
}

/**
* PAGE: index
* This method handles what happens when you move to http://yourproject/songs/index
Expand Down Expand Up @@ -58,6 +75,24 @@ public function addSong()
header('location: ' . URL . 'songs/index');
}

/**
* ACTION: addSongFromQueryString
* This method handles what happens when you move to http://yourproject/songs/addsongFromQueryString
* IMPORTANT: This is not a normal page, it's an ACTION. This is where the "add a song (Using Ajax with Query String)" form on songs/index
* directs the ajax request. This method handles all the POST data from the query string
* This is an example of how to handle a POST request via JavaScript using Query Strings.
*/
public function addSongFromQueryString()
{
// if we have POST data to create a new song entry
if (!empty((array)$this->query_string)) {
// Instance new Model (Song)
$Song = new Song();
// do addSong() in model/model.php
$Song->addSong($this->query_string->artist, $this->query_string->track, $this->query_string->link);
}
}

/**
* ACTION: deleteSong
* This method handles what happens when you move to http://yourproject/songs/deletesong
Expand Down
28 changes: 27 additions & 1 deletion application/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/** For more info about namespaces plase @see http://php.net/manual/en/language.namespaces.importing.php */
namespace Mini\Core;

require APP . 'Core/CoreFunctions.php';

class Application
{
/** @var null The controller */
Expand All @@ -13,6 +15,11 @@ class Application
/** @var array URL parameters */
private $url_params = array();

/** @var object Query String (Key: Value pairs)
* Example: \Controller\action\?artist=Singer&track=Song&link=http://example.com
*/
private $query_string = null;

/**
* "Start" the application:
* Analyze the URL elements and calls the according controller/method or the fallback
Expand All @@ -34,7 +41,10 @@ public function __construct()
// if so, then load this file and create this controller
// like \Mini\Controller\CarController
$controller = "\\Mini\\Controller\\" . ucfirst($this->url_controller) . 'Controller';
$this->url_controller = new $controller();

// Construct the requested controller and pass in the $query_string variable as a parameter
// to the class constructor.
$this->url_controller = new $controller($this->query_string);

// check for method: does such a method exist in the controller ?
if (method_exists($this->url_controller, $this->url_action)) {
Expand Down Expand Up @@ -84,6 +94,22 @@ private function splitUrl()
// Rebase array keys and store the URL params
$this->url_params = array_values($url);

if (isset($_SERVER['QUERY_STRING'])) {
// Parse the query string to retreive an array containing
// the URL and query string and assign it to a variable.
parse_str($_SERVER['QUERY_STRING'], $data);

// Remove the URL index from the array.
unset($data['url']);

// If the array is empty, no query string is present.
// Otherwise convert the array to an object and assign
// it to the $query_string variable.
if (!empty($data)) {
$this->query_string = (object) $data;
}
}

// for debugging. uncomment this if you have problems with the URL
//echo 'Controller: ' . $this->url_controller . '<br>';
//echo 'Action: ' . $this->url_action . '<br>';
Expand Down
16 changes: 16 additions & 0 deletions application/Core/CoreFunctions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Mini\Controller;

function view($path, $data = [])
{
foreach ($data as $k => $v) {
$$k = $v;
}
require APP . "view/{$path}";
}


function redirect($path) {
header('location: ' . URL . $path);
}
9 changes: 8 additions & 1 deletion application/Core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Model
function __construct()
{
try {
self::openDatabaseConnection();
$this->openDatabaseConnection();
} catch (\PDOException $e) {
exit('Database connection could not be established.');
}
Expand All @@ -34,6 +34,13 @@ private function openDatabaseConnection()
// @see http://www.php.net/manual/en/pdostatement.fetch.php
$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);

// setting the encoding is different when using PostgreSQL
if (DB_TYPE == "pgsql") {
$databaseEncodingenc = " options='--client_encoding=" . DB_CHARSET . "'";
} else {
$databaseEncodingenc = "; charset=" . DB_CHARSET;
}

// generate a database connection, using the PDO connector
// @see http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
$this->db = new PDO(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=' . DB_CHARSET, DB_USER, DB_PASS, $options);
Expand Down
10 changes: 10 additions & 0 deletions application/view/songs/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
<input type="text" name="link" value="" />
<input type="submit" name="submit_add_song" value="Submit" />
</form>
<h3>Add a song (Using Ajax with Query String)</h3>
<form>
<label>Artist</label>
<input type="text" name="artist" id="artist" value="" required />
<label>Track</label>
<input type="text" name="track" id="track" value="" required />
<label>Link</label>
<input type="text" name="link" id="link" value="" />
<button type="button" name="submit_add_song_button" id="submit_add_song_button">Submit</button>
</form>
</div>
<!-- main content output -->
<div class="box">
Expand Down
25 changes: 25 additions & 0 deletions public/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,29 @@ $(function() {
});
}


// if #submit_add_song_button exists
if ($('#submit_add_song_button').length !== 0) {

$('#submit_add_song_button').on('click', () => {
let artist = document.getElementById('artist')
let track = document.getElementById('track')
let link = document.getElementById('link')

// send an ajax-request to this URL: current-server.com/songs/ajaxGetStats
// "url" is defined in views/_templates/footer.php
$.ajax({
url: `${url}/songs/addSongFromQueryString/?artist=${artist.value}&track=${track.value}&link=${link.value}`,
success: () => {
console.log('Ajax request succeeded.')

// Reload the page on success
window.location.reload()
},
fail: () => {
console.log('Some error handling.')
}
})
});
}
});