Dave Jarvis' Repositories

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

require "constants.php";
require "class.Recipe.php";

/**
 * Allows the user to search for recipes, taking into consideration a variety
 * of aspects, including: potluck attendees, dietary preferences, etc.
 */
class Search extends BaseController {
  private $criteria;

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

  /**
   * Searches the site for recipes and returns an XML document with the
   * list of matching recipes.
   */
  private function search( $query ) {
    $comparator = $this->getParameter( "comparator" );
    $recipe = new Recipe();
    $decoded = json_decode( $recipe->parseIngredientText( $query ) );

    // Copied from Recipe::interpretIngredient( ... )
    // \todo Create reusable method.
    list( $min, $max ) = $recipe->extractIngredientMeasures( $decoded, 0 );
    $unit = $recipe->extractIngredientUnit( $decoded );
    $ingredient = $recipe->extractIngredientName( $decoded );

    //$this->log( "Search: $min - $max : $unit => $ingredient" );

    // Perform an exact search by default.
    if( !isset( $comparator ) ) {
      $comparator = "eq";
    }

    $this->setCriteria( array(
      "query" => $query,
      "compare" => $comparator,
      "min" => $min,
      "max" => $max,
      "unit" => $unit,
      "ingredient" => $ingredient )
    );
  }

  /**
   * Unused.
   * @param $id Unused
   */
  protected function exists( $id ) {
    return true;
  }

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

  /**
   * Unused?
   *
   * @return "search-id"
   */
  protected function getParameterIdName() {
    return "search-id";
  }

  /**
   * Probably shouldn't be called...
   */
  protected function getAuthorizationFunctionName() {
    return "";
  }

  /**
   * No authorization is necessary. This prevents the database call that
   * uses the return value from getAuthorizationFunctionName().
   */
  protected function authorize() {
    return false;
  }

  /**
   * Returns XML for searching.
   *
   * @return An XML document containing data used for search parameters.
   */
  private function getXml() {
    $criteria = $this->getCriteria();

    if( !empty( $criteria ) ) {
      $result = $this->call( "generate_search_recipe_xml", "x",
        $criteria["query"],
        $criteria["compare"],
        $criteria["min"],
        $criteria["max"],
        $criteria["unit"],
        $criteria["ingredient"] );
    }

    return isset( $result[0] ) ? $result[0]["x"] :
      $this->getErrorXml( "search" );
  }

  /**
   * Returns the stylesheet used for marking up the XML returned by
   * getXml().
   *
   * @return "xsl/search.xsl"
   */
  private function getStylesheetName() {
    return "xsl/search.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() );

    return $xslt->transform();
  }

  /**
   * Sets editable to true so that commands can be parsed.
   *
   * \todo Split this functionality into a canParseCommand method.
   *
   * @return true Indicates that the content commands can be parsed.
   */
  protected function isEditable() {
    return true;
  }

  private function setCriteria( $criteria ) {
    $this->criteria = $criteria;
  }

  /**
   * Returns the search criteria used to find recipes.
   */
  private function getCriteria() {
    return $this->criteria;
  }

  /**
   * Executes database transactions related to the supplied commands.
   */
  protected function handleRequest() {
    $command    = $this->getCommand();
    $subcommand = $this->getSubCommand();

    $query = $this->getParameter( "q" );

    if( empty( $query ) === false ) {
      $this->search( $query );
    }

    $this->render( false );
  }
}