<?php
/**
 * Front controller for handling colours
 * @author Barry Parkin (https://github.com/barryparkin | http://ba.rrypark.in)
 * @package Bunches
 *
 * Requires:
 * .htaccess file with a RewriteBase and RewriteRule that include the directory
 * colourtemplate.inc.php file in this folder
 * pagenotfound.php file in folder above
 *
 */

/**
 * Get * from bunColours by name
 * @param string $name
 * @return string SQL query
 */
function getColourByName($name) {
    return sprintf("	SELECT *
						FROM bunColours
						WHERE Colour = '%s'
						LIMIT 1;",
            cleanDbInput($name)
    );
}

require_once("../includes/application.inc.php");


//find the directory we're currently in
$dirName = dirname(__FILE__);
$dirArray = explode('/', $dirName);
$type = array_pop($dirArray);

$name = $_SERVER['REQUEST_URI']; //grab request url
$name = explode('?', $name); // knock off any query parameters
$name = $name[0]; //use first part of request url
$name = str_ireplace('/'. $type . '/', "", $name); //remove this directory
$name = str_ireplace('-', " ", $name); //replace hyphens with spaces
$name = str_ireplace('.php', "", $name); //remove php extension


$query = getColourByName($name);
$data = processQuery($query);
if($data['numberOfRows'] > 0) {
    $colourid = $data['assocResults'][0]['ColourID'];
}

if($data['numberOfRows'] == 0) {
    //nothing found, 404 :¬)
    header('HTTP/1.1 404 Not Found');
    require('../pagenotfound.php');

} else {
    //productid found, display the product!
    require('colourtemplate.inc.php');
}