<?php
namespace com\whitemagicsoftware;
require "constants.php";
require "class.BaseController.php";
class GroceryList extends BaseController {
private $recipeId;
function __construct() {
parent::__construct();
}
protected function getLastResortId() {
return 0;
}
protected function exists( $id ) {
return true;
}
protected function getParameterIdName() {
return "grocery-list-id";
}
protected function getAuthorizationFunctionName() {
return "is_authorized_grocery_list";
}
private function insertIngredients() {
$ingredients = $this->getParameter( "ingredients" );
if( isset( $ingredients ) ) {
$ingredients = $this->arrayToString( $ingredients );
$id = $this->getId();
$this->call( "grocery_list_upsert", "", $id, $ingredients );
}
}
private function deleteIngredient() {
$this->call( "grocery_list_ingredient_delete", "",
$this->getId(),
$this->getParameterId( "delete_ingredient" ) );
}
private function selectRecipe() {
$this->setRecipeId( $this->getParameterId( "recipe-id" ) );
}
private function getXml() {
$recipes = $this->call( "generate_recipe_list_xml", "x",
$this->getAccountId() );
$ingredients = $this->call( "generate_ingredients_xml", "x",
$this->getRecipeId() );
$groceries = $this->call( "generate_grocery_list_xml", "x",
$this->getGroceryListId() );
$result = "<grocery>";
if( isset( $recipes[0] ) ) {
$result .= $recipes[0]["x"];
}
if( isset( $ingredients[0] ) ) {
$result .= $ingredients[0]["x"];
}
if( isset( $groceries[0] ) ) {
$result .= $groceries[0]["x"];
}
$result .= "</grocery>";
return $result;
}
private function getStylesheetName() {
return "xsl/grocery.xsl";
}
protected function getXhtml() {
$xslt = $this->getXsltEngine();
$xslt->setXml( $this->getXml() );
$xslt->setStylesheet( $this->getStylesheetName() );
$xslt->setParameter( "cookie", $this->getCookieToken() );
$xslt->setParameter( "recipe-id", $this->getRecipeId() );
$xslt->setParameter( "grocery-list-id", $this->getGroceryListId() );
return $xslt->transform();
}
private function setRecipeId( $recipeId ) {
$this->recipeId = $recipeId;
}
private function getRecipeId() {
return $this->recipeId;
}
private function getGroceryListId() {
return 1;
}
protected function handleRequest() {
$command = $this->getCommand();
$subcommand = $this->getSubCommand();
if( $command === "select" ) {
if( $subcommand === "recipe" ) {
$this->selectRecipe();
}
}
else if( $command === "insert" ) {
if( $subcommand === "ingredients" ) {
$this->insertIngredients();
}
}
else if( $command === "delete" ) {
if( $subcommand === "ingredient" ) {
$this->deleteIngredient();
}
}
$this->render();
}
}