<?php
require_once('myCookbook/Recipe.php');
require_once('config.php');
/**
* Gets a list of category types stored in the database.
*/
function mycookbook_get_categories( ) {
$query = "show fields from recipes";
$result = mycookbook_internal_run_query($query);
$row = mysql_fetch_array($result);
while ($row['Field'] != "category") {
$row = mysql_fetch_array($result);
}
// Extract a list of types from the enumerated "Types" field.
$enum = str_replace('enum(', '', $row['Type']);
$enum = ereg_replace('\\)$', '', $enum);
$enum = explode('\',\'', substr($enum, 1, -1));
sort($enum);
reset($enum);
return $enum;
}
function mycookbook_add_category($newCategory) {
if ($newCategory == "") {
return;
}
$categories = mycookbook_get_categories( );
$query = 'ALTER TABLE `recipes` CHANGE `category` `category` ENUM(';
foreach ($categories as $category) {
$query .= '\''.$category.'\',';
}
$query .= '\''.$newCategory.'\') NULL DEFAULT NULL';
mycookbook_internal_run_query($query);
}
/**
* Gets a list of cuisine types stored in the database.
*/
function mycookbook_get_cuisines( ) {
$query = "show fields from recipes";
$result = mycookbook_internal_run_query($query);
$row = mysql_fetch_array($result);
while ($row['Field'] != "cuisine") {
$row = mysql_fetch_array($result);
}
// Extract a list of types from the enumerated "Types" field.
$enum = str_replace('enum(', '', $row['Type']);
$enum = ereg_replace('\\)$', '', $enum);
$enum = explode('\',\'', substr($enum, 1, -1));
sort($enum);
reset($enum);
return $enum;
}
/**
* Gets the recipe of a particular name
*/
function mycookbook_get_recipe( $recipe_uid ) {
$query = 'select * from recipes where uid = '.$recipe_uid;
$result = mycookbook_internal_run_query($query);
$row = mysql_fetch_array($result);
return new Recipe($row);
}
/**
* Gets a random recipe
*/
function mycookbook_get_featured_recipe() {
$query = 'select * from recipes order by rand() limit 1';
$result = mycookbook_internal_run_query($query);
$row = mysql_fetch_array($result);
return new Recipe( $row );
}
function mycookbook_rate_recipe($uid, $rating) {
$query = 'UPDATE `recipes` SET `rating` = \''.$rating.'\' WHERE `uid` = '.$uid.' LIMIT 1';
mycookbook_internal_run_query($query);
}
/**
* Return an array of results from a search
*/
function mycookbook_do_search( $recipe_category, $recipe_title, $recipe_source, $recipe_contains, $recipe_serves ) {
$query = "select * from recipes";
if ( $recipe_category != "" )
$query .= " where category = '".$recipe_category."' and";
if ( $recipe_title != "" )
$query .= " where title like '%".$recipe_title."%' and";
if ( $recipe_source != "" )
$query .= " where source like '%".$recipe_source."%' and";
if ( $recipe_contains != "" )
$query .= " where ingredients like '%".$recipe_contains."%' and";
if ( $recipe_serves != "" )
$query .= " where servings = '".$recipe_serves."'";
if ( substr($query, -4) == " and" )
$query = substr($query, 0, -4);
$query .= " order by category";
$result = mycookbook_internal_run_query($query);
$num_rows = mysql_num_rows($result);
for ( $i = 0; $i < $num_rows; $i++ ) {
$row = mysql_fetch_array($result);
$search_results[$i] = $row;
}
return $search_results;
}
function mycookbook_add_recipe( $title, $servings, $category, $preptime, $cooktime, $cuisine, $rating, $source, $ingredients, $instructions ) {
$query = 'INSERT INTO `recipes` (`uid`, `title`, `servings`, `category`, `preptime`, `cooktime`, `cuisine`, `rating`, `source`, `ingredients`, `instructions`, `image`) VALUES (\'\', ';
$query .= '\''.$title.'\', ';
if ($servings != "") {
$query .= '\''.$servings.'\', ';
} else {
$query .= 'NULL, ';
}
if ($category != "") {
$query .= '\''.$category.'\', ';
} else {
$query .= 'NULL, ';
}
if ($preptime != "") {
$query .= '\''.$preptime.'\', ';
} else {
$query .= 'NULL, ';
}
if ($cooktime != "") {
$query .= '\''.$cooktime.'\', ';
} else {
$query .= 'NULL, ';
}
if ($cuisine != "") {
$query .= '\''.$cuisine.'\', ';
} else {
$query .= 'NULL, ';
}
if ($rating != "") {
$query .= '\''.$rating.'\', ';
} else {
$query .= 'NULL, ';
}
if ($source != "") {
$query .= '\''.$source.'\', ';
} else {
$query .= 'NULL, ';
}
$query .= '\''.$ingredients.'\', ';
$query .= '\''.$instructions.'\', ';
$query .= 'NULL'; // Image not implemented
$query .= ')';
mycookbook_internal_run_query($query);
}
/**
* Setup function that will install the database to a MySQL server
*/
function mycookbook_install_database() {
require_once('config.php');
if ($config_done == "no") {
return 'fail';
}
$query = 'CREATE TABLE IF NOT EXISTS `recipes` (';
$query .= '`uid` int(11) NOT NULL auto_increment,';
$query .= '`title` varchar(50) NOT NULL,';
$query .= '`servings` smallint(6) default NULL,';
$query .= '`category` enum(\'Entree\',\'Salad\',\'Dessert\',\'Soup\',\'Breakfast\') default NULL,';
$query .= '`preptime` varchar(20) default NULL,';
$query .= '`cooktime` varchar(20) default NULL,';
$query .= '`cuisine` enum(\'Asian\',\'Mexican\',\'American\',\'Italian\',\'Greek\',\'German\',\'French\',\'British\') default NULL,';
$query .= '`rating` tinyint(4) default NULL,';
$query .= '`source` varchar(50) default NULL,';
$query .= '`ingredients` text NOT NULL,';
$query .= '`instructions` text NOT NULL,';
$query .= '`image` binary(1) default NULL,';
$query .= 'PRIMARY KEY (`uid`)';
$query .= ') AUTO_INCREMENT=4 ;';
mycookbook_internal_run_query($query);
return 'ok';
}
/**
* INTERNAL method for the purpose of centralizing code
*/
function mycookbook_internal_run_query($query) {
require('config.php');
// Run the query to add a recipe
$connection = mysql_connect($sql_server, $sql_user, $sql_pass)
or die("Could not connect to server");
$db = mysql_select_db($sql_database, $connection)
or die("Could not select database");
$result = mysql_query($query, $connection)
or die("Query Failed: ".$query);
return $result;
}
?>