<?php
namespace com\whitemagicsoftware;
require "constants.php";
require "class.Recipe.php";
class Search extends BaseController {
private $criteria;
function __construct() {
parent::__construct();
}
private function search( $query ) {
$comparator = $this->getParameter( "comparator" );
$recipe = new Recipe();
$decoded = json_decode( $recipe->parseIngredientText( $query ) );
list( $min, $max ) = $recipe->extractIngredientMeasures( $decoded, 0 );
$unit = $recipe->extractIngredientUnit( $decoded );
$ingredient = $recipe->extractIngredientName( $decoded );
if( !isset( $comparator ) ) {
$comparator = "eq";
}
$this->setCriteria( array(
"query" => $query,
"compare" => $comparator,
"min" => $min,
"max" => $max,
"unit" => $unit,
"ingredient" => $ingredient )
);
}
protected function exists( $id ) {
return true;
}
protected function getLastResortId() {
return 0;
}
protected function getParameterIdName() {
return "search-id";
}
protected function getAuthorizationFunctionName() {
return "";
}
protected function authorize() {
return false;
}
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" );
}
private function getStylesheetName() {
return "xsl/search.xsl";
}
protected function getXhtml() {
$xslt = $this->getXsltEngine();
$xslt->setXml( $this->getXml() );
$xslt->setStylesheet( $this->getStylesheetName() );
return $xslt->transform();
}
protected function isEditable() {
return true;
}
private function setCriteria( $criteria ) {
$this->criteria = $criteria;
}
private function getCriteria() {
return $this->criteria;
}
protected function handleRequest() {
$command = $this->getCommand();
$subcommand = $this->getSubCommand();
$query = $this->getParameter( "q" );
if( empty( $query ) === false ) {
$this->search( $query );
}
$this->render( false );
}
}