Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/recipe-fiddle.git
<?php
namespace com\whitemagicsoftware;

require "constants.php";
require "class.BaseController.php";

/**
 * Allows the user to create a grocery list from recipes.
 */
class GroceryList extends BaseController {
  /* Set when the user clicks on a recipe. */
  private $recipeId;

  function __construct() {
    parent::__construct();
  }

  /**
   * Unused.
   *
   * @return 0 No last resort ID required.
   */
  protected function getLastResortId() {
    return 0;
  }

  /**
   * Indicates whether or not a new object should be created.
   *
   * @return true
   */
  protected function exists( $id ) {
    return true;
  }

  /**
   * Returns the grocery-list identifier.
   *
   * @return "grocery-list-id"
   */
  protected function getParameterIdName() {
    return "grocery-list-id";
  }

  /**
   * Returns the name of the function to call to authorize the user
   * for editing grocery lists.
   */
  protected function getAuthorizationFunctionName() {
    return "is_authorized_grocery_list";
  }

  /**
   * Associates a set of ingredients with a grocery list ID.
   */
  private function insertIngredients() {
    $ingredients = $this->getParameter( "ingredients" );

    if( isset( $ingredients ) ) {
      $ingredients = $this->arrayToString( $ingredients );
      $id = $this->getId();

      $this->call( "grocery_list_upsert", "", $id, $ingredients );
    }
  }

  /**
   * Deletes all ingredients with the name corresponding to the user's
   * ingredient selection.
   */
  private function deleteIngredient() {
    $this->call( "grocery_list_ingredient_delete", "",
      $this->getId(),
      $this->getParameterId( "delete_ingredient" ) );
  }

  /**
   * Changes the ID of the recipe that has its ingredients listed. The
   * user can then pick the ingredients.
   */
  private function selectRecipe() {
    $this->setRecipeId( $this->getParameterId( "recipe-id" ) );
  }

  /**
   * Returns XML for editing grocery lists.
   *
   * @return An XML document containing data used for grocery list editing.
   */
  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;
  }

  /**
   * Returns the stylesheet used for marking up the XML returned by
   * getXml().
   *
   * @return "xsl/grocery.xsl"
   */
  private function getStylesheetName() {
    return "xsl/grocery.xsl";
  }

  /**
   * Returns an XHTML version of the XML specified by $this->getXml().
   *
   * @see getXml
   * @return An XHTML document.
   */
  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;
  }

  /**
   * Executes database transactions related to the supplied commands.
   */
  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();
  }
}