<?php
namespace com\whitemagicsoftware;
require "constants.php";
require "class.BaseController.php";
class Diet extends BaseController {
function __construct() {
parent::__construct();
}
protected function exists( $id ) {
return $this->isTrue(
$this->call( "is_existing_dietary_preference", "exists", $id ) );
}
protected function getLastResortId() {
$result = $this->call( "get_account_id", "id",
$this->getAuthenticationId() );
return isset( $result[0] ) ? $result[0]["id"] : 0;
}
private function insertDietaryPreference( $preference, $name ) {
$result = $this->call( "dietary_preference_insert", "",
$this->getId(), $preference, $name );
}
private function deleteDietaryPreference() {
$result = $this->call( "dietary_preference_delete", "",
$this->getId(), $this->getParameterId( "delete_ingredient" )
);
}
protected function getParameterIdName() {
return "diet-id";
}
protected function getAuthorizationFunctionName() {
return "";
}
protected function isEditable() {
return $this->getUrlId() == $this->getAccountId();
}
protected function authorize() {
return false;
}
private function getXml() {
$result = $this->call( "generate_dietary_preference_xml", "x",
$this->getId() );
return isset( $result[0] ) ? $result[0]["x"] : $this->getErrorXml( "diet" );
}
private function getStylesheetName() {
return "xsl/diet.xsl";
}
protected function getXhtml() {
$xslt = $this->getXsltEngine();
$xslt->setXml( $this->getXml() );
$xslt->setStylesheet( $this->getStylesheetName() );
$xslt->setParameter( $this->getParameterIdName(), $this->getId() );
$xslt->setParameter( "editable", $this->isEditable() );
$xslt->setParameter( "cookie", $this->getCookieToken() );
$xslt->setParameter( "account-label", $this->getAccountLabel() );
return $xslt->transform();
}
protected function handleRequest() {
$command = $this->getCommand();
$subcommand = $this->getSubCommand();
if( $command === "update" ) {
$old = $this->getParameter( "original_html" );
$new = $this->getParameter( "update_value" );
$seq = $this->getParameterId( "element_id" );
if( $subcommand === "title" ) {
echo $this->setAccountLabel( $new );
}
else if( $this->startsWith( $subcommand, "diet." ) ) {
list( $unused, $preference_category ) = explode( ".", $subcommand );
$this->insertDietaryPreference( $preference_category, $new );
}
}
else if( $command === "delete" ) {
if( $subcommand === "ingredient" ) {
$this->deleteDietaryPreference();
}
}
if( $command === "view" && $subcommand === "xml" ) {
$this->sendHttpHeaders( "application/xml" );
echo $this->getXml();
return;
}
if( $subcommand !== "title" ) {
global $BASE_DIET;
$id = $this->getUrlId();
if( $id == 0 ) {
$id = $this->getId();
}
if( !$this->exists( $id ) ) {
$id = $this->getId();
}
$accountLabel = $this->getAccountLabel( $id );
if( $this->redirect( $BASE_DIET, $id, $accountLabel ) ) {
return;
}
}
if( $command !== "update" && $command !== "get" ) {
$this->render();
}
}
}