<?php
namespace com\whitemagicsoftware;
require "constants.php";
require "class.BaseController.php";
class Admin extends BaseController {
function __construct() {
parent::__construct();
}
protected function exists( $id ) {
return true;
}
private function ingredientNameDelete( $label ) {
$this->call( "ingredient_name_delete", "", $label );
}
private function ingredientNameUpdate( $ingredient_name_id, $label ) {
$this->call( "ingredient_name_update", "", $ingredient_name_id, $label );
}
private function ingredientCategoryInsert(
$ingredient_name_ids, $category_id ) {
foreach( $ingredient_name_ids as $id ) {
$this->call( "ingredient_category_insert", "", $id, $category_id );
}
}
private function getXml() {
$i = $this->call( "generate_ingredient_names_xml", "ingredients" );
$i = isset( $i[0] ) ? $i[0]['ingredients'] : "<ingredients/>";
$c = $this->call( "generate_ingredient_category_names_xml", "categories" );
$c = isset( $c[0] ) ? $c[0]["categories"] : "<categories/>";
return "<admin>$i$c</admin>";
}
private function getStylesheetName() {
return "xsl/category.xsl";
}
protected function getXhtml() {
$xslt = $this->getXsltEngine();
$xslt->setXml( $this->getXml() );
$xslt->setStylesheet( $this->getStylesheetName() );
return $xslt->transform();
}
private function explodeIngredientIds( $ingredients ) {
$result = array();
if( $ingredients ) {
foreach( $ingredients as $ingredient ) {
$ids = explode( "::", $ingredient );
array_push( $result, $ids[0] );
}
}
return $result;
}
private function invalid_request() {
echo "<html><body>No.</body></html>";
}
protected function getParameterIdName() {
return "";
}
protected function getAuthorizationFunctionName() {
return "is_authorized_admin";
}
protected function handleRequest() {
if( !$this->isEditable() ) {
$this->invalid_request();
return;
}
$command = $this->getCommand();
$ids =
$this->explodeIngredientIds( $this->getParameter( "ingredients" ) );
switch( $command ) {
case "assign": {
$category_id = $this->getParameter( "category" );
$this->ingredientCategoryInsert( $ids, $category_id );
break;
}
case "delete.ingredient": {
$this->ingredientNameDelete( $this->getParameter( "ingredient" ) );
break;
}
case "rename.ingredient": {
$ingredient = $this->getParameter( "ingredient" );
$this->ingredientNameUpdate( $ids[0], $ingredient );
break;
}
}
echo $this->getXhtml();
}
}